<?php

namespace app\api\logic;

use app\common\model\NoticeSetting;
use think\facade\Hook;
use think\Db;
use think\Exception;
use app\api\model\{Orderexe};
class StaffLogic{
    
    //保洁师服务加时间显示列表
    public static function infoaddtim($get){
        $lists=Db::name('order_timeadd')->where('orderid',$get['order_id'])->select();
        foreach ($lists as $key => $level){
          $lists[$key]['create_time']=date("Y-m-d H:i:s",$level['create_time']);;
         
        }
        return $lists;
    }
    
    //员工请假的增加
    public static function   leaveadd($get){
        
       $staff=Db::name('staff')->where('id',$get['staff_id'])->find();
    //    var_dump($post);

       $starttime_h = date('H',strtotime($get['start_time']));
       $enttime_h = date('H',strtotime($get['end_time']));
       //处理0
       $enttime_0 = date('H:i:s',strtotime($get['end_time']));
       if($enttime_0 == '00:00:00'){
        $get['end_time'] = date('Y-m-d',strtotime($get['end_time'])). '00:00:01';
       }

       $days = [];
     
       $get['start_time']=date('Y-m-d H:i:s',$get['start_time']);
      $get['end_time']=date('Y-m-d H:i:s',$get['end_time']);
        
       $period = new \DatePeriod(
            new \DateTime($get['start_time']),
            new \DateInterval('P1D'),
            new \DateTime($get['end_time'])
        );
        $len = 0;
      
        foreach($period as $k=>$v){ 
            $len++;
        }
        foreach($period as $k=>$v){ 
            if($k == 0 && $starttime_h > 12){
                $days[] = ['addtime'=>1,'time'=>$v->format('Y-m-d')];
                continue;
            }

            if($k === ($len - 1) && $enttime_h < 12){
                $days[] = ['addtime'=>1,'time'=>$v->format('Y-m-d')];
                continue;
            } 

            $days[] = ['addtime'=>1,'time'=>$v->format('Y-m-d')];
            $days[] = ['addtime'=>2,'time'=>$v->format('Y-m-d')];
            // echo $v->format('Y-m-d');
        }
        

        if(empty($days)){
            return;
        }

        $data = [];
        foreach($days as $v){
            
            $data[] = [
                'name'=> $staff['name'],
                'phone'=>$staff['mobile'],
                'user_id'=>$staff['id'],
                'status'=>0,
                'time'=>strtotime($v['time']),
                'type'=>1,
                'remark'=>'',
                'addtime'=>$v['addtime'],
                'create_time'=>time()
             ];
        }

        return Db::name('leave')->insertAll($data);

       
       $data=[
          'name'=> $staff['name'],
          'phone'=>$staff['mobile'],
          'user_id'=>$staff['id'],
          'status'=>0,
          'time'=>strtotime($post['end_time']),
          'type'=>1,
          'remark'=>'',
          'addtime'=>$post['level'],
          'create_time'=>time()
       ];

      // return Db::name('leave')->insertGetId($data);
   

    }
    
    //员工订单的
    public static function  order_list($get){
         $today = date('Y-m-d');
        // 计算明天的日期
        $tomorrow = date('Y-m-d', strtotime($today . ' +1 day'));
        $tomorrows = date('Y-m-d', strtotime($today . ' +2 day'));
        $order = new Orderexe();
            $lists = $order->where('staff_id',$get['staff_id'])
            ->where('staff_status',$get['staff_status'])
            ->whereTime('autotime', 'between', ["$today 00:00:00", "$tomorrow 23:59:59"])
            ->order('autotime desc')
            ->select();
            
        foreach ($lists as $list){
              $custom=Db::name('order')->where('order_sn',$list['order_sn'])->find();  
              $list['address']= $custom['address'];
              $list['lat']= $custom['lat'];
              $list['lng']= $custom['lng'];
              $goods=Db::name('goods')->where('id',$custom['goods_id'])->find();
              $list['goods_name']= $goods['name'];
             if(intval(($list['autotime']-time())/86400)>1){
                   $list['jldate']=intval(($list['autotime']-time())/86400); 
              }else{
                $list['jldate']='-';
              }
             
              $adder=Db::name('user_address')->where('telephone',$custom['mobile'])->find();
              if($adder){
                    $admin=Db::name('admin')->where('id',$adder['admin_id'])->find();
                      if($admin){
                         $list['admin_name']=$admin['name'];
                         $list['admin_phone']=$admin['phone'];
                      }else{
                         $list['admin_name']='-';
                         $list['admin_phone']='-'; 
                      }
                }
                
                if($list['addtime']==1){
                     $list['sw']='上午';
                     $list['sw_time']='8:00-12:00';
                }else{
                     $list['sw']='下午';
                     $list['sw_time']='14:00-18:00'; 
                }
                
                //查询其他用户
                $more_where = [
                    ['staff_id','<>',$get['staff_id']],
                    ['order_sn','=',$list['order_sn']],  
                    ['autotime','=',$list['autotime']]
                ];
                $more_users = $order->where($more_where) 
                ->whereTime('autotime', 'between', ["$today 00:00:00", "$tomorrow 23:59:59"])
                ->order('autotime desc')
                ->column('staff_id');
                if(!empty($more_users)){
                     $list['users'] = Db::name('staff')->field('mobile,name,addr')->whereIn('id',$more_users)->select();
                }
                $list['autotime']=date("Y-m-d",$list['autotime']);
                $list['number']=0;  
        }
        return $lists;
    }
}