Statmarketing.php 18 KB

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