Statisticsgeneral.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. /**
  3. * 店铺概况统计
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Statisticsgeneral extends BaseSeller {
  21. public function initialize() {
  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. $stat_model = model('stat');
  31. //统计的日期0点
  32. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  33. /*
  34. * 近30天
  35. */
  36. $stime = $stat_time - (86400 * 29); //30天前
  37. $etime = $stat_time + 86400 - 1; //昨天23:59
  38. $statnew_arr = array();
  39. //查询订单表下单量、下单金额、下单客户数
  40. $where = array();
  41. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  42. $where[] = array('store_id','=',session('store_id'));
  43. $where[] = array('order_add_time','between',array($stime, $etime));
  44. $field = ' COUNT(*) as ordernum, SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as ordermembernum, AVG(order_amount) as avgorderamount ';
  45. $stat_order = $stat_model->getoneByStatorder($where, $field);
  46. $statnew_arr['ordernum'] = ($t = $stat_order['ordernum']) ? $t : 0;
  47. $statnew_arr['orderamount'] = ds_price_format(($t = $stat_order['orderamount']) ? $t : (0));
  48. $statnew_arr['ordermembernum'] = ($t = $stat_order['ordermembernum']) > 0 ? $t : 0;
  49. $statnew_arr['avgorderamount'] = ds_price_format(($t = $stat_order['avgorderamount']) ? $t : (0));
  50. unset($stat_order);
  51. //下单高峰期
  52. $where = array();
  53. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  54. $where[] = array('store_id','=',session('store_id'));
  55. $where[] = array('order_add_time','between',array($stime, $etime));
  56. $field = ' HOUR(FROM_UNIXTIME(order_add_time)) as hourval,COUNT(*) as ordernum ';
  57. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, 'ordernum desc,hourval asc', 'hourval');
  58. foreach ((array) $orderlist as $k => $v) {
  59. if ($k < 2) {//取前两个订单量高的时间段
  60. if (!isset($statnew_arr['hothour'])) {
  61. $statnew_arr['hothour'] = ($v['hourval'] . ":00~" . ($v['hourval'] + 1) . ":00");
  62. } else {
  63. $statnew_arr['hothour'] .= ("," . ($v['hourval'] . ":00~" . ($v['hourval'] + 1) . ":00"));
  64. }
  65. }
  66. }
  67. unset($orderlist);
  68. //查询订单商品表下单商品数
  69. $where = array();
  70. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  71. $where[] = array('store_id','=',session('store_id'));
  72. $where[] = array('order_add_time','between',array($stime, $etime));
  73. $field = ' SUM(goods_num) as ordergoodsnum, AVG(goods_pay_price/goods_num) as avggoodsprice ';
  74. $stat_ordergoods = $stat_model->getoneByStatordergoods($where, $field);
  75. $statnew_arr['ordergoodsnum'] = ($t = $stat_ordergoods['ordergoodsnum']) ? $t : 0;
  76. $statnew_arr['avggoodsprice'] = ds_price_format(($t = $stat_ordergoods['avggoodsprice']) ? $t : (0));
  77. unset($stat_ordergoods);
  78. //商品总数、收藏量
  79. $goods_list = $stat_model->statByGoods(array('store_id' => session('store_id'), 'is_virtual' => 0), 'COUNT(*) as goodsnum, SUM(goods_collect) as gcollectnum');
  80. $statnew_arr['goodsnum'] = ($t = $goods_list[0]['goodsnum']) > 0 ? $t : 0;
  81. $statnew_arr['gcollectnum'] = ($t = $goods_list[0]['gcollectnum']) > 0 ? $t : 0;
  82. //店铺收藏量
  83. $store_list = $stat_model->getOneByStore(array('store_id' => session('store_id')), 'store_collect');
  84. $statnew_arr['store_collect'] = ($t = $store_list['store_collect']) > 0 ? $t : 0;
  85. /*
  86. * 销售走势
  87. */
  88. //构造横轴数据
  89. for ($i = $stime; $i < $etime; $i += 86400) {
  90. //当前数据的时间
  91. $timetext = date('n', $i) . '-' . date('j', $i);
  92. //统计图数据
  93. $stat_list[$timetext] = 0;
  94. //横轴
  95. $stat_arr['xAxis']['categories'][] = $timetext;
  96. }
  97. $where = array();
  98. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  99. $where[] = array('store_id','=',session('store_id'));
  100. $where[] = array('order_add_time','between',array($stime, $etime));
  101. $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 ';
  102. $stat_order = $stat_model->statByStatorder($where, $field, 0, 0, '', 'monthval,dayval');
  103. if ($stat_order) {
  104. foreach ($stat_order as $k => $v) {
  105. $stat_list[$v['monthval'] . '-' . $v['dayval']] = floatval($v['orderamount']);
  106. }
  107. }
  108. $stat_arr['legend']['enabled'] = false;
  109. $stat_arr['series'][0]['name'] = lang('place_order_amount');
  110. $stat_arr['series'][0]['data'] = array_values($stat_list);
  111. //得到统计图数据
  112. $stat_arr['title'] = lang('sales_trends_last_days');
  113. $stat_arr['yAxis'] = lang('place_order_amount');
  114. $stattoday_json = getStatData_LineLabels($stat_arr);
  115. unset($stat_arr);
  116. /*
  117. * 7日内商品销售TOP30
  118. */
  119. $stime = $stat_time - 86400 * 6; //7天前0点
  120. $etime = $stat_time + 86400 - 1; //今天24点
  121. $where = array();
  122. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  123. $where[] = array('store_id','=',session('store_id'));
  124. $where[] = array('order_add_time','between',array($stime, $etime));
  125. $field = ' sum(goods_num) as ordergoodsnum, goods_id, goods_name ';
  126. $goodstop30_arr = $stat_model->statByStatordergoods($where, $field, 0, 30, 'ordergoodsnum desc', 'goods_id');
  127. /**
  128. * 7日内同行热卖商品
  129. */
  130. $where = array();
  131. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  132. $where[] = array('order_add_time', 'between', array($stime, $etime));
  133. $where[] = array('store_id', '<>', session('store_id'));
  134. if (!check_platform_store()) {//如果不是平台店铺,则查询店铺经营类目的同行数据
  135. //查询店铺经营类目
  136. $store_bindclass = model('storebindclass')->getStorebindclassList(array('store_id' => session('store_id')));
  137. $goodsclassid_arr = array();
  138. foreach ((array) $store_bindclass as $k => $v) {
  139. if (intval($v['class_3']) > 0) {
  140. $goodsclassid_arr[3][] = intval($v['class_3']);
  141. } elseif (intval($v['class_2']) > 0) {
  142. $goodsclassid_arr[2][] = intval($v['class_2']);
  143. } elseif (intval($v['class_1']) > 0) {
  144. $goodsclassid_arr[1][] = intval($v['class_1']);
  145. }
  146. }
  147. //拼接商品分类条件
  148. if ($goodsclassid_arr) {
  149. ksort($goodsclassid_arr);
  150. $gc_parentidwhere_keyarr = array();
  151. $gc_parentidwhere_arr = array();
  152. foreach ((array) $goodsclassid_arr as $k => $v) {
  153. $gc_parentidwhere_keyarr[] = 'gc_parentid_' . $k;
  154. $gc_parentidwhere_arr = array_merge($gc_parentidwhere_arr,$goodsclassid_arr[$k]);
  155. }
  156. if (count($gc_parentidwhere_keyarr) == 1) {
  157. $where[] = array($gc_parentidwhere_keyarr[0], 'in', $gc_parentidwhere_arr);
  158. } else {
  159. $where[] = array(implode('|', $gc_parentidwhere_keyarr), 'in', $gc_parentidwhere_arr);
  160. }
  161. }
  162. }
  163. $field = ' sum(goods_num) as ordergoodsnum, goods_id, goods_name ';
  164. $othergoodstop30_arr = $stat_model->statByStatordergoods($where, $field, 0, 30, 'ordergoodsnum desc', 'goods_id');
  165. View::assign('goodstop30_arr', $goodstop30_arr);
  166. View::assign('othergoodstop30_arr', $othergoodstop30_arr);
  167. View::assign('stattoday_json', $stattoday_json);
  168. View::assign('statnew_arr', $statnew_arr);
  169. View::assign('stat_time', $stat_time);
  170. $this->setSellerCurMenu('Statisticsgeneral');
  171. $this->setSellerCurItem('index');
  172. return View::fetch($this->template_dir . 'index');
  173. }
  174. /**
  175. * 价格区间设置
  176. */
  177. public function pricesetting() {
  178. $storeextend_model = model('storeextend');
  179. if (request()->isPost()) {
  180. $update_array = array();
  181. $pricerange_array = input('post.pricerange/a'); #获取数组
  182. if (!empty($pricerange_array)) {
  183. foreach ($pricerange_array as $k => $v) {
  184. $pricerange_arr[] = $v;
  185. }
  186. $update_array['pricerange'] = serialize($pricerange_arr);
  187. } else {
  188. $update_array['pricerange'] = '';
  189. }
  190. $result = $storeextend_model->editStoreextend($update_array, array('store_id' => session('store_id')));
  191. if ($result) {
  192. $this->success(lang('ds_common_save_succ'), 'statisticsgeneral/pricesetting');
  193. } else {
  194. $this->error(lang('ds_common_save_fail'));
  195. }
  196. }
  197. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'pricerange');
  198. $pricerange = $pricerange ? unserialize($pricerange) : array();
  199. View::assign('pricerange', $pricerange);
  200. $this->setSellerCurItem('pricesetting');
  201. $this->setSellerCurMenu('Statisticsgeneral');
  202. return View::fetch($this->template_dir . 'pricesetting');
  203. }
  204. /**
  205. * 订单价格区间设置
  206. */
  207. public function orderprange() {
  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. $menu_array = array(
  243. array(
  244. 'name' => 'index', 'text' => lang('store_profile'),
  245. 'url' => (string) url('Statisticsgeneral/index')
  246. ),
  247. array(
  248. 'name' => 'pricesetting', 'text' => lang('commodity_price_range'),
  249. 'url' => (string) url('Statisticsgeneral/pricesetting')
  250. ), array(
  251. 'name' => 'orderprange', 'text' => lang('order_amount_range'),
  252. 'url' => (string) url('Statisticsgeneral/orderprange')
  253. )
  254. );
  255. return $menu_array;
  256. }
  257. }