Statisticsgeneral.php 8.2 KB

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