Statmember.php 45 KB

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