<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/12
 * Time: 21:59
 */

namespace app\index\model;
use think\Model;
use think\Log;

class gworkermodel extends Model {
    protected $table = 't_workerinfo';

    public function getidbytel($tel) {
        $recordlist = $this->where('telno', $tel)->find();
        if ($recordlist) {
            return $recordlist['id'];
        } else {
            return 0;
        }
    }

    //获取所有工人信息，并带项目名称
    public function getallworker($start, $rlen) {
        $strsql = "select a.*,c.pname,c.is_worker ";
        $strsql = $strsql . "from t_workerinfo a left join t_p2wrelation b on a.id=b.wid and b.checkstatus=1 left join t_pinfo c on b.pid=c.id";
        $strsql = $strsql . " limit " . $start . "," . $rlen;

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

    }

    /*
     * 20230214
     * 获取一个企业的项目的所有工人
     */
    public function getallworkerbycid($start, $rlen, $cid) {
        $strsql = "select a.*,c.pname,c.is_worker ";
        $strsql = $strsql . "from t_workerinfo a left join t_p2wrelation b on a.id=b.wid and b.checkstatus=1 left join t_pinfo c on b.pid=c.id";
        $strsql = $strsql . " where c.companyid=" . $cid;
        $strsql = $strsql . " order by a.dayprice,create_date desc";
        $strsql = $strsql . " limit " . $start . "," . $rlen;

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

    /**
     *
     *
     * @param  [type] $start
     * @param  [type] $rlen
     * @param  [type] $cid
     * @return void
     * @author wj
     * @date 2025-02-17
     */
    public function getnotinprojectuserlist($start, $rlen) {
        $strsql = "select a.*,c.pname,c.is_worker ";
        $strsql = $strsql . "from t_workerinfo a left join t_p2wrelation b on a.id=b.wid and b.checkstatus=1 left join t_pinfo c on b.pid=c.id";
        $strsql = $strsql . " where a.isactive=1 and a.cprojectid=3";
        $strsql = $strsql . " order by a.dayprice,create_date desc";
        $strsql = $strsql . " limit " . $start . "," . $rlen;

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

    /*
     * steelxu
     * 20200212
     * 获取指定项目员工
     */
    public function getprjworker($start, $rlen, $pid) {
        $strsql = "select * ";
        $strsql = $strsql . "from t_workerinfo ";
        $strsql = $strsql . " where cprojectid=" . $pid;
        $strsql = $strsql . " order by wname";
        $strsql = $strsql . " limit " . $start . "," . $rlen;

        $workerlist = $this->query($strsql);
        return $workerlist;
    }
    /*
     * 20200303
     * 预检查是否有索引字段重复
     */
    private function precheckindex($info) {
        $where_arr['telno'] = $info['telno'];
        $rec = $this->where($where_arr)->find();
        if ($rec) {
            return false;
        } else {
            return true;
        }
    }
/*
 * 20200303
 * edit
 * 有索引表,增加预检 *
 */
    public function saveworkerinfo($worker) {
        //预检
        $ishave = $this->precheckindex($worker);
        Log::info($ishave);
        if (!$ishave) {
            return -1;
        }
        Log::info($worker);
        $sid = $this->allowField(true)->isUpdate(false)->setAttr('id', null)->save($worker);

        if ($sid == 1) {
            return $this->id;
        } else {
            return $sid;
        }

    }
    /*
     * 20200204
     * steelxu
     * 更新工人的cprojectid,
     * 实现工人指量设置，换项目的功能
     * */
    public function updateprj($pid, $wid) {
        $where_arr['id'] = $wid;
        $update_arr['cprojectid'] = $pid;
        $res = $this->where($where_arr)->update($update_arr);
        return $res;
    }

    /*
     * steelxu
     * 20200211
     * 根据工人ID获取信息
     */
    public function getinfobyid($wid) {
        $where_arr['id'] = $wid;
        $rec = $this->where($where_arr)->find();
        return $rec;
    }

    /*
     * 20200325
     * 获取所有工人的id和身份证
     */
    public function selshengfengzhenglist() {
        $strsql = "select id,shengfengid ";
        $strsql = $strsql . " from t_workerinfo";
        $wlist = $this->query($strsql);
        return $wlist;
    }

    /*
     * 2030215
     * 只返回公司的员工id和身份证
     */
    public function selcompanyshengfengzhenglist($cid) {
        $strsql = "select a.id,a.shengfengid ";
        $strsql = $strsql . " from t_workerinfo a left join t_pinfo b on a.cprojectid=b.id";
        $strsql = $strsql . " where b.companyid=" . $cid;
        $wlist = $this->query($strsql);
        return $wlist;
    }
/*
 * 20200417
 * 根据电话获取用户信息
 *
 */
    public function seluinfobytel($tel) {
        $where_arr['telno'] = $tel;
        $rec = $this->where($where_arr)->find();
        return $rec;
    }

    /*
     * 20200515
     * 根据身份证获取数据
     */
    public function selinfobysfz($sfzid) {
        $where_arr['shengfengid'] = $sfzid;
        $rec = $this->where($where_arr)->find();
        return $rec;

    }

    /*
     * 20210311
     * 根据姓名获取数据
     */
    public function selinfobyname($name) {
        $strsql = "select * from t_workerinfo where wname like '%" . $name . "%' order by id,cprojectid";
        $rlist = $this->query($strsql);
        $rlist = collection($rlist)->toArray();
        return $rlist;
    }

    /*
     * 20210416
     * 获取生日是今天的员工
     */
    public function selworklistbybirthday($birthday) {
        if ($birthday != '0301') {
            $strsql = "select * from t_workerinfo where SUBSTR(shengfengid FROM 11 FOR 4)='" . $birthday . "'";
        } else {
            $strsql = "select * from t_workerinfo where SUBSTR(shengfengid FROM 11 FOR 4)='" . $birthday . "' or SUBSTR(shengfengid FROM 11 FOR 4)='0209'";
        }
        $rlist = $this->query($strsql);
        $rlist = collection($rlist)->toArray();
        return $rlist;

    }

    /*
     * 20210416
     * 更新工人年龄
     */
    public function updwagebyid($id, $wage) {
        $where_arr['id'] = $id;
        $upd_arr['wage'] = $wage;
        $this->where($where_arr)->update($upd_arr);
    }

    /*
     * 20210427
     * 更新工人信息（电话、工种）
     */
    public function updinfobyid($id, $arr) {
        $where_arr['id'] = $id;
        $count = $this->where($where_arr)->update($arr);
        return $count;

    }

    /*
     * 20210725
     * 获取每个工地的人数统计
     */
    public function selwcountbyprj() {
        $strsql = 'select cprojectid, count(*) as pwcount ';
        $strsql = $strsql . ' from t_workerinfo ';
        $strsql = $strsql . " where cprojectid>3";
        $strsql = $strsql . " group by cprojectid";
        $rlist = $this->query($strsql);
        $rlist = collection($rlist)->toArray();
        return $rlist;

    }

    /*
     * 20210807
     * 获取所有cprojectid>3的用户
     */
    public function selinfolistwithprjid() {
        $strsql = 'select * from t_workerinfo where cprojectid>3';
        $rlist = $this->query($strsql);
        $rlist = collection($rlist)->toArray();
        return $rlist;
    }

}