<?php
namespace app\admin\logic;
use think\Db;
use app\common\server\UrlServer;
use think\helper\Time;
use app\common\server\ConfigServer;

class OrderstaffLogic{
    public static function lists($get){
       $where = [];

        if(isset($get['type']) && $get['type'] != ''){
            switch($get['type']) {
                case '1':

                      $where[] = ['staff_status', '=', 3];
                 
                    break;
        
                default:
                    $where[] = ['status', '=', $get['type']];
                    break;
            }
        }



        if(isset($get['satff']) && $get['satff']!=''){
            $where[] = ['staff_id','=',$get['satff']];
        }

        if(isset($get['order_sn']) && $get['order_sn']!=''){
            $where[] = ['order_sn','=',$get['order_sn']];
        }
        if(isset($get['id']) && $get['id']!=''){
            $where[] = ['id','=',$get['id']];
        }
        if(isset($get['staf_stuts']) && $get['staf_stuts']!=''){
            if($get['staf_stuts']==0){
                 $where[] = ['staff_id','=',0]; 
            }

        }
        if(isset($get['phone']) && $get['phone']!=''){
            $where[] = ['phone','=',$get['phone']];
         }
       if(isset($get['admin']) && $get['admin']!=''){
            $where[] = ['admin','=',$get['admin']];
         }  
        if(isset($get['name']) && $get['name']!=''){
             $where[] = ['name', 'like', '%' . $get['name'] . '%'];
         } 
        //下单时间检索
        if(isset($get['start_time']) && $get['start_time']!=''){
            $where[] = ['autotime','>=',strtotime($get['start_time'])];
          
        }
        if(isset($get['end_time']) && $get['end_time']!=''){
            $where[] = ['autotime','<=',strtotime($get['end_time'])];
       
        }
        if(isset($get['channel']) && $get['channel']!=''){
                $where[] = ['channel_id','=',$get['channel']];
        }
        
        $user_count =Db::name('order_exe')
                   ->where($where)
                    ->count();
        $user_list =Db::name('order_exe')
                   ->where($where)
                   ->page($get['page'],$get['limit'])
                   ->order('id desc')
                   ->select();
        foreach ($user_list as &$item){
            $item['autotime'] = date("Y-m-d",$item['autotime']);
            $cumst=Db::name('order')->where('order_sn',$item['order_sn'])->find();       //获取订单的基本信息
            
            if($cumst){
            $item['consignee']= $cumst['consignee'];
            $item['phone']= $cumst['mobile'];
            $item['address']= $cumst['address'];
            $item['typeorder']= $cumst['type'];  
             $item['order_remarks']= $cumst['order_remarks'];
            $cumsts=Db::name('user_address')->where('telephone',$cumst['mobile'])->find();   //获取到客户的基本信息
            if($cumsts){
                $item['pet']= $cumsts['pet'];
                $item['hbl']= $cumsts['hbl'];
                $item['user_remarks']= $cumsts['user_remarks'];
                $item['square']= $cumsts['areas'];
                $item['admin_remarks']= $cumsts['admin_remarks'];
                $item['staff_remarks']= $cumsts['staff_remarks']; 
            }

            }
            $admin=Db::name('admin')->where('id',$item['admin'])->find();            //获取管理员信息
            if($admin){
                 $item['admin']=$admin['name'];
            }else{
                 $item['admin']='-';
            }
           
            $staff=Db::name('staff')->where('id',$item['staff_id'])->find();           //获取到订单的保洁师人员
            if($staff){
                $item['staff_name']=$staff['name'];
                $item['staff_id']= $staff['id'];
                $item['staff_mobile']= $staff['mobile'];
                $item['staff_addr']= $staff['addr'];     
              }

            
            $goods=Db::name('goods')->where('id',$item['goods_id'])->find();            //获取商品到信息
            
            $channel=Db::name('orderchannel')->where('id',$cumst['channel_id'])->find();
            if($channel){
                $item['channel_name']= $channel['name'];
            }else{
                 $item['channel_name']='客户自助下单';
            }
            
            if($goods){
                $item['goods_name']=$goods['name'];
            }else{
                 $item['goods_name']='-';
            }
            
            //保洁师服务时间
            if($item['start_time']){
                $item['start_time']=date("Y-m-d H:i:s",$item['start_time']);
            }else{
                $item['start_time']='-';
            }
            
            if($item['timeout']){
                  $item['timeout']=date("Y-m-d H:i:s",$item['timeout']);
             }else{
                 $item['timeout']='-';
            }
             
            //售后订单回访控制 
            $track=Db::name('order_trace')->where('order_id',$item['id'])->find();
            if($track){
                $item['is_show']=-1;
            }else{
                 $item['is_show']=1;
            }
            
            //控制异常订单显示
            
            $abnormal=Db::name('order_sale')->where('order_id',$item['id'])->find();
            if($abnormal){
                $item['sale_show']=-1;
            }else{
                 $item['sale_show']=1;
            }
        
            if($item['admin']==0){
              $coustdd=Db::name('user_address')->where('telephone',$cumst['mobile'])->find();
              if($coustdd){
                       $date=Db::name('order_exe')->where('id',$item['id'])->update(['name'=>$cumst['consignee'],'phone'=>$cumst['mobile'],'admin'=>$coustdd['admin_id']]);
              }
         
            }
            //自动更新套餐
            if($item['goods_id']==0){
               $date=Db::name('order_exe')->where('id',$item['id'])->update(['goods_id'=>$cumst['goods_id']]);
             }
             if($item['channel_id']==0){
                $date=Db::name('order_exe')->where('id',$item['id'])->update(['channel_id'=>$cumst['channel_id']]);
             }
         
             if(strtotime($item['autotime'])!=strtotime($item['date'])){
                $date=Db::name('order_exe')->where('id',$item['id'])->update(['date'=>$item['autotime']]);
             }
     
             
       }

        return ['count'=>$user_count , 'lists'=>$user_list];
    }

