Statisticssale.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. <?php
  2. /**
  3. * 运营报告
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. use think\facade\Db;
  9. /**
  10. *
  11. *
  12. * ----------------------------------------------------------------------------
  13. *
  14. * 控制器
  15. */
  16. class Statisticssale extends BaseSeller
  17. {
  18. private $search_arr; //处理后的参数
  19. private $gc_arr; //分类数组
  20. private $choose_gcid; //选择的分类ID
  21. public function initialize()
  22. {
  23. parent::initialize(); // TODO: Change the autogenerated stub
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statisticssale.lang.php');
  25. include_once root_path() . 'extend/mall/statistics.php';
  26. include_once root_path() . 'extend/mall/datehelper.php';
  27. $stat_model = model('stat');
  28. //存储参数
  29. $this->search_arr = input('param.');
  30. //处理搜索时间
  31. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  32. //获得系统年份
  33. $year_arr = getSystemYearArr();
  34. //获得系统月份
  35. $month_arr = getSystemMonthArr();
  36. //获得本月的周时间段
  37. $week_arr = @getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  38. View::assign('year_arr', $year_arr);
  39. View::assign('month_arr', $month_arr);
  40. View::assign('week_arr', $week_arr);
  41. View::assign('search_arr', $this->search_arr);
  42. }
  43. /**
  44. * 销售统计
  45. */
  46. public function index()
  47. {
  48. $stat_model = model('stat');
  49. //默认统计当前数据
  50. if (!isset($this->search_arr['search_type'])) {
  51. $this->search_arr['search_type'] = 'day';
  52. }
  53. //计算昨天和今天时间
  54. if ($this->search_arr['search_type'] == 'day') {
  55. $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
  56. $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
  57. $curr_stime = $this->search_arr['day']['search_time']; //今天0点
  58. } elseif ($this->search_arr['search_type'] == 'week') {
  59. $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
  60. $stime = strtotime($current_weekarr[0]) - 86400 * 7;
  61. $etime = strtotime($current_weekarr[1]) + 86400 - 1;
  62. $curr_stime = strtotime($current_weekarr[0]); //本周0点
  63. } elseif ($this->search_arr['search_type'] == 'month') {
  64. $stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01 -1 month");
  65. $etime = getMonthLastDay($this->search_arr['month']['current_year'], $this->search_arr['month']['current_month']) + 86400 - 1;
  66. $curr_stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01");; //本月0点
  67. }
  68. $where = array();
  69. $where[] = array('store_id', '=', session('store_id'));
  70. $where[] = array('order_add_time', 'between', array($stime, $etime));
  71. if (trim(input('param.order_type')) != '') {
  72. $where[] = array('order_state', '=', trim(input('param.order_type')));
  73. }
  74. //走势图
  75. $field = ' COUNT(*) as ordernum,SUM(order_amount) as orderamount ';
  76. $stat_arr = array();
  77. //$searchtime_arr = array($stime,$etime);
  78. if ($this->search_arr['search_type'] == 'day') {
  79. //构造横轴数据
  80. for ($i = 0; $i < 24; $i++) {
  81. //统计图数据
  82. $curr_arr['orderamount'][$i] = 0; //今天
  83. $up_arr['orderamount'][$i] = 0; //昨天
  84. $curr_arr['ordernum'][$i] = 0; //今天
  85. $up_arr['ordernum'][$i] = 0; //昨天
  86. //横轴
  87. $stat_arr['orderamount']['xAxis']['categories'][] = "$i";
  88. $stat_arr['ordernum']['xAxis']['categories'][] = "$i";
  89. }
  90. $today_day = @date('d', $etime); //今天日期
  91. $yesterday_day = @date('d', $stime); //昨天日期
  92. $field .= ' ,DAY(FROM_UNIXTIME(order_add_time)) as dayval,HOUR(FROM_UNIXTIME(order_add_time)) as hourval ';
  93. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', '');
  94. foreach ((array)$orderlist as $k => $v) {
  95. if ($today_day == $v['dayval']) {
  96. $curr_arr['ordernum'][$v['hourval']] = intval($v['ordernum']);
  97. $curr_arr['orderamount'][$v['hourval']] = floatval($v['orderamount']);
  98. }
  99. if ($yesterday_day == $v['dayval']) {
  100. $up_arr['ordernum'][$v['hourval']] = intval($v['ordernum']);
  101. $up_arr['orderamount'][$v['hourval']] = floatval($v['orderamount']);
  102. }
  103. }
  104. $stat_arr['ordernum']['series'][0]['name'] = lang('yesterday');
  105. $stat_arr['ordernum']['series'][0]['data'] = array_values($up_arr['ordernum']);
  106. $stat_arr['ordernum']['series'][1]['name'] = lang('today');
  107. $stat_arr['ordernum']['series'][1]['data'] = array_values($curr_arr['ordernum']);
  108. $stat_arr['orderamount']['series'][0]['name'] = lang('yesterday');
  109. $stat_arr['orderamount']['series'][0]['data'] = array_values($up_arr['orderamount']);
  110. $stat_arr['orderamount']['series'][1]['name'] = lang('today');
  111. $stat_arr['orderamount']['series'][1]['data'] = array_values($curr_arr['orderamount']);
  112. }
  113. if ($this->search_arr['search_type'] == 'week') {
  114. $up_week = @date('W', $stime); //上周
  115. $curr_week = @date('W', $etime); //本周
  116. //构造横轴数据
  117. for ($i = 1; $i <= 7; $i++) {
  118. $tmp_weekarr = getSystemWeekArr();
  119. //统计图数据
  120. $up_arr['ordernum'][$i] = 0;
  121. $curr_arr['ordernum'][$i] = 0;
  122. $up_arr['orderamount'][$i] = 0;
  123. $curr_arr['orderamount'][$i] = 0;
  124. //横轴
  125. $stat_arr['ordernum']['xAxis']['categories'][] = $tmp_weekarr[$i];
  126. $stat_arr['orderamount']['xAxis']['categories'][] = $tmp_weekarr[$i];
  127. unset($tmp_weekarr);
  128. }
  129. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(order_add_time)) as weekval,WEEKDAY(FROM_UNIXTIME(order_add_time))+1 as dayofweekval ';
  130. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', '');
  131. foreach ((array)$orderlist as $k => $v) {
  132. if ($up_week == $v['weekval']) {
  133. $up_arr['ordernum'][$v['dayofweekval']] = intval($v['ordernum']);
  134. $up_arr['orderamount'][$v['dayofweekval']] = intval($v['orderamount']);
  135. }
  136. if ($curr_week == $v['weekval']) {
  137. $curr_arr['ordernum'][$v['dayofweekval']] = intval($v['ordernum']);
  138. $curr_arr['orderamount'][$v['dayofweekval']] = intval($v['orderamount']);
  139. }
  140. }
  141. $stat_arr['ordernum']['series'][0]['name'] = lang('last_week');
  142. $stat_arr['ordernum']['series'][0]['data'] = array_values($up_arr['ordernum']);
  143. $stat_arr['ordernum']['series'][1]['name'] = lang('this_week');
  144. $stat_arr['ordernum']['series'][1]['data'] = array_values($curr_arr['ordernum']);
  145. $stat_arr['orderamount']['series'][0]['name'] = lang('last_week');
  146. $stat_arr['orderamount']['series'][0]['data'] = array_values($up_arr['orderamount']);
  147. $stat_arr['orderamount']['series'][1]['name'] = lang('this_week');
  148. $stat_arr['orderamount']['series'][1]['data'] = array_values($curr_arr['orderamount']);
  149. }
  150. if ($this->search_arr['search_type'] == 'month') {
  151. $up_month = date('m', $stime);
  152. $curr_month = date('m', $etime);
  153. //计算横轴的最大量(由于每个月的天数不同)
  154. $up_dayofmonth = date('t', $stime);
  155. $curr_dayofmonth = date('t', $etime);
  156. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  157. //构造横轴数据
  158. for ($i = 1; $i <= $x_max; $i++) {
  159. //统计图数据
  160. $up_arr['ordernum'][$i] = 0;
  161. $curr_arr['ordernum'][$i] = 0;
  162. $up_arr['orderamount'][$i] = 0;
  163. $curr_arr['orderamount'][$i] = 0;
  164. //横轴
  165. $stat_arr['ordernum']['xAxis']['categories'][] = $i;
  166. $stat_arr['orderamount']['xAxis']['categories'][] = $i;
  167. }
  168. $field .= ',MONTH(FROM_UNIXTIME(order_add_time)) as monthval,day(FROM_UNIXTIME(order_add_time)) as dayval ';
  169. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'monthval,dayval');
  170. foreach ($orderlist as $k => $v) {
  171. if ($up_month == $v['monthval']) {
  172. $up_arr['ordernum'][$v['dayval']] = intval($v['ordernum']);
  173. $up_arr['orderamount'][$v['dayval']] = floatval($v['orderamount']);
  174. }
  175. if ($curr_month == $v['monthval']) {
  176. $curr_arr['ordernum'][$v['dayval']] = intval($v['ordernum']);
  177. $curr_arr['orderamount'][$v['dayval']] = intval($v['orderamount']);
  178. }
  179. }
  180. $stat_arr['ordernum']['series'][0]['name'] = lang('last_month');
  181. $stat_arr['ordernum']['series'][0]['data'] = array_values($up_arr['ordernum']);
  182. $stat_arr['ordernum']['series'][1]['name'] = lang('this_month');
  183. $stat_arr['ordernum']['series'][1]['data'] = array_values($curr_arr['ordernum']);
  184. $stat_arr['orderamount']['series'][0]['name'] = lang('last_month');
  185. $stat_arr['orderamount']['series'][0]['data'] = array_values($up_arr['orderamount']);
  186. $stat_arr['orderamount']['series'][1]['name'] = lang('this_month');
  187. $stat_arr['orderamount']['series'][1]['data'] = array_values($curr_arr['orderamount']);
  188. }
  189. $stat_arr['ordernum']['title'] = lang('order_statistics');
  190. $stat_arr['ordernum']['yAxis'] = lang('order_quantity');
  191. $stat_arr['orderamount']['title'] = lang('order_amount_statistics');
  192. $stat_arr['orderamount']['yAxis'] = lang('place_order_amount');
  193. $stat_json['ordernum'] = getStatData_LineLabels($stat_arr['ordernum']);
  194. $stat_json['orderamount'] = getStatData_LineLabels($stat_arr['orderamount']);
  195. View::assign('stat_json', $stat_json);
  196. View::assign('stattype', $this->search_arr['search_type']);
  197. //总数统计
  198. $where = array();
  199. $where[] = array('store_id', '=', session('store_id'));
  200. $where[] = array('order_add_time', 'between', array($curr_stime, $etime));
  201. if (trim(input('param.order_type')) != '') {
  202. $where[] = array('order_state', '=', trim(input('param.order_type')));
  203. }
  204. $statcount_arr = $stat_model->getoneByStatorder($where, ' COUNT(*) as ordernum, SUM(order_amount) as orderamount');
  205. $statcount_arr['ordernum'] = ($t = intval($statcount_arr['ordernum'])) > 0 ? $t : 0;
  206. $statcount_arr['orderamount'] = ds_price_format(($t = floatval($statcount_arr['orderamount'])) > 0 ? $t : 0);
  207. View::assign('statcount_arr', $statcount_arr);
  208. View::assign('searchtime', implode(';', array($curr_stime, $etime)));
  209. $this->setSellerCurMenu('Statisticssale');
  210. $this->setSellerCurItem('sale');
  211. return View::fetch($this->template_dir . 'index');
  212. }
  213. /**
  214. * 订单列表
  215. */
  216. public function salelist()
  217. {
  218. $stat_model = model('stat');
  219. $searchtime_arr_tmp = explode(';', urldecode($this->search_arr['t']));
  220. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  221. $searchtime_arr[] = intval($v);
  222. }
  223. $where = array();
  224. $where[] = array('store_id', '=', session('store_id'));
  225. $where[] = array('order_add_time', 'between', $searchtime_arr);
  226. if (trim(input('param.order_type')) != '') {
  227. $where[] = array('order_state', '=', trim(input('param.order_type')));
  228. }
  229. if (input('param.exporttype') == 'excel') {
  230. $order_list = $stat_model->statByStatorder($where, '', 0, 0, 'order_id desc');
  231. } else {
  232. $order_list = $stat_model->statByStatorder($where, '', 10, 0, 'order_id desc');
  233. }
  234. //统计数据标题
  235. $statlist = array();
  236. $statheader = array();
  237. $statheader[] = array('text' => lang('order_no'), 'key' => 'order_sn');
  238. $statheader[] = array('text' => lang('buyers'), 'key' => 'buyer_name');
  239. $statheader[] = array('text' => lang('place_order_time'), 'key' => 'order_add_time');
  240. $statheader[] = array('text' => lang('total_order'), 'key' => 'order_amount');
  241. $statheader[] = array('text' => lang('order_status'), 'key' => 'order_statetext');
  242. foreach ((array)$order_list as $k => $v) {
  243. $v['order_add_time'] = @date('Y-m-d H:i:s', $v['order_add_time']);
  244. switch ($v['order_state']) {
  245. case ORDER_STATE_CANCEL:
  246. $v['order_statetext'] = lang('canceled');
  247. break;
  248. case ORDER_STATE_NEW:
  249. $v['order_statetext'] = lang('waiting_payment');
  250. break;
  251. case ORDER_STATE_PAY:
  252. $v['order_statetext'] = lang('waiting_delivery');
  253. break;
  254. case ORDER_STATE_SEND:
  255. $v['order_statetext'] = lang('waiting_receiving');
  256. break;
  257. case ORDER_STATE_SUCCESS:
  258. $v['order_statetext'] = lang('transaction_completed');
  259. break;
  260. }
  261. $statlist[$k] = $v;
  262. }
  263. //导出Excel
  264. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  265. //导出Excel
  266. $excel_obj = new \excel\Excel();
  267. $excel_data = array();
  268. //设置样式
  269. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => lang('song_typeface'), 'Size' => '12', 'Bold' => '1')));
  270. //header
  271. foreach ($statheader as $k => $v) {
  272. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v['text']);
  273. }
  274. //data
  275. foreach ($statlist as $k => $v) {
  276. foreach ($statheader as $h_k => $h_v) {
  277. $excel_data[$k + 1][] = array('data' => $v[$h_v['key']]);
  278. }
  279. }
  280. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  281. $excel_obj->addArray($excel_data);
  282. $excel_obj->addWorksheet($excel_obj->charset(lang('order_list'), CHARSET));
  283. $excel_obj->generateXML($excel_obj->charset(lang('order_list'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  284. exit();
  285. }
  286. View::assign('statheader', $statheader);
  287. View::assign('statlist', $statlist);
  288. View::assign('show_page', $stat_model->page_info->render());
  289. View::assign('actionurl', (string)url('Statisticssale/salelist', ['t' => $this->search_arr['t']]));
  290. echo View::fetch($this->template_dir . 'stat_listandorder');
  291. }
  292. /**
  293. * 地区分布
  294. */
  295. public function area()
  296. {
  297. if (!isset($this->search_arr['search_type'])) {
  298. $this->search_arr['search_type'] = 'day';
  299. }
  300. $stat_model = model('stat');
  301. //获得搜索的开始时间和结束时间
  302. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  303. $where = array();
  304. $where[] = array('store_id', '=', session('store_id'));
  305. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  306. $where[] = array('order_add_time', 'between', $searchtime_arr);
  307. $memberlist = array();
  308. //查询统计数据
  309. $field = ' reciver_province_id,SUM(order_amount) as orderamount,COUNT(*) as ordernum,COUNT(DISTINCT buyer_id) as membernum ';
  310. $orderby = 'reciver_province_id';
  311. $statlist = $stat_model->statByStatorder($where, $field, 10, 0, $orderby, 'reciver_province_id');
  312. $datatype = array('orderamount' => lang('place_order_amount'), 'ordernum' => lang('order_quantity'), 'membernum' => lang('number_members_placed'));
  313. //处理数组,将数组按照$datatype分组排序
  314. $statlist_tmp = array();
  315. foreach ((array)$statlist as $k => $v) {
  316. foreach ((array)$datatype as $t_k => $t_v) {
  317. $statlist_tmp[$t_k][$k] = $v[$t_k];
  318. }
  319. }
  320. foreach ((array)$statlist_tmp as $t_k => $t_v) {
  321. arsort($statlist_tmp[$t_k]);
  322. }
  323. $statlist_tmp2 = $statlist_tmp;
  324. $statlist_tmp = array();
  325. foreach ((array)$statlist_tmp2 as $t_k => $t_v) {
  326. foreach ($t_v as $k => $v) {
  327. $statlist[$k]['orderamount'] = floatval($statlist[$k]['orderamount']);
  328. $statlist[$k]['ordernum'] = intval($statlist[$k]['ordernum']);
  329. $statlist[$k]['membernum'] = intval($statlist[$k]['membernum']);
  330. $statlist_tmp[$t_k][] = $statlist[$k];
  331. }
  332. }
  333. // 地区
  334. $province_array = model('area')->getTopLevelAreas();
  335. //地图显示等级数组
  336. $level_arr = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9), array(10, 11, 12));
  337. $statlist = array();
  338. $stat_arr_bar = array();
  339. foreach ((array)$statlist_tmp as $t_k => $t_v) {
  340. foreach ((array)$t_v as $k => $v) {
  341. $v['level'] = 4; //排名
  342. foreach ($level_arr as $lk => $lv) {
  343. if (in_array($k + 1, $lv)) {
  344. $v['level'] = $lk; //排名
  345. }
  346. }
  347. $province_id = intval($v['reciver_province_id']);
  348. $statlist[$t_k][$province_id] = $v;
  349. if ($province_id) {
  350. //数据
  351. $stat_arr_bar[$t_k]['series'][0]['data'][] = array(
  352. 'name' => strval($province_array[$province_id]), 'y' => $v[$t_k]
  353. );
  354. //横轴
  355. $stat_arr_bar[$t_k]['xAxis']['categories'][] = strval($province_array[$province_id]);
  356. } else {
  357. //数据
  358. $stat_arr_bar[$t_k]['series'][0]['data'][] = array('name' => lang('unknown'), 'y' => $v[$t_k]);
  359. //横轴
  360. $stat_arr_bar[$t_k]['xAxis']['categories'][] = lang('unknown');
  361. }
  362. }
  363. }
  364. $stat_arr_map = array();
  365. foreach ((array)$province_array as $k => $v) {
  366. foreach ($datatype as $t_k => $t_v) {
  367. if (@$statlist[$t_k][$k][$t_k]) {
  368. $des = ",{$t_v}:{$statlist[$t_k][$k][$t_k]}";
  369. $stat_arr_map[$t_k][] = array(
  370. 'cha' => $k, 'name' => $v, 'des' => $des, 'level' => $statlist[$t_k][$k]['level']
  371. );
  372. } else {
  373. $des = lang('no_order_data');
  374. $stat_arr_map[$t_k][] = array('cha' => $k, 'name' => $v, 'des' => $des, 'level' => 4);
  375. }
  376. }
  377. }
  378. $stat_json_map = array();
  379. $stat_json_bar = array();
  380. if ($statlist) {
  381. foreach ($datatype as $t_k => $t_v) {
  382. //得到地图数据
  383. $stat_json_map[$t_k] = getStatData_Map($stat_arr_map[$t_k]);
  384. //得到统计图数据
  385. $stat_arr_bar[$t_k]['series'][0]['name'] = $t_v;
  386. $stat_arr_bar[$t_k]['title'] = lang('regional_ranking');
  387. $stat_arr_bar[$t_k]['legend']['enabled'] = false;
  388. $stat_arr_bar[$t_k]['yAxis']['title']['text'] = $t_v;
  389. $stat_arr_bar[$t_k]['yAxis']['title']['align'] = 'high';
  390. $stat_json_bar[$t_k] = getStatData_Basicbar($stat_arr_bar[$t_k]);
  391. }
  392. }
  393. View::assign('stat_json_map', $stat_json_map);
  394. View::assign('stat_json_bar', $stat_json_bar);
  395. $this->setSellerCurMenu('Statisticssale');
  396. $this->setSellerCurItem('area');
  397. return View::fetch($this->template_dir . 'area');
  398. }
  399. /**
  400. * 购买分析
  401. */
  402. public function buying()
  403. {
  404. if (!isset($this->search_arr['search_type'])) {
  405. $this->search_arr['search_type'] = 'day';
  406. }
  407. $stat_model = model('stat');
  408. //获得搜索的开始时间和结束时间
  409. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  410. /*
  411. * 客单价分布
  412. */
  413. $where = array();
  414. $where[] = array('store_id', '=', session('store_id'));
  415. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  416. $where[] = array('order_add_time', 'between time', $searchtime_arr);
  417. $field = '1';
  418. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'orderpricerange');
  419. $pricerange_arr = $pricerange ? unserialize($pricerange) : array();
  420. if ($pricerange_arr) {
  421. $stat_arr['series'][0]['name'] = lang('order_quantity');
  422. //设置价格区间最后一项,最后一项只有开始值没有结束值
  423. $pricerange_count = count($pricerange_arr);
  424. if ($pricerange_arr[$pricerange_count - 1]['e']) {
  425. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count - 1]['e'] + 1;
  426. $pricerange_arr[$pricerange_count]['e'] = '';
  427. }
  428. foreach ((array)$pricerange_arr as $k => $v) {
  429. $v['s'] = intval($v['s']);
  430. $v['e'] = intval($v['e']);
  431. //构造查询字段
  432. if ($v['e']) {
  433. $field .= " ,SUM(IF(order_amount > {$v['s']} and order_amount <= {$v['e']},1,0)) as ordernum_{$k}";
  434. } else {
  435. $field .= " ,SUM(IF(order_amount > $v[s],1,0)) as ordernum_$k";
  436. }
  437. }
  438. $orderlist = Db::query('SELECT ' . $field . ' FROM ' . config('database.connections.mysql.prefix') . 'statorder WHERE store_id=' . session('store_id') . ' AND order_isvalid=1 AND order_add_time BETWEEN ' . $searchtime_arr[0] . ' AND ' . $searchtime_arr[1]);
  439. if ($orderlist) {
  440. $orderlist = current($orderlist);
  441. foreach ((array)$pricerange_arr as $k => $v) {
  442. //横轴
  443. if ($v['e']) {
  444. $stat_arr['xAxis']['categories'][] = $v['s'] . '-' . $v['e'];
  445. } else {
  446. $stat_arr['xAxis']['categories'][] = $v['s'] . lang('above');
  447. }
  448. //统计图数据
  449. if (isset($orderlist['ordernum_' . $k])) {
  450. $stat_arr['series'][0]['data'][] = intval($orderlist['ordernum_' . $k]);
  451. } else {
  452. $stat_arr['series'][0]['data'][] = 0;
  453. }
  454. }
  455. }
  456. //得到统计图数据
  457. $stat_arr['title'] = lang('passenger_unit_price_distribution');
  458. $stat_arr['legend']['enabled'] = false;
  459. $stat_arr['yAxis'] = lang('order_quantity');
  460. $guestprice_statjson = getStatData_LineLabels($stat_arr);
  461. } else {
  462. $guestprice_statjson = '';
  463. }
  464. unset($stat_arr);
  465. //购买时段分布
  466. $where = array();
  467. $where[] = array('store_id', '=', session('store_id'));
  468. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  469. $where[] = array('order_add_time', 'between', $searchtime_arr);
  470. $field = ' HOUR(FROM_UNIXTIME(order_add_time)) as hourval,COUNT(*) as ordernum ';
  471. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'hourval');
  472. $stat_arr = array();
  473. $stat_arr['series'][0]['name'] = lang('order_quantity');
  474. //构造横轴坐标
  475. for ($i = 0; $i < 24; $i++) {
  476. //横轴
  477. $stat_arr['xAxis']['categories'][] = $i;
  478. $stat_arr['series'][0]['data'][$i] = 0;
  479. }
  480. foreach ((array)$orderlist as $k => $v) {
  481. //统计图数据
  482. $stat_arr['series'][0]['data'][$v['hourval']] = intval($v['ordernum']);
  483. }
  484. //得到统计图数据
  485. $stat_arr['title'] = lang('purchase_time_distribution');
  486. $stat_arr['legend']['enabled'] = false;
  487. $stat_arr['yAxis'] = lang('order_quantity');
  488. $hour_statjson = getStatData_LineLabels($stat_arr);
  489. View::assign('hour_statjson', $hour_statjson);
  490. View::assign('guestprice_statjson', $guestprice_statjson);
  491. $this->setSellerCurMenu('Statisticssale');
  492. $this->setSellerCurItem('buying');
  493. return View::fetch($this->template_dir . 'stat_buying');
  494. }
  495. /**
  496. * 用户中心右边,小导航
  497. *
  498. * @param string $menu_type 导航类型
  499. * @param string $name 当前导航的name
  500. * @return
  501. */
  502. protected function getSellerItemList()
  503. {
  504. $menu_array = array(
  505. array(
  506. 'name' => 'sale', 'text' => lang('sales_statistics'), 'url' => (string)url('Statisticssale/index')
  507. ), array(
  508. 'name' => 'area', 'text' => lang('regional_distribution'), 'url' => (string)url('Statisticssale/area')
  509. ), array(
  510. 'name' => 'buying', 'text' => lang('buy_analysis'), 'url' => (string)url('Statisticssale/buying')
  511. ),
  512. );
  513. return $menu_array;
  514. }
  515. }