Statstore.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. <?php
  2. /**
  3. * 店铺统计
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Db;
  8. use excel\Excel;
  9. use think\facade\Lang;
  10. /**
  11. * ============================================================================
  12. *
  13. * ============================================================================
  14. *
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Statstore extends AdminControl
  21. {
  22. private $search_arr; //处理后的参数
  23. private $store_class; //店铺分类
  24. public function initialize()
  25. {
  26. parent::initialize(); // TODO: Change the autogenerated stub
  27. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/stat.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. if (in_array(request()->action(), array('hotrank', 'storesales'))) {
  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. }
  46. View::assign('search_arr', $this->search_arr);
  47. //店铺分类
  48. $this->store_class = rkcache('storeclass', true);
  49. View::assign('store_class', $this->store_class);
  50. }
  51. /**
  52. * 新增店铺
  53. */
  54. public function newstore()
  55. {
  56. //导出excel连接地址
  57. $actionurl = (string)url('Statstore/newstore');
  58. $where = array();
  59. //所属店铺分类
  60. $search_sclass = intval(input('param.search_sclass'));
  61. if ($search_sclass > 0) {
  62. $where[] = array('storeclass_id', '=', $search_sclass);;
  63. $actionurl .= "&search_sclass=$search_sclass";
  64. }
  65. $field = ' count(*) as allnum ';
  66. if (!input('param.search_type')) {
  67. $search_type = 'day';
  68. } else {
  69. $search_type = input('param.search_type');
  70. }
  71. //初始化时间
  72. //天
  73. if (!input('param.search_time')) {
  74. $search_times = date('Y-m-d', TIMESTAMP - 86400);
  75. } else {
  76. $search_times = input('param.search_time');
  77. }
  78. $search_time = strtotime($search_times); //搜索的时间
  79. View::assign('search_time', $search_times);
  80. //周
  81. if (!input('param.search_time_year')) {
  82. $search_time_year = date('Y', TIMESTAMP);
  83. } else {
  84. $search_time_year = input('param.search_time_year');
  85. }
  86. if (!input('param.search_time_month')) {
  87. $search_time_month = date('m', TIMESTAMP);
  88. } else {
  89. $search_time_month = input('param.search_time_month');
  90. }
  91. if (!input('param.search_time_week')) {
  92. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  93. } else {
  94. $search_time_week = input('param.search_time_week');
  95. }
  96. $current_year = $search_time_year;
  97. $current_month = $search_time_month;
  98. $current_week = $search_time_week;
  99. $year_arr = getSystemYearArr();
  100. $month_arr = getSystemMonthArr();
  101. $week_arr = getMonthWeekArr($current_year, $current_month);
  102. View::assign('current_year', $current_year);
  103. View::assign('current_month', $current_month);
  104. View::assign('current_week', $current_week);
  105. View::assign('year_arr', $year_arr);
  106. View::assign('month_arr', $month_arr);
  107. View::assign('week_arr', $week_arr);
  108. $stat_model = model('stat');
  109. $statlist = array(); //统计数据列表
  110. if ($search_type == 'day') {
  111. //构造横轴数据
  112. for ($i = 0; $i < 24; $i++) {
  113. //统计图数据
  114. $curr_arr[$i] = 0; //今天
  115. $up_arr[$i] = 0; //昨天
  116. //统计表数据
  117. $uplist_arr[$i]['timetext'] = $i;
  118. $currlist_arr[$i]['timetext'] = $i;
  119. //方便搜索会员列表,计算开始时间和结束时间
  120. $currlist_arr[$i]['stime'] = $search_time + $i * 3600;
  121. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 3600;
  122. $uplist_arr[$i]['val'] = 0;
  123. $currlist_arr[$i]['val'] = 0;
  124. //横轴
  125. $stat_arr['xAxis']['categories'][] = "$i";
  126. }
  127. $stime = $search_time - 86400; //昨天0点
  128. $etime = $search_time + 86400 - 1; //今天24点
  129. $today_day = @date('d', $search_time); //今天日期
  130. $yesterday_day = @date('d', $stime); //昨天日期
  131. $where[] = array('store_addtime', 'between', array($stime, $etime));
  132. $field .= ' ,DAY(FROM_UNIXTIME(store_addtime)) as dayval,HOUR(FROM_UNIXTIME(store_addtime)) as hourval ';
  133. $memberlist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'dayval,dayval');
  134. if ($memberlist) {
  135. foreach ($memberlist as $k => $v) {
  136. if ($today_day == $v['dayval']) {
  137. $curr_arr[$v['hourval']] = intval($v['allnum']);
  138. $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
  139. }
  140. if ($yesterday_day == $v['dayval']) {
  141. $up_arr[$v['hourval']] = intval($v['allnum']);
  142. $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
  143. }
  144. }
  145. }
  146. $stat_arr['series'][0]['name'] = lang('yestoday');
  147. $stat_arr['series'][0]['data'] = array_values($up_arr);
  148. $stat_arr['series'][1]['name'] = lang('today');
  149. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  150. //统计数据标题
  151. $statlist['headertitle'] = array(lang('hour'), lang('yestoday'), lang('today'), lang('compare_to'));
  152. View::assign('actionurl', $actionurl . '&search_type=day&search_time=' . date('Y-m-d', $search_time));
  153. }
  154. if ($search_type == 'week') {
  155. $current_weekarr = explode('|', $current_week);
  156. $stime = strtotime($current_weekarr[0]) - 86400 * 7;
  157. $etime = strtotime($current_weekarr[1]) + 86400 - 1;
  158. $up_week = @date('W', $stime); //上周
  159. $curr_week = @date('W', $etime); //本周
  160. //构造横轴数据
  161. for ($i = 1; $i <= 7; $i++) {
  162. //统计图数据
  163. $up_arr[$i] = 0;
  164. $curr_arr[$i] = 0;
  165. $tmp_weekarr = getSystemWeekArr();
  166. //统计表数据
  167. $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  168. $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  169. //方便搜索会员列表,计算开始时间和结束时间
  170. $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0]) + ($i - 1) * 86400;
  171. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;
  172. $uplist_arr[$i]['val'] = 0;
  173. $currlist_arr[$i]['val'] = 0;
  174. //横轴
  175. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  176. unset($tmp_weekarr);
  177. }
  178. $where[] = array('store_addtime', 'between', array($stime, $etime));
  179. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(store_addtime)) as weekval,WEEKDAY(FROM_UNIXTIME(store_addtime))+1 as dayofweekval ';
  180. $memberlist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'weekval,dayofweekval');
  181. if ($memberlist) {
  182. foreach ($memberlist as $k => $v) {
  183. if ($up_week == $v['weekval']) {
  184. $up_arr[$v['dayofweekval']] = intval($v['allnum']);
  185. $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
  186. }
  187. if ($curr_week == $v['weekval']) {
  188. $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
  189. $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
  190. }
  191. }
  192. }
  193. $stat_arr['series'][0]['name'] = lang('last_week');
  194. $stat_arr['series'][0]['data'] = array_values($up_arr);
  195. $stat_arr['series'][1]['name'] = lang('this_week');
  196. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  197. //统计数据标题
  198. $statlist['headertitle'] = array(lang('week'), lang('last_week'), lang('this_week'), lang('compare_to'));
  199. View::assign('actionurl', $actionurl . '&search_type=week&search_time_year=' . $current_year . '&search_time_month=' . $current_month . '&search_time_week=' . $current_week);
  200. }
  201. if ($search_type == 'month') {
  202. $stime = strtotime($current_year . '-' . $current_month . "-01 -1 month");
  203. $etime = getMonthLastDay($current_year, $current_month) + 86400 - 1;
  204. $up_month = date('m', $stime);
  205. $curr_month = date('m', $etime);
  206. //计算横轴的最大量(由于每个月的天数不同)
  207. $up_dayofmonth = date('t', $stime);
  208. $curr_dayofmonth = date('t', $etime);
  209. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  210. //构造横轴数据
  211. for ($i = 1; $i <= $x_max; $i++) {
  212. //统计图数据
  213. $up_arr[$i] = 0;
  214. $curr_arr[$i] = 0;
  215. //统计表数据
  216. $uplist_arr[$i]['timetext'] = $i;
  217. $currlist_arr[$i]['timetext'] = $i;
  218. //方便搜索会员列表,计算开始时间和结束时间
  219. $currlist_arr[$i]['stime'] = strtotime($current_year . '-' . $current_month . "-01") + ($i - 1) * 86400;
  220. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;
  221. $uplist_arr[$i]['val'] = 0;
  222. $currlist_arr[$i]['val'] = 0;
  223. //横轴
  224. $stat_arr['xAxis']['categories'][] = $i;
  225. }
  226. $where[] = array('store_addtime', 'between', array($stime, $etime));
  227. $field .= ',MONTH(FROM_UNIXTIME(store_addtime)) as monthval,day(FROM_UNIXTIME(store_addtime)) as dayval ';
  228. $memberlist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'monthval,dayval');
  229. if ($memberlist) {
  230. foreach ($memberlist as $k => $v) {
  231. if ($up_month == $v['monthval']) {
  232. $up_arr[$v['dayval']] = intval($v['allnum']);
  233. $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
  234. }
  235. if ($curr_month == $v['monthval']) {
  236. $curr_arr[$v['dayval']] = intval($v['allnum']);
  237. $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
  238. }
  239. }
  240. }
  241. $stat_arr['series'][0]['name'] = lang('last_month');
  242. $stat_arr['series'][0]['data'] = array_values($up_arr);
  243. $stat_arr['series'][1]['name'] = lang('this_month');
  244. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  245. //统计数据标题
  246. $statlist['headertitle'] = array(lang('date'), lang('last_month'), lang('this_month'), lang('compare_to'));
  247. View::assign('actionurl', $actionurl . '&search_type=month&search_time_year=' . $current_year . '&search_time_month=' . $current_month);
  248. }
  249. //计算同比
  250. foreach ((array)$currlist_arr as $k => $v) {
  251. $tmp = array();
  252. $tmp['seartime'] = $v['stime'] . '|' . $v['etime'];
  253. $tmp['timetext'] = $v['timetext'];
  254. $tmp['currentdata'] = $v['val'];
  255. $tmp['updata'] = $uplist_arr[$k]['val'];
  256. $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
  257. $statlist['data'][] = $tmp;
  258. }
  259. //导出Excel
  260. if (input('param.exporttype') == 'excel') {
  261. //导出Excel
  262. $excel_obj = new \excel\Excel();
  263. $excel_data = array();
  264. //设置样式
  265. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  266. //header
  267. foreach ($statlist['headertitle'] as $v) {
  268. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
  269. }
  270. //data
  271. foreach ($statlist['data'] as $k => $v) {
  272. $excel_data[$k + 1][] = array('data' => $v['timetext']);
  273. $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['updata']);
  274. $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['currentdata']);
  275. $excel_data[$k + 1][] = array('data' => $v['tbrate']);
  276. }
  277. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  278. $excel_obj->addArray($excel_data);
  279. $excel_obj->addWorksheet($excel_obj->charset(lang('add_store_statis'), CHARSET));
  280. $excel_obj->generateXML($excel_obj->charset(lang('add_store_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  281. exit();
  282. } else {
  283. //得到统计图数据
  284. $stat_arr['title'] = lang('add_store_statis');
  285. $stat_arr['yAxis'] = lang('add_store_number');
  286. $stat_json = getStatData_LineLabels($stat_arr);
  287. View::assign('stat_json', $stat_json);
  288. View::assign('statlist', $statlist);
  289. $this->setAdminCurItem('newstore');
  290. return View::fetch('stat_newstore');
  291. }
  292. }
  293. /**
  294. * 热卖排行
  295. */
  296. public function hotrank()
  297. {
  298. if (!isset($this->search_arr['search_type'])) {
  299. $this->search_arr['search_type'] = 'day';
  300. }
  301. $stat_model = model('stat');
  302. //获得搜索的开始时间和结束时间
  303. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  304. View::assign('searchtime', implode('|', $searchtime_arr));
  305. //周
  306. if (!input('param.search_time_year')) {
  307. $search_time_year = date('Y', TIMESTAMP);
  308. } else {
  309. $search_time_year = input('param.search_time_year');
  310. }
  311. if (!input('param.search_time_month')) {
  312. $search_time_month = date('m', TIMESTAMP);
  313. } else {
  314. $search_time_month = input('param.search_time_month');
  315. }
  316. if (!input('param.search_time_week')) {
  317. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  318. } else {
  319. $search_time_week = input('param.search_time_week');
  320. }
  321. $current_year = $search_time_year;
  322. $current_month = $search_time_month;
  323. $current_week = $search_time_week;
  324. View::assign('current_year', $current_year);
  325. View::assign('current_month', $current_month);
  326. View::assign('current_week', $current_week);
  327. $this->setAdminCurItem('hotrank');
  328. return View::fetch('store_hotrank');
  329. }
  330. /**
  331. * 热卖排行列表
  332. */
  333. public function hotrank_list()
  334. {
  335. $datanum = 30;
  336. $stat_model = model('stat');
  337. $type = input('param.type');
  338. switch ($type) {
  339. case 'ordernum':
  340. $sort_text = lang('statstore_ordernum');
  341. break;
  342. default:
  343. $type = 'orderamount';
  344. $sort_text = lang('statstore_orderamount');
  345. break;
  346. }
  347. $where = array();
  348. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  349. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  350. $searchtime_arr[] = intval($v);
  351. }
  352. //店铺分类
  353. $search_sclass = intval(input('param.search_sclass'));
  354. if ($search_sclass) {
  355. $where[] = array('storeclass_id', '=', $search_sclass);
  356. }
  357. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  358. $where[] = array('order_add_time', 'between', $searchtime_arr);
  359. //查询统计数据
  360. $field = ' store_id,store_name ';
  361. switch ($type) {
  362. case 'ordernum':
  363. $field .= ' ,COUNT(*) as ordernum ';
  364. $orderby = 'ordernum desc';
  365. break;
  366. default:
  367. $type = 'orderamount';
  368. $field .= ' ,SUM(order_amount) as orderamount ';
  369. $orderby = 'orderamount desc';
  370. break;
  371. }
  372. $orderby .= ',store_id';
  373. $statlist = $stat_model->statByStatorder($where, $field, 0, $datanum, $orderby, 'store_id');
  374. foreach ((array)$statlist as $k => $v) {
  375. $statlist[$k]['sort'] = $k + 1;
  376. }
  377. /**
  378. * 飙升榜
  379. */
  380. $soaring_statlist = array(); //飙升榜数组
  381. //查询期间产生订单的店铺数
  382. $where = array();
  383. //店铺分类
  384. $search_sclass = intval(input('param.search_sclass'));
  385. if ($search_sclass) {
  386. $where[] = array('storeclass_id', '=', $search_sclass);
  387. }
  388. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  389. $where[] = array('order_add_time', 'between', $searchtime_arr);
  390. $field = 'COUNT(*) as countnum';
  391. $countnum = $stat_model->getoneByStatorder($where, $field);
  392. $countnum = $countnum['countnum'];
  393. if ($countnum > 0) {
  394. $store_arr = array();
  395. $field = 'store_id,store_name,order_amount';
  396. for ($i = 0; $i < $countnum; $i += 1000) { //由于数据库底层的限制,所以每次查询1000条
  397. $order_list = array();
  398. $order_list = Db::name('statorder')->field($field)->where($where)->page($i, 1000)->order('order_id')->select()->toArray();
  399. foreach ((array)$order_list as $k => $v) {
  400. if (!isset($store_arr[$v['store_id']])) {
  401. $store_arr[$v['store_id']] = array();
  402. $store_arr[$v['store_id']]['orderamount'] = 0;
  403. $store_arr[$v['store_id']]['ordernum'] = 0;
  404. }
  405. $store_arr[$v['store_id']]['orderamount'] = $store_arr[$v['store_id']]['orderamount'] + $v['order_amount'];
  406. $store_arr[$v['store_id']]['ordernum'] = intval($store_arr[$v['store_id']]['ordernum']) + 1;
  407. $store_arr[$v['store_id']]['store_name'] = $v['store_name'];
  408. $store_arr[$v['store_id']]['store_id'] = $v['store_id'];
  409. }
  410. }
  411. //查询同一时间周期相比的环比数值
  412. $where = array();
  413. $stime = $searchtime_arr[0] - ($searchtime_arr[1] - $searchtime_arr[0]) - 1;
  414. $etime = $searchtime_arr[0] - 1;
  415. //店铺分类
  416. $search_sclass = intval($_REQUEST['search_sclass']);
  417. if ($search_sclass) {
  418. $where[] = array('storeclass_id', '=', $search_sclass);
  419. }
  420. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  421. $where[] = array('order_add_time', 'between', array($stime, $etime));
  422. $field = 'COUNT(*) as up_countnum';
  423. $up_countnum = $stat_model->getoneByStatorder($where, $field);
  424. $up_countnum = $up_countnum['up_countnum'];
  425. $up_store_arr = array();
  426. if ($up_countnum > 0) {
  427. $field = 'store_id,store_name,order_amount';
  428. for ($i = 0; $i < $up_countnum; $i += 1000) { //由于数据库底层的限制,所以每次查询1000条
  429. $order_list = array();
  430. $order_list = Db::name('statorder')->field($field)->where($where)->page($i, 1000)->order('store_id')->select()->toArray();
  431. foreach ((array)$order_list as $k => $v) {
  432. if (!isset($up_store_arr[$v['store_id']])) {
  433. $up_store_arr[$v['store_id']] = array('orderamount' => 0, 'ordernum' => 0);
  434. }
  435. $up_store_arr[$v['store_id']]['orderamount'] = $up_store_arr[$v['store_id']]['orderamount'] + $v['order_amount'];
  436. $up_store_arr[$v['store_id']]['ordernum'] = intval($up_store_arr[$v['store_id']]['ordernum']) + 1;
  437. }
  438. }
  439. }
  440. //计算环比飙升数值
  441. $soaring_arr = array();
  442. foreach ((array)$store_arr as $k => $v) {
  443. if (isset($up_store_arr[$k][$type]) && $up_store_arr[$k][$type] > 0) { //上期数值大于0,则计算飙升值,否则不计入统计
  444. $soaring_arr[$k] = round((($v[$type] - $up_store_arr[$k][$type]) / $up_store_arr[$k][$type] * 100), 2);
  445. }
  446. }
  447. arsort($soaring_arr); //降序排列数组
  448. $i = 1;
  449. //取出前10名飙升店铺
  450. foreach ((array)$soaring_arr as $k => $v) {
  451. if ($i <= $datanum) {
  452. $tmp = array();
  453. $tmp['sort'] = $i;
  454. $tmp['store_name'] = $store_arr[$k]['store_name'];
  455. $tmp['store_id'] = $store_arr[$k]['store_id'];
  456. $tmp['hb'] = $v;
  457. switch ($type) {
  458. case 'ordernum':
  459. $tmp['ordernum'] = $store_arr[$k]['ordernum'];
  460. break;
  461. case 'orderamount':
  462. $tmp['orderamount'] = ds_price_format($store_arr[$k]['orderamount']);
  463. break;
  464. }
  465. $soaring_statlist[] = $tmp;
  466. $i++;
  467. } else {
  468. break;
  469. }
  470. }
  471. }
  472. View::assign('soaring_statlist', $soaring_statlist);
  473. View::assign('statlist', $statlist);
  474. View::assign('sort_text', $sort_text);
  475. View::assign('stat_field', $type);
  476. echo View::fetch('store_hotrank_list');
  477. }
  478. /**
  479. * 店铺等级
  480. */
  481. public function degree()
  482. {
  483. $where = array();
  484. $field = ' count(*) as allnum,grade_id ';
  485. $stat_model = model('stat');
  486. //查询店铺分类下的店铺
  487. $search_sclass = input('param.search_sclass');
  488. if ($search_sclass > 0) {
  489. $where[] = array('storeclass_id', '=', $search_sclass);
  490. }
  491. $storelist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'grade_id');
  492. $sd_list = $stat_model->getStoreDegree();
  493. $statlist['headertitle'] = array();
  494. $statlist['data'] = array();
  495. //处理数组数据
  496. if (!empty($storelist)) {
  497. foreach ($storelist as $k => $v) {
  498. $storelist[$k]['p_name'] = isset($sd_list[$v['grade_id']]) > 0 ? $sd_list[$v['grade_id']] : lang('ds_ownshop');
  499. $storelist[$k]['allnum'] = intval($v['allnum']);
  500. $statlist['headertitle'][] = isset($sd_list[$v['grade_id']]) > 0 ? $sd_list[$v['grade_id']] : lang('ds_ownshop');
  501. $statlist['data'][] = $v['allnum'];
  502. }
  503. $data = array(
  504. 'title' => lang('store_grade_statis'),
  505. 'name' => lang('store_number'),
  506. 'label_show' => true,
  507. 'series' => $storelist
  508. );
  509. View::assign('stat_json', getStatData_Pie($data));
  510. }
  511. $this->setAdminCurItem('degree');
  512. return View::fetch('stat_storedegree');
  513. }
  514. /**
  515. * 查看店铺列表
  516. */
  517. public function showstore()
  518. {
  519. $stat_model = model('stat');
  520. $where = array();
  521. if (in_array(input('type'), array('newbyday', 'newbyweek', 'newbymonth', 'storearea'))) {
  522. $actionurl = (string)url('Statstore/showstore', ['type' => input('type'), 't' => $this->search_arr['t']]);
  523. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  524. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  525. $searchtime_arr[] = intval($v);
  526. }
  527. $where[] = array('store_addtime', 'between', $searchtime_arr);
  528. }
  529. //商品分类
  530. $storeclass_id = intval(input('scid'));
  531. if ($storeclass_id > 0) {
  532. $where[] = array('storeclass_id', '=', $storeclass_id);
  533. $actionurl .= "&scid=$storeclass_id";
  534. }
  535. //省份
  536. if (input('provid')) {
  537. $province_id = intval(input('param.provid'));
  538. $where[] = array('province_id', '=', $province_id);
  539. $actionurl .= "&provid=$province_id";
  540. }
  541. if (input('exporttype') == 'excel') {
  542. $store_list = $stat_model->getNewStoreStatList($where);
  543. } else {
  544. $store_list = $stat_model->getNewStoreStatList($where, '', 10);
  545. }
  546. //店铺等级
  547. $storegrade_model = model('storegrade');
  548. $grade_list = $storegrade_model->getStoregradeList();
  549. if (!empty($grade_list)) {
  550. $search_grade_list = array();
  551. foreach ($grade_list as $k => $v) {
  552. $search_grade_list[$v['storegrade_id']] = $v['storegrade_name'];
  553. }
  554. }
  555. //导出Excel
  556. if (input('exporttype') == 'excel') {
  557. //导出Excel
  558. $excel_obj = new \excel\Excel();
  559. $excel_data = array();
  560. //设置样式
  561. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  562. //header
  563. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
  564. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_member_name'));
  565. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_seller_name'));
  566. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('stat_storedegree'));
  567. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('statstore_expire'));
  568. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('statstore_addtime'));
  569. //data
  570. foreach ($store_list as $k => $v) {
  571. $excel_data[$k + 1][] = array('data' => $v['store_name']);
  572. $excel_data[$k + 1][] = array('data' => $v['member_name']);
  573. $excel_data[$k + 1][] = array('data' => $v['seller_name']);
  574. $excel_data[$k + 1][] = array('data' => $search_grade_list[$v['grade_id']]);
  575. $excel_data[$k + 1][] = array('data' => $v['store_endtime'] ? date('Y-m-d', $v['store_endtime']) : lang('state_no_tile_limit'));
  576. $excel_data[$k + 1][] = array('data' => date('Y-m-d', $v['store_addtime']));
  577. }
  578. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  579. $excel_obj->addArray($excel_data);
  580. $excel_obj->addWorksheet($excel_obj->charset(lang('ds_new') . lang('ds_store'), CHARSET));
  581. $excel_obj->generateXML($excel_obj->charset(lang('ds_new') . lang('ds_store'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  582. exit();
  583. }
  584. View::assign('search_grade_list', $search_grade_list);
  585. View::assign('actionurl', $actionurl);
  586. View::assign('store_list', $store_list);
  587. View::assign('show_page', $stat_model->page_info->render());
  588. $this->setAdminCurItem('showstore');
  589. return View::fetch('storelist');
  590. }
  591. /**
  592. * 销售统计
  593. */
  594. public function storesales()
  595. {
  596. if (!isset($this->search_arr['search_type'])) {
  597. $this->search_arr['search_type'] = 'day';
  598. }
  599. $stat_model = model('stat');
  600. //获得搜索的开始时间和结束时间
  601. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  602. View::assign('searchtime', implode('|', $searchtime_arr));
  603. //周
  604. if (!input('param.search_time_year')) {
  605. $search_time_year = date('Y', TIMESTAMP);
  606. } else {
  607. $search_time_year = input('param.search_time_year');
  608. }
  609. if (!input('param.search_time_month')) {
  610. $search_time_month = date('m', TIMESTAMP);
  611. } else {
  612. $search_time_month = input('param.search_time_month');
  613. }
  614. if (!input('param.search_time_week')) {
  615. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  616. } else {
  617. $search_time_week = input('param.search_time_week');
  618. }
  619. $current_year = $search_time_year;
  620. $current_month = $search_time_month;
  621. $current_week = $search_time_week;
  622. View::assign('current_year', $current_year);
  623. View::assign('current_month', $current_month);
  624. View::assign('current_week', $current_week);
  625. $this->setAdminCurItem('storesales');
  626. return View::fetch('stat_store_sales');
  627. }
  628. /**
  629. * 店铺销售统计列表
  630. */
  631. public function storesales_list()
  632. {
  633. $stat_model = model('stat');
  634. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  635. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  636. $searchtime_arr[] = intval($v);
  637. }
  638. $where = array();
  639. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  640. $where[] = array('order_add_time', 'between', $searchtime_arr);
  641. //店铺分类
  642. $search_sclass = intval(input('param.search_sclass'));
  643. if ($search_sclass) {
  644. $where[] = array('storeclass_id', '=', $search_sclass);
  645. }
  646. //店铺名称
  647. $where[] = array('store_name', 'like', "%" . input('param.search_sname') . "%");
  648. //查询总条数
  649. $count_arr = $stat_model->getoneByStatorder($where, 'COUNT(DISTINCT store_id) as countnum');
  650. $countnum = intval($count_arr['countnum']);
  651. //列表字段
  652. $field = " store_id,store_name,SUM(order_amount) as orderamount, COUNT(*) as ordernum, COUNT(DISTINCT buyer_id) as membernum";
  653. //排序
  654. $orderby_arr = array('membernum asc', 'membernum desc', 'ordernum asc', 'ordernum desc', 'orderamount asc', 'orderamount desc');
  655. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  656. $this->search_arr['orderby'] = 'membernum desc';
  657. }
  658. $orderby = trim($this->search_arr['orderby']) . ',store_id asc';
  659. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  660. $statlist = $stat_model->statByStatorder($where, $field, 0, 0, $orderby, 'store_id');
  661. } else {
  662. $statlist = $stat_model->statByStatorder($where, $field, 20, 0, $orderby, 'store_id');
  663. foreach ((array)$statlist as $k => $v) {
  664. $v['view'] = "<a href='javascript:void(0);' ds_type='showtrends' data-param='{\"storeid\":\"{$v['store_id']}\"}'>走势图</a>";
  665. $statlist[$k] = $v;
  666. }
  667. }
  668. //列表header
  669. $statheader = array();
  670. $statheader[] = array('text' => lang('ds_store_name'), 'key' => 'store_name');
  671. $statheader[] = array('text' => lang('membernum'), 'key' => 'membernum', 'isorder' => 1);
  672. $statheader[] = array('text' => lang('statstore_ordernum'), 'key' => 'ordernum', 'isorder' => 1);
  673. $statheader[] = array('text' => lang('statstore_orderamount'), 'key' => 'orderamount', 'isorder' => 1);
  674. //导出Excel
  675. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  676. //导出Excel
  677. $excel_obj = new \excel\Excel();
  678. $excel_data = array();
  679. //设置样式
  680. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  681. //header
  682. foreach ($statheader as $k => $v) {
  683. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v['text']);
  684. }
  685. //data
  686. foreach ($statlist as $k => $v) {
  687. foreach ($statheader as $h_k => $h_v) {
  688. $excel_data[$k + 1][] = array('data' => $v[$h_v['key']]);
  689. }
  690. }
  691. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  692. $excel_obj->addArray($excel_data);
  693. $excel_obj->addWorksheet($excel_obj->charset(lang('store_sale_statis'), CHARSET));
  694. $excel_obj->generateXML($excel_obj->charset(lang('store_sale_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  695. exit();
  696. }
  697. View::assign('statlist', $statlist);
  698. View::assign('statheader', $statheader);
  699. View::assign('orderby', $this->search_arr['orderby']);
  700. View::assign('actionurl', (string)url('Statstore/storesales_list', ['t' => $this->search_arr['t'], 'search_sclass' => $search_sclass, 'search_sname' => input('param.search_sname')]));
  701. View::assign('show_page', $stat_model->page_info->render());
  702. echo View::fetch('stat_listandorder');
  703. }
  704. /**
  705. * 销售走势
  706. */
  707. public function storesales_trends()
  708. {
  709. $storeid = intval(input('param.storeid'));
  710. if ($storeid <= 0) {
  711. View::assign('stat_error', lang('stat_error'));
  712. return View::fetch('salestrends');
  713. exit();
  714. }
  715. $search_type = input('param.search_type');
  716. if (!$search_type) {
  717. $search_type = 'day';
  718. }
  719. $stat_model = model('stat');
  720. $where = array();
  721. $where[] = array('store_id', '=', $storeid);
  722. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  723. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  724. $searchtime_arr[] = intval($v);
  725. }
  726. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  727. $where[] = array('order_add_time', 'between', $searchtime_arr);
  728. $field = " store_id,store_name,SUM(order_amount) as orderamount, COUNT(*) as ordernum, COUNT(DISTINCT buyer_id) as membernum";
  729. $stat_arr = array('orderamount' => array(), 'ordernum' => array(), 'membernum' => array());
  730. $statlist = array();
  731. if ($search_type == 'day') {
  732. //构造横轴数据
  733. for ($i = 0; $i < 24; $i++) {
  734. //横轴
  735. foreach ($stat_arr as $k => $v) {
  736. $stat_arr[$k]['xAxis']['categories'][] = "$i";
  737. $statlist[$k][$i] = 0;
  738. }
  739. }
  740. $field .= ' ,HOUR(FROM_UNIXTIME(order_add_time)) as timeval ';
  741. }
  742. if ($search_type == 'week') {
  743. //构造横轴数据
  744. for ($i = 1; $i <= 7; $i++) {
  745. $tmp_weekarr = getSystemWeekArr();
  746. //横轴
  747. foreach ($stat_arr as $k => $v) {
  748. $stat_arr[$k]['xAxis']['categories'][] = $tmp_weekarr[$i];
  749. $statlist[$k][$i] = 0;
  750. }
  751. unset($tmp_weekarr);
  752. }
  753. $field .= ' ,WEEKDAY(FROM_UNIXTIME(order_add_time))+1 as timeval ';
  754. }
  755. if ($search_type == 'month') {
  756. //计算横轴的最大量(由于每个月的天数不同)
  757. $dayofmonth = date('t', $searchtime_arr[0]);
  758. //构造横轴数据
  759. for ($i = 1; $i <= $dayofmonth; $i++) {
  760. //横轴
  761. foreach ($stat_arr as $k => $v) {
  762. $stat_arr[$k]['xAxis']['categories'][] = $i;
  763. $statlist[$k][$i] = 0;
  764. }
  765. }
  766. $field .= ' ,day(FROM_UNIXTIME(order_add_time)) as timeval ';
  767. }
  768. //查询数据
  769. $statlist_tmp = $stat_model->statByStatorder($where, $field, 0, '');
  770. //整理统计数组
  771. $storename = '';
  772. if ($statlist_tmp) {
  773. foreach ($statlist_tmp as $k => $v) {
  774. $storename = $v['store_name'];
  775. foreach ($stat_arr as $t_k => $t_v) {
  776. if ($k == 'orderamount') {
  777. $statlist[$t_k][$v['timeval']] = round($v[$t_k], 2);
  778. } else {
  779. $statlist[$t_k][$v['timeval']] = intval($v[$t_k]);
  780. }
  781. }
  782. }
  783. }
  784. foreach ($stat_arr as $k => $v) {
  785. $stat_arr[$k]['legend']['enabled'] = false;
  786. switch ($k) {
  787. case 'orderamount':
  788. $caption = lang('statstore_orderamount');
  789. break;
  790. case 'ordernum':
  791. $caption = lang('statstore_ordernum');
  792. break;
  793. default:
  794. $caption = lang('membernum');
  795. break;
  796. }
  797. $stat_arr[$k]['series'][0]['name'] = $caption;
  798. $stat_arr[$k]['series'][0]['data'] = array_values($statlist[$k]);
  799. $stat_arr[$k]['title'] = $caption . lang('sale_trend');
  800. $stat_arr[$k]['yAxis'] = $caption;
  801. //得到统计图数据
  802. $stat_json[$k] = getStatData_LineLabels($stat_arr[$k]);
  803. }
  804. View::assign('storename', $storename);
  805. View::assign('stat_json', $stat_json);
  806. echo View::fetch('store_salestrends');
  807. }
  808. /**
  809. * 地区分布
  810. */
  811. public function storearea()
  812. {
  813. $stat_model = model('stat');
  814. //获得搜索的开始时间和结束时间
  815. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  816. $where = array();
  817. if (trim(input('param.search_time'))) {
  818. $where[] = array('store_addtime', '<=', strtotime(input('param.search_time')));
  819. }
  820. $search_sclass = intval(input('param.search_sclass'));
  821. if ($search_sclass > 0) {
  822. $where[] = array('storeclass_id', '=', $search_sclass);
  823. }
  824. $field = ' region_id, COUNT(*) as storenum ';
  825. $statlist_tmp = $stat_model->statByStore($where, $field, 'region_id', 'region_id');
  826. // 地区
  827. $province_array = model('area')->getTopLevelAreas();
  828. //地图显示等级数组
  829. $level_arr = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9), array(10, 11, 12));
  830. $statlist = array();
  831. $temp_list = array();
  832. foreach ((array)$statlist_tmp as $k => $v) {
  833. $v['level'] = 4; //排名
  834. foreach ($level_arr as $lk => $lv) {
  835. if (in_array($k + 1, $lv)) {
  836. $v['level'] = $lk; //排名
  837. }
  838. }
  839. //获取该地区的省
  840. if ($v['region_id']) {
  841. if (!isset($temp_list[$v['region_id']])) {
  842. $temp = Db::name('area')->where('area_id=' . $v['region_id'])->value('area_parent_id');
  843. if ($temp) {
  844. $temp_list[$v['region_id']] = $temp;
  845. } else {
  846. $temp_list[$v['region_id']] = $v['region_id'];
  847. }
  848. $index = 0;
  849. while ($temp && $index < 4) {
  850. $temp_list[$v['region_id']] = $temp;
  851. $temp_0 = $temp;
  852. $temp = Db::name('area')->where('area_id=' . $temp)->value('area_parent_id');
  853. if ($temp) {
  854. $temp_list[$temp_0] = $temp;
  855. } else {
  856. $temp_list[$temp_0] = $temp_0;
  857. }
  858. $index++;
  859. }
  860. }
  861. $province_id = $temp_list[$v['region_id']];
  862. } else {
  863. $province_id = 0;
  864. }
  865. $v['sort'] = $k + 1;
  866. $v['provincename'] = isset($province_array[$province_id]) ? $province_array[$province_id] : lang('other');
  867. if (!isset($statlist[$province_id])) {
  868. $statlist[$province_id] = $v;
  869. } else {
  870. $statlist[$province_id]['storenum'] += $v['storenum'];
  871. }
  872. }
  873. $stat_arr = array();
  874. foreach ((array)$province_array as $k => $v) {
  875. if (isset($statlist[$k])) {
  876. $stat_arr[] = array('cha' => $k, 'name' => $v, 'des' => ",店铺量:{$statlist[$k]['storenum']}", 'level' => $statlist[$k]['level']);
  877. } else {
  878. $stat_arr[] = array('cha' => $k, 'name' => $v, 'des' => ',无订单数据', 'level' => 4);
  879. }
  880. }
  881. $stat_json = getStatData_Map($stat_arr);
  882. View::assign('stat_json', $stat_json);
  883. View::assign('statlist', $statlist);
  884. $actionurl = (string)url('Statstore/storearea');
  885. if (trim(input('param.search_time'))) {
  886. $actionurl = $actionurl . '&t=0|' . strtotime(input('param.search_time'));
  887. }
  888. if ($search_sclass > 0) {
  889. $actionurl .= "&scid=$search_sclass";
  890. }
  891. View::assign('actionurl', $actionurl);
  892. $this->setAdminCurItem('storearea');
  893. return View::fetch('stat_storearea');
  894. }
  895. protected function getAdminItemList()
  896. {
  897. $menu_array = array(
  898. array('name' => 'newstore', 'text' => lang('stat_newstore'), 'url' => (string)url('Statstore/newstore')),
  899. array('name' => 'hotrank', 'text' => lang('stat_storehotrank'), 'url' => (string)url('Statstore/hotrank')),
  900. array('name' => 'storesales', 'text' => lang('stat_storesales'), 'url' => (string)url('Statstore/storesales')),
  901. array('name' => 'degree', 'text' => lang('stat_storedegree'), 'url' => (string)url('Statstore/degree')),
  902. array('name' => 'storearea', 'text' => lang('stat_storearea'), 'url' => (string)url('Statstore/storearea')),
  903. );
  904. return $menu_array;
  905. }
  906. }