CommerCountTj.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\store\model\user;
  13. use app\common\model\user\CommerCountTj as CommerCountTjModel;
  14. use app\common\model\ApplySeller;
  15. use app\store\model\User;
  16. /**
  17. * 门店销售数据每日统计模型
  18. * Class ShopsDailySalesSt
  19. * @package app\common\model
  20. */
  21. class CommerCountTj extends CommerCountTjModel
  22. {
  23. protected $ftype = [1=>'day',2=>'week',3=>'month'];
  24. public function day($baseTimestamp,$dif = 1){
  25. $start = strtotime(Date("Y-m-d",$baseTimestamp-86400*$dif)." 00:00:00");
  26. $end = strtotime(Date("Y-m-d",$baseTimestamp-86400*$dif)." 23:59:59");
  27. $remark = Date("Y-m-d",$baseTimestamp-86400*$dif);
  28. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  29. $data = $this->search($start,$end,1,$remark,$tj_time);
  30. $this->add($data);
  31. return true;
  32. }
  33. public function month($baseTimestamp){
  34. //如果是1号,重新统计上月的数据
  35. if(intval(date('d',$baseTimestamp)) == 1){
  36. $standardTime = date('Y-m-1',$baseTimestamp);
  37. $_lastMonthStart = date('Y-m-01 00:00:00', strtotime("-1 month", strtotime($standardTime)));
  38. $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime)));
  39. $start = strtotime($_lastMonthStart);
  40. $end = strtotime($_lastMonthEnd);
  41. $remark = substr($_lastMonthStart,0,7);
  42. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  43. $data = $this->search($start,$end,3,$remark,$tj_time);
  44. $this->add($data);
  45. }
  46. $standardTime = date('Y-m-01 00:00:00',$baseTimestamp);
  47. $_lastMonthStart = date('Y-m-01 00:00:00', strtotime($standardTime));
  48. $start = strtotime($standardTime);
  49. $end = $baseTimestamp;
  50. $remark = substr($_lastMonthStart,0,7);
  51. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  52. $data = $this->search($start,$end,3,$remark,$tj_time);
  53. $this->add($data);
  54. return true;
  55. }
  56. public function week($baseTimestamp){
  57. //上周一
  58. $w = intval(date("w",$baseTimestamp));
  59. //如果是周一,统计上周的数据
  60. if($w==1){
  61. $t = $w==1?1:2; //如果当前时间是周一,则-1,如果已经是周二 要-2
  62. $d1 = date('Y-m-d 00:00:00',strtotime('-'.$t.' monday',$baseTimestamp)); //无论今天几号,-1 monday为上一个有效周未
  63. //上周日
  64. $d7 = date('Y-m-d 23:59:59',strtotime('-1 sunday', $baseTimestamp)); //
  65. //上一个有效周日,同样适用于其它星期
  66. $remark = substr($d1,0,10).'--'.substr($d7,0,10);
  67. $start = strtotime($d1);
  68. $end = strtotime($d7);
  69. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  70. $data = $this->search($start,$end,2,$remark,$tj_time);
  71. $this->add($data);
  72. }
  73. $start = mktime(0,0,0,intval(date("m",$baseTimestamp)),date("d",$baseTimestamp)-date("N",$baseTimestamp)+1,intval(date("y",$baseTimestamp)));
  74. $end = $start + 7*86400 - 1;
  75. $d1 = date('Y-m-d 00:00:00',$start);
  76. $d7 = date('Y-m-d H:i:s',$end);
  77. //上一个有效周日,同样适用于其它星期
  78. $remark = substr($d1,0,10).'--'.substr($d7,0,10);
  79. $start = strtotime($d1);
  80. $end = strtotime($d7);
  81. $tj_time = Date("Y-m-d H:i:s",$start + 10);
  82. $data = $this->search($start,$end,2,$remark,$tj_time);
  83. $this->add($data);
  84. return true;
  85. }
  86. //查找时间段
  87. public function search($start,$end,$ftype,$remark,$tj_time): array
  88. {
  89. $data['ftype'] =$ftype;
  90. $data['remark'] = $remark;
  91. $data['tj_time'] = $tj_time;
  92. $where = [['state','=',1],
  93. ['create_time','>=',$start],
  94. ['create_time','<=',$end]
  95. ];
  96. $data['counts'] = ApplySeller::countCommer($where);
  97. return $data;
  98. }
  99. //添加
  100. public function add($data){
  101. $m = new CommerCountTj();
  102. $one = $m->where('remark',$data['remark'])
  103. ->where("ftype",$data['ftype'])
  104. ->find();
  105. if(empty($one)){
  106. $m->save($data);
  107. }else{
  108. $one->where('id',$one->id)->update($data);
  109. }
  110. }
  111. public function getList($ftype,$times){
  112. // 检查查询条件
  113. $filter = [];
  114. $ftype>-1 && $filter[] = ['ftype', '=', (int)$ftype];
  115. $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])];
  116. $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 10)];
  117. return $this->where($filter)->field('remark,tj_time,sum(counts) as counts')
  118. ->group('remark')
  119. ->order(['tj_time' => 'asc'])->select();
  120. }
  121. public function zxExport($ftype,$times){
  122. $volumes = $this->getList($ftype,$times);
  123. $lists = $volumes?$volumes->toArray():[];
  124. $data['header'] = ['序号', '时间范围', '推荐官数量'];
  125. $data['filename'] = '推荐官人数增长趋势';
  126. $data['data'] = [];
  127. foreach ($lists as $k=>$arr){
  128. $new_list['key'] = $k+1;
  129. $new_list['remark'] = $arr['remark'];
  130. $new_list['counts'] = $arr['counts'];
  131. $data['data'][] = $new_list;
  132. }
  133. return $data;
  134. }
  135. }