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. class Statisticsgoods extends BaseSeller
  17. {
  18. private $search_arr; //处理后的参数
  19. private $gc_arr; //分类数组
  20. private $choose_gcid; //选择的分类ID
  21. public function initialize()
  22. {
  23. parent::initialize(); // TODO: Change the autogenerated stub
  24. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statisticsgoods.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. if (in_array(request()->action(), array('price', 'hotgoods'))) {
  32. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  33. //获得系统年份
  34. $year_arr = getSystemYearArr();
  35. //获得系统月份
  36. $month_arr = getSystemMonthArr();
  37. //获得本月的周时间段
  38. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  39. View::assign('year_arr', $year_arr);
  40. View::assign('month_arr', $month_arr);
  41. View::assign('week_arr', $week_arr);
  42. }
  43. View::assign('search_arr', $this->search_arr);
  44. /**
  45. * 处理商品分类
  46. */
  47. $this->choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
  48. $gccache_arr = model('goodsclass')->getGoodsclassCache($this->choose_gcid, 3);
  49. $this->gc_arr = $gccache_arr['showclass'];
  50. View::assign('gc_json', json_encode($gccache_arr['showclass']));
  51. View::assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));
  52. }
  53. /**
  54. * 商品列表
  55. */
  56. public function index()
  57. {
  58. $stat_model = model('stat');
  59. //统计的日期0点
  60. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  61. /*
  62. * 近30天
  63. */
  64. $stime = $stat_time - (86400 * 29); //30天前
  65. $etime = $stat_time + 86400 - 1; //昨天23:59
  66. //查询订单商品表下单商品数
  67. $where = array();
  68. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  69. $where[] = array('store_id', '=', session('store_id'));
  70. $where[] = array('order_add_time', 'between', array($stime, $etime));
  71. if ($this->choose_gcid > 0) {
  72. $gc_depth = $this->gc_arr[$this->choose_gcid]['depth'];
  73. $where[] = array('gc_parentid_' . $gc_depth, '=', $this->choose_gcid);
  74. }
  75. if (trim(input('param.search_gname'))) {
  76. $where[] = array('goods_name', 'like', "%" . trim(input('param.search_gname')) . "%");
  77. }
  78. //查询总条数
  79. $count_arr = $stat_model->statByStatordergoods($where, 'count(DISTINCT goods_id) as countnum');
  80. $countnum = intval($count_arr[0]['countnum']);
  81. $field = ' goods_id,goods_name,goods_image,goods_price,SUM(goods_num) as ordergoodsnum,SUM(goods_pay_price) as ordergamount ';
  82. //排序
  83. $orderby_arr = array('ordergoodsnum asc', 'ordergoodsnum desc', 'ordergamount asc', 'ordergamount desc');
  84. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  85. $this->search_arr['orderby'] = 'ordergoodsnum desc';
  86. }
  87. $orderby = trim($this->search_arr['orderby']) . ',goods_id';
  88. $stat_ordergoods = $stat_model->statByStatordergoods($where, $field, 5, 0, $this->search_arr['orderby'], 'goods_id');
  89. View::assign('goodslist', $stat_ordergoods);
  90. View::assign('show_page', $stat_model->page_info->render());
  91. View::assign('orderby', $this->search_arr['orderby']);
  92. $this->setSellerCurMenu('Statisticsgoods');
  93. $this->setSellerCurItem('goodslist');
  94. return View::fetch($this->template_dir . 'goodslist');
  95. }
  96. /**
  97. * 商品详细
  98. */
  99. public function goodsinfo()
  100. {
  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. {
  193. if (!isset($this->search_arr['search_type'])) {
  194. $this->search_arr['search_type'] = 'day';
  195. }
  196. $stat_model = model('stat');
  197. //获得搜索的开始时间和结束时间
  198. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  199. $where = array();
  200. $where[] = array('store_id', '=', session('store_id'));
  201. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  202. $where[] = array('order_add_time', 'between', $searchtime_arr);
  203. //商品分类
  204. if ($this->choose_gcid > 0) {
  205. //获得分类深度
  206. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  207. $where[] = array('gc_parentid_' . $depth, '=', $this->choose_gcid);
  208. }
  209. $field = '1';
  210. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'pricerange');
  211. $pricerange_arr = $pricerange ? unserialize($pricerange) : array();
  212. if ($pricerange_arr) {
  213. $stat_arr['series'][0]['name'] = lang('order_quantity');
  214. //设置价格区间最后一项,最后一项只有开始值没有结束值
  215. $pricerange_count = count($pricerange_arr);
  216. if ($pricerange_arr[$pricerange_count - 1]['e']) {
  217. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count - 1]['e'] + 1;
  218. $pricerange_arr[$pricerange_count]['e'] = '';
  219. }
  220. foreach ((array) $pricerange_arr as $k => $v) {
  221. $v['s'] = intval($v['s']);
  222. $v['e'] = intval($v['e']);
  223. //构造查询字段
  224. if ($v['e']) {
  225. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']} and goods_pay_price/goods_num <= {$v['e']},goods_num,0)) as goodsnum_{$k}";
  226. } else {
  227. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']},goods_num,0)) as goodsnum_{$k}";
  228. }
  229. }
  230. $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) : ''));
  231. if ($ordergooods_list) {
  232. $ordergooods_list = current($ordergooods_list);
  233. foreach ((array) $pricerange_arr as $k => $v) {
  234. //横轴
  235. if ($v['e']) {
  236. $stat_arr['xAxis']['categories'][] = $v['s'] . '-' . $v['e'];
  237. } else {
  238. $stat_arr['xAxis']['categories'][] = $v['s'] . lang('above');
  239. }
  240. //统计图数据
  241. if (isset($ordergooods_list['goodsnum_' . $k])) {
  242. $stat_arr['series'][0]['data'][] = intval($ordergooods_list['goodsnum_' . $k]);
  243. } else {
  244. $stat_arr['series'][0]['data'][] = 0;
  245. }
  246. }
  247. }
  248. //得到统计图数据
  249. $stat_arr['title'] = lang('price_distribution');
  250. $stat_arr['legend']['enabled'] = false;
  251. $stat_arr['yAxis'] = lang('sales');
  252. $pricerange_statjson = getStatData_LineLabels($stat_arr);
  253. } else {
  254. $pricerange_statjson = '';
  255. }
  256. View::assign('statjson', $pricerange_statjson);
  257. $this->setSellerCurMenu('Statisticsgoods');
  258. $this->setSellerCurItem('price');
  259. return View::fetch($this->template_dir . 'goods_price');
  260. }
  261. /**
  262. * 热卖商品
  263. */
  264. public function hotgoods()
  265. {
  266. $topnum = 30;
  267. if (!isset($this->search_arr['search_type'])) {
  268. $this->search_arr['search_type'] = 'day';
  269. }
  270. $stat_model = model('stat');
  271. //获得搜索的开始时间和结束时间
  272. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  273. $stat_model = model('stat');
  274. $where = array();
  275. $where[] = array('store_id', '=', session('store_id'));
  276. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  277. $where[] = array('order_add_time', 'between', $searchtime_arr);
  278. //查询销量top
  279. //构造横轴数据
  280. for ($i = 1; $i <= $topnum; $i++) {
  281. //数据
  282. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  283. //横轴
  284. $stat_arr['xAxis']['categories'][] = "$i";
  285. }
  286. $field = ' goods_id,goods_name,SUM(goods_num) as goodsnum ';
  287. $orderby = 'goodsnum desc,goods_id';
  288. $statlist = array();
  289. $statlist['goodsnum'] = $stat_model->statByStatordergoods($where, $field, 0, $topnum, $orderby, 'goods_id');
  290. foreach ((array) $statlist['goodsnum'] as $k => $v) {
  291. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => intval($v['goodsnum']));
  292. }
  293. $stat_arr['series'][0]['name'] = lang('order_quantity');
  294. $stat_arr['legend']['enabled'] = false;
  295. //得到统计图数据
  296. $stat_arr['title'] = lang('hot_commodity_top') . $topnum;
  297. $stat_arr['yAxis'] = lang('order_quantity');
  298. $stat_json['goodsnum'] = getStatData_Column2D($stat_arr);
  299. unset($stat_arr);
  300. //查询下单金额top
  301. //构造横轴数据
  302. for ($i = 1; $i <= $topnum; $i++) {
  303. //数据
  304. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  305. //横轴
  306. $stat_arr['xAxis']['categories'][] = "$i";
  307. }
  308. $field = ' goods_id,goods_name,SUM(goods_pay_price) as orderamount ';
  309. $orderby = 'orderamount desc,goods_id';
  310. $statlist['orderamount'] = $stat_model->statByStatordergoods($where, $field, 0, $topnum, $orderby, 'goods_id');
  311. foreach ((array) $statlist['orderamount'] as $k => $v) {
  312. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => floatval($v['orderamount']));
  313. }
  314. $stat_arr['series'][0]['name'] = lang('place_order_amount');
  315. $stat_arr['legend']['enabled'] = false;
  316. //得到统计图数据
  317. $stat_arr['title'] = lang('hot_commodity_top') . $topnum;
  318. $stat_arr['yAxis'] = lang('place_order_amount');
  319. $stat_json['orderamount'] = getStatData_Column2D($stat_arr);
  320. View::assign('stat_json', $stat_json);
  321. View::assign('statlist', $statlist);
  322. $this->setSellerCurItem('hotgoods');
  323. $this->setSellerCurMenu('Statisticsgoods');
  324. return View::fetch($this->template_dir . 'hotgoods');
  325. }
  326. /**
  327. * 用户中心右边,小导航
  328. *
  329. * @param string $menu_type 导航类型
  330. * @param string $name 当前导航的name
  331. * @return
  332. */
  333. protected function getSellerItemList()
  334. {
  335. $menu_array = array(
  336. array('name' => 'goodslist', 'text' => lang('goods_details'), 'url' => (string) url('Statisticsgoods/index')),
  337. array('name' => 'price', 'text' => lang('sales_price'), 'url' => (string) url('Statisticsgoods/price')),
  338. array('name' => 'hotgoods', 'text' => lang('selling_goods'), 'url' => (string) url('Statisticsgoods/hotgoods')),
  339. );
  340. return $menu_array;
  341. }
  342. }