Stat0626.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. // $store_list = ( new StoreModel() )->getStoreList([ [ 'site_id', '=', $this->site_id ] ], 'store_id,store_name');
  34. $this->assign('store_list', $store_list[ 'data' ]);
  35. $this->assign('today', Carbon::today()->toDateString());
  36. $this->assign('yesterday', Carbon::yesterday()->toDateString());
  37. return $this->fetch('stat/store');
  38. }
  39. /**
  40. * 统计数据总和
  41. */
  42. public function statTotal()
  43. {
  44. if (request()->isAjax()) {
  45. $store_id = input('store_id', 0);
  46. $start_time = $end_time = input('start_time', strtotime(date('Y-m-d', time())));
  47. $end_time = input('end_time', time());
  48. if ($start_time > $end_time) {
  49. $start_time = input('end_time');
  50. $end_time = input('start_time');
  51. }
  52. $user_info = $this->user_info;
  53. $user_id = $user_info['uid'];
  54. $uwhere['uid'] = $user_id;
  55. $UgObj = new UserGroup();
  56. $user_store_list =$UgObj->getUserList($uwhere,'uid,store_id');
  57. $store_id_arr = array_column($user_store_list['data'],'store_id');
  58. $stat_model = new StatModel();
  59. $data = $stat_model->getShopStatSum($this->site_id, $start_time, $end_time, $store_id,$store_id_arr);
  60. return $data;
  61. }
  62. }
  63. /**
  64. * 获取天统计趋势数据
  65. */
  66. public function dayStatData()
  67. {
  68. if (request()->isAjax()) {
  69. $store_id = input('store_id', 0);
  70. $start_time = input('start_time', strtotime(date('Y-m-d', strtotime('-6 day'))));
  71. $end_time = input('end_time', time());
  72. if ($start_time > $end_time) {
  73. $start_time = input('end_time');
  74. $end_time = input('start_time');
  75. }
  76. $stat_model = new StatModel();
  77. $fields = $stat_model->getStatField();
  78. $fields[] = 'expected_earnings_total_money';
  79. $stat_list = $stat_model->getShopStatList($this->site_id, $start_time, $end_time, $store_id)[ 'data' ];
  80. $stat_list = array_map(function($item) {
  81. $item[ 'day_time' ] = date('Y-m-d', $item[ 'day_time' ]);
  82. return $item;
  83. }, $stat_list);
  84. $stat_list = array_column($stat_list, null, 'day_time');
  85. $day = ceil(( $end_time - $start_time ) / 86400);
  86. foreach ($fields as $field) {
  87. $value = [];
  88. $time = [];
  89. for ($i = 0; $i < $day; $i++) {
  90. $date = date('Y-m-d', $start_time + $i * 86400);
  91. $time[] = $date;
  92. $value[] = isset($stat_list[ $date ]) ? $stat_list[ $date ][ $field ] : 0;
  93. }
  94. $data[ $field ] = $value;
  95. $data[ 'time' ] = $time;
  96. }
  97. return success(0, '', $data);
  98. }
  99. }
  100. /**
  101. * 获取小时统计趋势数据
  102. */
  103. public function hourStatData()
  104. {
  105. if (request()->isAjax()) {
  106. $time = input('start_time', time());
  107. $store_id = input('store_id', 0);
  108. $carbon = Carbon::createFromTimestamp($time);
  109. $stat_model = new StatModel();
  110. $fields = $stat_model->getStatHourField();
  111. $fields[] = 'expected_earnings_total_money';
  112. $stat_list = $stat_model->getShopStatHourList($this->site_id, $carbon->year, $carbon->month, $carbon->day, $store_id)[ 'data' ];
  113. $data = [];
  114. $empty = array_map(function() { return 0; }, range(0, 23, 1));
  115. if (!empty($stat_list)) {
  116. $stat_list = array_column($stat_list, null, 'hour');
  117. foreach ($fields as $field) {
  118. $value = [];
  119. for ($i = 0; $i < 24; $i++) {
  120. $value[ $i ] = isset($stat_list[ $i ]) ? $stat_list[ $i ][ $field ] : 0;
  121. }
  122. $data[ $field ] = $value;
  123. }
  124. } else {
  125. foreach ($fields as $field) {
  126. $data[ $field ] = $empty;
  127. }
  128. }
  129. $data[ 'time' ] = array_map(function($value) { return $value . '时'; }, range(0, 23, 1));
  130. return success(0, '', $data);
  131. }
  132. }
  133. }