<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/13
 * Time: 15:54
 */
namespace app\index\model;
use think\Model;

class pwrelationmodel extends Model{
    protected $table='t_p2wrelation';

    public function getworkerlistbypid($pid){
        $where_arr['pid']=$pid;
        $where_arr['checkstatus']=1;
        $workerlist=$this->where($where_arr)->select();
        return $workerlist;
    }
/*
 * 20200416
 * 修改：原有的不能发现在其它工地已记工的员工，导致重复记录
 * 修改为联表查询，相关员工的记工记录
 *todo 在人员再增加时，联表查询会有问题，此功能要取消，改为从上一级查询
 */
    public function getworkerunlist($pid,$checkdate,$shift){
//        $strsql="select a.*,b.wname,b.workclass,b.telno,b.wage, 5 as setscore,0 as mcheck from t_p2wrelation a left join t_workerinfo b on a.wid=b.id ";
//
//        $strsql=$strsql." where a.pid = ".$pid." and a.checkstatus=1 and a.wid not in ";
//        if ($shift==1){
//            $strsql=$strsql."(select wid from t_daycheckinfo ";
//            $strsql=$strsql." where pid=".$pid."  and checkdate='".$checkdate."' and score_1>0)";
//        }else{
//            $strsql=$strsql."(select wid from t_daycheckinfo ";
//            $strsql=$strsql." where pid=".$pid."  and checkdate='".$checkdate."' and score_2>0)";
//        }
        $strsql="select a.*,b.wname,b.workclass,b.telno,b.wage, b.dayprice,5 as setscore,0 as mcheck from t_p2wrelation a left join t_workerinfo b on a.wid=b.id ";
        $strsql=$strsql." left join t_daycheckinfo c on a.wid=c.wid and c.checkdate='".$checkdate."'";
        $strsql=$strsql." where a.pid = ".$pid." and a.checkstatus=1 and a.startdate<='".$checkdate."' and ";
        if ($shift==1){
            $strsql=$strsql." (c.score_1 is null or c.score_1=0)";
        }else{
            $strsql=$strsql." (c.score_2 is null or c.score_2=0)";
        }

        $strsql=$strsql." order by workclass,wname";



        $workerlist=$this->query($strsql);
        return $workerlist;
    }
  /*
   * 20200603
   * 补考勤用新的，排除掉进入项目日期必须在考勤日期之前
   */
    public function selunwlistforupdate($pid,$checkdate,$shift){

        $strsql="select a.*,b.wname,b.workclass,b.telno,b.wage, b.dayprice,5 as setscore,0 as mcheck from t_p2wrelation a left join t_workerinfo b on a.wid=b.id ";
        $strsql=$strsql." left join t_daycheckinfo c on a.wid=c.wid and c.checkdate='".$checkdate."'";
        $strsql=$strsql." where a.pid = ".$pid." and a.checkstatus=1  and ";
        if ($shift==1){
            $strsql=$strsql." (c.score_1 is null or c.score_1=0)";
        }else{
            $strsql=$strsql." (c.score_2 is null or c.score_2=0)";
        }

        $strsql=$strsql." order by workclass,wname";



        $workerlist=$this->query($strsql);
        return $workerlist;
    }




    public function savenewrelation($relation){
        $id=$this->save($relation);
        return $id;
    }

    public function getpidbywid($wid){
        $where_arr['wid']=$wid;
        $where_arr['checkstatus']=1;
        $pwinfo=$this->where($where_arr)->find();
        return $pwinfo['pid'];
    }

    //取消某用户以前的历史关系，将checkstatus set=0
    private  function cancelpwhistory($wid,$date){
        $where_arr['wid']=$wid;
        $where_arr['checkstatus']=1;
        $update_arr['checkstatus']=0;
        $update_arr['enddate']=$date;
        $this->where($where_arr)->update($update_arr);
        return 1;
    }

    public function addnewrelation($relation_arr){
        //先取消原有的关系
        $this->cancelpwhistory($relation_arr['wid'],$relation_arr['startdate']);

        $this->isUpdate(false)->save($relation_arr);
        return 1;
    }
}