Statmarketing.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * 市场统计分析
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Db;
  8. use think\facade\Lang;
  9. /**
  10. * ============================================================================
  11. *
  12. * ============================================================================
  13. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  14. * 网站地址: https://www.valimart.net/
  15. * ----------------------------------------------------------------------------
  16. *
  17. * ============================================================================
  18. * 控制器
  19. */
  20. class Statmarketing extends AdminControl
  21. {
  22. public function initialize()
  23. {
  24. parent::initialize(); // TODO: Change the autogenerated stub
  25. Lang::load(base_path().'admin/lang/'.config('lang.default_lang').'/stat.lang.php');
  26. include_once root_path(). 'extend/mall/statistics.php';
  27. include_once root_path(). 'extend/mall/datehelper.php';
  28. $stat_model = model('stat');
  29. //存储参数
  30. $this->search_arr = input('param.');
  31. //处理搜索时间
  32. if (in_array(request()->action(),array('promotion','group'))){
  33. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  34. //获得系统年份
  35. $year_arr = getSystemYearArr();
  36. //获得系统月份
  37. $month_arr = getSystemMonthArr();
  38. //获得本月的周时间段
  39. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  40. View::assign('year_arr', $year_arr);
  41. View::assign('month_arr', $month_arr);
  42. View::assign('week_arr', $week_arr);
  43. }
  44. View::assign('search_arr', $this->search_arr);
  45. }
  46. /**
  47. * 促销分析
  48. */
  49. public function promotion(){
  50. if(!isset($this->search_arr['search_type'])){
  51. $this->search_arr['search_type'] = 'day';
  52. }
  53. $stat_model = model('stat');
  54. //获得搜索的开始时间和结束时间
  55. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  56. $where = array();
  57. $where[]=array('order_isvalid','=',1);//计入统计的有效订单
  58. $where[] = array('order_add_time','between',$searchtime_arr);
  59. $where[]=array('goods_type','in',array(2,3,4));
  60. //下单量
  61. $field = ' goods_type,count(DISTINCT order_id) as ordernum,SUM(goods_num) as goodsnum,SUM(goods_pay_price) as orderamount';
  62. $statlist_tmp = $stat_model->statByStatordergoods($where, $field, 0, 0,'', 'goods_type');
  63. //优惠类型数组
  64. $goodstype_arr = array(2=>lang('goodstype_2'),3=>lang('goodstype_3'),4=>lang('goodstype_4'));
  65. $statlist = array();
  66. $statcount = array('ordernum'=>0,'goodsnum'=>0,'orderamount'=>0.00);
  67. $stat_arr = array();
  68. $stat_json = array('ordernum'=>'','goodsnum'=>'','orderamount'=>'');
  69. if ($statlist_tmp){
  70. foreach((array)$statlist_tmp as $k=>$v){
  71. $statcount['ordernum'] += intval($v['ordernum']);
  72. $statcount['goodsnum'] += intval($v['goodsnum']);
  73. $statcount['orderamount'] += floatval($v['orderamount']);
  74. }
  75. foreach((array)$statlist_tmp as $k=>$v){
  76. $v['ordernumratio'] = round($v['ordernum']/$statcount['ordernum'],4)*100;
  77. $v['goodsnumratio'] = round($v['goodsnum']/$statcount['goodsnum'],4)*100;
  78. $v['orderamountratio'] = round($v['orderamount']/$statcount['orderamount'],4)*100;
  79. $statlist_tmp2[$v['goods_type']] = $v;
  80. $stat_arr['ordernum'][] = array('p_name'=>$goodstype_arr[$v['goods_type']],'allnum'=>$v['ordernumratio']);
  81. $stat_arr['goodsnum'][] = array('p_name'=>$goodstype_arr[$v['goods_type']],'allnum'=>$v['goodsnumratio']);
  82. $stat_arr['orderamount'][] = array('p_name'=>$goodstype_arr[$v['goods_type']],'allnum'=>$v['orderamountratio']);
  83. }
  84. foreach ($goodstype_arr as $k=>$v){
  85. if (isset($statlist_tmp2[$k])){
  86. $statlist_tmp2[$k]['goodstype_text'] = $v;
  87. $statlist[] = $statlist_tmp2[$k];
  88. } else {
  89. $statlist[] = array('goodstype_text'=>$k,'goodstype_text'=>$v,'ordernum'=>0,'goodsnum'=>0,'orderamount'=>0.00);
  90. }
  91. }
  92. $stat_json['ordernum'] = getStatData_Pie(array('title'=>lang('statstore_ordernum'),'name'=>lang('statstore_ordernum').'(%)','label_show'=>false,'series'=>$stat_arr['ordernum']));
  93. $stat_json['goodsnum'] = getStatData_Pie(array('title'=>lang('goodsnum'),'name'=>lang('goodsnum').'(%)','label_show'=>false,'series'=>$stat_arr['goodsnum']));
  94. $stat_json['orderamount'] = getStatData_Pie(array('title'=>lang('statstore_orderamount'),'name'=>lang('statstore_orderamount').'(%)','label_show'=>false,'series'=>$stat_arr['orderamount']));
  95. }
  96. View::assign('statcount',$statcount);
  97. View::assign('statlist',$statlist);
  98. View::assign('stat_json',$stat_json);
  99. View::assign('searchtime',implode('|',$searchtime_arr));
  100. $this->setAdminCurItem('promotion');
  101. return View::fetch('marketing_promotion');
  102. }
  103. /**
  104. * 促销销售趋势分析
  105. */
  106. public function promotiontrend(){
  107. //优惠类型数组
  108. $goodstype_arr = array(2=>lang('goodstype_2'),3=>lang('goodstype_3'),4=>lang('goodstype_4'));
  109. $stat_model = model('stat');
  110. $where = array();
  111. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  112. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  113. $searchtime_arr[] = intval($v);
  114. }
  115. $where[]=array('order_isvalid','=',1);//计入统计的有效订单
  116. $where[] = array('order_add_time','between',$searchtime_arr);
  117. $where[]=array('goods_type','in',array(2,3,4));
  118. $field = ' goods_type';
  119. switch ($this->search_arr['stattype']){
  120. case 'orderamount':
  121. $field .= " ,SUM(goods_pay_price) as orderamount";
  122. $caption = lang('statstore_orderamount');
  123. break;
  124. case 'goodsnum':
  125. $field .= " ,SUM(goods_num) as goodsnum";
  126. $caption = lang('goodsnum');
  127. break;
  128. default:
  129. $field .= " ,count(DISTINCT order_id) as ordernum";
  130. $caption = lang('statstore_ordernum');
  131. break;
  132. }
  133. if($this->search_arr['search_type'] == 'day'){
  134. //构造横轴数据
  135. for($i=0; $i<24; $i++){
  136. //横轴
  137. $stat_arr['xAxis']['categories'][] = "$i";
  138. foreach ($goodstype_arr as $k=>$v){
  139. $statlist[$k][$i] = 0;
  140. }
  141. }
  142. $field .= ' ,HOUR(FROM_UNIXTIME(order_add_time)) as timeval ';
  143. }
  144. if($this->search_arr['search_type'] == 'week'){
  145. //构造横轴数据
  146. for($i=1; $i<=7; $i++){
  147. $tmp_weekarr = getSystemWeekArr();
  148. //横轴
  149. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  150. unset($tmp_weekarr);
  151. foreach ($goodstype_arr as $k=>$v){
  152. $statlist[$k][$i] = 0;
  153. }
  154. }
  155. $field .= ' ,WEEKDAY(FROM_UNIXTIME(order_add_time))+1 as timeval ';
  156. }
  157. if($this->search_arr['search_type'] == 'month'){
  158. //计算横轴的最大量(由于每个月的天数不同)
  159. $dayofmonth = date('t',$searchtime_arr[0]);
  160. //构造横轴数据
  161. for($i=1; $i<=$dayofmonth; $i++){
  162. //横轴
  163. $stat_arr['xAxis']['categories'][] = $i;
  164. foreach ($goodstype_arr as $k=>$v){
  165. $statlist[$k][$i] = 0;
  166. }
  167. }
  168. $field .= ' ,day(FROM_UNIXTIME(order_add_time)) as timeval ';
  169. }
  170. //查询数据
  171. $statlist_tmp = $stat_model->statByStatordergoods($where, $field, 0, 0, '','goods_type');
  172. //整理统计数组
  173. if($statlist_tmp){
  174. foreach($statlist_tmp as $k => $v){
  175. //将数据按照不同的促销方式分组
  176. foreach ($goodstype_arr as $t_k=>$t_v){
  177. if ($t_k == $v['goods_type']){
  178. switch ($this->search_arr['stattype']){
  179. case 'orderamount':
  180. $statlist[$t_k][$v['timeval']] = round($v[$this->search_arr['stattype']],2);
  181. break;
  182. case 'goodsnum':
  183. $statlist[$t_k][$v['timeval']] = intval($v[$this->search_arr['stattype']]);
  184. break;
  185. default:
  186. $statlist[$t_k][$v['timeval']] = intval($v[$this->search_arr['stattype']]);
  187. break;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. foreach ($goodstype_arr as $k=>$v){
  194. $tmp = array();
  195. $tmp['name'] = $v;
  196. $tmp['data'] = array_values($statlist[$k]);
  197. $stat_arr['series'][] = $tmp;
  198. }
  199. //得到统计图数据
  200. $stat_arr['title'] = $caption.lang('ds_stat');
  201. $stat_arr['yAxis'] = $caption;
  202. $stat_json = getStatData_LineLabels($stat_arr);
  203. View::assign('stat_json',$stat_json);
  204. View::assign('stattype',input('param.stattype'));
  205. echo View::fetch('stat_linelabels');
  206. }
  207. /**
  208. * 抢购统计
  209. */
  210. public function group(){
  211. if(!isset($this->search_arr['search_type'])){
  212. $this->search_arr['search_type'] = 'day';
  213. }
  214. $stat_model = model('stat');
  215. //获得搜索的开始时间和结束时间
  216. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  217. View::assign('searchtime',implode('|',$searchtime_arr));
  218. $this->setAdminCurItem('group');
  219. return View::fetch('marketing_group');
  220. }
  221. /**
  222. * 抢购统计
  223. */
  224. public function grouplist(){
  225. $groupbuy_model = model('groupbuy');
  226. $where = array();
  227. $where[]=array('groupbuy_is_vr','=',0);//不统计虚拟抢购
  228. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  229. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  230. $searchtime_arr[] = intval($v);
  231. }
  232. $where[] = array('groupbuy_starttime','between',"$searchtime_arr[0],$searchtime_arr[1]");
  233. $where[]=array('groupbuy_state','in',array(10,20,30));
  234. $gname = trim(input('param.gname'));
  235. if ($gname){
  236. $where[]=array('groupbuy_name','like',"%{$gname}%");
  237. }
  238. $grouplist_tmp = $groupbuy_model->getGroupbuyExtendList($where,10,'groupbuy_starttime asc');
  239. $grouplist=array();
  240. if ($grouplist_tmp){
  241. foreach ((array)$grouplist_tmp as $k=>$v){
  242. $v['goodsnum'] = 0;
  243. $v['ordernum'] = 0;
  244. $v['orderrate'] = round(0,2);
  245. $v['goodsamount'] = ds_price_format(0);
  246. $grouplist[$v['groupbuy_id']] = $v;
  247. }
  248. //查询抢购的订单
  249. $where = array();
  250. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  251. $where[]=array('goods_type','=',2);//抢购
  252. $where[]=array('promotions_id','in',array_keys($grouplist));
  253. $field = 'promotions_id,SUM(goods_num) as goodsnum,COUNT(DISTINCT order_id) as ordernum,SUM(goods_pay_price) as goodsamount';
  254. $order_list = model('stat')->statByStatordergoods($where, $field, 0, 0, '', 'promotions_id');
  255. foreach ((array)$order_list as $k=>$v){
  256. $grouplist[$v['promotions_id']]['goodsnum'] = $v['goodsnum'];
  257. $grouplist[$v['promotions_id']]['ordernum'] = $v['ordernum'];
  258. if (intval($grouplist[$v['promotions_id']]['groupbuy_views']) > 0){
  259. $grouplist[$v['promotions_id']]['orderrate'] = round(($v['ordernum']/$grouplist[$v['promotions_id']]['views'])*100,2);
  260. }
  261. $grouplist[$v['promotions_id']]['goodsamount'] = $v['goodsamount'];
  262. }
  263. }
  264. View::assign('grouplist',$grouplist);
  265. View::assign('show_page',$groupbuy_model->page_info->render());
  266. View::assign('searchtime',input('param.t'));
  267. echo View::fetch('marketing_grouplist');
  268. }
  269. /**
  270. * 抢购商品统计
  271. */
  272. public function groupgoods(){
  273. $stat_model = model('stat');
  274. $where = array();
  275. $statlist= array();
  276. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  277. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  278. $searchtime_arr[] = intval($v);
  279. }
  280. $where[] = array('order_add_time','between',$searchtime_arr);
  281. $where[] = array('goods_type','=',2);//抢购
  282. $field = " goods_id,goods_name";
  283. $field .= " ,SUM(goods_num) as goodsnum";
  284. $field .= " ,SUM(goods_pay_price) as goodsamount";
  285. $field .= " ,SUM(IF(order_state='".ORDER_STATE_CANCEL."',goods_num,0)) as cancelgoodsnum";
  286. $field .= " ,SUM(IF(order_state='".ORDER_STATE_CANCEL."',goods_pay_price,0)) as cancelgoodsamount";
  287. $field .= " ,SUM(IF(order_state<>'".ORDER_STATE_CANCEL."' and order_state<>'".ORDER_STATE_NEW."',goods_num,0)) as finishgoodsnum";
  288. $field .= " ,SUM(IF(order_state<>'".ORDER_STATE_CANCEL."' and order_state<>'".ORDER_STATE_NEW."',goods_pay_price,0)) as finishgoodsamount";
  289. $orderby_arr = array('goodsnum asc','goodsnum desc','goodsamount asc','goodsamount desc','cancelgoodsnum asc','cancelgoodsnum desc','cancelgoodsamount asc','cancelgoodsamount desc','finishgoodsnum asc','finishgoodsnum desc','finishgoodsamount asc','finishgoodsamount desc');
  290. if (!in_array(trim(@$this->search_arr['orderby']),$orderby_arr)){
  291. $this->search_arr['orderby'] = 'goodsnum desc';
  292. }
  293. $orderby = trim($this->search_arr['orderby']).',goods_id desc';
  294. //统计记录总条数
  295. $count_arr = $stat_model->getoneByStatordergoods($where, 'count(DISTINCT goods_id) as countnum');
  296. $countnum = intval($count_arr['countnum']);
  297. if (@$this->search_arr['exporttype'] == 'excel'){
  298. $statlist_tmp = Db::query('SELECT '.$field.' FROM '.config('database.connections.mysql.prefix').'statordergoods WHERE goods_type=2 AND order_add_time BETWEEN '.$searchtime_arr[0].' AND '.$searchtime_arr[1].' GROUP BY goods_id'.' ORDER BY '.$orderby);
  299. } else {
  300. $statlist_tmp = Db::query('SELECT '.$field.' FROM '.config('database.connections.mysql.prefix').'statordergoods WHERE goods_type=2 AND order_add_time BETWEEN '.$searchtime_arr[0].' AND '.$searchtime_arr[1].' GROUP BY goods_id'.' ORDER BY '.$orderby.' LIMIT '.(input('param.page')?input('param.page'):0).',10');
  301. }
  302. $statheader = array();
  303. $statheader[] = array('text'=>lang('ds_goods_name'),'key'=>'goods_name','class'=>'alignleft');
  304. $statheader[] = array('text'=>lang('goodsnum'),'key'=>'goodsnum','isorder'=>1);
  305. $statheader[] = array('text'=>lang('statstore_orderamount'),'key'=>'goodsamount','isorder'=>1);
  306. $statheader[] = array('text'=>lang('cancel_goods_number'),'key'=>'cancelgoodsnum','isorder'=>1);
  307. $statheader[] = array('text'=>lang('cancel_amount'),'key'=>'cancelgoodsamount','isorder'=>1);
  308. $statheader[] = array('text'=>lang('finish_goods_number'),'key'=>'finishgoodsnum','isorder'=>1);
  309. $statheader[] = array('text'=>lang('finish_amount'),'key'=>'finishgoodsamount','isorder'=>1);
  310. foreach ((array)$statlist_tmp as $k=>$v){
  311. $tmp = $v;
  312. foreach ($statheader as $h_k=>$h_v){
  313. $tmp[$h_v['key']] = $v[$h_v['key']];
  314. if ($h_v['key'] == 'goods_name'){
  315. $tmp[$h_v['key']] = '<a href="'.(string)url('home/Goods/index', array('goods_id' => $v['goods_id'])).'" target="_blank">'.$v['goods_name'].'</a>';
  316. }
  317. }
  318. $statlist[] = $tmp;
  319. }
  320. if (@$this->search_arr['exporttype'] == 'excel'){
  321. //导出Excel
  322. $excel_obj = new \excel\Excel();
  323. $excel_data = array();
  324. //设置样式
  325. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  326. //header
  327. foreach ($statheader as $k=>$v){
  328. $excel_data[0][] = array('styleid'=>'s_title','data'=>$v['text']);
  329. }
  330. //data
  331. foreach ((array)$statlist as $k=>$v){
  332. foreach ($statheader as $h_k=>$h_v){
  333. $excel_data[$k+1][] = array('data'=>$v[$h_v['key']]);
  334. }
  335. }
  336. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  337. $excel_obj->addArray($excel_data);
  338. $excel_obj->addWorksheet($excel_obj->charset(lang('groupbuy_goods_statis'),CHARSET));
  339. $excel_obj->generateXML($excel_obj->charset(lang('groupbuy_goods_statis'),CHARSET).date('Y-m-d-H',TIMESTAMP));
  340. exit();
  341. } else {
  342. View::assign('statheader',$statheader);
  343. View::assign('statlist',$statlist);
  344. View::assign('show_page',Db::name('statordergoods')->paginate(10,$countnum)->render());
  345. View::assign('searchtime',input('param.t'));
  346. View::assign('orderby',$this->search_arr['orderby']);
  347. View::assign('actionurl',(string)url('Statmarketing/groupgoods',['t'=>$this->search_arr['t']]));
  348. echo View::fetch('stat_listandorder');
  349. }
  350. }
  351. protected function getAdminItemList()
  352. {
  353. $menu_array=array(
  354. array('name'=>'promotion','text'=>lang('stat_promotion'),'url'=>(string)url('Statmarketing/promotion')),
  355. array('name'=>'group','text'=>lang('stat_group'),'url'=>(string)url('Statmarketing/group'))
  356. );
  357. return $menu_array;
  358. }
  359. }