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