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