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