    public static function  orderlist($get){ 
        //今天时间戳
        $where=[];
        $where1=[];
        $where2=[];
        $where3=[];
  
        
        $startTimestamp = strtotime(date('Y-m-d 00:00:00'));
         
        // 获取今天结束时间戳
        $endTimestamp = strtotime(date('Y-m-d 23:59:59'));
        
        if(empty($get['start_time'])){
            $get['start_time'] = date('Y-m-d'); 
        }
        
        if(isset($get['start_time']) && $get['start_time']==''){
            
            $where[] = ['autotime','>=',$startTimestamp];
            $where[] = ['autotime','<=',$endTimestamp]; 
            $where1[] = ['time','>=', $startTimestamp];
            $where1[] = ['time','<=', $endTimestamp];
            
        }
        

        if(isset($get['start_time']) && $get['start_time']!=''){
              $edtime=strtotime($get['start_time'])+(24 * 60 * 60) - 1;
              $where[] = ['autotime','>=',strtotime($get['start_time'])]; 
              $where[] = ['autotime','<=', $edtime];
              $where1[] = ['time','<=',strtotime($get['start_time'])+(24 * 60 * 60) - 1];
              $where1[] = ['time','>=',strtotime($get['start_time'])];
              
              $where4[] = ['time','>=',strtotime($get['start_time'])];
              $where4[] = ['time','<=',strtotime($get['start_time'])+(24 * 60 * 60) - 1];
             
        }
        if(isset($get['staff_id']) && $get['staff_id']!=''){
            $where2[] = ['id','=',$get['staff_id']];
         }
        if(isset($get['type']) && $get['type']!=''){
             $where3[] = ['goods_id','=',$get['type']];
         }
        $order_count=Db::name('staff')
         ->where('onwork',1)
        ->where($where2)->where($where3)
        ->count();
         $order_list =Db::name('staff')
         ->where($where2)
         ->where($where3)
         ->where('onwork',1)
         ->page($get['page'],$get['limit'])
         ->select();
         foreach ($order_list as &$item){
             $addersw=Db::name('order_exe')->where($where)->where('staff_id',$item['id'])->where('addtime',1)->find();   // 上午订单订单
            if($addersw){
                 $order=Db::name('order')->where('order_sn',$addersw['order_sn'])->find();
                 $item['morning']=$order['address'];
                 $item['ids']='订单ID:'.$addersw['id'];
                 $item['types']=$addersw['type'];
              }else{
                
                 $leave=Db::name('leave')->where($where1)->where('user_id',$item['id'])->where('addtime',1)->find();
                 if($leave){
                     $item['morning']="占用";  
                      $item['ids']="";
                      $item['types']="";
                 }else{
                     $item['morning']="-"; 
                      $item['ids']="";
                      $item['types']="";
                 }
             }
             
             $addersws=Db::name('order_exe')->where($where)->where('staff_id',$item['id'])->where('addtime',2)->find();   // 下午的订单
             
             if($addersws){
                  $orderss=Db::name('order')->where('order_sn',$addersws['order_sn'])->find();
                  $item['noon']=$orderss['address']; 
                  $item['idss']='订单ID:'.$addersws['id'];
                  $item['types']= $addersws['type'];
            }else{
                 $leave=Db::name('leave')->where('user_id',$item['id'])->where('addtime',2)->where($where1)->find(); 
                 if($leave){
                      $item['noon']='占用';  
                        $item['idss']="";
                        $item['types']="";
                 }else{
                     $item['noon']='-'; 
                     $item['idss']="";
                     $item['types']="";
                 }
                 
             }
             
             $leade=Db::name('leavesd')
             ->where('staff_id',$item['id'])
             ->where('status','=',1)
             ->where('time','>=',strtotime($get['start_time']))
             ->where('time','<=',strtotime($get['start_time'])+(24 * 60 * 60) - 1)
             ->select();
          
             if(!empty($leade)){
                 foreach ($leade as $v){
                     if($v['addtime']==0){
                          $item['order_count']='请假';
                      }else if($v['addtime']==1){                               
                          $item['order_counts']='请假';
                     }else if($v['addtime']==2){                               
                          $item['order_count']='请假';
                          $item['order_counts']='请假';
                     }else{
                         $item['order_count']='-';
                          $item['order_counts']='-';
                     }
                 }
                
            }
     

         }

         return ['count'=>$order_count , 'lists'=>$order_list];

    }


