Statisticsgoods.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. /**
  3. * 商品分析
  4. */
  5. namespace app\home\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 Statisticsgoods extends BaseSeller {
  22. private $search_arr; //处理后的参数
  23. private $gc_arr; //分类数组
  24. private $choose_gcid; //选择的分类ID
  25. public function initialize() {
  26. parent::initialize(); // TODO: Change the autogenerated stub
  27. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/statisticsgoods.lang.php');
  28. include_once root_path() . 'extend/mall/statistics.php';
  29. include_once root_path() . 'extend/mall/datehelper.php';
  30. $stat_model = model('stat');
  31. //存储参数
  32. $this->search_arr = $_REQUEST;
  33. //处理搜索时间
  34. if (in_array(request()->action(), array('price', 'hotgoods'))) {
  35. $this->search_arr = $stat_model->dealwithSearchTime($this->search_arr);
  36. //获得系统年份
  37. $year_arr = getSystemYearArr();
  38. //获得系统月份
  39. $month_arr = getSystemMonthArr();
  40. //获得本月的周时间段
  41. $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
  42. View::assign('year_arr', $year_arr);
  43. View::assign('month_arr', $month_arr);
  44. View::assign('week_arr', $week_arr);
  45. }
  46. View::assign('search_arr', $this->search_arr);
  47. /**
  48. * 处理商品分类
  49. */
  50. $this->choose_gcid = ($t = intval(input('param.choose_gcid'))) > 0 ? $t : 0;
  51. $gccache_arr = model('goodsclass')->getGoodsclassCache($this->choose_gcid, 3);
  52. $this->gc_arr = $gccache_arr['showclass'];
  53. View::assign('gc_json', json_encode($gccache_arr['showclass']));
  54. View::assign('gc_choose_json', json_encode($gccache_arr['choose_gcid']));
  55. }
  56. /**
  57. * 商品列表
  58. */
  59. public function index() {
  60. $stat_model = model('stat');
  61. //统计的日期0点
  62. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  63. /*
  64. * 近30天
  65. */
  66. $stime = $stat_time - (86400 * 29); //30天前
  67. $etime = $stat_time + 86400 - 1; //昨天23:59
  68. //查询订单商品表下单商品数
  69. $where = array();
  70. $where[] = array('order_isvalid', '=', 1); //计入统计的有效订单
  71. $where[] = array('store_id', '=', session('store_id'));
  72. $where[] = array('order_add_time', 'between', array($stime, $etime));
  73. if ($this->choose_gcid > 0) {
  74. $gc_depth = $this->gc_arr[$this->choose_gcid]['depth'];
  75. $where[] = array('gc_parentid_' . $gc_depth, '=', $this->choose_gcid);
  76. }
  77. if (trim(input('param.search_gname'))) {
  78. $where[] = array('goods_name', 'like', "%" . trim(input('param.search_gname')) . "%");
  79. }
  80. //查询总条数
  81. $count_arr = $stat_model->statByStatordergoods($where, 'count(DISTINCT goods_id) as countnum');
  82. $countnum = intval($count_arr[0]['countnum']);
  83. $field = ' goods_id,goods_name,goods_image,goods_price,SUM(goods_num) as ordergoodsnum,SUM(goods_pay_price) as ordergamount ';
  84. //排序
  85. $orderby_arr = array('ordergoodsnum asc', 'ordergoodsnum desc', 'ordergamount asc', 'ordergamount desc');
  86. if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']), $orderby_arr)) {
  87. $this->search_arr['orderby'] = 'ordergoodsnum desc';
  88. }
  89. $orderby = trim($this->search_arr['orderby']) . ',goods_id';
  90. $stat_ordergoods = $stat_model->statByStatordergoods($where, $field, 5, 0, $this->search_arr['orderby'], 'goods_id');
  91. View::assign('goodslist', $stat_ordergoods);
  92. View::assign('show_page', $stat_model->page_info->render());
  93. View::assign('orderby', $this->search_arr['orderby']);
  94. $this->setSellerCurMenu('Statisticsgoods');
  95. $this->setSellerCurItem('goodslist');
  96. return View::fetch($this->template_dir . 'goodslist');
  97. }
  98. /**
  99. * 商品详细
  100. */
  101. public function goodsinfo() {
  102. $templatesname = 'goodsinfo';
  103. $goods_id = intval(input('param.gid'));
  104. if ($goods_id <= 0) {
  105. View::assign('stat_msg', lang('param_error'));
  106. echo View::fetch($this->template_dir . $templatesname);
  107. exit;
  108. }
  109. //查询商品信息
  110. $goods_info = model('goods')->getGoodsInfoByID($goods_id);
  111. if (!$goods_info) {
  112. View::assign('stat_msg', lang('param_error'));
  113. echo View::fetch($this->template_dir . $templatesname);
  114. exit;
  115. }
  116. $stat_model = model('stat');
  117. //统计的日期0点
  118. $stat_time = strtotime(date('Y-m-d', TIMESTAMP)) - 86400;
  119. /*
  120. * 近30天
  121. */
  122. $stime = $stat_time - (86400 * 29); //30天前
  123. $etime = $stat_time + 86400 - 1; //昨天23:59
  124. $stat_arr = array();
  125. for ($i = $stime; $i < $etime; $i += 86400) {
  126. //当前数据的时间
  127. $timetext = date('n', $i) . '-' . date('j', $i);
  128. //统计图数据
  129. $stat_list['ordergoodsnum'][$timetext] = 0;
  130. $stat_list['ordergamount'][$timetext] = 0;
  131. $stat_list['ordernum'][$timetext] = 0;
  132. //横轴
  133. $stat_arr['ordergoodsnum']['xAxis']['categories'][] = $timetext;
  134. $stat_arr['ordergamount']['xAxis']['categories'][] = $timetext;
  135. $stat_arr['ordernum']['xAxis']['categories'][] = $timetext;
  136. }
  137. //查询订单商品表下单商品数
  138. $where = array();
  139. $where[] = array('goods_id','=',$goods_id);
  140. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  141. $where[] = array('store_id','=',session('store_id'));
  142. $where[] = array('order_add_time','between',array($stime, $etime));
  143. $field = ' goods_id,goods_name,COUNT(DISTINCT order_id) as ordernum,SUM(goods_num) as ordergoodsnum,SUM(goods_pay_price) as ordergamount,MONTH(FROM_UNIXTIME(order_add_time)) as monthval,DAY(FROM_UNIXTIME(order_add_time)) as dayval ';
  144. $stat_ordergoods = $stat_model->statByStatordergoods($where, $field, 0, 0, '', '');
  145. $stat_count = array();
  146. if ($stat_ordergoods) {
  147. foreach ($stat_ordergoods as $k => $v) {
  148. $stat_list['ordergoodsnum'][$v['monthval'] . '-' . $v['dayval']] = intval($v['ordergoodsnum']);
  149. $stat_list['ordergamount'][$v['monthval'] . '-' . $v['dayval']] = floatval($v['ordergamount']);
  150. $stat_list['ordernum'][$v['monthval'] . '-' . $v['dayval']] = intval($v['ordernum']);
  151. if (!isset($stat_count['ordergoodsnum'])) {
  152. $stat_count['ordergoodsnum'] = 0;
  153. }
  154. if (!isset($stat_count['ordergamount'])) {
  155. $stat_count['ordergamount'] = 0;
  156. }
  157. if (!isset($stat_count['ordernum'])) {
  158. $stat_count['ordernum'] = 0;
  159. }
  160. $stat_count['ordergoodsnum'] = intval($stat_count['ordergoodsnum']) + $v['ordergoodsnum'];
  161. $stat_count['ordergamount'] = floatval($stat_count['ordergamount']) + floatval($v['ordergamount']);
  162. $stat_count['ordernum'] = intval($stat_count['ordernum']) + $v['ordernum'];
  163. }
  164. }
  165. $stat_count['ordergamount'] = ds_price_format($stat_count['ordergamount']);
  166. $stat_arr['ordergoodsnum']['legend']['enabled'] = false;
  167. $stat_arr['ordergoodsnum']['series'][0]['name'] = lang('order_quantity');
  168. $stat_arr['ordergoodsnum']['series'][0]['data'] = array_values($stat_list['ordergoodsnum']);
  169. $stat_arr['ordergoodsnum']['title'] = lang('recent_single_commodity_trend');
  170. $stat_arr['ordergoodsnum']['yAxis'] = lang('place_order_amount');
  171. $stat_json['ordergoodsnum'] = getStatData_LineLabels($stat_arr['ordergoodsnum']);
  172. $stat_arr['ordergamount']['legend']['enabled'] = false;
  173. $stat_arr['ordergamount']['series'][0]['name'] = lang('place_order_amount');
  174. $stat_arr['ordergamount']['series'][0]['data'] = array_values($stat_list['ordergamount']);
  175. $stat_arr['ordergamount']['title'] = lang('recent_order_amount_trend');
  176. $stat_arr['ordergamount']['yAxis'] = lang('place_order_amount');
  177. $stat_json['ordergamount'] = getStatData_LineLabels($stat_arr['ordergamount']);
  178. $stat_arr['ordernum']['legend']['enabled'] = false;
  179. $stat_arr['ordernum']['series'][0]['name'] = lang('order_quantity');
  180. $stat_arr['ordernum']['series'][0]['data'] = array_values($stat_list['ordernum']);
  181. $stat_arr['ordernum']['title'] = lang('recent_orders_trend');
  182. $stat_arr['ordernum']['yAxis'] = lang('place_order_amount');
  183. $stat_json['ordernum'] = getStatData_LineLabels($stat_arr['ordernum']);
  184. View::assign('stat_json', $stat_json);
  185. View::assign('stat_count', $stat_count);
  186. View::assign('goods_info', $goods_info);
  187. echo View::fetch($this->template_dir . $templatesname);
  188. }
  189. /**
  190. * 价格销量统计
  191. */
  192. public function price() {
  193. if (!isset($this->search_arr['search_type'])) {
  194. $this->search_arr['search_type'] = 'day';
  195. }
  196. $stat_model = model('stat');
  197. //获得搜索的开始时间和结束时间
  198. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  199. $where = array();
  200. $where[] = array('store_id','=',session('store_id'));
  201. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  202. $where[] = array('order_add_time','between',$searchtime_arr);
  203. //商品分类
  204. if ($this->choose_gcid > 0) {
  205. //获得分类深度
  206. $depth = $this->gc_arr[$this->choose_gcid]['depth'];
  207. $where[] = array('gc_parentid_' . $depth,'=',$this->choose_gcid);
  208. }
  209. $field = '1';
  210. $pricerange = ds_getvalue_byname('storeextend', 'store_id', session('store_id'), 'pricerange');
  211. $pricerange_arr = $pricerange ? unserialize($pricerange) : array();
  212. if ($pricerange_arr) {
  213. $stat_arr['series'][0]['name'] = lang('order_quantity');
  214. //设置价格区间最后一项,最后一项只有开始值没有结束值
  215. $pricerange_count = count($pricerange_arr);
  216. if ($pricerange_arr[$pricerange_count - 1]['e']) {
  217. $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count - 1]['e'] + 1;
  218. $pricerange_arr[$pricerange_count]['e'] = '';
  219. }
  220. foreach ((array) $pricerange_arr as $k => $v) {
  221. $v['s'] = intval($v['s']);
  222. $v['e'] = intval($v['e']);
  223. //构造查询字段
  224. if ($v['e']) {
  225. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']} and goods_pay_price/goods_num <= {$v['e']},goods_num,0)) as goodsnum_{$k}";
  226. } else {
  227. $field .= " ,SUM(IF(goods_pay_price/goods_num > {$v['s']},goods_num,0)) as goodsnum_{$k}";
  228. }
  229. }
  230. $ordergooods_list = Db::query('SELECT ' . $field . ' FROM ' . config('database.connections.mysql.prefix') . 'statordergoods WHERE store_id=' . session('store_id') . ' AND 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) : ''));
  231. if ($ordergooods_list) {
  232. $ordergooods_list = current($ordergooods_list);
  233. foreach ((array) $pricerange_arr as $k => $v) {
  234. //横轴
  235. if ($v['e']) {
  236. $stat_arr['xAxis']['categories'][] = $v['s'] . '-' . $v['e'];
  237. } else {
  238. $stat_arr['xAxis']['categories'][] = $v['s'] . lang('above');
  239. }
  240. //统计图数据
  241. if (isset($ordergooods_list['goodsnum_' . $k])) {
  242. $stat_arr['series'][0]['data'][] = intval($ordergooods_list['goodsnum_' . $k]);
  243. } else {
  244. $stat_arr['series'][0]['data'][] = 0;
  245. }
  246. }
  247. }
  248. //得到统计图数据
  249. $stat_arr['title'] = lang('price_distribution');
  250. $stat_arr['legend']['enabled'] = false;
  251. $stat_arr['yAxis'] = lang('sales');
  252. $pricerange_statjson = getStatData_LineLabels($stat_arr);
  253. } else {
  254. $pricerange_statjson = '';
  255. }
  256. View::assign('statjson', $pricerange_statjson);
  257. $this->setSellerCurMenu('Statisticsgoods');
  258. $this->setSellerCurItem('price');
  259. return View::fetch($this->template_dir . 'goods_price');
  260. }
  261. /**
  262. * 热卖商品
  263. */
  264. public function hotgoods() {
  265. $topnum = 30;
  266. if (!isset($this->search_arr['search_type'])) {
  267. $this->search_arr['search_type'] = 'day';
  268. }
  269. $stat_model = model('stat');
  270. //获得搜索的开始时间和结束时间
  271. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  272. $stat_model = model('stat');
  273. $where = array();
  274. $where[] = array('store_id','=',session('store_id'));
  275. $where[] = array('order_isvalid','=',1); //计入统计的有效订单
  276. $where[] = array('order_add_time','between',$searchtime_arr);
  277. //查询销量top
  278. //构造横轴数据
  279. for ($i = 1; $i <= $topnum; $i++) {
  280. //数据
  281. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  282. //横轴
  283. $stat_arr['xAxis']['categories'][] = "$i";
  284. }
  285. $field = ' goods_id,goods_name,SUM(goods_num) as goodsnum ';
  286. $orderby = 'goodsnum desc,goods_id';
  287. $statlist = array();
  288. $statlist['goodsnum'] = $stat_model->statByStatordergoods($where, $field, 0, $topnum, $orderby, 'goods_id');
  289. foreach ((array) $statlist['goodsnum'] as $k => $v) {
  290. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => intval($v['goodsnum']));
  291. }
  292. $stat_arr['series'][0]['name'] = lang('order_quantity');
  293. $stat_arr['legend']['enabled'] = false;
  294. //得到统计图数据
  295. $stat_arr['title'] = lang('hot_commodity_top') . $topnum;
  296. $stat_arr['yAxis'] = lang('order_quantity');
  297. $stat_json['goodsnum'] = getStatData_Column2D($stat_arr);
  298. unset($stat_arr);
  299. //查询下单金额top
  300. //构造横轴数据
  301. for ($i = 1; $i <= $topnum; $i++) {
  302. //数据
  303. $stat_arr['series'][0]['data'][] = array('name' => '', 'y' => 0);
  304. //横轴
  305. $stat_arr['xAxis']['categories'][] = "$i";
  306. }
  307. $field = ' goods_id,goods_name,SUM(goods_pay_price) as orderamount ';
  308. $orderby = 'orderamount desc,goods_id';
  309. $statlist['orderamount'] = $stat_model->statByStatordergoods($where, $field, 0, $topnum, $orderby, 'goods_id');
  310. foreach ((array) $statlist['orderamount'] as $k => $v) {
  311. $stat_arr['series'][0]['data'][$k] = array('name' => strval($v['goods_name']), 'y' => floatval($v['orderamount']));
  312. }
  313. $stat_arr['series'][0]['name'] = lang('place_order_amount');
  314. $stat_arr['legend']['enabled'] = false;
  315. //得到统计图数据
  316. $stat_arr['title'] = lang('hot_commodity_top') . $topnum;
  317. $stat_arr['yAxis'] = lang('place_order_amount');
  318. $stat_json['orderamount'] = getStatData_Column2D($stat_arr);
  319. View::assign('stat_json', $stat_json);
  320. View::assign('statlist', $statlist);
  321. $this->setSellerCurItem('hotgoods');
  322. $this->setSellerCurMenu('Statisticsgoods');
  323. return View::fetch($this->template_dir . 'hotgoods');
  324. }
  325. /**
  326. * 用户中心右边,小导航
  327. *
  328. * @param string $menu_type 导航类型
  329. * @param string $name 当前导航的name
  330. * @return
  331. */
  332. protected function getSellerItemList() {
  333. $menu_array = array(
  334. array('name' => 'goodslist', 'text' => lang('goods_details'), 'url' => (string) url('Statisticsgoods/index')),
  335. array('name' => 'price', 'text' => lang('sales_price'), 'url' => (string) url('Statisticsgoods/price')),
  336. array('name' => 'hotgoods', 'text' => lang('selling_goods'), 'url' => (string) url('Statisticsgoods/hotgoods')),
  337. );
  338. return $menu_array;
  339. }
  340. }