Statisticsgeneral.php 8.4 KB

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