Statmember.php 45 KB

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