Stat.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $where = [] ;
  31. $where[]=[ 'site_id', '=', $this->site_id ];
  32. if(!$user_info['is_admin']){
  33. $user_store_list =$UgObj->getUserList($uwhere,'uid,store_id');
  34. $store_id_arr = array_column($user_store_list['data'],'store_id');
  35. $where[]=['store_id','in',$store_id_arr] ;
  36. }
  37. $store_list = ( new StoreModel() )->getStoreList($where, 'store_id,store_name');
  38. $this->assign('store_list', $store_list[ 'data' ]);
  39. $this->assign('today', Carbon::today()->toDateString());
  40. $this->assign('yesterday', Carbon::yesterday()->toDateString());
  41. return $this->fetch('stat/store');
  42. }
  43. /**
  44. * 统计数据总和
  45. */
  46. public function statTotal()
  47. {
  48. if (request()->isAjax()) {
  49. $store_id = input('store_id', 0);
  50. $start_time = $end_time = input('start_time', strtotime(date('Y-m-d', time())));
  51. $end_time = input('end_time', time());
  52. if ($start_time > $end_time) {
  53. $start_time = input('end_time');
  54. $end_time = input('start_time');
  55. }
  56. $store_id_arr = [];
  57. $userGroupModel = new UserGroup();
  58. $userInfo = $this->user_info;
  59. if(!$userInfo['is_admin']){
  60. $userGroupWhere['uid'] = $userInfo['uid'];
  61. $userGroupList = $userGroupModel->getUserList($userGroupWhere,'uid,store_id');
  62. $store_id_arr = array_column($userGroupList['data'],'store_id');
  63. }
  64. $stat_model = new StatModel();
  65. $data = $stat_model->getShopStatSum($this->site_id, $start_time, $end_time, $store_id,$store_id_arr);
  66. return $data;
  67. }
  68. }
  69. /**
  70. * 获取天统计趋势数据
  71. */
  72. public function dayStatData()
  73. {
  74. if (request()->isAjax()) {
  75. $store_id = input('store_id', 0);
  76. $start_time = input('start_time', strtotime(date('Y-m-d', strtotime('-6 day'))));
  77. $end_time = input('end_time', time());
  78. if ($start_time > $end_time) {
  79. $start_time = input('end_time');
  80. $end_time = input('start_time');
  81. }
  82. if($store_id==0){
  83. $store_id_arr = [];
  84. $userGroupModel = new UserGroup();
  85. $userInfo = $this->user_info;
  86. $userGroupWhere['uid'] = $userInfo['uid'];
  87. $userGroupList = $userGroupModel->getUserList($userGroupWhere,'uid,store_id');
  88. $store_id_arr = array_column($userGroupList['data'],'store_id');
  89. if(!empty($store_id_arr) && !$userInfo['is_admin']){
  90. $store_id = $store_id_arr;
  91. }
  92. }
  93. $stat_model = new StatModel();
  94. $fields = $stat_model->getStatField();
  95. $fields[] = 'expected_earnings_total_money';
  96. $stat_list = $stat_model->getShopStatList($this->site_id, $start_time, $end_time, $store_id)[ 'data' ];
  97. $stat_lists = [];
  98. foreach($stat_list as $slv){
  99. $slsk = $slv['day'];
  100. unset($slv['day']);
  101. $stat_lists[$slsk] = $slv;
  102. }
  103. // dump($stat_lists);die;
  104. // $array_key = array_keys($stat_list[0]);
  105. // return success(0, '', $stat_list);
  106. // $stat_list = array_map(function($item) {
  107. // $item[ 'day_time' ] = date('Y-m-d', $item[ 'day_time' ]);
  108. // return $item;
  109. // }, $stat_list);
  110. // $stat_list = array_column($stat_list, null, 'day_time');
  111. $day = ceil(( $end_time - $start_time ) / 86400);
  112. $time = [];
  113. for ($i = 0; $i < $day; $i++) {
  114. $date = date('Y-m-d', $start_time + $i * 86400);
  115. $time[] = $date;
  116. $day = date('d', $start_time + $i * 86400);
  117. dump($day);
  118. }die;
  119. $data[ 'time' ] = $time;
  120. // return success(0, '', $data);
  121. foreach ($fields as $field) {
  122. $value = [];
  123. // $time = [];
  124. // for ($i = 0; $i < $day; $i++) {
  125. // $date = date('Y-m-d', $start_time + $i * 86400);
  126. // $time[] = $date;
  127. // $value[] = isset($stat_list[ $date ]) ? $stat_list[ $date ][ $field ] : 0;
  128. // }
  129. $value = array_column($stat_list,$field);
  130. // foreach ($stat_list as &$slv){
  131. // if()
  132. // }
  133. $data[ $field ] = $value;
  134. // $data[ 'time' ] = $time;
  135. }
  136. return success(0, '', $data);
  137. }
  138. }
  139. /**
  140. * 获取小时统计趋势数据
  141. */
  142. public function hourStatData()
  143. {
  144. if (request()->isAjax()) {
  145. $time = input('start_time', time());
  146. $store_id = input('store_id', 0);
  147. $carbon = Carbon::createFromTimestamp($time);
  148. $stat_model = new StatModel();
  149. $fields = $stat_model->getStatHourField();
  150. $fields[] = 'expected_earnings_total_money';
  151. if($store_id==0){
  152. $store_id_arr = [];
  153. $userGroupModel = new UserGroup();
  154. $userInfo = $this->user_info;
  155. $userGroupWhere['uid'] = $userInfo['uid'];
  156. $userGroupList = $userGroupModel->getUserList($userGroupWhere,'uid,store_id');
  157. $store_id_arr = array_column($userGroupList['data'],'store_id');
  158. if(!empty($store_id_arr) && !$userInfo['is_admin']){
  159. $store_id = $store_id_arr;
  160. }
  161. }
  162. $stat_list = $stat_model->getShopStatHourList($this->site_id, $carbon->year, $carbon->month, $carbon->day, $store_id)[ 'data' ];
  163. $data = [];
  164. $empty = array_map(function() { return 0; }, range(0, 23, 1));
  165. if (!empty($stat_list)) {
  166. $stat_list = array_column($stat_list, null, 'hour');
  167. foreach ($fields as $field) {
  168. $value = [];
  169. for ($i = 0; $i < 24; $i++) {
  170. $value[ $i ] = isset($stat_list[ $i ]) ? $stat_list[ $i ][ $field ] : 0;
  171. }
  172. $data[ $field ] = $value;
  173. }
  174. } else {
  175. foreach ($fields as $field) {
  176. $data[ $field ] = $empty;
  177. }
  178. }
  179. $data[ 'time' ] = array_map(function($value) { return $value . '时'; }, range(0, 23, 1));
  180. return success(0, '', $data);
  181. }
  182. }
  183. }