Statgoods.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /**
  3. * 商品统计分析
  4. */
  5. namespace app\admin\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 Statgoods extends AdminControl
  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() . 'admin/lang/' . config('lang.default_lang') . '/stat.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 = input('param.');
  33. //处理搜索时间
  34. if (in_array(request()->action(), array('pricerange', 'hotgoods', 'goods_sale'))) {
  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 pricerange()
  60. {
  61. if (!isset($this->search_arr['search_type']) || !$this->search_arr['search_type']) {
  62. $this->search_arr['search_type'] = 'day';
  63. }
  64. $stat_model = model('stat');
  65. //获得搜索的开始时间和结束时间
  66. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  67. $where = array();
  68. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  69. $where[] = array('order_add_time', 'between', $searchtime_arr);
  70. //商品分类
  71. if ($this->choose_gcid > 0) {
  72. //获得分类深度
  73. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  74. $where[] = array('gc_parentid_' . $depth, '=', $this->choose_gcid);
  75. }
  76. $field = '1';
  77. $pricerange_arr = ($t = trim(cache('config')['stat_pricerange'])) ? unserialize($t) : '';
  78. if ($pricerange_arr) {
  79. $stat_arr['series'][0]['name'] = lang('statstore_ordernum');
  80. //设置价格区间最后一项,最后一项只有开始值没有结束值
  81. $pricerange_count = count($pricerange_arr);
  82. if ($pricerange_arr[$pricerange_count - 1]['e']) {
  83. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count - 1]['e'] + 1;
  84. $pricerange_arr[$pricerange_count]['e'] = '';
  85. }
  86. foreach ((array)$pricerange_arr as $k => $v) {
  87. $v['s'] = intval($v['s']);
  88. $v['e'] = intval($v['e']);
  89. //构造查询字段
  90. if ($v['e']) {
  91. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']} and goods_pay_price/goods_num <= {$v['e']},goods_num,0)) as goodsnum_{$k}";
  92. } else {
  93. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']},goods_num,0)) as goodsnum_{$k}";
  94. }
  95. }
  96. $ordergooods_list = Db::query('SELECT ' . $field . ' FROM ' . config('database.connections.mysql.prefix') . 'statordergoods WHERE 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) : ''));
  97. if ($ordergooods_list) {
  98. $ordergooods_list = current($ordergooods_list);
  99. foreach ((array)$pricerange_arr as $k => $v) {
  100. //横轴
  101. if ($v['e']) {
  102. $stat_arr['xAxis']['categories'][] = $v['s'] . '-' . $v['e'];
  103. } else {
  104. $stat_arr['xAxis']['categories'][] = $v['s'] . lang('above');
  105. }
  106. //统计图数据
  107. if (isset($ordergooods_list['goodsnum_' . $k])) {
  108. $stat_arr['series'][0]['data'][] = intval($ordergooods_list['goodsnum_' . $k]);
  109. } else {
  110. $stat_arr['series'][0]['data'][] = 0;
  111. }
  112. }
  113. }
  114. //得到统计图数据
  115. $stat_arr['title'] = lang('statgoods_price_distribution');
  116. $stat_arr['legend']['enabled'] = false;
  117. $stat_arr['yAxis'] = lang('ds_order_sn');
  118. $pricerange_statjson = getStatData_LineLabels($stat_arr);
  119. } else {
  120. $pricerange_statjson = '';
  121. }
  122. View::assign('pricerange_statjson', $pricerange_statjson);
  123. View::assign('searchtime', implode('|', $searchtime_arr));
  124. $this->setAdminCurItem('pricerange');
  125. return View::fetch('stat_goods_prange');
  126. }
  127. /**
  128. * 热卖商品
  129. */
  130. public function hotgoods()
  131. {
  132. if (!isset($this->search_arr['search_type']) || !$this->search_arr['search_type']) {
  133. $this->search_arr['search_type'] = 'day';
  134. }
  135. $stat_model = model('stat');
  136. //获得搜索的开始时间和结束时间
  137. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  138. View::assign('searchtime', implode('|', $searchtime_arr));
  139. $this->setAdminCurItem('hotgoods');
  140. return View::fetch('stat_goods_hotgoods');
  141. }
  142. /**
  143. * 热卖商品列表
  144. */
  145. public function hotgoods_list()
  146. {
  147. $stat_model = model('stat');
  148. $type = input('param.type');
  149. switch ($type) {
  150. case 'goodsnum':
  151. $sort_text = lang('statstore_ordernum');
  152. break;
  153. default:
  154. $type = 'orderamount';
  155. $sort_text = lang('statstore_orderamount');
  156. break;
  157. }
  158. //构造横轴数据
  159. for ($i = 1; $i <= 50; $i++) {
  160. //数据
  161. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  162. //横轴
  163. $stat_arr['xAxis']['categories'][] = "$i";
  164. }
  165. $where = array();
  166. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  167. $searchtime_arr_tmp = explode('|', $this->search_arr['t']);
  168. foreach ((array)$searchtime_arr_tmp as $k => $v) {
  169. $searchtime_arr[] = intval($v);
  170. }
  171. $where[] = array('order_add_time', 'between', $searchtime_arr);
  172. //商品分类
  173. if ($this->choose_gcid > 0) {
  174. //获得分类深度
  175. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  176. $where[] = array('gc_parentid_' . $depth, '=', $this->choose_gcid);
  177. }
  178. //查询统计数据
  179. $field = ' goods_id,goods_name ';
  180. switch ($type) {
  181. case 'goodsnum':
  182. $field .= ' ,SUM(goods_num) as goodsnum ';
  183. $orderby = 'goodsnum desc';
  184. break;
  185. default:
  186. $type = 'orderamount';
  187. $field .= ' ,SUM(goods_pay_price) as orderamount ';
  188. $orderby = 'orderamount desc';
  189. break;
  190. }
  191. $orderby .= ',goods_id';
  192. $statlist = $stat_model->statByStatordergoods($where, $field, 0, 50, $orderby, 'goods_id');
  193. foreach ((array)$statlist as $k => $v) {
  194. switch ($type) {
  195. case 'goodsnum':
  196. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => intval($v[input('get.type')]));
  197. break;
  198. case 'orderamount':
  199. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => floatval($v[input('get.type')]));
  200. break;
  201. }
  202. $statlist[$k]['sort'] = $k + 1;
  203. }
  204. $stat_arr['series'][0]['name'] = $sort_text;
  205. $stat_arr['legend']['enabled'] = false;
  206. //得到统计图数据
  207. $stat_arr['title'] = lang('statgoods_hot_top_50');
  208. $stat_arr['yAxis'] = $sort_text;
  209. $stat_json = getStatData_Column2D($stat_arr);
  210. View::assign('stat_json', $stat_json);
  211. View::assign('statlist', $statlist);
  212. View::assign('sort_text', $sort_text);
  213. View::assign('stat_field', $type);
  214. echo View::fetch('stat_hotgoods_list');
  215. }
  216. /**
  217. * 商品销售明细
  218. */
  219. public function goods_sale()
  220. {
  221. if (!isset($this->search_arr['search_type']) || !$this->search_arr['search_type']) {
  222. $this->search_arr['search_type'] = 'day';
  223. }
  224. $stat_model = model('stat');
  225. //获得搜索的开始时间和结束时间
  226. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  227. //获取相关数据
  228. $where = array();
  229. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  230. $where[] = array('order_add_time', 'between', $searchtime_arr);
  231. //品牌
  232. $brand_id = intval(input('param.b_id'));
  233. if ($brand_id > 0) {
  234. $where[] = array('brand_id', '=', $brand_id);
  235. }
  236. //商品分类
  237. if ($this->choose_gcid > 0) {
  238. //获得分类深度
  239. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  240. $where[] = array('gc_parentid_' . $depth, '=', $this->choose_gcid);
  241. }
  242. if (trim(input('param.goods_name'))) {
  243. $where[] = array('goods_name', 'like', '%' . trim(input('param.goods_name')) . '%');
  244. }
  245. if (trim(input('param.store_name'))) {
  246. $where[] = array('store_name', 'like', '%' . trim(input('param.store_name')) . '%');
  247. }
  248. $field = 'goods_id,goods_name,store_id,store_name,goods_commonid,SUM(goods_num) as goodsnum,COUNT(DISTINCT order_id) as ordernum,SUM(goods_pay_price) as goodsamount';
  249. //排序
  250. $orderby_arr = array('goodsnum asc', 'goodsnum desc', 'ordernum asc', 'ordernum desc', 'goodsamount asc', 'goodsamount desc');
  251. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  252. $this->search_arr['orderby'] = 'goodsnum desc';
  253. }
  254. $orderby = trim($this->search_arr['orderby']) . ',goods_id asc';
  255. //查询记录总条数
  256. $count_arr = $stat_model->getoneByStatordergoods($where, 'COUNT(DISTINCT goods_id) as countnum');
  257. $countnum = intval($count_arr['countnum']);
  258. if (input('param.exporttype') == 'excel') {
  259. $goods_list = $stat_model->statByStatordergoods($where, $field, 0, 0, $orderby, 'goods_id');
  260. } else {
  261. $goods_list = $stat_model->statByStatordergoods($where, $field, 10, 0, $orderby, 'goods_id');
  262. }
  263. //导出Excel
  264. if (input('param.exporttype') == 'excel') {
  265. //导出Excel
  266. $excel_obj = new \excel\Excel();
  267. $excel_data = array();
  268. //设置样式
  269. $excel_obj->setStyle(array('id' => 's_title', 'Font' => array('FontName' => '宋体', 'Size' => '12', 'Bold' => '1')));
  270. //header
  271. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_goods_name'));
  272. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('goods_commonid'));
  273. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('ds_store_name'));
  274. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('goodsnum'));
  275. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('statstore_ordernum'));
  276. $excel_data[0][] = array('styleid' => 's_title', 'data' => lang('statstore_orderamount'));
  277. //data
  278. foreach ($goods_list as $k => $v) {
  279. $excel_data[$k + 1][] = array('data' => $v['goods_name']);
  280. $excel_data[$k + 1][] = array('data' => $v['goods_commonid']);
  281. $excel_data[$k + 1][] = array('data' => $v['store_name']);
  282. $excel_data[$k + 1][] = array('data' => $v['goodsnum']);
  283. $excel_data[$k + 1][] = array('data' => $v['ordernum']);
  284. $excel_data[$k + 1][] = array('data' => $v['goodsamount']);
  285. }
  286. $excel_data = $excel_obj->charset($excel_data, CHARSET);
  287. $excel_obj->addArray($excel_data);
  288. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_goods_sale'), CHARSET));
  289. $excel_obj->generateXML($excel_obj->charset(lang('stat_goods_sale'), CHARSET) . date('Y-m-d-H', TIMESTAMP));
  290. exit();
  291. } else {
  292. //查询品牌
  293. $brand_list = model('brand')->getBrandList(array('brand_apply' => 1));
  294. View::assign('brand_list', $brand_list);
  295. View::assign('goods_list', $goods_list);
  296. View::assign('show_page', $stat_model->page_info->render());
  297. View::assign('orderby', $this->search_arr['orderby']);
  298. $this->setAdminCurItem('goods_sale');
  299. return View::fetch('stat_goodssale');
  300. }
  301. }
  302. protected function getAdminItemList()
  303. {
  304. $menu_array = array(
  305. array('name' => 'pricerange', 'text' => lang('stat_goods_pricerange'), 'url' => (string)url('Statgoods/pricerange')),
  306. array('name' => 'hotgoods', 'text' => lang('stat_hotgoods'), 'url' => (string)url('Statgoods/hotgoods')),
  307. array('name' => 'goods_sale', 'text' => lang('stat_goods_sale'), 'url' => (string)url('Statgoods/goods_sale')),
  308. );
  309. return $menu_array;
  310. }
  311. }