Statisticsgoods.php 17 KB

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