Statisticsgoods.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. /**
  3. * 商品分析
  4. */
  5. namespace app\home\controller;
  6. use think\facade\View;
  7. use think\facade\Lang;
  8. use think\facade\Db;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  14. * 网站地址: https://www.valimart.net/
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Statisticsgoods extends BaseSeller {
  21. private $search_arr; //处理后的参数
  22. private $gc_arr; //分类数组
  23. private $choose_gcid; //选择的分类ID
  24. public function initialize() {
  25. parent::initialize(); // TODO: Change the autogenerated stub
  26. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statisticsgoods.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 = $_REQUEST;
  32. //处理搜索时间
  33. if (in_array(request()->action(), array('price', 'hotgoods'))) {
  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. $this->choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
  50. $gccache_arr = model('goodsclass')->getGoodsclassCache($this->choose_gcid, 3);
  51. $this->gc_arr = $gccache_arr['showclass'];
  52. View::assign('gc_json', json_encode($gccache_arr['showclass']));
  53. View::assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));
  54. }
  55. /**
  56. * 商品列表
  57. */
  58. public function index() {
  59. $stat_model = model('stat');
  60. //统计的日期0点
  61. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  62. /*
  63. * 近30天
  64. */
  65. $stime = $stat_time - (86400 * 29); //30天前
  66. $etime = $stat_time + 86400 - 1; //昨天23:59
  67. //查询订单商品表下单商品数
  68. $where = array();
  69. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  70. $where[] = array('store_id', '=', session('store_id'));
  71. $where[] = array('order_add_time', 'between', array($stime, $etime));
  72. if ($this->choose_gcid > 0) {
  73. $gc_depth = $this->gc_arr[$this->choose_gcid]['depth'];
  74. $where[] = array('gc_parentid_' . $gc_depth, '=', $this->choose_gcid);
  75. }
  76. if (trim(input('param.search_gname'))) {
  77. $where[] = array('goods_name', 'like', "%" . trim(input('param.search_gname')) . "%");
  78. }
  79. //查询总条数
  80. $count_arr = $stat_model->statByStatordergoods($where, 'count(DISTINCT goods_id) as countnum');
  81. $countnum = intval($count_arr[0]['countnum']);
  82. $field = ' goods_id,goods_name,goods_image,goods_price,SUM(goods_num) as ordergoodsnum,SUM(goods_pay_price) as ordergamount ';
  83. //排序
  84. $orderby_arr = array('ordergoodsnum asc', 'ordergoodsnum desc', 'ordergamount asc', 'ordergamount desc');
  85. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  86. $this->search_arr['orderby'] = 'ordergoodsnum desc';
  87. }
  88. $orderby = trim($this->search_arr['orderby']) . ',goods_id';
  89. $stat_ordergoods = $stat_model->statByStatordergoods($where, $field, 5, 0, $this->search_arr['orderby'], 'goods_id');
  90. View::assign('goodslist', $stat_ordergoods);
  91. View::assign('show_page', $stat_model->page_info->render());
  92. View::assign('orderby', $this->search_arr['orderby']);
  93. $this->setSellerCurMenu('Statisticsgoods');
  94. $this->setSellerCurItem('goodslist');
  95. return View::fetch($this->template_dir . 'goodslist');
  96. }
  97. /**
  98. * 商品详细
  99. */
  100. public function goodsinfo() {
  101. $templatesname = 'goodsinfo';
  102. $goods_id = intval(input('param.gid'));
  103. if ($goods_id <= 0) {
  104. View::assign('stat_msg', lang('param_error'));
  105. echo View::fetch($this->template_dir . $templatesname);
  106. exit;
  107. }
  108. //查询商品信息
  109. $goods_info = model('goods')->getGoodsInfoByID($goods_id);
  110. if (!$goods_info) {
  111. View::assign('stat_msg', lang('param_error'));
  112. echo View::fetch($this->template_dir . $templatesname);
  113. exit;
  114. }
  115. $stat_model = model('stat');
  116. //统计的日期0点
  117. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  118. /*
  119. * 近30天
  120. */
  121. $stime = $stat_time - (86400 * 29); //30天前
  122. $etime = $stat_time + 86400 - 1; //昨天23:59
  123. $stat_arr = array();
  124. for ($i = $stime; $i < $etime; $i += 86400) {
  125. //当前数据的时间
  126. $timetext = date('n', $i) . '-' . date('j', $i);
  127. //统计图数据
  128. $stat_list['ordergoodsnum'][$timetext] = 0;
  129. $stat_list['ordergamount'][$timetext] = 0;
  130. $stat_list['ordernum'][$timetext] = 0;
  131. //横轴
  132. $stat_arr['ordergoodsnum']['xAxis']['categories'][] = $timetext;
  133. $stat_arr['ordergamount']['xAxis']['categories'][] = $timetext;
  134. $stat_arr['ordernum']['xAxis']['categories'][] = $timetext;
  135. }
  136. //查询订单商品表下单商品数
  137. $where = array();
  138. $where[] = array('goods_id','=',$goods_id);
  139. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  140. $where[] = array('store_id','=',session('store_id'));
  141. $where[] = array('order_add_time','between',array($stime, $etime));
  142. $field = ' goods_id,goods_name,COUNT(DISTINCT order_id) as ordernum,SUM(goods_num) as ordergoodsnum,SUM(goods_pay_price) as ordergamount,MONTH(FROM_UNIXTIME(order_add_time)) as monthval,DAY(FROM_UNIXTIME(order_add_time)) as dayval ';
  143. $stat_ordergoods = $stat_model->statByStatordergoods($where, $field, 0, 0, '', '');
  144. $stat_count = array();
  145. if ($stat_ordergoods) {
  146. foreach ($stat_ordergoods as $k => $v) {
  147. $stat_list['ordergoodsnum'][$v['monthval'] . '-' . $v['dayval']] = intval($v['ordergoodsnum']);
  148. $stat_list['ordergamount'][$v['monthval'] . '-' . $v['dayval']] = floatval($v['ordergamount']);
  149. $stat_list['ordernum'][$v['monthval'] . '-' . $v['dayval']] = intval($v['ordernum']);
  150. if (!isset($stat_count['ordergoodsnum'])) {
  151. $stat_count['ordergoodsnum'] = 0;
  152. }
  153. if (!isset($stat_count['ordergamount'])) {
  154. $stat_count['ordergamount'] = 0;
  155. }
  156. if (!isset($stat_count['ordernum'])) {
  157. $stat_count['ordernum'] = 0;
  158. }
  159. $stat_count['ordergoodsnum'] = intval($stat_count['ordergoodsnum']) + $v['ordergoodsnum'];
  160. $stat_count['ordergamount'] = floatval($stat_count['ordergamount']) + floatval($v['ordergamount']);
  161. $stat_count['ordernum'] = intval($stat_count['ordernum']) + $v['ordernum'];
  162. }
  163. }
  164. $stat_count['ordergamount'] = ds_price_format($stat_count['ordergamount']);
  165. $stat_arr['ordergoodsnum']['legend']['enabled'] = false;
  166. $stat_arr['ordergoodsnum']['series'][0]['name'] = lang('order_quantity');
  167. $stat_arr['ordergoodsnum']['series'][0]['data'] = array_values($stat_list['ordergoodsnum']);
  168. $stat_arr['ordergoodsnum']['title'] = lang('recent_single_commodity_trend');
  169. $stat_arr['ordergoodsnum']['yAxis'] = lang('place_order_amount');
  170. $stat_json['ordergoodsnum'] = getStatData_LineLabels($stat_arr['ordergoodsnum']);
  171. $stat_arr['ordergamount']['legend']['enabled'] = false;
  172. $stat_arr['ordergamount']['series'][0]['name'] = lang('place_order_amount');
  173. $stat_arr['ordergamount']['series'][0]['data'] = array_values($stat_list['ordergamount']);
  174. $stat_arr['ordergamount']['title'] = lang('recent_order_amount_trend');
  175. $stat_arr['ordergamount']['yAxis'] = lang('place_order_amount');
  176. $stat_json['ordergamount'] = getStatData_LineLabels($stat_arr['ordergamount']);
  177. $stat_arr['ordernum']['legend']['enabled'] = false;
  178. $stat_arr['ordernum']['series'][0]['name'] = lang('order_quantity');
  179. $stat_arr['ordernum']['series'][0]['data'] = array_values($stat_list['ordernum']);
  180. $stat_arr['ordernum']['title'] = lang('recent_orders_trend');
  181. $stat_arr['ordernum']['yAxis'] = lang('place_order_amount');
  182. $stat_json['ordernum'] = getStatData_LineLabels($stat_arr['ordernum']);
  183. View::assign('stat_json', $stat_json);
  184. View::assign('stat_count', $stat_count);
  185. View::assign('goods_info', $goods_info);
  186. echo View::fetch($this->template_dir . $templatesname);
  187. }
  188. /**
  189. * 价格销量统计
  190. */
  191. public function price() {
  192. if (!isset($this->search_arr['search_type'])) {
  193. $this->search_arr['search_type'] = 'day';
  194. }
  195. $stat_model = model('stat');
  196. //获得搜索的开始时间和结束时间
  197. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  198. $where = array();
  199. $where[] = array('store_id','=',session('store_id'));
  200. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  201. $where[] = array('order_add_time','between',$searchtime_arr);
  202. //商品分类
  203. if ($this->choose_gcid > 0) {
  204. //获得分类深度
  205. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  206. $where[] = array('gc_parentid_' . $depth,'=',$this->choose_gcid);
  207. }
  208. $field = '1';
  209. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'pricerange');
  210. $pricerange_arr = $pricerange ? unserialize($pricerange) : array();
  211. if ($pricerange_arr) {
  212. $stat_arr['series'][0]['name'] = lang('order_quantity');
  213. //设置价格区间最后一项,最后一项只有开始值没有结束值
  214. $pricerange_count = count($pricerange_arr);
  215. if ($pricerange_arr[$pricerange_count - 1]['e']) {
  216. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count - 1]['e'] + 1;
  217. $pricerange_arr[$pricerange_count]['e'] = '';
  218. }
  219. foreach ((array) $pricerange_arr as $k => $v) {
  220. $v['s'] = intval($v['s']);
  221. $v['e'] = intval($v['e']);
  222. //构造查询字段
  223. if ($v['e']) {
  224. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']} and goods_pay_price/goods_num <= {$v['e']},goods_num,0)) as goodsnum_{$k}";
  225. } else {
  226. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']},goods_num,0)) as goodsnum_{$k}";
  227. }
  228. }
  229. $ordergooods_list = Db::query('SELECT ' . $field . ' FROM ' . config('database.connections.mysql.prefix') . 'statordergoods WHERE store_id=' . session('store_id') . ' AND order_isvalid=1 AND order_add_time BETWEEN ' . $searchtime_arr[0] . ' AND ' . $searchtime_arr[1] . ($this->choose_gcid > 0 ? (' AND gc_parentid_' . $depth . '=' . $this->choose_gcid) : ''));
  230. if ($ordergooods_list) {
  231. $ordergooods_list = current($ordergooods_list);
  232. foreach ((array) $pricerange_arr as $k => $v) {
  233. //横轴
  234. if ($v['e']) {
  235. $stat_arr['xAxis']['categories'][] = $v['s'] . '-' . $v['e'];
  236. } else {
  237. $stat_arr['xAxis']['categories'][] = $v['s'] . lang('above');
  238. }
  239. //统计图数据
  240. if (isset($ordergooods_list['goodsnum_' . $k])) {
  241. $stat_arr['series'][0]['data'][] = intval($ordergooods_list['goodsnum_' . $k]);
  242. } else {
  243. $stat_arr['series'][0]['data'][] = 0;
  244. }
  245. }
  246. }
  247. //得到统计图数据
  248. $stat_arr['title'] = lang('price_distribution');
  249. $stat_arr['legend']['enabled'] = false;
  250. $stat_arr['yAxis'] = lang('sales');
  251. $pricerange_statjson = getStatData_LineLabels($stat_arr);
  252. } else {
  253. $pricerange_statjson = '';
  254. }
  255. View::assign('statjson', $pricerange_statjson);
  256. $this->setSellerCurMenu('Statisticsgoods');
  257. $this->setSellerCurItem('price');
  258. return View::fetch($this->template_dir . 'goods_price');
  259. }
  260. /**
  261. * 热卖商品
  262. */
  263. public function hotgoods() {
  264. $topnum = 30;
  265. if (!isset($this->search_arr['search_type'])) {
  266. $this->search_arr['search_type'] = 'day';
  267. }
  268. $stat_model = model('stat');
  269. //获得搜索的开始时间和结束时间
  270. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  271. $stat_model = model('stat');
  272. $where = array();
  273. $where[] = array('store_id','=',session('store_id'));
  274. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  275. $where[] = array('order_add_time','between',$searchtime_arr);
  276. //查询销量top
  277. //构造横轴数据
  278. for ($i = 1; $i <= $topnum; $i++) {
  279. //数据
  280. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  281. //横轴
  282. $stat_arr['xAxis']['categories'][] = "$i";
  283. }
  284. $field = ' goods_id,goods_name,SUM(goods_num) as goodsnum ';
  285. $orderby = 'goodsnum desc,goods_id';
  286. $statlist = array();
  287. $statlist['goodsnum'] = $stat_model->statByStatordergoods($where, $field, 0, $topnum, $orderby, 'goods_id');
  288. foreach ((array) $statlist['goodsnum'] as $k => $v) {
  289. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => intval($v['goodsnum']));
  290. }
  291. $stat_arr['series'][0]['name'] = lang('order_quantity');
  292. $stat_arr['legend']['enabled'] = false;
  293. //得到统计图数据
  294. $stat_arr['title'] = lang('hot_commodity_top') . $topnum;
  295. $stat_arr['yAxis'] = lang('order_quantity');
  296. $stat_json['goodsnum'] = getStatData_Column2D($stat_arr);
  297. unset($stat_arr);
  298. //查询下单金额top
  299. //构造横轴数据
  300. for ($i = 1; $i <= $topnum; $i++) {
  301. //数据
  302. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  303. //横轴
  304. $stat_arr['xAxis']['categories'][] = "$i";
  305. }
  306. $field = ' goods_id,goods_name,SUM(goods_pay_price) as orderamount ';
  307. $orderby = 'orderamount desc,goods_id';
  308. $statlist['orderamount'] = $stat_model->statByStatordergoods($where, $field, 0, $topnum, $orderby, 'goods_id');
  309. foreach ((array) $statlist['orderamount'] as $k => $v) {
  310. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => floatval($v['orderamount']));
  311. }
  312. $stat_arr['series'][0]['name'] = lang('place_order_amount');
  313. $stat_arr['legend']['enabled'] = false;
  314. //得到统计图数据
  315. $stat_arr['title'] = lang('hot_commodity_top') . $topnum;
  316. $stat_arr['yAxis'] = lang('place_order_amount');
  317. $stat_json['orderamount'] = getStatData_Column2D($stat_arr);
  318. View::assign('stat_json', $stat_json);
  319. View::assign('statlist', $statlist);
  320. $this->setSellerCurItem('hotgoods');
  321. $this->setSellerCurMenu('Statisticsgoods');
  322. return View::fetch($this->template_dir . 'hotgoods');
  323. }
  324. /**
  325. * 用户中心右边,小导航
  326. *
  327. * @param string $menu_type 导航类型
  328. * @param string $name 当前导航的name
  329. * @return
  330. */
  331. protected function getSellerItemList() {
  332. $menu_array = array(
  333. array('name' => 'goodslist', 'text' => lang('goods_details'), 'url' => (string) url('Statisticsgoods/index')),
  334. array('name' => 'price', 'text' => lang('sales_price'), 'url' => (string) url('Statisticsgoods/price')),
  335. array('name' => 'hotgoods', 'text' => lang('selling_goods'), 'url' => (string) url('Statisticsgoods/hotgoods')),
  336. );
  337. return $menu_array;
  338. }
  339. }