Statgoods.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. * DSMall多用户商城
  12. * ============================================================================
  13. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  14. * 网站地址: http://www.csdeshang.com
  15. * ----------------------------------------------------------------------------
  16. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  17. * 不允许对程序代码以任何形式任何目的的再发布。
  18. * ============================================================================
  19. * 控制器
  20. */
  21. class Statgoods extends AdminControl
  22. {
  23. private $search_arr;//处理后的参数
  24. private $gc_arr;//分类数组
  25. private $choose_gcid;//选择的分类ID
  26. public function initialize()
  27. {
  28. parent::initialize(); // TODO: Change the autogenerated stub
  29. Lang::load(base_path().'admin/lang/'.config('lang.default_lang').'/stat.lang.php');
  30. include_once root_path(). 'extend/mall/statistics.php';
  31. include_once root_path(). 'extend/mall/datehelper.php';
  32. $stat_model = model('stat');
  33. //存储参数
  34. $this->search_arr = input('param.');
  35. //处理搜索时间
  36. if (in_array(request()->action(),array('pricerange','hotgoods','goods_sale'))){
  37. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  38. //获得系统年份
  39. $year_arr = getSystemYearArr();
  40. //获得系统月份
  41. $month_arr = getSystemMonthArr();
  42. //获得本月的周时间段
  43. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  44. View::assign('year_arr', $year_arr);
  45. View::assign('month_arr', $month_arr);
  46. View::assign('week_arr', $week_arr);
  47. }
  48. View::assign('search_arr', $this->search_arr);
  49. /**
  50. * 处理商品分类
  51. */
  52. $this->choose_gcid = ($t = intval(input('param.choose_gcid')))>0?$t:0;
  53. $gccache_arr = model('goodsclass')->getGoodsclassCache($this->choose_gcid,3);
  54. $this->gc_arr = $gccache_arr['showclass'];
  55. View::assign('gc_json',json_encode($gccache_arr['showclass']));
  56. View::assign('gc_choose_json',json_encode($gccache_arr['choose_gcid']));
  57. }
  58. /**
  59. * 价格区间统计
  60. */
  61. public function pricerange(){
  62. if(!isset($this->search_arr['search_type']) || !$this->search_arr['search_type']){
  63. $this->search_arr['search_type'] = 'day';
  64. }
  65. $stat_model = model('stat');
  66. //获得搜索的开始时间和结束时间
  67. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  68. $where = array();
  69. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  70. $where[] = array('order_add_time','between',$searchtime_arr);
  71. //商品分类
  72. if ($this->choose_gcid > 0){
  73. //获得分类深度
  74. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  75. $where[] = array('gc_parentid_'.$depth,'=',$this->choose_gcid);
  76. }
  77. $field = '1';
  78. $pricerange_arr = ($t = trim(cache('config')['stat_pricerange']))?unserialize($t):'';
  79. if ($pricerange_arr){
  80. $stat_arr['series'][0]['name'] = lang('statstore_ordernum');
  81. //设置价格区间最后一项,最后一项只有开始值没有结束值
  82. $pricerange_count = count($pricerange_arr);
  83. if ($pricerange_arr[$pricerange_count-1]['e']){
  84. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count-1]['e'] + 1;
  85. $pricerange_arr[$pricerange_count]['e'] = '';
  86. }
  87. foreach ((array)$pricerange_arr as $k=>$v){
  88. $v['s'] = intval($v['s']);
  89. $v['e'] = intval($v['e']);
  90. //构造查询字段
  91. if ($v['e']){
  92. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']} and goods_pay_price/goods_num <= {$v['e']},goods_num,0)) as goodsnum_{$k}";
  93. } else {
  94. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']},goods_num,0)) as goodsnum_{$k}";
  95. }
  96. }
  97. $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):''));
  98. if($ordergooods_list){
  99. $ordergooods_list= current($ordergooods_list);
  100. foreach ((array)$pricerange_arr as $k=>$v){
  101. //横轴
  102. if($v['e']){
  103. $stat_arr['xAxis']['categories'][] = $v['s'].'-'.$v['e'];
  104. } else {
  105. $stat_arr['xAxis']['categories'][] = $v['s'].lang('above');
  106. }
  107. //统计图数据
  108. if (isset($ordergooods_list['goodsnum_'.$k])){
  109. $stat_arr['series'][0]['data'][] = intval($ordergooods_list['goodsnum_'.$k]);
  110. } else {
  111. $stat_arr['series'][0]['data'][] = 0;
  112. }
  113. }
  114. }
  115. //得到统计图数据
  116. $stat_arr['title'] = lang('statgoods_price_distribution');
  117. $stat_arr['legend']['enabled'] = false;
  118. $stat_arr['yAxis'] = lang('ds_order_sn');
  119. $pricerange_statjson = getStatData_LineLabels($stat_arr);
  120. } else {
  121. $pricerange_statjson = '';
  122. }
  123. View::assign('pricerange_statjson',$pricerange_statjson);
  124. View::assign('searchtime',implode('|',$searchtime_arr));
  125. $this->setAdminCurItem('pricerange');
  126. return View::fetch('stat_goods_prange');
  127. }
  128. /**
  129. * 热卖商品
  130. */
  131. public function hotgoods(){
  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. $stat_model = model('stat');
  147. $type=input('param.type');
  148. switch ($type){
  149. case 'goodsnum':
  150. $sort_text = lang('statstore_ordernum');
  151. break;
  152. default:
  153. $type = 'orderamount';
  154. $sort_text = lang('statstore_orderamount');
  155. break;
  156. }
  157. //构造横轴数据
  158. for($i=1; $i<=50; $i++){
  159. //数据
  160. $stat_arr['series'][0]['data'][] = array('name'=>'','y'=>0);
  161. //横轴
  162. $stat_arr['xAxis']['categories'][] = "$i";
  163. }
  164. $where = array();
  165. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  166. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  167. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  168. $searchtime_arr[] = intval($v);
  169. }
  170. $where[] = array('order_add_time','between',$searchtime_arr);
  171. //商品分类
  172. if ($this->choose_gcid > 0){
  173. //获得分类深度
  174. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  175. $where[] = array('gc_parentid_'.$depth,'=',$this->choose_gcid);
  176. }
  177. //查询统计数据
  178. $field = ' goods_id,goods_name ';
  179. switch ($type){
  180. case 'goodsnum':
  181. $field .= ' ,SUM(goods_num) as goodsnum ';
  182. $orderby = 'goodsnum desc';
  183. break;
  184. default:
  185. $type = 'orderamount';
  186. $field .= ' ,SUM(goods_pay_price) as orderamount ';
  187. $orderby = 'orderamount desc';
  188. break;
  189. }
  190. $orderby .= ',goods_id';
  191. $statlist = $stat_model->statByStatordergoods($where, $field, 0, 50, $orderby, 'goods_id');
  192. foreach ((array)$statlist as $k=>$v){
  193. switch ($type){
  194. case 'goodsnum':
  195. $stat_arr['series'][0]['data'][$k] = array('name'=>strval($v['goods_name']),'y'=>intval($v[input('get.type')]));
  196. break;
  197. case 'orderamount':
  198. $stat_arr['series'][0]['data'][$k] = array('name'=>strval($v['goods_name']),'y'=>floatval($v[input('get.type')]));
  199. break;
  200. }
  201. $statlist[$k]['sort'] = $k+1;
  202. }
  203. $stat_arr['series'][0]['name'] = $sort_text;
  204. $stat_arr['legend']['enabled'] = false;
  205. //得到统计图数据
  206. $stat_arr['title'] = lang('statgoods_hot_top_50');
  207. $stat_arr['yAxis'] = $sort_text;
  208. $stat_json = getStatData_Column2D($stat_arr);
  209. View::assign('stat_json',$stat_json);
  210. View::assign('statlist',$statlist);
  211. View::assign('sort_text',$sort_text);
  212. View::assign('stat_field',$type);
  213. echo View::fetch('stat_hotgoods_list');
  214. }
  215. /**
  216. * 商品销售明细
  217. */
  218. public function goods_sale(){
  219. if(!isset($this->search_arr['search_type']) || !$this->search_arr['search_type']){
  220. $this->search_arr['search_type'] = 'day';
  221. }
  222. $stat_model = model('stat');
  223. //获得搜索的开始时间和结束时间
  224. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  225. //获取相关数据
  226. $where = array();
  227. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  228. $where[] = array('order_add_time','between',$searchtime_arr);
  229. //品牌
  230. $brand_id = intval(input('param.b_id'));
  231. if ($brand_id > 0){
  232. $where[] = array('brand_id','=',$brand_id);
  233. }
  234. //商品分类
  235. if ($this->choose_gcid > 0){
  236. //获得分类深度
  237. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  238. $where[]=array('gc_parentid_'.$depth,'=',$this->choose_gcid);
  239. }
  240. if(trim(input('param.goods_name'))){
  241. $where[]=array('goods_name','like','%'.trim(input('param.goods_name')).'%');
  242. }
  243. if(trim(input('param.store_name'))){
  244. $where[]=array('store_name','like','%'.trim(input('param.store_name')).'%');
  245. }
  246. $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';
  247. //排序
  248. $orderby_arr = array('goodsnum asc','goodsnum desc','ordernum asc','ordernum desc','goodsamount asc','goodsamount desc');
  249. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']),$orderby_arr)){
  250. $this->search_arr['orderby'] = 'goodsnum desc';
  251. }
  252. $orderby = trim($this->search_arr['orderby']).',goods_id asc';
  253. //查询记录总条数
  254. $count_arr = $stat_model->getoneByStatordergoods($where, 'COUNT(DISTINCT goods_id) as countnum');
  255. $countnum = intval($count_arr['countnum']);
  256. if (input('param.exporttype') == 'excel'){
  257. $goods_list = $stat_model->statByStatordergoods($where, $field, 0, 0, $orderby, 'goods_id');
  258. } else {
  259. $goods_list = $stat_model->statByStatordergoods($where, $field, 10, 0, $orderby, 'goods_id');
  260. }
  261. //导出Excel
  262. if (input('param.exporttype') == 'excel'){
  263. //导出Excel
  264. $excel_obj = new \excel\Excel();
  265. $excel_data = array();
  266. //设置样式
  267. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  268. //header
  269. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('ds_goods_name'));
  270. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('goods_commonid'));
  271. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('ds_store_name'));
  272. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('goodsnum'));
  273. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('statstore_ordernum'));
  274. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('statstore_orderamount'));
  275. //data
  276. foreach ($goods_list as $k=>$v){
  277. $excel_data[$k+1][] = array('data'=>$v['goods_name']);
  278. $excel_data[$k+1][] = array('data'=>$v['goods_commonid']);
  279. $excel_data[$k+1][] = array('data'=>$v['store_name']);
  280. $excel_data[$k+1][] = array('data'=>$v['goodsnum']);
  281. $excel_data[$k+1][] = array('data'=>$v['ordernum']);
  282. $excel_data[$k+1][] = array('data'=>$v['goodsamount']);
  283. }
  284. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  285. $excel_obj->addArray($excel_data);
  286. $excel_obj->addWorksheet($excel_obj->charset(lang('stat_goods_sale'),CHARSET));
  287. $excel_obj->generateXML($excel_obj->charset(lang('stat_goods_sale'),CHARSET).date('Y-m-d-H',TIMESTAMP));
  288. exit();
  289. } else {
  290. //查询品牌
  291. $brand_list = model('brand')->getBrandList(array('brand_apply'=>1));
  292. View::assign('brand_list',$brand_list);
  293. View::assign('goods_list',$goods_list);
  294. View::assign('show_page',$stat_model->page_info->render());
  295. View::assign('orderby',$this->search_arr['orderby']);
  296. $this->setAdminCurItem('goods_sale');
  297. return View::fetch('stat_goodssale');
  298. }
  299. }
  300. protected function getAdminItemList()
  301. {
  302. $menu_array = array(
  303. array('name' => 'pricerange', 'text' => lang('stat_goods_pricerange'), 'url' => (string)url('Statgoods/pricerange')),
  304. array('name' => 'hotgoods', 'text' => lang('stat_hotgoods'), 'url' => (string)url('Statgoods/hotgoods')),
  305. array('name' => 'goods_sale', 'text' => lang('stat_goods_sale'), 'url' => (string)url('Statgoods/goods_sale')),
  306. );
  307. return $menu_array;
  308. }
  309. }