Statisticsflow.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * 店铺浏量统计
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use Think\facade\Lang;
  8. /**
  9. * ============================================================================
  10. * DSMall多用户商城
  11. * ============================================================================
  12. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  13. * 网站地址: http://www.csdeshang.com
  14. * ----------------------------------------------------------------------------
  15. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  16. * 不允许对程序代码以任何形式任何目的的再发布。
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Statisticsflow extends BaseSeller {
  21. private $search_arr; //处理后的参数
  22. public function initialize() {
  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. $store_id = intval(session('store_id'));
  48. //确定统计分表名称
  49. $last_num = $store_id % 10; //获取店铺ID的末位数字
  50. $tablenum = ($t = intval(config('ds_config.flowstat_tablenum'))) > 1 ? $t : 1; //处理流量统计记录表数量
  51. $flow_tablename = ($t = ($last_num % $tablenum)) > 0 ? "flowstat_$t" : 'flowstat';
  52. if (!isset($this->search_arr['search_type'])) {
  53. $this->search_arr['search_type'] = 'week';
  54. }
  55. $stat_model = model('stat');
  56. //获得搜索的开始时间和结束时间
  57. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  58. $where = array();
  59. $where[] = array('store_id','=',$store_id);
  60. $where[] = array('flowstat_stattime','between',$searchtime_arr);
  61. $where[] = array('flowstat_type','=','sum');
  62. $field = ' SUM(flowstat_clicknum) as amount';
  63. if ($this->search_arr['search_type'] == 'week') {
  64. //构造横轴数据
  65. for ($i = 1; $i <= 7; $i++) {
  66. $tmp_weekarr = getSystemWeekArr();
  67. //横轴
  68. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  69. unset($tmp_weekarr);
  70. $statlist[$i] = 0;
  71. }
  72. $field .= ' ,WEEKDAY(FROM_UNIXTIME(flowstat_stattime))+1 as timeval ';
  73. }
  74. if ($this->search_arr['search_type'] == 'month') {
  75. //计算横轴的最大量(由于每个月的天数不同)
  76. $dayofmonth = date('t', $searchtime_arr[0]);
  77. //构造横轴数据
  78. for ($i = 1; $i <= $dayofmonth; $i++) {
  79. //横轴
  80. $stat_arr['xAxis']['categories'][] = $i;
  81. $statlist[$i] = 0;
  82. }
  83. $field .= ' ,day(FROM_UNIXTIME(flowstat_stattime)) as timeval ';
  84. }
  85. $statlist_tmp = $stat_model->statByFlowstat($flow_tablename, $where, $field, 0, 0, 'timeval asc', 'timeval');
  86. if ($statlist_tmp) {
  87. foreach ((array) $statlist_tmp as $k => $v) {
  88. $statlist[$v['timeval']] = floatval($v['amount']);
  89. }
  90. }
  91. //得到统计图数据
  92. $stat_arr['legend']['enabled'] = false;
  93. $stat_arr['series'][0]['name'] = lang('traffic');
  94. $stat_arr['series'][0]['data'] = array_values($statlist);
  95. $stat_arr['title'] = lang('store_traffic_statistics');
  96. $stat_arr['yAxis'] = lang('visits');
  97. $stat_json = getStatData_LineLabels($stat_arr);
  98. View::assign('stat_json', $stat_json);
  99. $this->setSellerCurMenu('Statisticsflow');
  100. $this->setSellerCurItem('index');
  101. return View::fetch($this->template_dir . 'index');
  102. }
  103. /**
  104. * 商品流量统计
  105. */
  106. public function goodsflow() {
  107. $store_id = intval(session('store_id'));
  108. //确定统计分表名称
  109. $last_num = $store_id % 10; //获取店铺ID的末位数字
  110. $tablenum = ($t = intval(config('ds_config.flowstat_tablenum'))) > 1 ? $t : 1; //处理流量统计记录表数量
  111. $flow_tablename = ($t = ($last_num % $tablenum)) > 0 ? "flowstat_$t" : 'flowstat';
  112. if (!isset($this->search_arr['search_type'])) {
  113. $this->search_arr['search_type'] = 'week';
  114. }
  115. $stat_model = model('stat');
  116. //获得搜索的开始时间和结束时间
  117. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  118. $where = array();
  119. $where[] = array('store_id','=',$store_id);
  120. $where[] = array('flowstat_stattime','between',$searchtime_arr);
  121. $where[] = array('flowstat_type','=','goods');
  122. $field = ' goods_id,SUM(flowstat_clicknum) as amount';
  123. $stat_arr = array();
  124. //构造横轴数据
  125. for ($i = 1; $i <= 30; $i++) {
  126. //横轴
  127. $stat_arr['xAxis']['categories'][] = $i;
  128. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  129. }
  130. $statlist_tmp = $stat_model->statByFlowstat($flow_tablename, $where, $field, 0, 30, 'amount desc,goods_id asc', 'goods_id');
  131. if ($statlist_tmp) {
  132. $goodsid_arr = array();
  133. foreach ((array) $statlist_tmp as $k => $v) {
  134. $goodsid_arr[] = $v['goods_id'];
  135. }
  136. //查询相应商品
  137. $goods_list_tmp = $stat_model->statByGoods(array(array('goods_id', 'in', $goodsid_arr)), $field = 'goods_name,goods_id');
  138. foreach ((array) $goods_list_tmp as $k => $v) {
  139. $goods_list[$v['goods_id']] = $v;
  140. }
  141. foreach ((array) $statlist_tmp as $k => $v) {
  142. if (isset($goods_list[$v['goods_id']])) {
  143. $v['goods_name'] = $goods_list[$v['goods_id']];
  144. $v['amount'] = floatval($v['amount']);
  145. $statlist[] = $v;
  146. $stat_arr['series'][0]['data'][$k] = array('name' => strval($goods_list[$v['goods_id']]['goods_name']), 'y' => floatval($v['amount']));
  147. }
  148. }
  149. }
  150. //得到统计图数据
  151. $stat_arr['legend']['enabled'] = false;
  152. $stat_arr['series'][0]['name'] = lang('traffic');
  153. $stat_arr['title'] = lang('product_visits');
  154. $stat_arr['yAxis'] = lang('visits');
  155. $stat_json = getStatData_Column2D($stat_arr);
  156. View::assign('stat_json', $stat_json);
  157. $this->setSellerCurMenu('Statisticsflow');
  158. $this->setSellerCurItem('goodsflow');
  159. return View::fetch($this->template_dir . 'flowgoods');
  160. }
  161. /**
  162. * 用户中心右边,小导航
  163. *
  164. * @param string $menu_type 导航类型
  165. * @param string $name 当前导航的name
  166. * @return
  167. */
  168. protected function getSellerItemList() {
  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. }