Statistics.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 易而优团队 by 陈风任 <491085389@qq.com>
  11. * Date: 2019-11-21
  12. */
  13. namespace app\admin\controller;
  14. use think\Db;
  15. use think\Config;
  16. // 数据统计
  17. class Statistics extends Base {
  18. public function _initialize() {
  19. parent::_initialize();
  20. $this->language_access(); // 多语言功能操作权限
  21. }
  22. /**
  23. * 数据表列表
  24. */
  25. public function index()
  26. {
  27. // 近七日成交量成交额折线图数据
  28. $LineChartData = $this->GetLineChartData();
  29. $this->assign('DealNum', $LineChartData['DealNum']);
  30. $this->assign('DealAmount', $LineChartData['DealAmount']);
  31. // 起始时间
  32. $StartTime = $this->GetTime(6);
  33. $EndTime = getTime();
  34. $this->assign('StartTime', $StartTime);
  35. $this->assign('EndTime', $EndTime);
  36. // 数据统计
  37. $CycletData = $this->GetTimeCycletData($StartTime, $EndTime);
  38. $this->assign('CycletData', $CycletData);
  39. // 商品销售榜
  40. $OrderSalesList = $this->GetOrderSalesList();
  41. $this->assign('OrderSalesList', $OrderSalesList);
  42. // 用户消费榜
  43. $UserConsumption = $this->GetUserConsumption();
  44. $this->assign('UserConsumption', $UserConsumption);
  45. return $this->fetch();
  46. }
  47. // 用户消费榜
  48. private function GetUserConsumption()
  49. {
  50. $where = [
  51. 'order_status' => ['IN', [1, 2, 3]]
  52. ];
  53. $Return = Db::name('shop_order')->field('users_id, sum(order_total_amount+shipping_fee) as amount')->where($where)->group('users_id')->select();
  54. $users_id = get_arr_column($Return, 'users_id');
  55. $Return = convert_arr_key($Return, 'users_id');
  56. $UsersData = Db::name('users')->field('users_id, username, nickname')->select();
  57. foreach ($UsersData as $key => $value) {
  58. $UsersData[$key]['amount'] = !empty($Return[$value['users_id']]['amount']) ? $Return[$value['users_id']]['amount'] : 0;
  59. $UsersData[$key]['nickname'] = !empty($UsersData[$key]['nickname']) ? $UsersData[$key]['nickname'] : $UsersData[$key]['username'];
  60. }
  61. // 以消费金额排序
  62. array_multisort(get_arr_column($UsersData, 'amount'), SORT_DESC, $UsersData);
  63. // 读取前十数据
  64. $UsersData = array_slice($UsersData, 0, 10);
  65. return $UsersData;
  66. }
  67. // 商品销售榜
  68. private function GetOrderSalesList()
  69. {
  70. $Return = Db::name('archives')
  71. ->field('aid, title, sales_num')
  72. ->order('sales_num desc')
  73. ->limit('0, 10')
  74. ->where('channel', 2)
  75. ->select();
  76. $aid = get_arr_column($Return, 'aid');
  77. $where = [
  78. 'a.product_id' => ['IN', $aid],
  79. 'b.order_status' => ['IN', [1, 2, 3]],
  80. ];
  81. $Price = Db::name('shop_order_details')->alias('a')
  82. ->field('a.product_id, sum(a.product_price*a.num) as price, a.num')
  83. ->join('__SHOP_ORDER__ b', 'a.order_id = b.order_id', 'LEFT')
  84. ->where($where)
  85. ->group('product_id')
  86. ->select();
  87. $Price = convert_arr_key($Price, 'product_id');
  88. $array_new = get_archives_data($Return, 'aid');
  89. $Return = convert_arr_key($Return, 'aid');
  90. foreach ($Return as $key => $value) {
  91. $Return[$key]['sales_amount'] = !empty($Price[$value['aid']]['price']) ? $Price[$value['aid']]['price'] : 0;
  92. $Return[$key]['title'] = @msubstr($value['title'], 0, 25, '...');
  93. $Return[$key]['arcurl'] = get_arcurl($array_new[$value['aid']]);
  94. $Return[$key]['sales_num'] = !empty($Price[$value['aid']]['num']) ? $Price[$value['aid']]['num'] : 0;
  95. }
  96. return $Return;
  97. }
  98. // 获取时间周期内的指定数据
  99. public function GetTimeCycletData($Start = null, $End = null)
  100. {
  101. $param = input('param.');
  102. if (0 != $param['Year']) {
  103. $param['Start'] = strtotime("-0 year -{$param['Year']} month -0 day");
  104. $param['End'] = getTime();
  105. } else {
  106. if (empty($Start) || empty($End)) {
  107. $param['Start'] = strtotime($param['StartNew']);
  108. $param['End'] = strtotime($param['EndNew']);
  109. } else {
  110. $param = [
  111. 'Start' => $Start,
  112. 'End' => $End,
  113. ];
  114. }
  115. }
  116. // 会员查询条件
  117. $Uwhere = [
  118. 'reg_time' => ['between', [$param['Start'], $param['End']]]
  119. ];
  120. // 商品查询条件
  121. $Awhere = [
  122. 'channel' => 2,
  123. 'add_time' => ['between', [$param['Start'], $param['End']]]
  124. ];
  125. // 商品查询条件
  126. $Swhere = [
  127. 'add_time' => ['between', [$param['Start'], $param['End']]]
  128. ];
  129. // 订单查询条件
  130. $Owhere = [
  131. 'order_status' => ['IN', [1, 2, 3]],
  132. 'add_time' => ['between', [$param['Start'], $param['End']]]
  133. ];
  134. // 查询订单数据
  135. $Result = $this->GetTimeWhereData($Owhere);
  136. // 充值查询条件
  137. $Mwhere = [
  138. 'cause' => Config::get('global.pay_cause_type_arr')[1],
  139. 'cause_type' => 1,
  140. 'status' => 3,
  141. 'add_time' => ['between', [$param['Start'], $param['End']]]
  142. ];
  143. $Return = [
  144. // 会员人数
  145. 'UsersNum' => Db::name('users')->where($Uwhere)->count(),
  146. // 付款订单数
  147. 'PayOrderNum' => $Result['deal_num'],
  148. // 订单销售额
  149. 'OrderSales' => $Result['deal_amount'],
  150. // 商品数
  151. 'ProductNum' => Db::name('archives')->where($Awhere)->count(),
  152. // 消费人数
  153. 'OrderUsersNum' => Db::name('shop_order')->where($Swhere)->group('users_id')->count(),
  154. // 充值金额
  155. 'UsersRecharge' => Db::name('users_money')->where($Mwhere)->sum('money'),
  156. // 返回查询时间
  157. 'Start' => date("Y-m-d H:i:s", $param['Start']),
  158. 'End' => date("Y-m-d H:i:s", $param['End'])
  159. ];
  160. if (IS_AJAX_POST) {
  161. $this->success('查询成功!', null, $Return);
  162. } else {
  163. return $Return;
  164. }
  165. }
  166. // 近七日成交量成交额折线图数据
  167. private function GetLineChartData()
  168. {
  169. /*七日内每日起始结束时间戳*/
  170. $Time = [
  171. // 当天
  172. 'ToDaysStart' => strtotime(date("Y-m-d"),time()),
  173. 'ToDaysEnd' => getTime(),
  174. // 昨天
  175. 'YesterDaysStart' => $this->GetTime(1),
  176. 'YesterDaysEnd' => $this->GetTime(1) + 86399,
  177. // 前天
  178. 'AgoDaysStart' => $this->GetTime(2),
  179. 'AgoDaysEnd' => $this->GetTime(2) + 86399,
  180. // 三天前
  181. 'ThreeDaysAgoStart' => $this->GetTime(3),
  182. 'ThreeDaysAgoEnd' => $this->GetTime(3) + 86399,
  183. // 四天前
  184. 'FourDaysAgoStart' => $this->GetTime(4),
  185. 'FourDaysAgoEnd' => $this->GetTime(4) + 86399,
  186. // 五天前
  187. 'FiveDaysAgoStart' => $this->GetTime(5),
  188. 'FiveDaysAgoEnd' => $this->GetTime(5) + 86399,
  189. // 六天前
  190. 'SixDaysAgoStart' => $this->GetTime(6),
  191. 'SixDaysAgoEnd' => $this->GetTime(6) + 86399,
  192. ];
  193. /* END */
  194. $where['order_status'] = ['IN', [1, 2, 3]];
  195. // 六天前
  196. $where['add_time'] = ['between', [$Time['SixDaysAgoStart'], $Time['SixDaysAgoEnd']]];
  197. $Six = $this->GetTimeWhereData($where);
  198. $Six['deal_amount'] = !empty($Six['deal_amount']) ? $Six['deal_amount'] : 0;
  199. // 五天前
  200. $where['add_time'] = ['between', [$Time['FiveDaysAgoStart'], $Time['FiveDaysAgoEnd']]];
  201. $Five = $this->GetTimeWhereData($where);
  202. $Five['deal_amount'] = !empty($Five['deal_amount']) ? $Five['deal_amount'] : 0;
  203. // 四天前
  204. $where['add_time'] = ['between', [$Time['FourDaysAgoStart'], $Time['FourDaysAgoEnd']]];
  205. $Four = $this->GetTimeWhereData($where);
  206. $Four['deal_amount'] = !empty($Four['deal_amount']) ? $Four['deal_amount'] : 0;
  207. // 三天前
  208. $where['add_time'] = ['between', [$Time['ThreeDaysAgoStart'], $Time['ThreeDaysAgoEnd']]];
  209. $Three = $this->GetTimeWhereData($where);
  210. $Three['deal_amount'] = !empty($Three['deal_amount']) ? $Three['deal_amount'] : 0;
  211. // 前天
  212. $where['add_time'] = ['between', [$Time['AgoDaysStart'], $Time['AgoDaysEnd']]];
  213. $Ago = $this->GetTimeWhereData($where);
  214. $Ago['deal_amount'] = !empty($Ago['deal_amount']) ? $Ago['deal_amount'] : 0;
  215. // 昨天
  216. $where['add_time'] = ['between', [$Time['YesterDaysStart'], $Time['YesterDaysEnd']]];
  217. $Yester = $this->GetTimeWhereData($where);
  218. $Yester['deal_amount'] = !empty($Yester['deal_amount']) ? $Yester['deal_amount'] : 0;
  219. // 当天
  220. $where['add_time'] = ['between', [$Time['ToDaysStart'], $Time['ToDaysEnd']]];
  221. $ToDays = $this->GetTimeWhereData($where);
  222. $ToDays['deal_amount'] = !empty($ToDays['deal_amount']) ? $ToDays['deal_amount'] : 0;
  223. $Return = [
  224. 'DealAmount' => [$Six['deal_amount'], $Five['deal_amount'], $Four['deal_amount'], $Three['deal_amount'], $Ago['deal_amount'], $Yester['deal_amount'], $ToDays['deal_amount']],
  225. 'DealNum' => [$Six['deal_num'], $Five['deal_num'], $Four['deal_num'], $Three['deal_num'], $Ago['deal_num'], $Yester['deal_num'], $ToDays['deal_num']]
  226. ];
  227. return $Return;
  228. }
  229. // 获取指定日期下的数据
  230. private function GetTimeWhereData($where = [])
  231. {
  232. $field = 'sum(order_total_amount+shipping_fee) as deal_amount, count(users_id) as deal_num';
  233. $Return = Db::name('shop_order')->field($field)->where($where)->select();
  234. $Return[0]['deal_amount'] = $Return[0]['deal_amount'] ? $Return[0]['deal_amount'] : 0;
  235. $Return[0]['deal_num'] = $Return[0]['deal_num'] ? $Return[0]['deal_num'] : 0;
  236. return $Return[0];
  237. }
  238. // 获取指定日期时间戳
  239. private function GetTime($num = null)
  240. {
  241. $time = strtotime(date("Y-m-d", strtotime("-{$num} day")));
  242. return $time;
  243. }
  244. }