Statgeneral.php 12 KB

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