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