Statgeneral.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 控制器
  14. */
  15. class Statgeneral extends AdminControl
  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() . 'admin/lang/' . config('lang.default_lang') . '/stat.lang.php');
  22. }
  23. /**
  24. * 促销分析
  25. */
  26. public function general()
  27. {
  28. $stat_model = model('stat');
  29. //统计的日期0点
  30. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  31. /*
  32. * 昨日最新情报
  33. */
  34. $stime = $stat_time;
  35. $etime = $stat_time + 86400 - 1;
  36. $statnew_arr = array();
  37. //查询订单表下单量、下单金额、下单客户数、平均客单价
  38. $where = array();
  39. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  40. $where[] = array('order_add_time', 'between', array($stime, $etime));
  41. $field = ' COUNT(*) as ordernum, SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as ordermembernum, AVG(order_amount) as orderavg ';
  42. $stat_order = $stat_model->getoneByStatorder($where, $field);
  43. $statnew_arr['ordernum'] = ($t = $stat_order['ordernum']) ? $t : 0;
  44. $statnew_arr['orderamount'] = ds_price_format(($t = $stat_order['orderamount']) ? $t : (0));
  45. $statnew_arr['ordermembernum'] = ($t = $stat_order['ordermembernum']) ? $t : 0;
  46. $statnew_arr['orderavg'] = ds_price_format(($t = $stat_order['orderavg']) ? $t : 0);
  47. unset($stat_order);
  48. //查询订单商品表下单商品数
  49. $where = array();
  50. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  51. $where[] = array('order_add_time', 'between', array($stime, $etime));
  52. $field = ' SUM(goods_num) as ordergoodsnum,AVG(goods_pay_price/goods_num) as priceavg ';
  53. $stat_ordergoods = $stat_model->getoneByStatordergoods($where, $field);
  54. $statnew_arr['ordergoodsnum'] = ($t = $stat_ordergoods['ordergoodsnum']) ? $t : 0;
  55. $statnew_arr['priceavg'] = ds_price_format(($t = $stat_ordergoods['priceavg']) ? $t : 0);
  56. unset($stat_ordergoods);
  57. //新增会员数
  58. $where = array();
  59. $where[] = array('member_addtime', 'between', array($stime, $etime));
  60. $field = ' COUNT(*) as newmember ';
  61. $stat_member = $stat_model->getOneByMember($where, $field);
  62. $statnew_arr['newmember'] = ($t = $stat_member['newmember']) ? $t : 0;
  63. unset($stat_member);
  64. //会员总数
  65. $where = array();
  66. $field = ' COUNT(*) as membernum ';
  67. $stat_member = $stat_model->getOneByMember($where, $field);
  68. $statnew_arr['membernum'] = ($t = $stat_member['membernum']) ? $t : 0;
  69. unset($stat_member);
  70. //新增店铺
  71. $where = array();
  72. $where[] = array('store_addtime', 'between', array($stime, $etime));
  73. $field = ' COUNT(*) as newstore ';
  74. $stat_store = $stat_model->getOneByStore($where, $field);
  75. $statnew_arr['newstore'] = ($t = $stat_store['newstore']) ? $t : 0;
  76. unset($stat_store);
  77. //店铺总数
  78. $where = array();
  79. $field = ' COUNT(*) as storenum ';
  80. $stat_store = $stat_model->getOneByStore($where, $field);
  81. $statnew_arr['storenum'] = ($t = $stat_store['storenum']) ? $t : 0;
  82. unset($stat_store);
  83. //新增商品,商品总数
  84. $goods_list = $stat_model->statByGoods(array('is_virtual' => 0), "COUNT(*) as goodsnum, SUM(IF(goods_addtime>=$stime and goods_addtime<=$etime,'1',0)) as newgoods");
  85. $statnew_arr['goodsnum'] = ($t = $goods_list[0]['goodsnum']) > 0 ? $t : 0;
  86. $statnew_arr['newgoods'] = ($t = $goods_list[0]['newgoods']) > 0 ? $t : 0;
  87. /*
  88. * 昨日销售走势
  89. */
  90. //构造横轴数据
  91. for ($i = 0; $i < 24; $i++) {
  92. //统计图数据
  93. $curr_arr[$i] = 0; //今天
  94. $up_arr[$i] = 0; //昨天
  95. //横轴
  96. $stat_arr['xAxis']['categories'][] = "$i";
  97. }
  98. $stime = $stat_time - 86400; //昨天0点
  99. $etime = $stat_time + 86400 - 1; //今天24点
  100. $yesterday_day = @date('d', $stime); //昨天日期
  101. $today_day = @date('d', $etime); //今天日期
  102. $where = array();
  103. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  104. $where[] = array('order_add_time', 'between', array($stime, $etime));
  105. $field = ' SUM(order_amount) as orderamount,DAY(FROM_UNIXTIME(order_add_time)) as dayval,HOUR(FROM_UNIXTIME(order_add_time)) as hourval ';
  106. $stat_order = $stat_model->statByStatorder($where, $field, 0, 0, '', 'dayval,hourval');
  107. if ($stat_order) {
  108. foreach ($stat_order as $k => $v) {
  109. if ($today_day == $v['dayval']) {
  110. $curr_arr[$v['hourval']] = intval($v['orderamount']);
  111. }
  112. if ($yesterday_day == $v['dayval']) {
  113. $up_arr[$v['hourval']] = intval($v['orderamount']);
  114. }
  115. }
  116. }
  117. $stat_arr['series'][0]['name'] = lang('yestoday');
  118. $stat_arr['series'][0]['data'] = array_values($up_arr);
  119. $stat_arr['series'][1]['name'] = lang('today');
  120. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  121. //得到统计图数据
  122. $stat_arr['title'] = date('Y-m-d', $stat_time) . lang('sale_trend');
  123. $stat_arr['yAxis'] = lang('stattrade_order_amount');
  124. $stattoday_json = getStatData_LineLabels($stat_arr);
  125. unset($stat_arr);
  126. /*
  127. * 7日内店铺销售TOP30
  128. */
  129. $stime = $stat_time - 86400 * 6; //7天前0点
  130. $etime = $stat_time + 86400 - 1; //今天24点
  131. $where = array();
  132. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  133. $where[] = array('order_add_time', 'between', array($stime, $etime));
  134. $field = ' SUM(order_amount) as orderamount, store_id, store_name ';
  135. $storetop30_arr = $stat_model->statByStatorder($where, $field, 0, 0, 'orderamount desc', 'store_id');
  136. /*
  137. * 7日内商品销售TOP30
  138. */
  139. $stime = $stat_time - 86400 * 6; //7天前0点
  140. $etime = $stat_time + 86400 - 1; //今天24点
  141. $where = array();
  142. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  143. $where[] = array('order_add_time', 'between', array($stime, $etime));
  144. $field = ' sum(goods_num) as ordergoodsnum, goods_id, goods_name ';
  145. $goodstop30_arr = $stat_model->statByStatordergoods($where, $field, 0, 30, 'ordergoodsnum desc', 'goods_id');
  146. View::assign('goodstop30_arr', $goodstop30_arr);
  147. View::assign('storetop30_arr', $storetop30_arr);
  148. View::assign('stattoday_json', $stattoday_json);
  149. View::assign('statnew_arr', $statnew_arr);
  150. View::assign('stat_time', $stat_time);
  151. $this->setAdminCurItem('general');
  152. return View::fetch();
  153. }
  154. /**
  155. * 统计设置
  156. */
  157. public function setting()
  158. {
  159. $config_model = model('config');
  160. if (request()->isPost()) {
  161. $update_array = array();
  162. $pricerange_temp_array = input('post.pricerange/a');
  163. if (is_array($pricerange_temp_array)) {
  164. foreach ($pricerange_temp_array as $k => $v) {
  165. if (!is_numeric($v['s']) || !is_numeric($v['e'])) {
  166. $this->error(lang('is_numeric_error'));
  167. }
  168. if ($v['s'] < 0 || $v['e'] < 0) {
  169. $this->error(lang('is_zero_error'));
  170. }
  171. if ($v['s'] > $v['e']) {
  172. $this->error(lang('amount_set_error'));
  173. }
  174. $pricerange_arr[] = $v;
  175. }
  176. $update_array['stat_pricerange'] = serialize($pricerange_arr);
  177. } else {
  178. $update_array['stat_pricerange'] = '';
  179. }
  180. $result = $config_model->editConfig($update_array);
  181. if ($result === true) {
  182. $this->log(lang('ds_edit') . lang('stat_setting'), 1);
  183. $this->success(lang('ds_common_save_succ'));
  184. } else {
  185. $this->log(lang('ds_edit') . lang('stat_setting'), 0);
  186. $this->error(lang('ds_common_save_fail'));
  187. }
  188. } else {
  189. $list_setting = rkcache('config', true);
  190. $list_setting['stat_pricerange'] = unserialize($list_setting['stat_pricerange']);
  191. View::assign('list_setting', $list_setting);
  192. $this->setAdminCurItem('setting');
  193. return View::fetch();
  194. }
  195. }
  196. /**
  197. * 统计设置
  198. */
  199. public function orderprange()
  200. {
  201. $config_model = model('config');
  202. if (request()->isPost()) {
  203. $update_array = array();
  204. $pricerange_temp_array = input('post.pricerange/a');
  205. if (is_array($pricerange_temp_array)) {
  206. foreach ($pricerange_temp_array as $k => $v) {
  207. if (!is_numeric($v['s']) || !is_numeric($v['e'])) {
  208. $this->error(lang('is_numeric_error'));
  209. }
  210. if ($v['s'] < 0 || $v['e'] < 0) {
  211. $this->error(lang('is_zero_error'));
  212. }
  213. if ($v['s'] > $v['e']) {
  214. $this->error(lang('amount_set_error'));
  215. }
  216. $pricerange_arr[] = $v;
  217. }
  218. $update_array['stat_orderpricerange'] = serialize($pricerange_arr);
  219. } else {
  220. $update_array['stat_orderpricerange'] = '';
  221. }
  222. $result = $config_model->editConfig($update_array);
  223. if ($result === true) {
  224. $this->log(lang('ds_edit') . lang('stat_setting'), 1);
  225. $this->success(lang('ds_common_save_succ'));
  226. } else {
  227. $this->log(lang('ds_edit') . lang('stat_setting'), 0);
  228. $this->error(lang('ds_common_save_fail'));
  229. }
  230. } else {
  231. $list_setting = rkcache('config', true);
  232. $list_setting['stat_orderpricerange'] = unserialize($list_setting['stat_orderpricerange']);
  233. View::assign('list_setting', $list_setting);
  234. $this->setAdminCurItem('orderprange');
  235. return View::fetch();
  236. }
  237. }
  238. protected function getAdminItemList()
  239. {
  240. $menu_array = array(
  241. array(
  242. 'name' => 'general', 'text' => lang('stat_generalindex'), 'url' => (string)url('Statgeneral/general')
  243. ), array(
  244. 'name' => 'setting', 'text' => lang('stat_goodspricerange'), 'url' => (string)url('Statgeneral/setting')
  245. ), array(
  246. 'name' => 'orderprange', 'text' => lang('stat_orderpricerange'), 'url' => (string)url('Statgeneral/orderprange')
  247. )
  248. );
  249. return $menu_array;
  250. }
  251. }