Statmember.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  1. <?php
  2. /**
  3. * 会员统计
  4. *
  5. */
  6. namespace app\admin\controller;
  7. use think\facade\View;
  8. use think\facade\Lang;
  9. use think\facade\Db;
  10. /**
  11. *
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * 控制器
  16. */
  17. class Statmember extends AdminControl
  18. {
  19. public function initialize()
  20. {
  21. parent::initialize(); // TODO: Change the autogenerated stub
  22. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/stat.lang.php');
  23. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/member.lang.php');
  24. include_once root_path() . 'extend/mall/statistics.php';
  25. include_once root_path() . 'extend/mall/datehelper.php';
  26. $stat_model = model('stat');
  27. //存储参数
  28. $this->search_arr = input('param.');
  29. //处理搜索时间
  30. if (in_array(request()->action(), array('newmember', 'analyze', 'scale', 'area', 'buying'))) {
  31. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  32. //获得系统年份
  33. $year_arr = getSystemYearArr();
  34. //获得系统月份
  35. $month_arr = getSystemMonthArr();
  36. //获得本月的周时间段
  37. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  38. View::assign('year_arr', $year_arr);
  39. View::assign('month_arr', $month_arr);
  40. View::assign('week_arr', $week_arr);
  41. }
  42. View::assign('search_arr', $this->search_arr);
  43. }
  44. /**
  45. * 新增会员
  46. */
  47. public function newmember()
  48. {
  49. if (!isset($this->search_arr['search_type'])) {
  50. $this->search_arr['search_type'] = 'day';
  51. }
  52. $stat_model = model('stat');
  53. $statlist = array(); //统计数据列表
  54. //新增总数数组
  55. $count_arr = array('up' => 0, 'curr' => 0);
  56. $where = array();
  57. $field = ' COUNT(*) as allnum ';
  58. if ($this->search_arr['search_type'] == 'day') {
  59. //构造横轴数据
  60. for ($i = 0; $i < 24; $i++) {
  61. //统计图数据
  62. $curr_arr[$i] = 0; //今天
  63. $up_arr[$i] = 0; //昨天
  64. //统计表数据
  65. $currlist_arr[$i]['timetext'] = $i;
  66. //方便搜索会员列表,计算开始时间和结束时间
  67. $currlist_arr[$i]['stime'] = $this->search_arr['day']['search_time'] + $i * 3600;
  68. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 3600;
  69. $uplist_arr[$i]['val'] = 0;
  70. $currlist_arr[$i]['val'] = 0;
  71. //横轴
  72. $stat_arr['xAxis']['categories'][] = "$i";
  73. }
  74. $stime = $this->search_arr['day']['search_time'] - 86400; //昨天0点
  75. $etime = $this->search_arr['day']['search_time'] + 86400 - 1; //今天24点
  76. //总计的查询时间
  77. $count_arr['seartime'] = ($stime + 86400) . '|' . $etime;
  78. $today_day = @date('d', $this->search_arr['day']['search_time']); //今天日期
  79. $yesterday_day = @date('d', $stime); //昨天日期
  80. $where[] = array('member_addtime', 'between', array($stime, $etime));
  81. $field .= ' ,DAY(FROM_UNIXTIME(member_addtime)) as dayval,HOUR(FROM_UNIXTIME(member_addtime)) as hourval ';
  82. $memberlist = $stat_model->statByMember($where, $field, 0, '', 'dayval,hourval');
  83. if ($memberlist) {
  84. foreach ($memberlist as $k => $v) {
  85. if ($today_day == $v['dayval']) {
  86. $curr_arr[$v['hourval']] = intval($v['allnum']);
  87. $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
  88. $count_arr['curr'] += intval($v['allnum']);
  89. }
  90. if ($yesterday_day == $v['dayval']) {
  91. $up_arr[$v['hourval']] = intval($v['allnum']);
  92. $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
  93. $count_arr['up'] += intval($v['allnum']);
  94. }
  95. }
  96. }
  97. $stat_arr['series'][0]['name'] = lang('yestoday');
  98. $stat_arr['series'][0]['data'] = array_values($up_arr);
  99. $stat_arr['series'][1]['name'] = lang('today');
  100. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  101. //统计数据标题
  102. $statlist['headertitle'] = array(lang('hour'), lang('yestoday'), lang('today'), lang('compare_to'));
  103. View::assign('actionurl', 'statmember/newmember?search_type=day&search_time=' . date('Y-m-d', $this->search_arr['day']['search_time']));
  104. }
  105. if ($this->search_arr['search_type'] == 'week') {
  106. $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
  107. $stime = strtotime($current_weekarr[0]) - 86400 * 7;
  108. $etime = strtotime($current_weekarr[1]) + 86400 - 1;
  109. //总计的查询时间
  110. $count_arr['seartime'] = ($stime + 86400 * 7) . '|' . $etime;
  111. $up_week = @date('W', $stime); //上周
  112. $curr_week = @date('W', $etime); //本周
  113. //构造横轴数据
  114. for ($i = 1; $i <= 7; $i++) {
  115. //统计图数据
  116. $up_arr[$i] = 0;
  117. $curr_arr[$i] = 0;
  118. $tmp_weekarr = getSystemWeekArr();
  119. //统计表数据
  120. $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  121. //方便搜索会员列表,计算开始时间和结束时间
  122. $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0]) + ($i - 1) * 86400;
  123. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;
  124. $uplist_arr[$i]['val'] = 0;
  125. $currlist_arr[$i]['val'] = 0;
  126. //横轴
  127. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  128. unset($tmp_weekarr);
  129. }
  130. $where[] = array('member_addtime', 'between', array($stime, $etime));
  131. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(member_addtime)) as weekval,WEEKDAY(FROM_UNIXTIME(member_addtime))+1 as dayofweekval ';
  132. $memberlist = $stat_model->statByMember($where, $field, 0, '', 'weekval,dayofweekval');
  133. if ($memberlist) {
  134. foreach ($memberlist as $k => $v) {
  135. if ($up_week == intval($v['weekval'])) {
  136. $up_arr[$v['dayofweekval']] = intval($v['allnum']);
  137. $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
  138. $count_arr['up'] += intval($v['allnum']);
  139. }
  140. if ($curr_week == $v['weekval']) {
  141. $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
  142. $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
  143. $count_arr['curr'] += intval($v['allnum']);
  144. }
  145. }
  146. }
  147. $stat_arr['series'][0]['name'] = lang('last_week');
  148. $stat_arr['series'][0]['data'] = array_values($up_arr);
  149. $stat_arr['series'][1]['name'] = lang('this_week');
  150. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  151. //统计数据标题
  152. $statlist['headertitle'] = array(lang('week'), lang('last_week'), lang('this_week'), lang('compare_to'));
  153. View::assign('actionurl', 'statmember/newmember?search_type=week&searchweek_year=' . $this->search_arr['week']['current_year'] . '&searchweek_month=' . $this->search_arr['week']['current_month'] . '&searchweek_week=' . $this->search_arr['week']['current_week']);
  154. }
  155. if ($this->search_arr['search_type'] == 'month') {
  156. $stime = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01 -1 month");
  157. $etime = getMonthLastDay($this->search_arr['month']['current_year'], $this->search_arr['month']['current_month']) + 86400 - 1;
  158. //总计的查询时间
  159. $count_arr['seartime'] = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01") . '|' . $etime;
  160. $up_month = date('m', $stime);
  161. $curr_month = date('m', $etime);
  162. //计算横轴的最大量(由于每个月的天数不同)
  163. $up_dayofmonth = date('t', $stime);
  164. $curr_dayofmonth = date('t', $etime);
  165. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  166. //构造横轴数据
  167. for ($i = 1; $i <= $x_max; $i++) {
  168. //统计图数据
  169. $up_arr[$i] = 0;
  170. $curr_arr[$i] = 0;
  171. //统计表数据
  172. $currlist_arr[$i]['timetext'] = $i;
  173. //方便搜索会员列表,计算开始时间和结束时间
  174. $currlist_arr[$i]['stime'] = strtotime($this->search_arr['month']['current_year'] . '-' . $this->search_arr['month']['current_month'] . "-01") + ($i - 1) * 86400;
  175. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime'] + 86400 - 1;
  176. $uplist_arr[$i]['val'] = 0;
  177. $currlist_arr[$i]['val'] = 0;
  178. //横轴
  179. $stat_arr['xAxis']['categories'][] = $i;
  180. unset($tmp_montharr);
  181. }
  182. $where[] = array('member_addtime', 'between', array($stime, $etime));
  183. $field .= ',MONTH(FROM_UNIXTIME(member_addtime)) as monthval,day(FROM_UNIXTIME(member_addtime)) as dayval ';
  184. $memberlist = $stat_model->statByMember($where, $field, 0, '', 'monthval,dayval');
  185. if ($memberlist) {
  186. foreach ($memberlist as $k => $v) {
  187. if ($up_month == $v['monthval']) {
  188. $up_arr[$v['dayval']] = intval($v['allnum']);
  189. $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
  190. $count_arr['up'] += intval($v['allnum']);
  191. }
  192. if ($curr_month == $v['monthval']) {
  193. $curr_arr[$v['dayval']] = intval($v['allnum']);
  194. $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
  195. $count_arr['curr'] += intval($v['allnum']);
  196. }
  197. }
  198. }
  199. $stat_arr['series'][0]['name'] = lang('last_month');
  200. $stat_arr['series'][0]['data'] = array_values($up_arr);
  201. $stat_arr['series'][1]['name'] = lang('this_month');
  202. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  203. //统计数据标题
  204. $statlist['headertitle'] = array(lang('date'), lang('last_month'), lang('this_month'), lang('compare_to'));
  205. View::assign('actionurl', 'statmember/newmember?search_type=month&searchmonth_year=' . $this->search_arr['month']['current_year'] . '&searchmonth_month=' . $this->search_arr['month']['current_month']);
  206. }
  207. //计算同比
  208. foreach ((array) $currlist_arr as $k => $v) {
  209. $tmp = array();
  210. $tmp['timetext'] = $v['timetext'];
  211. $tmp['seartime'] = $v['stime'] . '|' . $v['etime'];
  212. $tmp['currentdata'] = $v['val'];
  213. $tmp['updata'] = $uplist_arr[$k]['val'];
  214. $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
  215. $statlist['data'][] = $tmp;
  216. }
  217. //计算总结同比
  218. $count_arr['tbrate'] = getTb($count_arr['up'], $count_arr['curr']);
  219. //导出Excel
  220. if (input('param.exporttype') == 'excel') {
  221. //导出Excel
  222. $excel_obj = new \excel\Excel();
  223. $excel_data = array();
  224. //设置样式
  225. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  226. //header
  227. foreach ($statlist['headertitle'] as $v) {
  228. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v);
  229. }
  230. //data
  231. foreach ($statlist['data'] as $k => $v) {
  232. $excel_data[$k + 1][] = array('data' => $v['timetext']);
  233. $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['updata']);
  234. $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['currentdata']);
  235. $excel_data[$k + 1][] = array('data' => $v['tbrate']);
  236. }
  237. $excel_data[count($statlist['data']) + 1][] = array('data' => lang('statstore_total'));
  238. $excel_data[count($statlist['data']) + 1][] = array('format' => 'Number', 'data' => $count_arr['up']);
  239. $excel_data[count($statlist['data']) + 1][] = array('format' => 'Number', 'data' => $count_arr['curr']);
  240. $excel_data[count($statlist['data']) + 1][] = array('data' => $count_arr['tbrate']);
  241. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  242. $excel_obj->addArray($excel_data);
  243. $excel_obj->addWorksheet($excel_obj->charset(lang('add_member_statis'), CHARSET));
  244. $excel_obj->generateXML($excel_obj->charset(lang('add_member_statis'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  245. exit();
  246. } else {
  247. //得到统计图数据
  248. $stat_arr['title'] = lang('add_member_statis');
  249. $stat_arr['yAxis'] = lang('add_member_number');
  250. $stat_json = getStatData_LineLabels($stat_arr);
  251. View::assign('stat_json', $stat_json);
  252. View::assign('statlist', $statlist);
  253. View::assign('count_arr', $count_arr);
  254. $this->setAdminCurItem('newmember');
  255. return View::fetch('stat_newmember');
  256. }
  257. }
  258. /**
  259. * 会员分析
  260. */
  261. public function analyze()
  262. {
  263. if (!isset($this->search_arr['search_type'])) {
  264. $this->search_arr['search_type'] = 'day';
  265. }
  266. $stat_model = model('stat');
  267. //构造横轴数据
  268. for ($i = 1; $i <= 15; $i++) {
  269. //横轴
  270. $stat_arr['xAxis']['categories'][] = $i;
  271. }
  272. $stat_arr['title'] = lang('buyer_rank') . 'Top15';
  273. $stat_arr['legend']['enabled'] = false;
  274. //获得搜索的开始时间和结束时间
  275. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  276. $where = array();
  277. $where[] = array('statm_time', 'between', $searchtime_arr);
  278. //下单量
  279. $where[] = array('statm_ordernum', '>', 0);
  280. $field = ' statm_memberid, statm_membername, SUM(statm_ordernum) as ordernum ';
  281. $ordernum_listtop15 = $stat_model->statByStatmember($where, $field, 0, 15, 'ordernum desc,statm_memberid desc', 'statm_memberid');
  282. $stat_ordernum_arr = $stat_arr;
  283. $stat_ordernum_arr['series'][0]['name'] = lang('statstore_ordernum');
  284. $stat_ordernum_arr['series'][0]['data'] = array();
  285. if (!empty($ordernum_listtop15)) {
  286. for ($i = 0; $i < 15; $i++) {
  287. if (isset($ordernum_listtop15[$i])) {
  288. $stat_ordernum_arr['series'][0]['data'][] = array(
  289. 'name' => strval($ordernum_listtop15[$i]['statm_membername']),
  290. 'y' => intval($ordernum_listtop15[$i]['ordernum'])
  291. );
  292. }
  293. }
  294. }
  295. $stat_ordernum_arr['yAxis'] = lang('statstore_ordernum');
  296. $statordernum_json = getStatData_Column2D($stat_ordernum_arr);
  297. unset($stat_ordernum_arr);
  298. View::assign('statordernum_json', $statordernum_json);
  299. View::assign('ordernum_listtop15', $ordernum_listtop15);
  300. //下单商品件数
  301. $where[] = array('statm_goodsnum', '>', 0);
  302. $field = ' statm_memberid, statm_membername, SUM(statm_goodsnum) as goodsnum ';
  303. $goodsnum_listtop15 = $stat_model->statByStatmember($where, $field, 0, 15, 'goodsnum desc,statm_memberid desc', 'statm_memberid');
  304. $stat_goodsnum_arr = $stat_arr;
  305. $stat_goodsnum_arr['series'][0]['name'] = lang('goodsnum');
  306. $stat_goodsnum_arr['series'][0]['data'] = array();
  307. if (!empty($goodsnum_listtop15)) {
  308. for ($i = 0; $i < 15; $i++) {
  309. if (isset($goodsnum_listtop15[$i])) {
  310. $stat_goodsnum_arr['series'][0]['data'][] = array(
  311. 'name' => strval($goodsnum_listtop15[$i]['statm_membername']),
  312. 'y' => intval($goodsnum_listtop15[$i]['goodsnum'])
  313. );
  314. }
  315. }
  316. }
  317. $stat_goodsnum_arr['yAxis'] = lang('goodsnum');
  318. $statgoodsnum_json = getStatData_Column2D($stat_goodsnum_arr);
  319. unset($stat_goodsnum_arr);
  320. View::assign('statgoodsnum_json', $statgoodsnum_json);
  321. View::assign('goodsnum_listtop15', $goodsnum_listtop15);
  322. //下单金额
  323. $where[] = array('statm_orderamount', '>', 0);
  324. $field = ' statm_memberid, statm_membername, SUM(statm_orderamount) as orderamount ';
  325. $orderamount_listtop15 = $stat_model->statByStatmember($where, $field, 0, 15, 'orderamount desc,statm_memberid desc', 'statm_memberid');
  326. $stat_orderamount_arr = $stat_arr;
  327. $stat_orderamount_arr['series'][0]['name'] = lang('statstore_orderamount');
  328. $stat_orderamount_arr['series'][0]['data'] = array();
  329. if (!empty($orderamount_listtop15)) {
  330. for ($i = 0; $i < 15; $i++) {
  331. if (isset($orderamount_listtop15[$i])) {
  332. $stat_orderamount_arr['series'][0]['data'][] = array(
  333. 'name' => strval($orderamount_listtop15[$i]['statm_membername']),
  334. 'y' => floatval($orderamount_listtop15[$i]['orderamount'])
  335. );
  336. }
  337. }
  338. }
  339. $stat_orderamount_arr['yAxis'] = lang('statstore_orderamount');
  340. $statorderamount_json = getStatData_Column2D($stat_orderamount_arr);
  341. unset($stat_orderamount_arr);
  342. View::assign('statorderamount_json', $statorderamount_json);
  343. View::assign('orderamount_listtop15', $orderamount_listtop15);
  344. View::assign('searchtime', implode('|', $searchtime_arr));
  345. $this->setAdminCurItem('analyze');
  346. return View::fetch('stat_memberanalyze');
  347. }
  348. /**
  349. * 会员分析异步详细列表
  350. */
  351. public function analyzeinfo()
  352. {
  353. $stat_model = model('stat');
  354. $where = array();
  355. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  356. foreach ((array) $searchtime_arr_tmp as $k => $v) {
  357. $searchtime_arr[] = intval($v);
  358. }
  359. $where[] = array('statm_time', 'between', $searchtime_arr);
  360. $memberlist = array();
  361. //查询统计数据
  362. $field = ' statm_memberid, statm_membername ';
  363. $type = '';
  364. switch (input('param.type')) {
  365. case 'orderamount':
  366. $where[] = array('statm_orderamount', '>', 0);
  367. $field .= ' ,SUM(statm_orderamount) as orderamount ';
  368. $caption = lang('statstore_orderamount');
  369. break;
  370. case 'goodsnum':
  371. $where[] = array('statm_goodsnum', '>', 0);
  372. $field .= ' ,SUM(statm_goodsnum) as goodsnum ';
  373. $caption = lang('goodsnum');
  374. break;
  375. default:
  376. $type = 'ordernum';
  377. $where[] = array('statm_ordernum', '>', 0);
  378. $field .= ' ,SUM(statm_ordernum) as ordernum ';
  379. $caption = lang('statstore_ordernum');
  380. break;
  381. }
  382. if (input('param.exporttype') == 'excel') {
  383. $memberlist = $stat_model->statByStatmember($where, $field, 0, 0, input('param.type') . " desc,statm_memberid desc", 'statm_memberid');
  384. } else {
  385. $memberlist = $stat_model->statByStatmember($where, $field, 10, 0, input('param.type') . " desc,statm_memberid desc", 'statm_memberid');
  386. }
  387. $curpage = ($t = intval(input('param.curpage'))) ? $t : 1;
  388. foreach ((array) $memberlist as $k => $v) {
  389. $v['number'] = ($curpage - 1) * 10 + $k + 1;
  390. $memberlist[$k] = $v;
  391. }
  392. //导出Excel
  393. if (input('param.exporttype') == 'excel') {
  394. //导出Excel
  395. $excel_obj = new \excel\Excel();
  396. $excel_data = array();
  397. //设置样式
  398. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  399. //header
  400. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('statstore_number'));
  401. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_member_name'));
  402. $excel_data[0][] = array('styleid' => 's_title', 'data' => $caption);
  403. //data
  404. foreach ($memberlist as $k => $v) {
  405. $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['number']);
  406. $excel_data[$k + 1][] = array('data' => $v['statm_membername']);
  407. $excel_data[$k + 1][] = array('data' => $v[$type]);
  408. }
  409. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  410. $excel_obj->addArray($excel_data);
  411. $excel_obj->addWorksheet($excel_obj->charset(lang('ds_member') . $caption . lang('ds_stat'), CHARSET));
  412. $excel_obj->generateXML($excel_obj->charset(lang('ds_member') . $caption . lang('ds_stat'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  413. exit();
  414. } else {
  415. View::assign('caption', $caption);
  416. View::assign('stat_field', $type);
  417. View::assign('memberlist', $memberlist);
  418. View::assign('show_page', $stat_model->page_info->render());
  419. echo View::fetch('stat_memberanalyze_info');
  420. }
  421. }
  422. /**
  423. * 查看会员列表
  424. */
  425. public function showmember()
  426. {
  427. $stat_model = model('stat');
  428. $where = array();
  429. $actionurl = '';
  430. if (in_array(input('param.type'), array('newbyday', 'newbyweek', 'newbymonth'))) {
  431. $actionurl = (string)url('Statmember/showmember', ['type' => 'newbyday', 't' => input('param.t')]);
  432. $searchtime_arr_tmp = explode('|', input('param.t'));
  433. foreach ((array) $searchtime_arr_tmp as $k => $v) {
  434. $searchtime_arr[] = intval($v);
  435. }
  436. $where[] = array('member_addtime', 'between', $searchtime_arr);
  437. }
  438. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  439. $member_list = $stat_model->getMemberList($where);
  440. } else {
  441. $member_list = $stat_model->getMemberList($where, '*', 10);
  442. }
  443. if (is_array($member_list)) {
  444. foreach ($member_list as $k => $v) {
  445. $member_list[$k]['member_addtime'] = isset($v['member_addtime']) ? date('Y-m-d H:i:s', $v['member_addtime']) : '';
  446. $member_list[$k]['member_logintime'] = $v['member_logintime'] ? date('Y-m-d H:i:s', $v['member_logintime']) : '';
  447. }
  448. }
  449. //导出Excel
  450. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  451. //导出Excel
  452. $excel_obj = new \excel\Excel();
  453. $excel_data = array();
  454. //设置样式
  455. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  456. //header
  457. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_member_name'));
  458. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('member_addtime'));
  459. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('member_loginnum'));
  460. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('member_logintime'));
  461. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_member_points'));
  462. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_predeposit'));
  463. //data
  464. foreach ($member_list as $k => $v) {
  465. $excel_data[$k + 1][] = array('data' => $v['member_name'] . '(' . lang('member_truename') . lang('ds_colon') . $v['member_truename'] . ')');
  466. $excel_data[$k + 1][] = array('data' => $v['member_addtime']);
  467. $excel_data[$k + 1][] = array('format' => 'Number', 'data' => $v['member_loginnum']);
  468. $excel_data[$k + 1][] = array('data' => $v['member_logintime'] . '(IP:' . $v['member_login_ip'] . ')');
  469. $excel_data[$k + 1][] = array('data' => $v['member_points']);
  470. $excel_data[$k + 1][] = array('data' => lang('member_index_available') . lang('ds_colon') . $v['available_predeposit'] . lang('ds_yuan') . '(' . lang('member_index_frozen') . lang('ds_colon') . $v['freeze_predeposit'] . lang('ds_yuan') . ')');
  471. }
  472. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  473. $excel_obj->addArray($excel_data);
  474. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_newmember'), CHARSET));
  475. $excel_obj->generateXML($excel_obj->charset(lang('stat_newmember'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  476. exit();
  477. }
  478. View::assign('actionurl', $actionurl);
  479. View::assign('member_list', $member_list);
  480. View::assign('show_page', $stat_model->page_info->render());
  481. $this->setAdminCurItem('showmember');
  482. return View::fetch('memberlist');
  483. }
  484. /**
  485. * 会员规模
  486. */
  487. public function scale()
  488. {
  489. if (!isset($this->search_arr['search_type'])) {
  490. $this->search_arr['search_type'] = 'day';
  491. }
  492. $stat_model = model('stat');
  493. $statlist = array(); //统计数据列表
  494. //获得搜索的开始时间和结束时间
  495. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  496. $where = array();
  497. $where[] = array('statm_time', 'between', $searchtime_arr);
  498. if (trim(isset($this->search_arr['membername']))) {
  499. $where[] = array('statm_membername', 'like', "%" . trim($this->search_arr['membername']) . "%");
  500. }
  501. $field = ' statm_memberid, statm_membername, statm_time, SUM(statm_orderamount) as orderamount, SUM(statm_predincrease) as predincrease, -SUM(statm_predreduce) as predreduce, SUM(statm_pointsincrease) as pointsincrease, -SUM(statm_pointsreduce) as pointsreduce ';
  502. //排序
  503. $orderby_arr = array('orderamount asc', 'orderamount desc', 'predincrease asc', 'predincrease desc', 'predreduce asc', 'predreduce desc', 'pointsincrease asc', 'pointsincrease desc', 'pointsreduce asc', 'pointsreduce desc');
  504. if (!in_array(trim(isset($this->search_arr['orderby'])), $orderby_arr)) {
  505. $this->search_arr['orderby'] = 'orderamount desc';
  506. }
  507. $orderby = trim($this->search_arr['orderby']) . ',statm_memberid desc';
  508. if (input('param.exporttype') == 'excel') {
  509. $statlist = $stat_model->statByStatmember($where, $field, 0, 0, $orderby, 'statm_memberid');
  510. } else {
  511. $statlist = $stat_model->statByStatmember($where, $field, 10, 0, $orderby, 'statm_memberid');
  512. }
  513. //导出Excel
  514. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  515. //导出Excel
  516. $excel_obj = new \excel\Excel();
  517. $excel_data = array();
  518. //设置样式
  519. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  520. //header
  521. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_member_name'));
  522. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('statstore_orderamount'));
  523. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('predincrease'));
  524. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('predreduce'));
  525. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('pointsincrease'));
  526. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('pointsreduce'));
  527. //data
  528. foreach ($statlist as $k => $v) {
  529. $excel_data[$k + 1][] = array('data' => $v['statm_membername']);
  530. $excel_data[$k + 1][] = array('data' => $v['orderamount']);
  531. $excel_data[$k + 1][] = array('data' => $v['predincrease']);
  532. $excel_data[$k + 1][] = array('data' => $v['predreduce']);
  533. $excel_data[$k + 1][] = array('data' => $v['pointsincrease']);
  534. $excel_data[$k + 1][] = array('data' => $v['pointsreduce']);
  535. }
  536. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  537. $excel_obj->addArray($excel_data);
  538. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_scaleanalyze'), CHARSET));
  539. $excel_obj->generateXML($excel_obj->charset(lang('stat_scaleanalyze'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  540. exit();
  541. }
  542. View::assign('statlist', $statlist);
  543. View::assign('show_page', $stat_model->page_info->render());
  544. View::assign('orderby', $this->search_arr['orderby']);
  545. $this->setAdminCurItem('scale');
  546. return View::fetch('stat_memberscale');
  547. }
  548. /**
  549. * 区域分析
  550. */
  551. public function area()
  552. {
  553. if (!isset($this->search_arr['search_type'])) {
  554. $this->search_arr['search_type'] = 'day';
  555. }
  556. $stat_model = model('stat');
  557. //获得搜索的开始时间和结束时间
  558. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  559. View::assign('searchtime', implode('|', $searchtime_arr));
  560. $this->setAdminCurItem('area');
  561. return View::fetch('stat_memberarea');
  562. }
  563. /**
  564. * 区域分析之详细列表
  565. */
  566. public function area_list()
  567. {
  568. $stat_model = model('stat');
  569. $where = array();
  570. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  571. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  572. foreach ((array) $searchtime_arr_tmp as $k => $v) {
  573. $searchtime_arr[] = intval($v);
  574. }
  575. $where[] = array('order_add_time', 'between', $searchtime_arr);
  576. $field = ' reciver_province_id, COUNT(*) as ordernum,SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as membernum ';
  577. $orderby_arr = array('membernum asc', 'membernum desc', 'orderamount asc', 'orderamount desc', 'ordernum asc', 'ordernum desc');
  578. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  579. $this->search_arr['orderby'] = 'membernum desc';
  580. }
  581. $orderby = trim($this->search_arr['orderby']) . ',reciver_province_id';
  582. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  583. $statlist_tmp = $stat_model->statByStatorder($where, $field, 0, 0, $orderby, 'reciver_province_id');
  584. } else {
  585. $statlist_tmp = $stat_model->statByStatorder($where, $field, 10, 0, $orderby, 'reciver_province_id');
  586. }
  587. // 地区
  588. $province_array = model('area')->getTopLevelAreas();
  589. $statheader = array();
  590. $statheader[] = array('text' => lang('statestore_province'), 'key' => 'provincename');
  591. $statheader[] = array('text' => lang('membernum'), 'key' => 'membernum', 'isorder' => 1);
  592. $statheader[] = array('text' => lang('statstore_orderamount'), 'key' => 'orderamount', 'isorder' => 1);
  593. $statheader[] = array('text' => lang('statstore_ordernum'), 'key' => 'ordernum', 'isorder' => 1);
  594. $statlist = array();
  595. foreach ((array) $statlist_tmp as $k => $v) {
  596. $province_id = intval($v['reciver_province_id']);
  597. $tmp = array();
  598. $tmp['provincename'] = ($t = $province_array[$province_id]) ? $t : lang('other');
  599. $tmp['membernum'] = $v['membernum'];
  600. $tmp['orderamount'] = $v['orderamount'];
  601. $tmp['ordernum'] = $v['ordernum'];
  602. $statlist[] = $tmp;
  603. }
  604. //导出Excel
  605. if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel') {
  606. //导出Excel
  607. $excel_obj = new \excel\Excel();
  608. $excel_data = array();
  609. //设置样式
  610. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  611. //header
  612. foreach ($statheader as $k => $v) {
  613. $excel_data[0][] = array('styleid' => 's_title', 'data' => $v['text']);
  614. }
  615. //data
  616. foreach ($statlist as $k => $v) {
  617. foreach ($statheader as $h_k => $h_v) {
  618. $excel_data[$k + 1][] = array('data' => $v[$h_v['key']]);
  619. }
  620. }
  621. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  622. $excel_obj->addArray($excel_data);
  623. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_areaanalyze'), CHARSET));
  624. $excel_obj->generateXML($excel_obj->charset(lang('stat_areaanalyze'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  625. exit();
  626. }
  627. View::assign('statlist', $statlist);
  628. View::assign('statheader', $statheader);
  629. View::assign('orderby', $this->search_arr['orderby']);
  630. View::assign('actionurl', (string)url('Statmember/area_list', ['t' => $this->search_arr['t']]));
  631. View::assign('show_page', $stat_model->page_info->render());
  632. $this->setAdminCurItem('area');
  633. echo View::fetch('stat_listandorder');
  634. }
  635. /**
  636. * 区域分析之地图数据
  637. */
  638. public function area_map()
  639. {
  640. $stat_model = model('stat');
  641. $where = array();
  642. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  643. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  644. foreach ((array) $searchtime_arr_tmp as $k => $v) {
  645. $searchtime_arr[] = intval($v);
  646. }
  647. $where[] = array('order_add_time', 'between', $searchtime_arr);
  648. $memberlist = array();
  649. //查询统计数据
  650. $field = ' reciver_province_id ';
  651. $type = input('param.type');
  652. switch ($type) {
  653. case 'orderamount':
  654. $field .= ' ,SUM(order_amount) as orderamount ';
  655. $orderby = 'orderamount desc';
  656. break;
  657. case 'ordernum':
  658. $field .= ' ,COUNT(*) as ordernum ';
  659. $orderby = 'ordernum desc';
  660. break;
  661. default:
  662. $type = 'membernum';
  663. $field .= ' ,COUNT(DISTINCT buyer_id) as membernum ';
  664. $orderby = 'membernum desc';
  665. break;
  666. }
  667. $orderby .= ',reciver_province_id';
  668. $statlist_tmp = $stat_model->statByStatorder($where, $field, 10, 0, $orderby, 'reciver_province_id');
  669. // 地区
  670. $province_array = model('area')->getTopLevelAreas();
  671. //地图显示等级数组
  672. $level_arr = array(array(1, 2, 3), array(4, 5, 6), array(7, 8, 9), array(10, 11, 12));
  673. $statlist = array();
  674. foreach ((array) $statlist_tmp as $k => $v) {
  675. $v['level'] = 4; //排名
  676. foreach ($level_arr as $lk => $lv) {
  677. if (in_array($k + 1, $lv)) {
  678. $v['level'] = $lk; //排名
  679. }
  680. }
  681. $province_id = intval($v['reciver_province_id']);
  682. $statlist[$province_id] = $v;
  683. }
  684. $stat_arr = array();
  685. foreach ((array) $province_array as $k => $v) {
  686. if (!empty($statlist[$k])) {
  687. switch ($type) {
  688. case 'orderamount':
  689. $des = ",下单金额:{$statlist[$k]['orderamount']}";
  690. break;
  691. case 'ordernum':
  692. $des = ",下单量:{$statlist[$k]['ordernum']}";
  693. break;
  694. default:
  695. $des = ",下单会员数:{$statlist[$k]['membernum']}";
  696. break;
  697. }
  698. $stat_arr[] = array('cha' => $k, 'name' => $v, 'des' => $des, 'level' => $statlist[$k]['level']);
  699. } else {
  700. $des = ",无订单数据";
  701. $stat_arr[] = array('cha' => $k, 'name' => $v, 'des' => $des, 'level' => 4);
  702. }
  703. }
  704. $stat_json = getStatData_Map($stat_arr);
  705. View::assign('stat_field', $type);
  706. View::assign('stat_json', $stat_json);
  707. echo View::fetch('stat_map');
  708. }
  709. /**
  710. * 购买分析
  711. */
  712. public function buying()
  713. {
  714. if (!isset($this->search_arr['search_type'])) {
  715. $this->search_arr['search_type'] = 'day';
  716. }
  717. $stat_model = model('stat');
  718. //获得搜索的开始时间和结束时间
  719. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  720. /*
  721. * 客单价分布
  722. */
  723. $where = array();
  724. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  725. $where[] = array('order_add_time', 'between', $searchtime_arr);
  726. $field = '1';
  727. $pricerange_arr = ($t = trim(config('ds_config.stat_orderpricerange'))) ? unserialize($t) : '';
  728. if ($pricerange_arr) {
  729. $stat_arr['series'][0]['name'] = lang('statstore_ordernum');
  730. //设置价格区间最后一项,最后一项只有开始值没有结束值
  731. $pricerange_count = count($pricerange_arr);
  732. if ($pricerange_arr[$pricerange_count - 1]['e']) {
  733. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count - 1]['e'] + 1;
  734. $pricerange_arr[$pricerange_count]['e'] = '';
  735. }
  736. foreach ((array) $pricerange_arr as $k => $v) {
  737. $v['s'] = intval($v['s']);
  738. $v['e'] = intval($v['e']);
  739. //构造查询字段
  740. if ($v['e']) {
  741. $field .= " ,SUM(IF(order_amount > {$v['s']} and order_amount <= {$v['e']},1,0)) as ordernum_{$k}";
  742. } else {
  743. $field .= " ,SUM(IF(order_amount > {$v['s']},1,0)) as ordernum_{$k}";
  744. }
  745. }
  746. $orderlist = Db::query('SELECT ' . $field . ' FROM ' . config('database.connections.mysql.prefix') . 'statorder WHERE order_isvalid=1 AND order_add_time BETWEEN ' . $searchtime_arr[0] . ' AND ' . $searchtime_arr[1]);
  747. if ($orderlist) {
  748. $orderlist = current($orderlist);
  749. foreach ((array) $pricerange_arr as $k => $v) {
  750. //横轴
  751. if ($v['e']) {
  752. $stat_arr['xAxis']['categories'][] = $v['s'] . '-' . $v['e'];
  753. } else {
  754. $stat_arr['xAxis']['categories'][] = $v['s'] . lang('above');
  755. }
  756. //统计图数据
  757. if (isset($orderlist['ordernum_' . $k])) {
  758. $stat_arr['series'][0]['data'][] = intval($orderlist['ordernum_' . $k]);
  759. } else {
  760. $stat_arr['series'][0]['data'][] = 0;
  761. }
  762. }
  763. }
  764. //得到统计图数据
  765. $stat_arr['title'] = lang('statmember_per_member_price_distribution');
  766. $stat_arr['legend']['enabled'] = false;
  767. $stat_arr['yAxis'] = lang('statstore_ordernum');
  768. $guestprice_statjson = getStatData_LineLabels($stat_arr);
  769. } else {
  770. $guestprice_statjson = '';
  771. }
  772. unset($stat_arr);
  773. /*
  774. * 购买频次分析
  775. */
  776. //统计期间会员下单量
  777. $where = array();
  778. $where[] = array('statm_time', 'between', $searchtime_arr);
  779. $where[] = array('statm_ordernum', '>', 0);
  780. $field = 'COUNT(*) as countnum';
  781. $countnum_arr = $stat_model->getOneStatmember($where, $field);
  782. $countnum = intval($countnum_arr['countnum']);
  783. $member_arr = array();
  784. for ($i = 0; $i < $countnum; $i += 1000) { //由于数据库底层的限制,所以每次查询1000条
  785. $statmember_list = Db::name('statmember')->field('statm_memberid,statm_ordernum')->where($where)->page($i, 1000)->group('statm_id')->select()->toArray();
  786. foreach ((array) $statmember_list as $k => $v) {
  787. $member_arr[$v['statm_memberid']] = isset($member_arr[$v['statm_memberid']]) ? intval($member_arr[$v['statm_memberid']]) : 0 + intval($v['statm_ordernum']);
  788. }
  789. }
  790. $stattimes_arr = array();
  791. if ($member_arr) {
  792. //整理期间各个频次的下单客户数
  793. for ($i = 1; $i <= 10; $i++) {
  794. $stattimes_arr[$i] = array('num' => 0, 'rate' => 0.00);
  795. if ($i >= 10) {
  796. $stattimes_arr[$i]['text'] = sprintf(lang('buy_time_info'), 10);
  797. } else {
  798. $stattimes_arr[$i]['text'] = sprintf(lang('buy_time_info'), $i);
  799. }
  800. }
  801. foreach ($member_arr as $k => $v) {
  802. if ($v >= 10) {
  803. $stattimes_arr[10]['num'] = intval($stattimes_arr[10]['num']) + 1;
  804. } else {
  805. $stattimes_arr[$v]['num'] = intval($stattimes_arr[$v]['num']) + 1;
  806. }
  807. }
  808. //计算期间各个频次的下单客户数占总数比例
  809. foreach ($stattimes_arr as $k => $v) {
  810. $stattimes_arr[$k]['rate'] = round(intval($v['num']) / count($member_arr) * 100, 2);
  811. }
  812. }
  813. //购买时段分布
  814. $where = array();
  815. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  816. $where[] = array('order_add_time', 'between', $searchtime_arr);
  817. $field = ' HOUR(FROM_UNIXTIME(order_add_time)) as hourval,COUNT(*) as ordernum ';
  818. $orderlist = $stat_model->statByStatorder($where, $field, 0, 0, '', 'hourval');
  819. $stat_arr = array();
  820. $stat_arr['series'][0]['name'] = lang('statstore_ordernum');
  821. //构造横轴坐标
  822. for ($i = 0; $i < 24; $i++) {
  823. //横轴
  824. $stat_arr['xAxis']['categories'][] = $i;
  825. $stat_arr['series'][0]['data'][$i] = 0;
  826. }
  827. foreach ((array) $orderlist as $k => $v) {
  828. //统计图数据
  829. $stat_arr['series'][0]['data'][$v['hourval']] = intval($v['ordernum']);
  830. }
  831. //得到统计图数据
  832. $stat_arr['title'] = lang('statmember_time_distribution');
  833. $stat_arr['legend']['enabled'] = false;
  834. $stat_arr['yAxis'] = lang('statstore_ordernum');
  835. $hour_statjson = getStatData_LineLabels($stat_arr);
  836. View::assign('hour_statjson', $hour_statjson);
  837. View::assign('stattimes_arr', $stattimes_arr);
  838. View::assign('guestprice_statjson', $guestprice_statjson);
  839. $this->setAdminCurItem('buying');
  840. return View::fetch('stat_buying');
  841. }
  842. protected function getAdminItemList()
  843. {
  844. $menu_array = array(
  845. array('name' => 'newmember', 'text' => lang('stat_newmember'), 'url' => (string)url('Statmember/newmember')),
  846. array('name' => 'analyze', 'text' => lang('stat_memberanalyze'), 'url' => (string)url('Statmember/analyze')),
  847. array('name' => 'scale', 'text' => lang('stat_scaleanalyze'), 'url' => (string)url('Statmember/scale')),
  848. array('name' => 'area', 'text' => lang('stat_areaanalyze'), 'url' => (string)url('Statmember/area')),
  849. array('name' => 'buying', 'text' => lang('stat_buying'), 'url' => (string)url('Statmember/buying')),
  850. array('name' => 'showmember', 'text' => lang('stat_memberlist'), 'url' => (string)url('Statmember/showmember'))
  851. );
  852. return $menu_array;
  853. }
  854. }