Statgoods.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  14. * 网站地址: https://www.valimart.net/
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Statgoods extends AdminControl
  21. {
  22. private $search_arr;//处理后的参数
  23. private $gc_arr;//分类数组
  24. private $choose_gcid;//选择的分类ID
  25. public function initialize()
  26. {
  27. parent::initialize(); // TODO: Change the autogenerated stub
  28. Lang::load(base_path().'admin/lang/'.config('lang.default_lang').'/stat.lang.php');
  29. include_once root_path(). 'extend/mall/statistics.php';
  30. include_once root_path(). 'extend/mall/datehelper.php';
  31. $stat_model = model('stat');
  32. //存储参数
  33. $this->search_arr = input('param.');
  34. //处理搜索时间
  35. if (in_array(request()->action(),array('pricerange','hotgoods','goods_sale'))){
  36. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  37. //获得系统年份
  38. $year_arr = getSystemYearArr();
  39. //获得系统月份
  40. $month_arr = getSystemMonthArr();
  41. //获得本月的周时间段
  42. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  43. View::assign('year_arr', $year_arr);
  44. View::assign('month_arr', $month_arr);
  45. View::assign('week_arr', $week_arr);
  46. }
  47. View::assign('search_arr', $this->search_arr);
  48. /**
  49. * 处理商品分类
  50. */
  51. $this->choose_gcid = ($t = intval(input('param.choose_gcid')))>0?$t:0;
  52. $gccache_arr = model('goodsclass')->getGoodsclassCache($this->choose_gcid,3);
  53. $this->gc_arr = $gccache_arr['showclass'];
  54. View::assign('gc_json',json_encode($gccache_arr['showclass']));
  55. View::assign('gc_choose_json',json_encode($gccache_arr['choose_gcid']));
  56. }
  57. /**
  58. * 价格区间统计
  59. */
  60. public function pricerange(){
  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. if(!isset($this->search_arr['search_type']) || !$this->search_arr['search_type']){
  132. $this->search_arr['search_type'] = 'day';
  133. }
  134. $stat_model = model('stat');
  135. //获得搜索的开始时间和结束时间
  136. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  137. View::assign('searchtime',implode('|',$searchtime_arr));
  138. $this->setAdminCurItem('hotgoods');
  139. return View::fetch('stat_goods_hotgoods');
  140. }
  141. /**
  142. * 热卖商品列表
  143. */
  144. public function hotgoods_list(){
  145. $stat_model = model('stat');
  146. $type=input('param.type');
  147. switch ($type){
  148. case 'goodsnum':
  149. $sort_text = lang('statstore_ordernum');
  150. break;
  151. default:
  152. $type = 'orderamount';
  153. $sort_text = lang('statstore_orderamount');
  154. break;
  155. }
  156. //构造横轴数据
  157. for($i=1; $i<=50; $i++){
  158. //数据
  159. $stat_arr['series'][0]['data'][] = array('name'=>'','y'=>0);
  160. //横轴
  161. $stat_arr['xAxis']['categories'][] = "$i";
  162. }
  163. $where = array();
  164. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  165. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  166. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  167. $searchtime_arr[] = intval($v);
  168. }
  169. $where[] = array('order_add_time','between',$searchtime_arr);
  170. //商品分类
  171. if ($this->choose_gcid > 0){
  172. //获得分类深度
  173. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  174. $where[] = array('gc_parentid_'.$depth,'=',$this->choose_gcid);
  175. }
  176. //查询统计数据
  177. $field = ' goods_id,goods_name ';
  178. switch ($type){
  179. case 'goodsnum':
  180. $field .= ' ,SUM(goods_num) as goodsnum ';
  181. $orderby = 'goodsnum desc';
  182. break;
  183. default:
  184. $type = 'orderamount';
  185. $field .= ' ,SUM(goods_pay_price) as orderamount ';
  186. $orderby = 'orderamount desc';
  187. break;
  188. }
  189. $orderby .= ',goods_id';
  190. $statlist = $stat_model->statByStatordergoods($where, $field, 0, 50, $orderby, 'goods_id');
  191. foreach ((array)$statlist as $k=>$v){
  192. switch ($type){
  193. case 'goodsnum':
  194. $stat_arr['series'][0]['data'][$k] = array('name'=>strval($v['goods_name']),'y'=>intval($v[input('get.type')]));
  195. break;
  196. case 'orderamount':
  197. $stat_arr['series'][0]['data'][$k] = array('name'=>strval($v['goods_name']),'y'=>floatval($v[input('get.type')]));
  198. break;
  199. }
  200. $statlist[$k]['sort'] = $k+1;
  201. }
  202. $stat_arr['series'][0]['name'] = $sort_text;
  203. $stat_arr['legend']['enabled'] = false;
  204. //得到统计图数据
  205. $stat_arr['title'] = lang('statgoods_hot_top_50');
  206. $stat_arr['yAxis'] = $sort_text;
  207. $stat_json = getStatData_Column2D($stat_arr);
  208. View::assign('stat_json',$stat_json);
  209. View::assign('statlist',$statlist);
  210. View::assign('sort_text',$sort_text);
  211. View::assign('stat_field',$type);
  212. echo View::fetch('stat_hotgoods_list');
  213. }
  214. /**
  215. * 商品销售明细
  216. */
  217. public function goods_sale(){
  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. }