Statisticssale.php 26 KB

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