Statisticssale.php 27 KB

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