Statisticsflow.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  13. * 网站地址: https://www.valimart.net/
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 控制器
  18. */
  19. class Statisticsflow extends BaseSeller {
  20. private $search_arr; //处理后的参数
  21. public function initialize() {
  22. parent::initialize(); // TODO: Change the autogenerated stub
  23. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statisticsflow.lang.php');
  24. include_once root_path() . 'extend/mall/statistics.php';
  25. include_once root_path() . 'extend/mall/datehelper.php';
  26. $stat_model = model('stat');
  27. //存储参数
  28. $this->search_arr = $_REQUEST;
  29. //处理搜索时间
  30. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  31. //获得系统年份
  32. $year_arr = getSystemYearArr();
  33. //获得系统月份
  34. $month_arr = getSystemMonthArr();
  35. //获得本月的周时间段
  36. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  37. View::assign('year_arr', $year_arr);
  38. View::assign('month_arr', $month_arr);
  39. View::assign('week_arr', $week_arr);
  40. View::assign('search_arr', $this->search_arr);
  41. }
  42. /**
  43. * 店铺流量统计
  44. */
  45. public function index() {
  46. $store_id = intval(session('store_id'));
  47. //确定统计分表名称
  48. $last_num = $store_id % 10; //获取店铺ID的末位数字
  49. $tablenum = ($t = intval(config('ds_config.flowstat_tablenum'))) > 1 ? $t : 1; //处理流量统计记录表数量
  50. $flow_tablename = ($t = ($last_num % $tablenum)) > 0 ? "flowstat_$t" : 'flowstat';
  51. if (!isset($this->search_arr['search_type'])) {
  52. $this->search_arr['search_type'] = 'week';
  53. }
  54. $stat_model = model('stat');
  55. //获得搜索的开始时间和结束时间
  56. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  57. $where = array();
  58. $where[] = array('store_id','=',$store_id);
  59. $where[] = array('flowstat_stattime','between',$searchtime_arr);
  60. $where[] = array('flowstat_type','=','sum');
  61. $field = ' SUM(flowstat_clicknum) as amount';
  62. if ($this->search_arr['search_type'] == 'week') {
  63. //构造横轴数据
  64. for ($i = 1; $i <= 7; $i++) {
  65. $tmp_weekarr = getSystemWeekArr();
  66. //横轴
  67. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  68. unset($tmp_weekarr);
  69. $statlist[$i] = 0;
  70. }
  71. $field .= ' ,WEEKDAY(FROM_UNIXTIME(flowstat_stattime))+1 as timeval ';
  72. }
  73. if ($this->search_arr['search_type'] == 'month') {
  74. //计算横轴的最大量(由于每个月的天数不同)
  75. $dayofmonth = date('t', $searchtime_arr[0]);
  76. //构造横轴数据
  77. for ($i = 1; $i <= $dayofmonth; $i++) {
  78. //横轴
  79. $stat_arr['xAxis']['categories'][] = $i;
  80. $statlist[$i] = 0;
  81. }
  82. $field .= ' ,day(FROM_UNIXTIME(flowstat_stattime)) as timeval ';
  83. }
  84. $statlist_tmp = $stat_model->statByFlowstat($flow_tablename, $where, $field, 0, 0, 'timeval asc', 'timeval');
  85. if ($statlist_tmp) {
  86. foreach ((array) $statlist_tmp as $k => $v) {
  87. $statlist[$v['timeval']] = floatval($v['amount']);
  88. }
  89. }
  90. //得到统计图数据
  91. $stat_arr['legend']['enabled'] = false;
  92. $stat_arr['series'][0]['name'] = lang('traffic');
  93. $stat_arr['series'][0]['data'] = array_values($statlist);
  94. $stat_arr['title'] = lang('store_traffic_statistics');
  95. $stat_arr['yAxis'] = lang('visits');
  96. $stat_json = getStatData_LineLabels($stat_arr);
  97. View::assign('stat_json', $stat_json);
  98. $this->setSellerCurMenu('Statisticsflow');
  99. $this->setSellerCurItem('index');
  100. return View::fetch($this->template_dir . 'index');
  101. }
  102. /**
  103. * 商品流量统计
  104. */
  105. public function goodsflow() {
  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. $menu_array = array(
  169. array('name' => 'index', 'text' => lang('total_store_flow'), 'url' => (string) url('Statisticsflow/index')),
  170. array('name' => 'goodsflow', 'text' => lang('commodity_flow_ranking'), 'url' => (string) url('Statisticsflow/goodsflow'))
  171. );
  172. return $menu_array;
  173. }
  174. }