Statgeneral.php 12 KB

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