// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\store\model\user; use app\common\model\user\CommerCountTj as CommerCountTjModel; use app\common\model\ApplySeller; use app\store\model\User; /** * 门店销售数据每日统计模型 * Class ShopsDailySalesSt * @package app\common\model */ class CommerCountTj extends CommerCountTjModel { protected $ftype = [1=>'day',2=>'week',3=>'month']; public function day($baseTimestamp,$dif = 1){ $start = strtotime(Date("Y-m-d",$baseTimestamp-86400*$dif)." 00:00:00"); $end = strtotime(Date("Y-m-d",$baseTimestamp-86400*$dif)." 23:59:59"); $remark = Date("Y-m-d",$baseTimestamp-86400*$dif); $tj_time = Date("Y-m-d H:i:s",$start + 10); $data = $this->search($start,$end,1,$remark,$tj_time); $this->add($data); return true; } public function month($baseTimestamp){ //如果是1号,重新统计上月的数据 if(intval(date('d',$baseTimestamp)) == 1){ $standardTime = date('Y-m-1',$baseTimestamp); $_lastMonthStart = date('Y-m-01 00:00:00', strtotime("-1 month", strtotime($standardTime))); $_lastMonthEnd = date('Y-m-d H:i:s', strtotime('-1 sec', strtotime($standardTime))); $start = strtotime($_lastMonthStart); $end = strtotime($_lastMonthEnd); $remark = substr($_lastMonthStart,0,7); $tj_time = Date("Y-m-d H:i:s",$start + 10); $data = $this->search($start,$end,3,$remark,$tj_time); $this->add($data); } $standardTime = date('Y-m-01 00:00:00',$baseTimestamp); $_lastMonthStart = date('Y-m-01 00:00:00', strtotime($standardTime)); $start = strtotime($standardTime); $end = $baseTimestamp; $remark = substr($_lastMonthStart,0,7); $tj_time = Date("Y-m-d H:i:s",$start + 10); $data = $this->search($start,$end,3,$remark,$tj_time); $this->add($data); return true; } public function week($baseTimestamp){ //上周一 $w = intval(date("w",$baseTimestamp)); //如果是周一,统计上周的数据 if($w==1){ $t = $w==1?1:2; //如果当前时间是周一,则-1,如果已经是周二 要-2 $d1 = date('Y-m-d 00:00:00',strtotime('-'.$t.' monday',$baseTimestamp)); //无论今天几号,-1 monday为上一个有效周未 //上周日 $d7 = date('Y-m-d 23:59:59',strtotime('-1 sunday', $baseTimestamp)); // //上一个有效周日,同样适用于其它星期 $remark = substr($d1,0,10).'--'.substr($d7,0,10); $start = strtotime($d1); $end = strtotime($d7); $tj_time = Date("Y-m-d H:i:s",$start + 10); $data = $this->search($start,$end,2,$remark,$tj_time); $this->add($data); } $start = mktime(0,0,0,intval(date("m",$baseTimestamp)),date("d",$baseTimestamp)-date("N",$baseTimestamp)+1,intval(date("y",$baseTimestamp))); $end = $start + 7*86400 - 1; $d1 = date('Y-m-d 00:00:00',$start); $d7 = date('Y-m-d H:i:s',$end); //上一个有效周日,同样适用于其它星期 $remark = substr($d1,0,10).'--'.substr($d7,0,10); $start = strtotime($d1); $end = strtotime($d7); $tj_time = Date("Y-m-d H:i:s",$start + 10); $data = $this->search($start,$end,2,$remark,$tj_time); $this->add($data); return true; } //查找时间段 public function search($start,$end,$ftype,$remark,$tj_time): array { $data['ftype'] =$ftype; $data['remark'] = $remark; $data['tj_time'] = $tj_time; $where = [['state','=',1], ['create_time','>=',$start], ['create_time','<=',$end] ]; $data['counts'] = ApplySeller::countCommer($where); return $data; } //添加 public function add($data){ $m = new CommerCountTj(); $one = $m->where('remark',$data['remark']) ->where("ftype",$data['ftype']) ->find(); if(empty($one)){ $m->save($data); }else{ $one->where('id',$one->id)->update($data); } } public function getList($ftype,$times){ // 检查查询条件 $filter = []; $ftype>-1 && $filter[] = ['ftype', '=', (int)$ftype]; $filter[] = ['tj_time', '>=', Date('Y-m-d H:i:s',$times['start_time'])]; $filter[] = ['tj_time', '<', Date('Y-m-d H:i:s',$times['end_time'] + 10)]; return $this->where($filter)->field('remark,tj_time,sum(counts) as counts') ->group('remark') ->order(['tj_time' => 'asc'])->select(); } public function zxExport($ftype,$times){ $volumes = $this->getList($ftype,$times); $lists = $volumes?$volumes->toArray():[]; $data['header'] = ['序号', '时间范围', '推荐官数量']; $data['filename'] = '推荐官人数增长趋势'; $data['data'] = []; foreach ($lists as $k=>$arr){ $new_list['key'] = $k+1; $new_list['remark'] = $arr['remark']; $new_list['counts'] = $arr['counts']; $data['data'][] = $new_list; } return $data; } }