Statstore.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <?php
  2. /**
  3. * 店铺统计
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use think\facade\Db;
  8. use excel\Excel;
  9. use think\facade\Lang;
  10. /**
  11. * ============================================================================
  12. * DSMall多用户商城
  13. * ============================================================================
  14. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  15. * 网站地址: http://www.csdeshang.com
  16. * ----------------------------------------------------------------------------
  17. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  18. * 不允许对程序代码以任何形式任何目的的再发布。
  19. * ============================================================================
  20. * 控制器
  21. */
  22. class Statstore extends AdminControl
  23. {
  24. private $search_arr;//处理后的参数
  25. private $store_class;//店铺分类
  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('hotrank','storesales'))){
  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. $this->store_class = rkcache('storeclass', true);
  51. View::assign('store_class', $this->store_class);
  52. }
  53. /**
  54. * 新增店铺
  55. */
  56. public function newstore(){
  57. //导出excel连接地址
  58. $actionurl = (string)url('Statstore/newstore');
  59. $where = array();
  60. //所属店铺分类
  61. $search_sclass = intval(input('param.search_sclass'));
  62. if ($search_sclass > 0){
  63. $where[] = array('storeclass_id','=',$search_sclass);;
  64. $actionurl .= "&search_sclass=$search_sclass";
  65. }
  66. $field = ' count(*) as allnum ';
  67. if(!input('param.search_type')){
  68. $search_type = 'day';
  69. }
  70. else{
  71. $search_type = input('param.search_type');
  72. }
  73. //初始化时间
  74. //天
  75. if(!input('param.search_time')) {
  76. $search_times = date('Y-m-d', TIMESTAMP - 86400);
  77. }else {
  78. $search_times = input('param.search_time');
  79. }
  80. $search_time = strtotime($search_times);//搜索的时间
  81. View::assign('search_time',$search_times);
  82. //周
  83. if(!input('param.search_time_year')) {
  84. $search_time_year = date('Y', TIMESTAMP);
  85. }else {
  86. $search_time_year = input('param.search_time_year');
  87. }
  88. if(!input('param.search_time_month')) {
  89. $search_time_month = date('m', TIMESTAMP);
  90. }else {
  91. $search_time_month = input('param.search_time_month');
  92. }
  93. if(!input('param.search_time_week')) {
  94. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  95. }else {
  96. $search_time_week = input('param.search_time_week');
  97. }
  98. $current_year =$search_time_year;
  99. $current_month =$search_time_month;
  100. $current_week = $search_time_week;
  101. $year_arr = getSystemYearArr();
  102. $month_arr = getSystemMonthArr();
  103. $week_arr = getMonthWeekArr($current_year, $current_month);
  104. View::assign('current_year', $current_year);
  105. View::assign('current_month', $current_month);
  106. View::assign('current_week', $current_week);
  107. View::assign('year_arr', $year_arr);
  108. View::assign('month_arr', $month_arr);
  109. View::assign('week_arr', $week_arr);
  110. $stat_model = model('stat');
  111. $statlist = array();//统计数据列表
  112. if($search_type == 'day'){
  113. //构造横轴数据
  114. for($i=0; $i<24; $i++){
  115. //统计图数据
  116. $curr_arr[$i] = 0;//今天
  117. $up_arr[$i] = 0;//昨天
  118. //统计表数据
  119. $uplist_arr[$i]['timetext'] = $i;
  120. $currlist_arr[$i]['timetext'] = $i;
  121. //方便搜索会员列表,计算开始时间和结束时间
  122. $currlist_arr[$i]['stime'] = $search_time+$i*3600;
  123. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime']+3600;
  124. $uplist_arr[$i]['val'] = 0;
  125. $currlist_arr[$i]['val'] = 0;
  126. //横轴
  127. $stat_arr['xAxis']['categories'][] = "$i";
  128. }
  129. $stime = $search_time - 86400;//昨天0点
  130. $etime = $search_time + 86400 - 1;//今天24点
  131. $today_day = @date('d', $search_time);//今天日期
  132. $yesterday_day = @date('d', $stime);//昨天日期
  133. $where[] = array('store_addtime','between',array($stime,$etime));
  134. $field .= ' ,DAY(FROM_UNIXTIME(store_addtime)) as dayval,HOUR(FROM_UNIXTIME(store_addtime)) as hourval ';
  135. $memberlist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'dayval,dayval');
  136. if($memberlist){
  137. foreach($memberlist as $k => $v){
  138. if($today_day == $v['dayval']){
  139. $curr_arr[$v['hourval']] = intval($v['allnum']);
  140. $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
  141. }
  142. if($yesterday_day == $v['dayval']){
  143. $up_arr[$v['hourval']] = intval($v['allnum']);
  144. $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
  145. }
  146. }
  147. }
  148. $stat_arr['series'][0]['name'] = lang('yestoday');
  149. $stat_arr['series'][0]['data'] = array_values($up_arr);
  150. $stat_arr['series'][1]['name'] = lang('today');
  151. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  152. //统计数据标题
  153. $statlist['headertitle'] = array(lang('hour'),lang('yestoday'),lang('today'),lang('compare_to'));
  154. View::assign('actionurl',$actionurl.'&search_type=day&search_time='.date('Y-m-d',$search_time));
  155. }
  156. if($search_type == 'week'){
  157. $current_weekarr = explode('|', $current_week);
  158. $stime = strtotime($current_weekarr[0])-86400*7;
  159. $etime = strtotime($current_weekarr[1])+86400-1;
  160. $up_week = @date('W', $stime);//上周
  161. $curr_week = @date('W', $etime);//本周
  162. //构造横轴数据
  163. for($i=1; $i<=7; $i++){
  164. //统计图数据
  165. $up_arr[$i] = 0;
  166. $curr_arr[$i] = 0;
  167. $tmp_weekarr = getSystemWeekArr();
  168. //统计表数据
  169. $uplist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  170. $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
  171. //方便搜索会员列表,计算开始时间和结束时间
  172. $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0])+($i-1)*86400;
  173. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime']+86400 - 1;
  174. $uplist_arr[$i]['val'] = 0;
  175. $currlist_arr[$i]['val'] = 0;
  176. //横轴
  177. $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
  178. unset($tmp_weekarr);
  179. }
  180. $where[] = array('store_addtime','between',array($stime,$etime));
  181. $field .= ',WEEKOFYEAR(FROM_UNIXTIME(store_addtime)) as weekval,WEEKDAY(FROM_UNIXTIME(store_addtime))+1 as dayofweekval ';
  182. $memberlist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'weekval,dayofweekval');
  183. if($memberlist){
  184. foreach($memberlist as $k=>$v){
  185. if ($up_week == $v['weekval']){
  186. $up_arr[$v['dayofweekval']] = intval($v['allnum']);
  187. $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
  188. }
  189. if ($curr_week == $v['weekval']){
  190. $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
  191. $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
  192. }
  193. }
  194. }
  195. $stat_arr['series'][0]['name'] = lang('last_week');
  196. $stat_arr['series'][0]['data'] = array_values($up_arr);
  197. $stat_arr['series'][1]['name'] = lang('this_week');
  198. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  199. //统计数据标题
  200. $statlist['headertitle'] = array(lang('week'),lang('last_week'),lang('this_week'),lang('compare_to'));
  201. View::assign('actionurl',$actionurl.'&search_type=week&search_time_year='.$current_year.'&search_time_month='.$current_month.'&search_time_week='.$current_week);
  202. }
  203. if($search_type == 'month'){
  204. $stime = strtotime($current_year.'-'.$current_month."-01 -1 month");
  205. $etime = getMonthLastDay($current_year,$current_month)+86400-1;
  206. $up_month = date('m',$stime);
  207. $curr_month = date('m',$etime);
  208. //计算横轴的最大量(由于每个月的天数不同)
  209. $up_dayofmonth = date('t',$stime);
  210. $curr_dayofmonth = date('t',$etime);
  211. $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;
  212. //构造横轴数据
  213. for($i=1; $i<=$x_max; $i++){
  214. //统计图数据
  215. $up_arr[$i] = 0;
  216. $curr_arr[$i] = 0;
  217. //统计表数据
  218. $uplist_arr[$i]['timetext'] = $i;
  219. $currlist_arr[$i]['timetext'] = $i;
  220. //方便搜索会员列表,计算开始时间和结束时间
  221. $currlist_arr[$i]['stime'] = strtotime($current_year.'-'.$current_month."-01")+($i-1)*86400;
  222. $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime']+86400 - 1;
  223. $uplist_arr[$i]['val'] = 0;
  224. $currlist_arr[$i]['val'] = 0;
  225. //横轴
  226. $stat_arr['xAxis']['categories'][] = $i;
  227. }
  228. $where[] = array('store_addtime','between',array($stime,$etime));
  229. $field .= ',MONTH(FROM_UNIXTIME(store_addtime)) as monthval,day(FROM_UNIXTIME(store_addtime)) as dayval ';
  230. $memberlist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0,'monthval,dayval');
  231. if($memberlist){
  232. foreach($memberlist as $k=>$v){
  233. if ($up_month == $v['monthval']){
  234. $up_arr[$v['dayval']] = intval($v['allnum']);
  235. $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
  236. }
  237. if ($curr_month == $v['monthval']){
  238. $curr_arr[$v['dayval']] = intval($v['allnum']);
  239. $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
  240. }
  241. }
  242. }
  243. $stat_arr['series'][0]['name'] = lang('last_month');
  244. $stat_arr['series'][0]['data'] = array_values($up_arr);
  245. $stat_arr['series'][1]['name'] = lang('this_month');
  246. $stat_arr['series'][1]['data'] = array_values($curr_arr);
  247. //统计数据标题
  248. $statlist['headertitle'] = array(lang('date'),lang('last_month'),lang('this_month'),lang('compare_to'));
  249. View::assign('actionurl',$actionurl.'&search_type=month&search_time_year='.$current_year.'&search_time_month='.$current_month);
  250. }
  251. //计算同比
  252. foreach ((array)$currlist_arr as $k=>$v){
  253. $tmp = array();
  254. $tmp['seartime'] = $v['stime'].'|'.$v['etime'];
  255. $tmp['timetext'] = $v['timetext'];
  256. $tmp['currentdata'] = $v['val'];
  257. $tmp['updata'] = $uplist_arr[$k]['val'];
  258. $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
  259. $statlist['data'][] = $tmp;
  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. foreach ($statlist['headertitle'] as $v){
  270. $excel_data[0][] = array('styleid'=>'s_title','data'=>$v);
  271. }
  272. //data
  273. foreach ($statlist['data'] as $k=>$v){
  274. $excel_data[$k+1][] = array('data'=>$v['timetext']);
  275. $excel_data[$k+1][] = array('format'=>'Number','data'=>$v['updata']);
  276. $excel_data[$k+1][] = array('format'=>'Number','data'=>$v['currentdata']);
  277. $excel_data[$k+1][] = array('data'=>$v['tbrate']);
  278. }
  279. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  280. $excel_obj->addArray($excel_data);
  281. $excel_obj->addWorksheet($excel_obj->charset(lang('add_store_statis'),CHARSET));
  282. $excel_obj->generateXML($excel_obj->charset(lang('add_store_statis'),CHARSET).date('Y-m-d-H',TIMESTAMP));
  283. exit();
  284. } else {
  285. //得到统计图数据
  286. $stat_arr['title'] = lang('add_store_statis');
  287. $stat_arr['yAxis'] = lang('add_store_number');
  288. $stat_json = getStatData_LineLabels($stat_arr);
  289. View::assign('stat_json',$stat_json);
  290. View::assign('statlist',$statlist);
  291. $this->setAdminCurItem('newstore');
  292. return View::fetch('stat_newstore');
  293. }
  294. }
  295. /**
  296. * 热卖排行
  297. */
  298. public function hotrank(){
  299. if(!isset($this->search_arr['search_type'])){
  300. $this->search_arr['search_type'] = 'day';
  301. }
  302. $stat_model = model('stat');
  303. //获得搜索的开始时间和结束时间
  304. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  305. View::assign('searchtime',implode('|',$searchtime_arr));
  306. //周
  307. if(!input('param.search_time_year')) {
  308. $search_time_year = date('Y', TIMESTAMP);
  309. }else {
  310. $search_time_year = input('param.search_time_year');
  311. }
  312. if(!input('param.search_time_month')) {
  313. $search_time_month = date('m', TIMESTAMP);
  314. }else {
  315. $search_time_month = input('param.search_time_month');
  316. }
  317. if(!input('param.search_time_week')) {
  318. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  319. }else {
  320. $search_time_week = input('param.search_time_week');
  321. }
  322. $current_year =$search_time_year;
  323. $current_month =$search_time_month;
  324. $current_week = $search_time_week;
  325. View::assign('current_year', $current_year);
  326. View::assign('current_month', $current_month);
  327. View::assign('current_week', $current_week);
  328. $this->setAdminCurItem('hotrank');
  329. return View::fetch('store_hotrank');
  330. }
  331. /**
  332. * 热卖排行列表
  333. */
  334. public function hotrank_list(){
  335. $datanum = 30;
  336. $stat_model = model('stat');
  337. $type = input('param.type');
  338. switch ($type){
  339. case 'ordernum':
  340. $sort_text = lang('statstore_ordernum');
  341. break;
  342. default:
  343. $type = 'orderamount';
  344. $sort_text = lang('statstore_orderamount');
  345. break;
  346. }
  347. $where = array();
  348. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  349. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  350. $searchtime_arr[] = intval($v);
  351. }
  352. //店铺分类
  353. $search_sclass = intval(input('param.search_sclass'));
  354. if ($search_sclass){
  355. $where[] = array('storeclass_id','=',$search_sclass);
  356. }
  357. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  358. $where[] = array('order_add_time','between',$searchtime_arr);
  359. //查询统计数据
  360. $field = ' store_id,store_name ';
  361. switch ($type){
  362. case 'ordernum':
  363. $field .= ' ,COUNT(*) as ordernum ';
  364. $orderby = 'ordernum desc';
  365. break;
  366. default:
  367. $type = 'orderamount';
  368. $field .= ' ,SUM(order_amount) as orderamount ';
  369. $orderby = 'orderamount desc';
  370. break;
  371. }
  372. $orderby .= ',store_id';
  373. $statlist = $stat_model->statByStatorder($where, $field, 0, $datanum, $orderby, 'store_id');
  374. foreach ((array)$statlist as $k=>$v){
  375. $statlist[$k]['sort'] = $k+1;
  376. }
  377. /**
  378. * 飙升榜
  379. */
  380. $soaring_statlist = array();//飙升榜数组
  381. //查询期间产生订单的店铺数
  382. $where = array();
  383. //店铺分类
  384. $search_sclass = intval(input('param.search_sclass'));
  385. if ($search_sclass){
  386. $where[] = array('storeclass_id','=',$search_sclass);
  387. }
  388. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  389. $where[] = array('order_add_time','between',$searchtime_arr);
  390. $field = 'COUNT(*) as countnum';
  391. $countnum = $stat_model->getoneByStatorder($where, $field);
  392. $countnum = $countnum['countnum'];
  393. if ($countnum > 0){
  394. $store_arr = array();
  395. $field = 'store_id,store_name,order_amount';
  396. for ($i=0; $i<$countnum; $i+=1000){//由于数据库底层的限制,所以每次查询1000条
  397. $order_list = array();
  398. $order_list = Db::name('statorder')->field($field)->where($where)->page($i,1000)->order('order_id')->select()->toArray();
  399. foreach ((array)$order_list as $k=>$v){
  400. if(!isset($store_arr[$v['store_id']])){
  401. $store_arr[$v['store_id']]=array();
  402. $store_arr[$v['store_id']]['orderamount'] = 0;
  403. $store_arr[$v['store_id']]['ordernum'] = 0;
  404. }
  405. $store_arr[$v['store_id']]['orderamount'] = $store_arr[$v['store_id']]['orderamount'] + $v['order_amount'];
  406. $store_arr[$v['store_id']]['ordernum'] = intval($store_arr[$v['store_id']]['ordernum']) + 1;
  407. $store_arr[$v['store_id']]['store_name'] = $v['store_name'];
  408. $store_arr[$v['store_id']]['store_id'] = $v['store_id'];
  409. }
  410. }
  411. //查询同一时间周期相比的环比数值
  412. $where = array();
  413. $stime = $searchtime_arr[0] - ($searchtime_arr[1] - $searchtime_arr[0]) - 1;
  414. $etime = $searchtime_arr[0] - 1;
  415. //店铺分类
  416. $search_sclass = intval($_REQUEST['search_sclass']);
  417. if ($search_sclass){
  418. $where[] = array('storeclass_id','=',$search_sclass);
  419. }
  420. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  421. $where[] = array('order_add_time','between',array($stime,$etime));
  422. $field = 'COUNT(*) as up_countnum';
  423. $up_countnum = $stat_model->getoneByStatorder($where, $field);
  424. $up_countnum = $up_countnum['up_countnum'];
  425. $up_store_arr = array();
  426. if ($up_countnum > 0){
  427. $field = 'store_id,store_name,order_amount';
  428. for ($i=0; $i<$up_countnum; $i+=1000){//由于数据库底层的限制,所以每次查询1000条
  429. $order_list = array();
  430. $order_list = Db::name('statorder')->field($field)->where($where)->page($i,1000)->order('store_id')->select()->toArray();
  431. foreach ((array)$order_list as $k=>$v){
  432. if(!isset($up_store_arr[$v['store_id']])){
  433. $up_store_arr[$v['store_id']]=array('orderamount'=>0,'ordernum'=>0);
  434. }
  435. $up_store_arr[$v['store_id']]['orderamount'] = $up_store_arr[$v['store_id']]['orderamount'] + $v['order_amount'];
  436. $up_store_arr[$v['store_id']]['ordernum'] = intval($up_store_arr[$v['store_id']]['ordernum']) + 1;
  437. }
  438. }
  439. }
  440. //计算环比飙升数值
  441. $soaring_arr = array();
  442. foreach ((array)$store_arr as $k=>$v){
  443. if (isset($up_store_arr[$k][$type]) && $up_store_arr[$k][$type] > 0){//上期数值大于0,则计算飙升值,否则不计入统计
  444. $soaring_arr[$k] = round((($v[$type]-$up_store_arr[$k][$type])/$up_store_arr[$k][$type]*100),2);
  445. }
  446. }
  447. arsort($soaring_arr);//降序排列数组
  448. $i = 1;
  449. //取出前10名飙升店铺
  450. foreach ((array)$soaring_arr as $k=>$v){
  451. if ($i <= $datanum){
  452. $tmp = array();
  453. $tmp['sort'] = $i;
  454. $tmp['store_name'] = $store_arr[$k]['store_name'];
  455. $tmp['store_id'] = $store_arr[$k]['store_id'];
  456. $tmp['hb'] = $v;
  457. switch ($type){
  458. case 'ordernum':
  459. $tmp['ordernum'] = $store_arr[$k]['ordernum'];
  460. break;
  461. case 'orderamount':
  462. $tmp['orderamount'] = ds_price_format($store_arr[$k]['orderamount']);
  463. break;
  464. }
  465. $soaring_statlist[] = $tmp;
  466. $i++;
  467. } else {
  468. break;
  469. }
  470. }
  471. }
  472. View::assign('soaring_statlist',$soaring_statlist);
  473. View::assign('statlist',$statlist);
  474. View::assign('sort_text',$sort_text);
  475. View::assign('stat_field',$type);
  476. echo View::fetch('store_hotrank_list');
  477. }
  478. /**
  479. * 店铺等级
  480. */
  481. public function degree(){
  482. $where = array();
  483. $field = ' count(*) as allnum,grade_id ';
  484. $stat_model = model('stat');
  485. //查询店铺分类下的店铺
  486. $search_sclass = input('param.search_sclass');
  487. if ($search_sclass > 0){
  488. $where[] = array('storeclass_id','=',$search_sclass);
  489. }
  490. $storelist = $stat_model->getNewStoreStatList($where, $field, 0, '', 0, 'grade_id');
  491. $sd_list = $stat_model->getStoreDegree();
  492. $statlist['headertitle'] = array();
  493. $statlist['data'] = array();
  494. //处理数组数据
  495. if(!empty($storelist)){
  496. foreach ($storelist as $k=>$v){
  497. $storelist[$k]['p_name'] = isset($sd_list[$v['grade_id']]) > 0 ? $sd_list[$v['grade_id']]:lang('ds_ownshop');
  498. $storelist[$k]['allnum'] = intval($v['allnum']);
  499. $statlist['headertitle'][] = isset($sd_list[$v['grade_id']]) > 0?$sd_list[$v['grade_id']]:lang('ds_ownshop');
  500. $statlist['data'][] = $v['allnum'];
  501. }
  502. $data = array(
  503. 'title'=>lang('store_grade_statis'),
  504. 'name'=>lang('store_number'),
  505. 'label_show'=>true,
  506. 'series'=>$storelist
  507. );
  508. View::assign('stat_json',getStatData_Pie($data));
  509. }
  510. $this->setAdminCurItem('degree');
  511. return View::fetch('stat_storedegree');
  512. }
  513. /**
  514. * 查看店铺列表
  515. */
  516. public function showstore(){
  517. $stat_model = model('stat');
  518. $where = array();
  519. if (in_array(input('type'),array('newbyday','newbyweek','newbymonth','storearea'))){
  520. $actionurl = (string)url('Statstore/showstore',['type'=>input('type'),'t'=>$this->search_arr['t']]);
  521. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  522. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  523. $searchtime_arr[] = intval($v);
  524. }
  525. $where[] = array('store_addtime','between',$searchtime_arr);
  526. }
  527. //商品分类
  528. $storeclass_id = intval(input('scid'));
  529. if ($storeclass_id > 0){
  530. $where[] = array('storeclass_id','=',$storeclass_id);
  531. $actionurl .="&scid=$storeclass_id";
  532. }
  533. //省份
  534. if (input('provid')){
  535. $province_id = intval(input('param.provid'));
  536. $where[] = array('province_id','=',$province_id);
  537. $actionurl .="&provid=$province_id";
  538. }
  539. if (input('exporttype') == 'excel'){
  540. $store_list = $stat_model->getNewStoreStatList($where);
  541. } else {
  542. $store_list = $stat_model->getNewStoreStatList($where, '', 10);
  543. }
  544. //店铺等级
  545. $storegrade_model = model('storegrade');
  546. $grade_list = $storegrade_model->getStoregradeList();
  547. if (!empty($grade_list)){
  548. $search_grade_list = array();
  549. foreach ($grade_list as $k => $v){
  550. $search_grade_list[$v['storegrade_id']] = $v['storegrade_name'];
  551. }
  552. }
  553. //导出Excel
  554. if (input('exporttype') == 'excel'){
  555. //导出Excel
  556. $excel_obj = new \excel\Excel();
  557. $excel_data = array();
  558. //设置样式
  559. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  560. //header
  561. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('ds_store_name'));
  562. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('ds_member_name'));
  563. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('ds_seller_name'));
  564. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('stat_storedegree'));
  565. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('statstore_expire'));
  566. $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('statstore_addtime'));
  567. //data
  568. foreach ($store_list as $k=>$v){
  569. $excel_data[$k+1][] = array('data'=>$v['store_name']);
  570. $excel_data[$k+1][] = array('data'=>$v['member_name']);
  571. $excel_data[$k+1][] = array('data'=>$v['seller_name']);
  572. $excel_data[$k+1][] = array('data'=>$search_grade_list[$v['grade_id']]);
  573. $excel_data[$k+1][] = array('data'=>$v['store_endtime']?date('Y-m-d', $v['store_endtime']):lang('state_no_tile_limit'));
  574. $excel_data[$k+1][] = array('data'=>date('Y-m-d', $v['store_addtime']));
  575. }
  576. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  577. $excel_obj->addArray($excel_data);
  578. $excel_obj->addWorksheet($excel_obj->charset(lang('ds_new').lang('ds_store'),CHARSET));
  579. $excel_obj->generateXML($excel_obj->charset(lang('ds_new').lang('ds_store'),CHARSET).date('Y-m-d-H',TIMESTAMP));
  580. exit();
  581. }
  582. View::assign('search_grade_list', $search_grade_list);
  583. View::assign('actionurl',$actionurl);
  584. View::assign('store_list',$store_list);
  585. View::assign('show_page',$stat_model->page_info->render());
  586. $this->setAdminCurItem('showstore');
  587. return View::fetch('storelist');
  588. }
  589. /**
  590. * 销售统计
  591. */
  592. public function storesales(){
  593. if(!isset($this->search_arr['search_type'])){
  594. $this->search_arr['search_type'] = 'day';
  595. }
  596. $stat_model = model('stat');
  597. //获得搜索的开始时间和结束时间
  598. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  599. View::assign('searchtime',implode('|',$searchtime_arr));
  600. //周
  601. if(!input('param.search_time_year')) {
  602. $search_time_year = date('Y', TIMESTAMP);
  603. }else {
  604. $search_time_year = input('param.search_time_year');
  605. }
  606. if(!input('param.search_time_month')) {
  607. $search_time_month = date('m', TIMESTAMP);
  608. }else {
  609. $search_time_month = input('param.search_time_month');
  610. }
  611. if(!input('param.search_time_week')) {
  612. $search_time_week = implode('|', getWeek_SdateAndEdate(TIMESTAMP));
  613. }else {
  614. $search_time_week = input('param.search_time_week');
  615. }
  616. $current_year =$search_time_year;
  617. $current_month =$search_time_month;
  618. $current_week = $search_time_week;
  619. View::assign('current_year', $current_year);
  620. View::assign('current_month', $current_month);
  621. View::assign('current_week', $current_week);
  622. $this->setAdminCurItem('storesales');
  623. return View::fetch('stat_store_sales');
  624. }
  625. /**
  626. * 店铺销售统计列表
  627. */
  628. public function storesales_list(){
  629. $stat_model = model('stat');
  630. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  631. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  632. $searchtime_arr[] = intval($v);
  633. }
  634. $where = array();
  635. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  636. $where[] = array('order_add_time','between',$searchtime_arr);
  637. //店铺分类
  638. $search_sclass = intval(input('param.search_sclass'));
  639. if ($search_sclass){
  640. $where[]=array('storeclass_id','=',$search_sclass);
  641. }
  642. //店铺名称
  643. $where[]=array('store_name','like',"%".input('param.search_sname')."%");
  644. //查询总条数
  645. $count_arr = $stat_model->getoneByStatorder($where, 'COUNT(DISTINCT store_id) as countnum');
  646. $countnum = intval($count_arr['countnum']);
  647. //列表字段
  648. $field = " store_id,store_name,SUM(order_amount) as orderamount, COUNT(*) as ordernum, COUNT(DISTINCT buyer_id) as membernum";
  649. //排序
  650. $orderby_arr = array('membernum asc','membernum desc','ordernum asc','ordernum desc','orderamount asc','orderamount desc');
  651. if (!isset($this->search_arr['orderby'])||!in_array(trim($this->search_arr['orderby']),$orderby_arr)){
  652. $this->search_arr['orderby'] = 'membernum desc';
  653. }
  654. $orderby = trim($this->search_arr['orderby']).',store_id asc';
  655. if (isset($this->search_arr['exporttype'])&&$this->search_arr['exporttype'] == 'excel'){
  656. $statlist = $stat_model->statByStatorder($where, $field, 0, 0, $orderby, 'store_id');
  657. } else {
  658. $statlist = $stat_model->statByStatorder($where, $field, 20, 0, $orderby, 'store_id');
  659. foreach ((array)$statlist as $k=>$v){
  660. $v['view'] = "<a href='javascript:void(0);' ds_type='showtrends' data-param='{\"storeid\":\"{$v['store_id']}\"}'>走势图</a>";
  661. $statlist[$k] = $v;
  662. }
  663. }
  664. //列表header
  665. $statheader = array();
  666. $statheader[] = array('text'=>lang('ds_store_name'),'key'=>'store_name');
  667. $statheader[] = array('text'=>lang('membernum'),'key'=>'membernum','isorder'=>1);
  668. $statheader[] = array('text'=>lang('statstore_ordernum'),'key'=>'ordernum','isorder'=>1);
  669. $statheader[] = array('text'=>lang('statstore_orderamount'),'key'=>'orderamount','isorder'=>1);
  670. //导出Excel
  671. if (isset($this->search_arr['exporttype'])&&$this->search_arr['exporttype'] == 'excel'){
  672. //导出Excel
  673. $excel_obj = new \excel\Excel();
  674. $excel_data = array();
  675. //设置样式
  676. $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
  677. //header
  678. foreach ($statheader as $k=>$v){
  679. $excel_data[0][] = array('styleid'=>'s_title','data'=>$v['text']);
  680. }
  681. //data
  682. foreach ($statlist as $k=>$v){
  683. foreach ($statheader as $h_k=>$h_v){
  684. $excel_data[$k+1][] = array('data'=>$v[$h_v['key']]);
  685. }
  686. }
  687. $excel_data = $excel_obj->charset($excel_data,CHARSET);
  688. $excel_obj->addArray($excel_data);
  689. $excel_obj->addWorksheet($excel_obj->charset(lang('store_sale_statis'),CHARSET));
  690. $excel_obj->generateXML($excel_obj->charset(lang('store_sale_statis'),CHARSET).date('Y-m-d-H',TIMESTAMP));
  691. exit();
  692. }
  693. View::assign('statlist',$statlist);
  694. View::assign('statheader',$statheader);
  695. View::assign('orderby',$this->search_arr['orderby']);
  696. View::assign('actionurl',(string)url('Statstore/storesales_list',['t'=>$this->search_arr['t'],'search_sclass'=>$search_sclass,'search_sname'=>input('param.search_sname')]));
  697. View::assign('show_page',$stat_model->page_info->render());
  698. echo View::fetch('stat_listandorder');
  699. }
  700. /**
  701. * 销售走势
  702. */
  703. public function storesales_trends(){
  704. $storeid = intval(input('param.storeid'));
  705. if ($storeid <= 0){
  706. View::assign('stat_error',lang('stat_error'));
  707. return View::fetch('salestrends');
  708. exit();
  709. }
  710. $search_type=input('param.search_type');
  711. if (!$search_type){
  712. $search_type= 'day';
  713. }
  714. $stat_model = model('stat');
  715. $where = array();
  716. $where[] = array('store_id','=',$storeid);
  717. $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
  718. foreach ((array)$searchtime_arr_tmp as $k=>$v){
  719. $searchtime_arr[] = intval($v);
  720. }
  721. $where[] = array('order_isvalid','=',1);//计入统计的有效订单
  722. $where[] = array('order_add_time','between',$searchtime_arr);
  723. $field = " store_id,store_name,SUM(order_amount) as orderamount, COUNT(*) as ordernum, COUNT(DISTINCT buyer_id) as membernum";
  724. $stat_arr = array('orderamount'=>array(),'ordernum'=>array(),'membernum'=>array());
  725. $statlist = array();
  726. if($search_type == 'day'){
  727. //构造横轴数据
  728. for($i=0; $i<24; $i++){
  729. //横轴
  730. foreach ($stat_arr as $k=>$v){
  731. $stat_arr[$k]['xAxis']['categories'][] = "$i";
  732. $statlist[$k][$i] = 0;
  733. }
  734. }
  735. $field .= ' ,HOUR(FROM_UNIXTIME(order_add_time)) as timeval ';
  736. }
  737. if($search_type == 'week'){
  738. //构造横轴数据
  739. for($i=1; $i<=7; $i++){
  740. $tmp_weekarr = getSystemWeekArr();
  741. //横轴
  742. foreach ($stat_arr as $k=>$v){
  743. $stat_arr[$k]['xAxis']['categories'][] = $tmp_weekarr[$i];
  744. $statlist[$k][$i] = 0;
  745. }
  746. unset($tmp_weekarr);
  747. }
  748. $field .= ' ,WEEKDAY(FROM_UNIXTIME(order_add_time))+1 as timeval ';
  749. }
  750. if($search_type == 'month'){
  751. //计算横轴的最大量(由于每个月的天数不同)
  752. $dayofmonth = date('t',$searchtime_arr[0]);
  753. //构造横轴数据
  754. for($i=1; $i<=$dayofmonth; $i++){
  755. //横轴
  756. foreach ($stat_arr as $k=>$v){
  757. $stat_arr[$k]['xAxis']['categories'][] = $i;
  758. $statlist[$k][$i] = 0;
  759. }
  760. }
  761. $field .= ' ,day(FROM_UNIXTIME(order_add_time)) as timeval ';
  762. }
  763. //查询数据
  764. $statlist_tmp = $stat_model->statByStatorder($where, $field, 0, '');
  765. //整理统计数组
  766. $storename = '';
  767. if($statlist_tmp){
  768. foreach($statlist_tmp as $k => $v){
  769. $storename = $v['store_name'];
  770. foreach ($stat_arr as $t_k=>$t_v){
  771. if ($k == 'orderamount'){
  772. $statlist[$t_k][$v['timeval']] = round($v[$t_k],2);
  773. } else {
  774. $statlist[$t_k][$v['timeval']] = intval($v[$t_k]);
  775. }
  776. }
  777. }
  778. }
  779. foreach ($stat_arr as $k=>$v){
  780. $stat_arr[$k]['legend']['enabled'] = false;
  781. switch ($k){
  782. case 'orderamount':
  783. $caption = lang('statstore_orderamount');
  784. break;
  785. case 'ordernum':
  786. $caption = lang('statstore_ordernum');
  787. break;
  788. default:
  789. $caption = lang('membernum');
  790. break;
  791. }
  792. $stat_arr[$k]['series'][0]['name'] = $caption;
  793. $stat_arr[$k]['series'][0]['data'] = array_values($statlist[$k]);
  794. $stat_arr[$k]['title'] = $caption.lang('sale_trend');
  795. $stat_arr[$k]['yAxis'] = $caption;
  796. //得到统计图数据
  797. $stat_json[$k] = getStatData_LineLabels($stat_arr[$k]);
  798. }
  799. View::assign('storename',$storename);
  800. View::assign('stat_json',$stat_json);
  801. echo View::fetch('store_salestrends');
  802. }
  803. /**
  804. * 地区分布
  805. */
  806. public function storearea(){
  807. $stat_model = model('stat');
  808. //获得搜索的开始时间和结束时间
  809. $searchtime_arr = $stat_model->getStarttimeAndEndtime($this->search_arr);
  810. $where = array();
  811. if (trim(input('param.search_time'))){
  812. $where[]=array('store_addtime','<=',strtotime(input('param.search_time')));
  813. }
  814. $search_sclass = intval(input('param.search_sclass'));
  815. if ($search_sclass > 0){
  816. $where[]=array('storeclass_id','=',$search_sclass);
  817. }
  818. $field = ' region_id, COUNT(*) as storenum ';
  819. $statlist_tmp = $stat_model->statByStore($where, $field, 'region_id', 'region_id');
  820. // 地区
  821. $province_array = model('area')->getTopLevelAreas();
  822. //地图显示等级数组
  823. $level_arr = array(array(1,2,3),array(4,5,6),array(7,8,9),array(10,11,12));
  824. $statlist = array();
  825. $temp_list=array();
  826. foreach ((array)$statlist_tmp as $k=>$v){
  827. $v['level'] = 4;//排名
  828. foreach ($level_arr as $lk=>$lv){
  829. if (in_array($k+1,$lv)){
  830. $v['level'] = $lk;//排名
  831. }
  832. }
  833. //获取该地区的省
  834. if($v['region_id']){
  835. if(!isset($temp_list[$v['region_id']])){
  836. $temp=Db::name('area')->where('area_id='.$v['region_id'])->value('area_parent_id');
  837. if($temp){
  838. $temp_list[$v['region_id']]=$temp;
  839. }else{
  840. $temp_list[$v['region_id']]=$v['region_id'];
  841. }
  842. $index=0;
  843. while($temp && $index<4){
  844. $temp_list[$v['region_id']]=$temp;
  845. $temp_0=$temp;
  846. $temp=Db::name('area')->where('area_id='.$temp)->value('area_parent_id');
  847. if($temp){
  848. $temp_list[$temp_0]=$temp;
  849. }else{
  850. $temp_list[$temp_0]=$temp_0;
  851. }
  852. $index++;
  853. }
  854. }
  855. $province_id = $temp_list[$v['region_id']];
  856. }else{
  857. $province_id=0;
  858. }
  859. $v['sort'] = $k+1;
  860. $v['provincename'] = isset($province_array[$province_id]) ? $province_array[$province_id] : lang('other');
  861. if(!isset($statlist[$province_id])){
  862. $statlist[$province_id] = $v;
  863. }else{
  864. $statlist[$province_id]['storenum'] += $v['storenum'];
  865. }
  866. }
  867. $stat_arr = array();
  868. foreach ((array)$province_array as $k=>$v){
  869. if (isset($statlist[$k])){
  870. $stat_arr[] = array('cha'=>$k,'name'=>$v,'des'=>",店铺量:{$statlist[$k]['storenum']}",'level'=>$statlist[$k]['level']);
  871. } else {
  872. $stat_arr[] = array('cha'=>$k,'name'=>$v,'des'=>',无订单数据','level'=>4);
  873. }
  874. }
  875. $stat_json = getStatData_Map($stat_arr);
  876. View::assign('stat_json',$stat_json);
  877. View::assign('statlist',$statlist);
  878. $actionurl = (string)url('Statstore/storearea');
  879. if (trim(input('param.search_time'))){
  880. $actionurl = $actionurl.'&t=0|'.strtotime(input('param.search_time'));
  881. }
  882. if ($search_sclass > 0){
  883. $actionurl .= "&scid=$search_sclass";
  884. }
  885. View::assign('actionurl',$actionurl);
  886. $this->setAdminCurItem('storearea');
  887. return View::fetch('stat_storearea');
  888. }
  889. protected function getAdminItemList()
  890. {
  891. $menu_array = array(
  892. array('name'=>'newstore','text'=>lang('stat_newstore'),'url'=>(string)url('Statstore/newstore')),
  893. array('name'=>'hotrank','text'=>lang('stat_storehotrank'),'url'=>(string)url('Statstore/hotrank')),
  894. array('name'=>'storesales','text'=>lang('stat_storesales'),'url'=>(string)url('Statstore/storesales')),
  895. array('name'=>'degree','text'=>lang('stat_storedegree'),'url'=>(string)url('Statstore/degree')),
  896. array('name'=>'storearea','text'=>lang('stat_storearea'),'url'=>(string)url('Statstore/storearea')),
  897. );
  898. return $menu_array;
  899. }
  900. }