    public  static function stafflist($get){
        $order_count=1;
        $where=[];
        $where12=[];
        list($start_t, $end_t) = Time::today();

  
       if(isset($get['start_time']) && $get['start_time']!=''){
          $edtime=strtotime($get['start_time'])+(24 * 60 * 60) - 1;
          $where[] = ['autotime','>=',strtotime($get['start_time'])]; 
          $where[] = ['autotime','<=', $edtime];
          $where12[] = ['time','<=',strtotime($get['start_time'])+(24 * 60 * 60) - 1];
          $where12[] = ['time','>=',strtotime($get['start_time'])];
        }
        
        
        $lists =Db::name('order_exe')
                   ->whereTime('autotime', 'today')
                   ->limit(1)
                   ->order('id desc')
                   ->select();
        foreach ($lists as &$item){
            $staff=Db::name('staff')->where('onwork',1)->count();     //保洁是总数
            $sworder=Db::name('order_exe')->where($where)->where('addtime',1)->group('staff_id')->count();    //上午订单
            $xworder=Db::name('order_exe')->where($where)->where('addtime',2)->group('staff_id')->count();    //上午订单
            $qtlent=Db::name('leave')->where($where12)->group('user_id')->count();  //上午请假
            $swlent=Db::name('leave')->where($where12)->where('addtime',1)->group('user_id')->count();  //上午请假
            $xwlent=Db::name('leave')->where($where12)->where('addtime',2)->group('user_id')->count();  //下午请假
            $item['zsnumber']=$sworder+$xworder;
            $item['swnumber']=$staff- $sworder- $swlent;
            $item['xwnumber']=$staff- $xworder-$xwlent;
            $item['cdsnumber']=  $item['swnumber']+  $item['xwnumber'];
            $item['wpd']=Db::name('order_exe')->where($where)->where('staff_id',null)->group('staff_id')->count();

        }         
        return ['count'=>$order_count , 'lists'=>$lists];
    }
        

    

}