Statisticsflow.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * 店铺浏量统计
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use Think\facade\Lang;
  8. /**
  9. *
  10. *
  11. * ----------------------------------------------------------------------------
  12. *
  13. * 控制器
  14. */
  15. class Statisticsflow extends BaseSeller
  16. {
  17. private $search_arr; //处理后的参数
  18. public function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statisticsflow.lang.php');
  22. include_once root_path() . 'extend/mall/statistics.php';
  23. include_once root_path() . 'extend/mall/datehelper.php';
  24. $stat_model = model('stat');
  25. //存储参数
  26. $this->search_arr = $_REQUEST;
  27. //处理搜索时间
  28. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  29. //获得系统年份
  30. $year_arr = getSystemYearArr();
  31. //获得系统月份
  32. $month_arr = getSystemMonthArr();
  33. //获得本月的周时间段
  34. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  35. View::assign('year_arr', $year_arr);
  36. View::assign('month_arr', $month_arr);
  37. View::assign('week_arr', $week_arr);
  38. View::assign('search_arr', $this->search_arr);
  39. }
  40. /**
  41. * 店铺流量统计
  42. */
  43. public function index()
  44. {
  45. $store_id = intval(session('store_id'));
  46. //确定统计分表名称
  47. $last_num = $store_id % 10; //获取店铺ID的末位数字
  48. $tablenum = ($t = intval(config('ds_config.flowstat_tablenum'))) > 1 ? $t : 1; //处理流量统计记录表数量
  49. $flow_tablename = ($t = ($last_num % $tablenum)) > 0 ? "flowstat_$t" : 'flowstat';
  50. if (!isset($this->search_arr['search_type'])) {
  51. $this->search_arr['search_type'] = 'week';
  52. }
  53. $stat_model = model('stat');
  54. //获得搜索的开始时间和结束时间
  55. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  56. $where = array();
  57. $where[] = array('store_id', '=', $store_id);
  58. $where[] = array('flowstat_stattime', 'between', $searchtime_arr);
  59. $where[] = array('flowstat_type', '=', 'sum');
  60. $field = ' SUM(flowstat_clicknum) as amount';
  61. if ($this->search_arr['search_type'] == 'week') {
  62. //构造横轴数据
  63. for ($i = 1; $i <= 7; $i++) {
  64. $tmp_weekarr = getSystemWeekArr();
  65. //横轴
  66. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  67. unset($tmp_weekarr);
  68. $statlist[$i] = 0;
  69. }
  70. $field .= ' ,WEEKDAY(FROM_UNIXTIME(flowstat_stattime))+1 as timeval ';
  71. }
  72. if ($this->search_arr['search_type'] == 'month') {
  73. //计算横轴的最大量(由于每个月的天数不同)
  74. $dayofmonth = date('t', $searchtime_arr[0]);
  75. //构造横轴数据
  76. for ($i = 1; $i <= $dayofmonth; $i++) {
  77. //横轴
  78. $stat_arr['xAxis']['categories'][] = $i;
  79. $statlist[$i] = 0;
  80. }
  81. $field .= ' ,day(FROM_UNIXTIME(flowstat_stattime)) as timeval ';
  82. }
  83. $statlist_tmp = $stat_model->statByFlowstat($flow_tablename, $where, $field, 0, 0, 'timeval asc', 'timeval');
  84. if ($statlist_tmp) {
  85. foreach ((array) $statlist_tmp as $k => $v) {
  86. $statlist[$v['timeval']] = floatval($v['amount']);
  87. }
  88. }
  89. //得到统计图数据
  90. $stat_arr['legend']['enabled'] = false;
  91. $stat_arr['series'][0]['name'] = lang('traffic');
  92. $stat_arr['series'][0]['data'] = array_values($statlist);
  93. $stat_arr['title'] = lang('store_traffic_statistics');
  94. $stat_arr['yAxis'] = lang('visits');
  95. $stat_json = getStatData_LineLabels($stat_arr);
  96. View::assign('stat_json', $stat_json);
  97. $this->setSellerCurMenu('Statisticsflow');
  98. $this->setSellerCurItem('index');
  99. return View::fetch($this->template_dir . 'index');
  100. }
  101. /**
  102. * 商品流量统计
  103. */
  104. public function goodsflow()
  105. {
  106. $store_id = intval(session('store_id'));
  107. //确定统计分表名称
  108. $last_num = $store_id % 10; //获取店铺ID的末位数字
  109. $tablenum = ($t = intval(config('ds_config.flowstat_tablenum'))) > 1 ? $t : 1; //处理流量统计记录表数量
  110. $flow_tablename = ($t = ($last_num % $tablenum)) > 0 ? "flowstat_$t" : 'flowstat';
  111. if (!isset($this->search_arr['search_type'])) {
  112. $this->search_arr['search_type'] = 'week';
  113. }
  114. $stat_model = model('stat');
  115. //获得搜索的开始时间和结束时间
  116. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  117. $where = array();
  118. $where[] = array('store_id', '=', $store_id);
  119. $where[] = array('flowstat_stattime', 'between', $searchtime_arr);
  120. $where[] = array('flowstat_type', '=', 'goods');
  121. $field = ' goods_id,SUM(flowstat_clicknum) as amount';
  122. $stat_arr = array();
  123. //构造横轴数据
  124. for ($i = 1; $i <= 30; $i++) {
  125. //横轴
  126. $stat_arr['xAxis']['categories'][] = $i;
  127. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  128. }
  129. $statlist_tmp = $stat_model->statByFlowstat($flow_tablename, $where, $field, 0, 30, 'amount desc,goods_id asc', 'goods_id');
  130. if ($statlist_tmp) {
  131. $goodsid_arr = array();
  132. foreach ((array) $statlist_tmp as $k => $v) {
  133. $goodsid_arr[] = $v['goods_id'];
  134. }
  135. //查询相应商品
  136. $goods_list_tmp = $stat_model->statByGoods(array(array('goods_id', 'in', $goodsid_arr)), $field = 'goods_name,goods_id');
  137. foreach ((array) $goods_list_tmp as $k => $v) {
  138. $goods_list[$v['goods_id']] = $v;
  139. }
  140. foreach ((array) $statlist_tmp as $k => $v) {
  141. if (isset($goods_list[$v['goods_id']])) {
  142. $v['goods_name'] = $goods_list[$v['goods_id']];
  143. $v['amount'] = floatval($v['amount']);
  144. $statlist[] = $v;
  145. $stat_arr['series'][0]['data'][$k] = array('name' => strval($goods_list[$v['goods_id']]['goods_name']), 'y' => floatval($v['amount']));
  146. }
  147. }
  148. }
  149. //得到统计图数据
  150. $stat_arr['legend']['enabled'] = false;
  151. $stat_arr['series'][0]['name'] = lang('traffic');
  152. $stat_arr['title'] = lang('product_visits');
  153. $stat_arr['yAxis'] = lang('visits');
  154. $stat_json = getStatData_Column2D($stat_arr);
  155. View::assign('stat_json', $stat_json);
  156. $this->setSellerCurMenu('Statisticsflow');
  157. $this->setSellerCurItem('goodsflow');
  158. return View::fetch($this->template_dir . 'flowgoods');
  159. }
  160. /**
  161. * 用户中心右边,小导航
  162. *
  163. * @param string $menu_type 导航类型
  164. * @param string $name 当前导航的name
  165. * @return
  166. */
  167. protected function getSellerItemList()
  168. {
  169. $menu_array = array(
  170. array('name' => 'index', 'text' => lang('total_store_flow'), 'url' => (string) url('Statisticsflow/index')),
  171. array('name' => 'goodsflow', 'text' => lang('commodity_flow_ranking'), 'url' => (string) url('Statisticsflow/goodsflow'))
  172. );
  173. return $menu_array;
  174. }
  175. }