Statisticsgeneral.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /**
  3. * 店铺概况统计
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. /**
  9. *
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * 控制器
  14. */
  15. class Statisticsgeneral extends BaseSeller
  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. * 促销分析
  25. */
  26. public function index()
  27. {
  28. $stat_model = model('stat');
  29. //统计的日期0点
  30. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  31. /*
  32. * 近30天
  33. */
  34. $stime = $stat_time - (86400 * 29); //30天前
  35. $etime = $stat_time + 86400 - 1; //昨天23:59
  36. $statnew_arr = array();
  37. //查询订单表下单量、下单金额、下单客户数
  38. $where = array();
  39. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  40. $where[] = array('store_id', '=', session('store_id'));
  41. $where[] = array('order_add_time', 'between', array($stime, $etime));
  42. $field = ' COUNT(*) as ordernum, SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as ordermembernum, AVG(order_amount) as avgorderamount ';
  43. $stat_order = $stat_model->getoneByStatorder($where, $field);
  44. $statnew_arr['ordernum'] = ($t = $stat_order['ordernum']) ? $t : 0;
  45. $statnew_arr['orderamount'] = ds_price_format(($t = $stat_order['orderamount']) ? $t : (0));
  46. $statnew_arr['ordermembernum'] = ($t = $stat_order['ordermembernum']) > 0 ? $t : 0;
  47. $statnew_arr['avgorderamount'] = ds_price_format(($t = $stat_order['avgorderamount']) ? $t : (0));
  48. unset($stat_order);
  49. //下单高峰期
  50. $where = array();
  51. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  52. $where[] = array('store_id', '=', session('store_id'));
  53. $where[] = array('order_add_time', 'between', array($stime, $etime));
  54. $field = ' HOUR(FROM_UNIXTIME(order_add_time)) as hourval,COUNT(*) as ordernum ';
  55. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, 'ordernum desc,hourval asc', 'hourval');
  56. foreach ((array) $orderlist as $k => $v) {
  57. if ($k < 2) { //取前两个订单量高的时间段
  58. if (!isset($statnew_arr['hothour'])) {
  59. $statnew_arr['hothour'] = ($v['hourval'] . ":00~" . ($v['hourval'] + 1) . ":00");
  60. } else {
  61. $statnew_arr['hothour'] .= ("," . ($v['hourval'] . ":00~" . ($v['hourval'] + 1) . ":00"));
  62. }
  63. }
  64. }
  65. unset($orderlist);
  66. //查询订单商品表下单商品数
  67. $where = array();
  68. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  69. $where[] = array('store_id', '=', session('store_id'));
  70. $where[] = array('order_add_time', 'between', array($stime, $etime));
  71. $field = ' SUM(goods_num) as ordergoodsnum, AVG(goods_pay_price/goods_num) as avggoodsprice ';
  72. $stat_ordergoods = $stat_model->getoneByStatordergoods($where, $field);
  73. $statnew_arr['ordergoodsnum'] = ($t = $stat_ordergoods['ordergoodsnum']) ? $t : 0;
  74. $statnew_arr['avggoodsprice'] = ds_price_format(($t = $stat_ordergoods['avggoodsprice']) ? $t : (0));
  75. unset($stat_ordergoods);
  76. //商品总数、收藏量
  77. $goods_list = $stat_model->statByGoods(array('store_id' => session('store_id'), 'is_virtual' => 0), 'COUNT(*) as goodsnum, SUM(goods_collect) as gcollectnum');
  78. $statnew_arr['goodsnum'] = ($t = $goods_list[0]['goodsnum']) > 0 ? $t : 0;
  79. $statnew_arr['gcollectnum'] = ($t = $goods_list[0]['gcollectnum']) > 0 ? $t : 0;
  80. //店铺收藏量
  81. $store_list = $stat_model->getOneByStore(array('store_id' => session('store_id')), 'store_collect');
  82. $statnew_arr['store_collect'] = ($t = $store_list['store_collect']) > 0 ? $t : 0;
  83. /*
  84. * 销售走势
  85. */
  86. //构造横轴数据
  87. for ($i = $stime; $i < $etime; $i += 86400) {
  88. //当前数据的时间
  89. $timetext = date('n', $i) . '-' . date('j', $i);
  90. //统计图数据
  91. $stat_list[$timetext] = 0;
  92. //横轴
  93. $stat_arr['xAxis']['categories'][] = $timetext;
  94. }
  95. $where = array();
  96. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  97. $where[] = array('store_id', '=', session('store_id'));
  98. $where[] = array('order_add_time', 'between', array($stime, $etime));
  99. $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 ';
  100. $stat_order = $stat_model->statByStatorder($where, $field, 0, 0, '', 'monthval,dayval');
  101. if ($stat_order) {
  102. foreach ($stat_order as $k => $v) {
  103. $stat_list[$v['monthval'] . '-' . $v['dayval']] = floatval($v['orderamount']);
  104. }
  105. }
  106. $stat_arr['legend']['enabled'] = false;
  107. $stat_arr['series'][0]['name'] = lang('place_order_amount');
  108. $stat_arr['series'][0]['data'] = array_values($stat_list);
  109. //得到统计图数据
  110. $stat_arr['title'] = lang('sales_trends_last_days');
  111. $stat_arr['yAxis'] = lang('place_order_amount');
  112. $stattoday_json = getStatData_LineLabels($stat_arr);
  113. unset($stat_arr);
  114. /*
  115. * 7日内商品销售TOP30
  116. */
  117. $stime = $stat_time - 86400 * 6; //7天前0点
  118. $etime = $stat_time + 86400 - 1; //今天24点
  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 = ' sum(goods_num) as ordergoodsnum, goods_id, goods_name ';
  124. $goodstop30_arr = $stat_model->statByStatordergoods($where, $field, 0, 30, 'ordergoodsnum desc', 'goods_id');
  125. /**
  126. * 7日内同行热卖商品
  127. */
  128. $where = array();
  129. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  130. $where[] = array('order_add_time', 'between', array($stime, $etime));
  131. $where[] = array('store_id', '<>', session('store_id'));
  132. if (!check_platform_store()) { //如果不是平台店铺,则查询店铺经营类目的同行数据
  133. //查询店铺经营类目
  134. $store_bindclass = model('storebindclass')->getStorebindclassList(array('store_id' => session('store_id')));
  135. $goodsclassid_arr = array();
  136. foreach ((array) $store_bindclass as $k => $v) {
  137. if (intval($v['class_3']) > 0) {
  138. $goodsclassid_arr[3][] = intval($v['class_3']);
  139. } elseif (intval($v['class_2']) > 0) {
  140. $goodsclassid_arr[2][] = intval($v['class_2']);
  141. } elseif (intval($v['class_1']) > 0) {
  142. $goodsclassid_arr[1][] = intval($v['class_1']);
  143. }
  144. }
  145. //拼接商品分类条件
  146. if ($goodsclassid_arr) {
  147. ksort($goodsclassid_arr);
  148. $gc_parentidwhere_keyarr = array();
  149. $gc_parentidwhere_arr = array();
  150. foreach ((array) $goodsclassid_arr as $k => $v) {
  151. $gc_parentidwhere_keyarr[] = 'gc_parentid_' . $k;
  152. $gc_parentidwhere_arr = array_merge($gc_parentidwhere_arr, $goodsclassid_arr[$k]);
  153. }
  154. if (count($gc_parentidwhere_keyarr) == 1) {
  155. $where[] = array($gc_parentidwhere_keyarr[0], 'in', $gc_parentidwhere_arr);
  156. } else {
  157. $where[] = array(implode('|', $gc_parentidwhere_keyarr), 'in', $gc_parentidwhere_arr);
  158. }
  159. }
  160. }
  161. $field = ' sum(goods_num) as ordergoodsnum, goods_id, goods_name ';
  162. $othergoodstop30_arr = $stat_model->statByStatordergoods($where, $field, 0, 30, 'ordergoodsnum desc', 'goods_id');
  163. View::assign('goodstop30_arr', $goodstop30_arr);
  164. View::assign('othergoodstop30_arr', $othergoodstop30_arr);
  165. View::assign('stattoday_json', $stattoday_json);
  166. View::assign('statnew_arr', $statnew_arr);
  167. View::assign('stat_time', $stat_time);
  168. $this->setSellerCurMenu('Statisticsgeneral');
  169. $this->setSellerCurItem('index');
  170. return View::fetch($this->template_dir . 'index');
  171. }
  172. /**
  173. * 价格区间设置
  174. */
  175. public function pricesetting()
  176. {
  177. $storeextend_model = model('storeextend');
  178. if (request()->isPost()) {
  179. $update_array = array();
  180. $pricerange_array = input('post.pricerange/a'); #获取数组
  181. if (!empty($pricerange_array)) {
  182. foreach ($pricerange_array as $k => $v) {
  183. $pricerange_arr[] = $v;
  184. }
  185. $update_array['pricerange'] = serialize($pricerange_arr);
  186. } else {
  187. $update_array['pricerange'] = '';
  188. }
  189. $result = $storeextend_model->editStoreextend($update_array, array('store_id' => session('store_id')));
  190. if ($result) {
  191. $this->success(lang('ds_common_save_succ'), 'statisticsgeneral/pricesetting');
  192. } else {
  193. $this->error(lang('ds_common_save_fail'));
  194. }
  195. }
  196. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'pricerange');
  197. $pricerange = $pricerange ? unserialize($pricerange) : array();
  198. View::assign('pricerange', $pricerange);
  199. $this->setSellerCurItem('pricesetting');
  200. $this->setSellerCurMenu('Statisticsgeneral');
  201. return View::fetch($this->template_dir . 'pricesetting');
  202. }
  203. /**
  204. * 订单价格区间设置
  205. */
  206. public function orderprange()
  207. {
  208. $storeextend_model = model('storeextend');
  209. if (request()->isPost()) {
  210. $update_array = array();
  211. $pricerange_array = input('post.pricerange/a'); #获取数组
  212. if (!empty($pricerange_array)) {
  213. foreach ($pricerange_array as $k => $v) {
  214. $pricerange_arr[] = $v;
  215. }
  216. $update_array['orderpricerange'] = serialize($pricerange_arr);
  217. } else {
  218. $update_array['orderpricerange'] = '';
  219. }
  220. $result = $storeextend_model->editStoreextend($update_array, array('store_id' => session('store_id')));
  221. if ($result) {
  222. $this->success(lang('ds_common_save_succ'), 'statisticsgeneral/orderprange');
  223. } else {
  224. $this->error(lang('ds_common_save_fail'));
  225. }
  226. }
  227. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'orderpricerange');
  228. $pricerange = $pricerange ? unserialize($pricerange) : array();
  229. View::assign('pricerange', $pricerange);
  230. $this->setSellerCurMenu('Statisticsgeneral');
  231. $this->setSellerCurItem('orderprange');
  232. return View::fetch($this->template_dir . 'orderpricerange');
  233. }
  234. /**
  235. * 用户中心右边,小导航
  236. *
  237. * @param string $menu_type 导航类型
  238. * @param string $menu_key 当前导航的menu_key
  239. * @return
  240. */
  241. protected function getSellerItemList()
  242. {
  243. $menu_array = array(
  244. array(
  245. 'name' => 'index', 'text' => lang('store_profile'),
  246. 'url' => (string) url('Statisticsgeneral/index')
  247. ),
  248. array(
  249. 'name' => 'pricesetting', 'text' => lang('commodity_price_range'),
  250. 'url' => (string) url('Statisticsgeneral/pricesetting')
  251. ), array(
  252. 'name' => 'orderprange', 'text' => lang('order_amount_range'),
  253. 'url' => (string) url('Statisticsgeneral/orderprange')
  254. )
  255. );
  256. return $menu_array;
  257. }
  258. }