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