// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\store\model; use app\common\model\ChannelSaleVolumeTj as ChannelStModel; use app\common\model\user\CommissionsDetail; /** * 门店销售数据每日统计模型 * Class ShopsDailySalesSt * @package app\common\model */ class ChannelSaleVolumeTj extends ChannelStModel { protected $ftype = [1=>'day',2=>'week',3=>'month']; protected $channelType = [1,2,3];//1:门店销售渠道,2:推荐官渠道,3:自然成交 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); foreach ($this->channelType as $channel_type){ $data = $this->search($start,$end,1,$remark,$channel_type,$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); foreach ($this->channelType as $channel_type){ $data = $this->search($start,$end,3,$remark,$channel_type,$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); foreach ($this->channelType as $channel_type){ $data = $this->search($start,$end,3,$remark,$channel_type,$tj_time); $this->add($data); } return true; } public function week($baseTimestamp){ $w = 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); foreach ($this->channelType as $channel_type){ $data = $this->search($start,$end,2,$remark,$channel_type,$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); foreach ($this->channelType as $channel_type){ $data = $this->search($start,$end,2,$remark,$channel_type,$tj_time); $this->add($data); } return true; } //查找时间段 public function search($start,$end,$ftype,$remark,$channel_type,$tj_time): array { $data['ftype'] =$ftype; $data['remark'] = $remark; $data['channel_type'] =$channel_type; $data['tj_time'] = $tj_time; $where = [['commission_level','=',1], ['role','=',$channel_type==2?User::COMMISSION_USER:User::SHOP_SELLER], ['order_create_time','>=',$start], ['order_create_time','<=',$end] ]; if($channel_type<3){ $data['sale_volume'] = CommissionsDetail::sumOrderSaleVolume($where); }else{ //自然成交额 $where = [ ['create_time','>=',$start], ['create_time','<=',$end], ['staff_user_id', '=', 0], ['staff_shop_id' ,'=', 0], ['is_delete','=',0], ['pay_status', '=', 20] ]; $order = new Order(); $data['sale_volume'] = $order->sumNatureSaleVolume($where); } return $data; } //添加 public function add($data){ $m = new ChannelSaleVolumeTj; $one = $m->where('remark',$data['remark']) ->where("ftype",$data['ftype']) ->where("channel_type",$data['channel_type']) ->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'] + 86400)]; return $this->where($filter)->field('remark,tj_time,sum(sale_volume) as sale_volume') ->group('remark') ->order(['tj_time' => 'asc'])->select(); } public function getPieList($times){ // 检查查询条件 $filter = []; $filter[] = ['ftype','=',1]; $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'])]; return $this->where($filter)->field('sum(sale_volume) as sale_volume,channel_type') ->group('channel_type') ->select()->toArray(); } }