Stat.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace addon\store\shop\controller;
  13. use app\model\store\Stat as StatModel;
  14. use app\model\store\Store as StoreModel;
  15. use app\shop\controller\BaseShop;
  16. use app\model\system\UserGroup;
  17. use Carbon\Carbon;
  18. use think\validate\ValidateRule;
  19. class Stat extends BaseShop
  20. {
  21. /**
  22. * 门店统计数据
  23. */
  24. public function store()
  25. {
  26. $user_info = $this->user_info;
  27. $user_id = $user_info['uid'];
  28. $uwhere['uid'] = $user_id;
  29. $UgObj = new UserGroup();
  30. $user_store_list =$UgObj->getUserList($uwhere,'uid,store_id');
  31. $store_id_arr = array_column($user_store_list['data'],'store_id');
  32. $store_list = ( new StoreModel() )->getStoreList([ [ 'site_id', '=', $this->site_id ],['store_id','in',$store_id_arr] ], 'store_id,store_name');
  33. $this->assign('store_list', $store_list[ 'data' ]);
  34. $this->assign('today', Carbon::today()->toDateString());
  35. $this->assign('yesterday', Carbon::yesterday()->toDateString());
  36. return $this->fetch('stat/store');
  37. }
  38. /**
  39. * 统计数据总和
  40. */
  41. public function statTotal()
  42. {
  43. if (request()->isAjax()) {
  44. $store_id = input('store_id', 0);
  45. $start_time = $end_time = input('start_time', strtotime(date('Y-m-d', time())));
  46. $end_time = input('end_time', time());
  47. if ($start_time > $end_time) {
  48. $start_time = input('end_time');
  49. $end_time = input('start_time');
  50. }
  51. $store_id_arr = [];
  52. $userGroupModel = new UserGroup();
  53. $userInfo = $this->user_info;
  54. if(!$userInfo['is_admin']){
  55. $userGroupWhere['uid'] = $userInfo['uid'];
  56. $userGroupList = $userGroupModel->getUserList($userGroupWhere,'uid,store_id');
  57. $store_id_arr = array_column($userGroupList['data'],'store_id');
  58. }
  59. // $user_info = $this->user_info;
  60. // $user_id = $user_info['uid'];
  61. // $uwhere['uid'] = $user_id;
  62. // $UgObj = new UserGroup();
  63. //
  64. // $user_store_list =$UgObj->getUserList($uwhere,'uid,store_id');
  65. //
  66. // $store_id_arr = array_column($user_store_list['data'],'store_id');
  67. $stat_model = new StatModel();
  68. $data = $stat_model->getShopStatSum($this->site_id, $start_time, $end_time, $store_id,$store_id_arr);
  69. return $data;
  70. }
  71. }
  72. /**
  73. * 获取天统计趋势数据
  74. */
  75. public function dayStatData()
  76. {
  77. if (request()->isAjax()) {
  78. $store_id = input('store_id', 0);
  79. $start_time = input('start_time', strtotime(date('Y-m-d', strtotime('-6 day'))));
  80. $end_time = input('end_time', time());
  81. if ($start_time > $end_time) {
  82. $start_time = input('end_time');
  83. $end_time = input('start_time');
  84. }
  85. if($store_id==0){
  86. $store_id_arr = [];
  87. $userGroupModel = new UserGroup();
  88. $userInfo = $this->user_info;
  89. $userGroupWhere['uid'] = $userInfo['uid'];
  90. $userGroupList = $userGroupModel->getUserList($userGroupWhere,'uid,store_id');
  91. $store_id_arr = array_column($userGroupList['data'],'store_id');
  92. if(!empty($store_id_arr) && !$userInfo['is_admin']){
  93. $store_id = $store_id_arr;
  94. }
  95. }
  96. $stat_model = new StatModel();
  97. $fields = $stat_model->getStatField();
  98. $fields[] = 'expected_earnings_total_money';
  99. $stat_list = $stat_model->getShopStatList($this->site_id, $start_time, $end_time, $store_id)[ 'data' ];
  100. $stat_list = array_map(function($item) {
  101. $item[ 'day_time' ] = date('Y-m-d', $item[ 'day_time' ]);
  102. return $item;
  103. }, $stat_list);
  104. $stat_list = array_column($stat_list, null, 'day_time');
  105. $day = ceil(( $end_time - $start_time ) / 86400);
  106. foreach ($fields as $field) {
  107. $value = [];
  108. $time = [];
  109. for ($i = 0; $i < $day; $i++) {
  110. $date = date('Y-m-d', $start_time + $i * 86400);
  111. $time[] = $date;
  112. $value[] = isset($stat_list[ $date ]) ? $stat_list[ $date ][ $field ] : 0;
  113. }
  114. $data[ $field ] = $value;
  115. $data[ 'time' ] = $time;
  116. }
  117. return success(0, '', $data);
  118. }
  119. }
  120. /**
  121. * 获取小时统计趋势数据
  122. */
  123. public function hourStatData()
  124. {
  125. if (request()->isAjax()) {
  126. $time = input('start_time', time());
  127. $store_id = input('store_id', 0);
  128. $carbon = Carbon::createFromTimestamp($time);
  129. $stat_model = new StatModel();
  130. $fields = $stat_model->getStatHourField();
  131. $fields[] = 'expected_earnings_total_money';
  132. if($store_id==0){
  133. $store_id_arr = [];
  134. $userGroupModel = new UserGroup();
  135. $userInfo = $this->user_info;
  136. $userGroupWhere['uid'] = $userInfo['uid'];
  137. $userGroupList = $userGroupModel->getUserList($userGroupWhere,'uid,store_id');
  138. $store_id_arr = array_column($userGroupList['data'],'store_id');
  139. if(!empty($store_id_arr) && !$userInfo['is_admin']){
  140. $store_id = $store_id_arr;
  141. }
  142. }
  143. $stat_list = $stat_model->getShopStatHourList($this->site_id, $carbon->year, $carbon->month, $carbon->day, $store_id)[ 'data' ];
  144. $data = [];
  145. $empty = array_map(function() { return 0; }, range(0, 23, 1));
  146. if (!empty($stat_list)) {
  147. $stat_list = array_column($stat_list, null, 'hour');
  148. foreach ($fields as $field) {
  149. $value = [];
  150. for ($i = 0; $i < 24; $i++) {
  151. $value[ $i ] = isset($stat_list[ $i ]) ? $stat_list[ $i ][ $field ] : 0;
  152. }
  153. $data[ $field ] = $value;
  154. }
  155. } else {
  156. foreach ($fields as $field) {
  157. $data[ $field ] = $empty;
  158. }
  159. }
  160. $data[ 'time' ] = array_map(function($value) { return $value . '时'; }, range(0, 23, 1));
  161. return success(0, '', $data);
  162. }
  163. }
  164. }