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