Statisticsgeneral.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 店铺销量统计控制器
  10. */
  11. class Statisticsgeneral extends MobileSeller
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize(); // TODO: Change the autogenerated stub
  16. include_once root_path() . 'extend/mall/statistics.php';
  17. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statistics.lang.php');
  18. }
  19. /**
  20. * @api {POST} api/Statisticsgeneral/index 近30天销售数据
  21. * @apiVersion 1.0.0
  22. * @apiGroup Statisticsgeneral
  23. *
  24. * @apiHeader {String} X-DS-KEY 卖家授权token
  25. *
  26. * @apiSuccess {String} code 返回码,10000为成功
  27. * @apiSuccess {String} message 返回消息
  28. * @apiSuccess {Object} result 返回数据
  29. * @apiSuccess {Object[]} result.goodstop30_arr 30天销售排行商品列表
  30. * @apiSuccess {Int} result.goodstop30_arr.goods_id 商品ID
  31. * @apiSuccess {Int} result.goodstop30_arr.goods_name 商品名称
  32. * @apiSuccess {Int} result.goodstop30_arr.ordergoodsnum 销量
  33. * @apiSuccess {Int} result.stat_time 统计时间
  34. * @apiSuccess {Object} result.statnew_arr 统计信息
  35. * @apiSuccess {Float} result.statnew_arr.avggoodsprice 评价商品价格
  36. * @apiSuccess {Float} result.statnew_arr.avgorderamount 平均订单价格
  37. * @apiSuccess {Int} result.statnew_arr.gcollectnum 商品收藏数量
  38. * @apiSuccess {Int} result.statnew_arr.goodsnum 商品总数
  39. * @apiSuccess {String} result.statnew_arr.hothour 热销时间
  40. * @apiSuccess {Float} result.statnew_arr.orderamount 订单总金额
  41. * @apiSuccess {Int} result.statnew_arr.ordergoodsnum 商品总销量
  42. * @apiSuccess {Int} result.statnew_arr.ordermembernum 用户总数
  43. * @apiSuccess {Int} result.statnew_arr.ordernum 订单总数
  44. * @apiSuccess {Int} result.statnew_arr.store_collect 店铺收藏量
  45. * @apiSuccess {String} result.stattoday_json 统计json数据
  46. */
  47. public function index()
  48. {
  49. $stat_model = model('stat');
  50. //统计的日期0点
  51. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  52. /*
  53. * 近30天
  54. */
  55. $stime = $stat_time - (86400 * 29); //30天前
  56. $etime = $stat_time + 86400 - 1; //昨天23:59
  57. $statnew_arr = array();
  58. //查询订单表下单量、下单金额、下单客户数
  59. $where = array();
  60. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  61. $where[] = array('store_id', '=', session('store_id'));
  62. $where[] = array('order_add_time', 'between', array($stime, $etime));
  63. $field = ' COUNT(*) as ordernum, SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as ordermembernum, AVG(order_amount) as avgorderamount ';
  64. $stat_order = $stat_model->getoneByStatorder($where, $field);
  65. $statnew_arr['ordernum'] = ($t = $stat_order['ordernum']) ? $t : 0;
  66. $statnew_arr['orderamount'] = ds_price_format(($t = $stat_order['orderamount']) ? $t : (0));
  67. $statnew_arr['ordermembernum'] = ($t = $stat_order['ordermembernum']) > 0 ? $t : 0;
  68. $statnew_arr['avgorderamount'] = ds_price_format(($t = $stat_order['avgorderamount']) ? $t : (0));
  69. unset($stat_order);
  70. //下单高峰期
  71. $where = array();
  72. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  73. $where[] = array('store_id', '=', session('store_id'));
  74. $where[] = array('order_add_time', 'between', array($stime, $etime));
  75. $field = ' HOUR(FROM_UNIXTIME(order_add_time)) as hourval,COUNT(*) as ordernum ';
  76. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, 'ordernum desc,hourval asc', 'hourval');
  77. foreach ((array)$orderlist as $k => $v) {
  78. if ($k < 2) { //取前两个订单量高的时间段
  79. if (!isset($statnew_arr['hothour'])) {
  80. $statnew_arr['hothour'] = ($v['hourval'] . ":00~" . ($v['hourval'] + 1) . ":00");
  81. } else {
  82. $statnew_arr['hothour'] .= ("," . ($v['hourval'] . ":00~" . ($v['hourval'] + 1) . ":00"));
  83. }
  84. }
  85. }
  86. unset($orderlist);
  87. //查询订单商品表下单商品数
  88. $where = array();
  89. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  90. $where[] = array('store_id', '=', session('store_id'));
  91. $where[] = array('order_add_time', 'between', array($stime, $etime));
  92. $field = ' SUM(goods_num) as ordergoodsnum, AVG(goods_pay_price/goods_num) as avggoodsprice ';
  93. $stat_ordergoods = $stat_model->getoneByStatordergoods($where, $field);
  94. $statnew_arr['ordergoodsnum'] = ($t = $stat_ordergoods['ordergoodsnum']) ? $t : 0;
  95. $statnew_arr['avggoodsprice'] = ds_price_format(($t = $stat_ordergoods['avggoodsprice']) ? $t : (0));
  96. unset($stat_ordergoods);
  97. //商品总数、收藏量
  98. $goods_list = $stat_model->statByGoods(array('store_id' => session('store_id')), 'COUNT(*) as goodsnum, SUM(goods_collect) as gcollectnum');
  99. $statnew_arr['goodsnum'] = ($t = $goods_list[0]['goodsnum']) > 0 ? $t : 0;
  100. $statnew_arr['gcollectnum'] = ($t = $goods_list[0]['gcollectnum']) > 0 ? $t : 0;
  101. //店铺收藏量
  102. $store_list = $stat_model->getOneByStore(array('store_id' => session('store_id')), 'store_collect');
  103. $statnew_arr['store_collect'] = ($t = $store_list['store_collect']) > 0 ? $t : 0;
  104. /*
  105. * 销售走势
  106. */
  107. //构造横轴数据
  108. for ($i = $stime; $i < $etime; $i += 86400) {
  109. //当前数据的时间
  110. $timetext = date('n', $i) . '-' . date('j', $i);
  111. //统计图数据
  112. $stat_list[$timetext] = 0;
  113. //横轴
  114. $stat_arr['xAxis']['categories'][] = $timetext;
  115. }
  116. $where = array();
  117. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  118. $where[] = array('store_id', '=', session('store_id'));
  119. $where[] = array('order_add_time', 'between', array($stime, $etime));
  120. $field = ' order_add_time,SUM(order_amount) as orderamount,MONTH(FROM_UNIXTIME(order_add_time)) as monthval,DAY(FROM_UNIXTIME(order_add_time)) as dayval ';
  121. $stat_order = $stat_model->statByStatorder($where, $field, 0, 0, '', 'monthval,dayval');
  122. if ($stat_order) {
  123. foreach ($stat_order as $k => $v) {
  124. $stat_list[$v['monthval'] . '-' . $v['dayval']] = floatval($v['orderamount']);
  125. }
  126. }
  127. $stat_arr['legend']['enabled'] = false;
  128. $stat_arr['series'][0]['name'] = lang('place_order_amount');
  129. $stat_arr['series'][0]['data'] = array_values($stat_list);
  130. //得到统计图数据
  131. $stat_arr['title'] = lang('sales_trends_last_days');
  132. $stat_arr['yAxis'] = lang('place_order_amount');
  133. $stattoday_json = getStatData_LineLabels($stat_arr);
  134. unset($stat_arr);
  135. /*
  136. * 7日内商品销售TOP30
  137. */
  138. $stime = $stat_time - 86400 * 6; //7天前0点
  139. $etime = $stat_time + 86400 - 1; //今天24点
  140. $where = array();
  141. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  142. $where[] = array('store_id', '=', session('store_id'));
  143. $where[] = array('order_add_time', 'between', array($stime, $etime));
  144. $field = ' sum(goods_num) as ordergoodsnum, goods_id, goods_name ';
  145. $goodstop30_arr = $stat_model->statByStatordergoods($where, $field, 0, 30, 'ordergoodsnum desc', 'goods_id');
  146. $result = array(
  147. 'goodstop30_arr' => $goodstop30_arr,
  148. 'stattoday_json' => $stattoday_json,
  149. 'statnew_arr' => $statnew_arr,
  150. 'stat_time' => $stat_time,
  151. );
  152. ds_json_encode(10000, '', $result);
  153. }
  154. }