<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/29
 * Time: 0:36
 */

namespace app\index\logic;
use app\index\model\gworkermodel;
use app\index\model\inventrecmodel;
use app\index\model\paydetailmodel;
use app\index\model\pwrelationmodel;
use app\index\model\daycheckinfo;
use app\index\model\daypricerecodemodel;
use app\index\model\pinfomodel;
use app\index\model\transfermodel;
use app\index\model\userlocationmodel;
use app\index\model\userlocationhismodel;
use app\index\model\prjlocationmodel;
use app\index\model\noticemodel;
use app\index\model\noticereadmodel;

class workermanger {
    public function getallworkerlist($pagenum, $pagesize) {
        $start = ($pagenum - 1) * $pagesize;
        $workertable = new gworkermodel();

        $worker_list = $workertable->getallworker($start, $pagesize);
        //var_dump($userinfo);
        return $worker_list;
    }
    /*
     * 20230214
     * 按公司获取所有的项目的工人
     */
    public function getallworkerlistbycid($pagenum, $pagesize, $cid) {
        $start = ($pagenum - 1) * $pagesize;
        $workertable = new gworkermodel();
        //先获取公司清单
        /*
        $t_p=new pinfomodel();
        $plist=$t_p->sellistbycid($cid);
        $strpinfo='';
        foreach($plist as $pinfo){
        $strpinfo=$strpinfo.$pinfo['id'].",";
        }
        if($strpinfo>''){
        $lenstr=strlen($strpinfo);
        $strp=substr($strpinfo,0,-1);
        }
         */
        $worker_list = $workertable->getallworkerbycid($start, $pagesize, $cid);
        //var_dump($userinfo);
        return $worker_list;
    }
    /**
     * 查询已不在公司项目的历史工作人员
     *
     * @return void
     * @author wj
     * @date 2025-02-17
     */
    public function getnotinprojectuserlist($pagenum, $pagesize) {
        $start = ($pagenum - 1) * $pagesize;
        $workertable = new gworkermodel();
        $worker_list = $workertable->getnotinprojectuserlist($start, $pagesize);
        return $worker_list;
    }
    /*
     * 20200212
     * steelxu
     *  获取指定项目的员工
     */
    public function getprjworkerlist($pagenum, $pagesize, $pid) {
        $start = ($pagenum - 1) * $pagesize;
        $workertable = new gworkermodel();

        $worker_list = $workertable->getprjworker($start, $pagesize, $pid);
        //var_dump($userinfo);
        return $worker_list;
    }

    public function saveworker($worker) {
        $t_worker = new gworkermodel();
        $datenow = date('Y-m-d ');
        $t_worker['create_date'] = $datenow;
        $newid = $t_worker->saveworkerinfo($worker);
        //如果重复，直接返回
        if ($newid == -1) {
            return $newid;
        }

        //如果插入正常

        if ($newid > 0) {
            $r_table = new pwrelationmodel();
            $relation['pid'] = $worker['cprojectid'];
            $relation['wid'] = $newid;
            $relation['startdate'] = $datenow;
            $relation['checkstatus'] = 1;
            $rid = $r_table->savenewrelation($relation);
        }

        return $newid;

    }

    /*
     * 20200312
     * 统计各项目当天记工人数，两天前起，指定天数内的出勤的工人数
     * 先处理日期参数
     * 获取所有项目，ID和项目名称
     * 按项目获取出勤统计、分两次获取，一次是当天的，一次是前一阶段的
     * 组装数据
     */

    public function getcheckcountbydate($arr) {
        $daycount = $arr['daycount'];
        $strdc = strval($daycount + 2);
        $strparam = '-' . $strdc . ' day';
        $datenow = date('Y-m-d ');
        $dateend = date('Y-m-d', strtotime('-2 day'));
        $datestart = date('Y-m-d', strtotime($strparam));

        //获取所有项目
        $t_p = new pinfomodel();
        $p_list = $t_p->getallpinfo();
        $t_dc = new daycheckinfo();
        foreach ($p_list as &$v) {
            $pid = $v['id'];
            //获取当天数据
            $w_today = $t_dc->selcinfowithdate($datenow, $datenow, $pid);
            if ($w_today) {
                //  var_dump($w_today);
                $v['todaycount'] = $w_today[0]['workernumber'];
                // echo 'ok<br>';
            } else {
                $v['todaycount'] = 0;
            }
            //获取历史记录
            $w_history = $t_dc->selcinfowithdate($datestart, $dateend, $pid);
            if ($w_history) {

                foreach ($w_history as $k) {

                    $checkdate = strval($k['checkdate']);
                    $v[$checkdate] = $k['workernumber'];
                }
            }
        }
        return $p_list;
    }

    /*
     * 20230214
     * 统计本公司各项目的记工人数，两天前起（不可更改）
     */
    public function getcheckcountbydatecompany($arr) {
        $daycount = $arr['daycount'];
        $cid = $arr['companyid'];
        $strdc = strval($daycount + 2);
        $strparam = '-' . $strdc . ' day';
        $datenow = date('Y-m-d ');
        $dateend = date('Y-m-d', strtotime('-2 day'));
        $datestart = date('Y-m-d', strtotime($strparam));

        //获取本公司项目
        $t_p = new pinfomodel();
        //$p_list=$t_p->getallpinfo();
        $p_list = $t_p->sellistbycid($cid);
        $t_dc = new daycheckinfo();
        foreach ($p_list as &$v) {
            $pid = $v['id'];
            //获取当天数据
            $w_today = $t_dc->selcinfowithdate($datenow, $datenow, $pid);
            if ($w_today) {
                //  var_dump($w_today);
                $v['todaycount'] = $w_today[0]['workernumber'];
                // echo 'ok<br>';
            } else {
                $v['todaycount'] = 0;
            }
            //获取历史记录
            $w_history = $t_dc->selcinfowithdate($datestart, $dateend, $pid);
            if ($w_history) {

                foreach ($w_history as $k) {

                    $checkdate = strval($k['checkdate']);
                    $v[$checkdate] = $k['workernumber'];
                }
            }
        }
        return $p_list;
    }
    /*
     * 20210303
     * 获取待安排调出人员
     */
    public function getuntransworker($arr) {
        $t_trans = new transfermodel();
        $page = isset($arr['pageNum']) && !empty($arr['pageNum']) && is_numeric($arr['pageNum']) ? $arr['pageNum'] : 1;
        $size = isset($arr['pageSize']) && !empty($arr['pageSize']) && is_numeric($arr['pageSize']) ? $arr['pageSize'] : 10;
        $wlist = $t_trans->selinfolistbyopr($page, $size);
        $data = [
            "pageSize" => $size,
            "pageNum" => $page,
            'list' => $wlist,
        ];
        return $data;
    }

    /*
     * 20210303
     * steelxu
     * 将用户由人才库，调入最终库
     * 没有组长权限处理，
     *更改调动表数据
     */

    public function oprtransferlist($prjid, $worker_arr) {
        $newdate = date('Y-m-d ');
        $r_tale = new pwrelationmodel();
        $w_table = new gworkermodel();

        $t_trans = new transfermodel();
        $icount = 0;

        //var_dump($worker_arr);
        foreach ($worker_arr as $k => $v) {
            //组织数据
            $relation['pid'] = $prjid;
            $relation['wid'] = $v['wid'];
            $relation['startdate'] = $newdate;
            $relation['checkstatus'] = 1;
            $w_table->updateprj($prjid, $v['wid']);
            $rid = $r_tale->addnewrelation($relation);
            if ($rid > 0) {
                $icount++;
            }

            //处理调动表
            $t_trans->updoprbywid($v['wid'], $prjid);
        }
        return $icount;

    }

    /*
     * 20210311
     * 根据用户姓名获取
     */
    public function getworkerinfobyname($arr) {
        $wname = $arr['wname'];
        $t_work = new gworkermodel();
        $rlist = $t_work->selinfobyname($wname);
        return $rlist;
    }

    /*
     * 20210416
     * 过生日更新员工年齿
     */
    public function updatewaggbybirthday() {
        $birthday = date('md');
        $t_w = new gworkermodel();
        $wlist = $t_w->selworklistbybirthday($birthday);
        $year = (int) date('Y');
        foreach ($wlist as $worker) {
            $byear = substr($worker['shengfengid'], 6, 4);
            $nage = $year - (int) $byear;
            $t_w->updwagebyid($worker['id'], $nage);
        }
    }

    /*
     * 20210427
     * 修改工人停止
     */
    public function updateworkerinfobyid($arr) {
        $wid = $arr['id'];
        $updarr['telno'] = $arr['telno'];
        $updarr['workclass'] = $arr['workclass'];
        $updarr['dayprice'] = $arr['dayprice'];
        $updarr['wage'] = $arr['wage'];

        $t_work = new gworkermodel();
        $winfo = $t_work->getinfobyid($wid);
        $rcount = $t_work->updinfobyid($wid, $updarr);
        if ($rcount > 0) {
            $m_dr = new daypricerecodemodel();
            $insertarr = [
                'user_id' => $wid,
                'old_dayprice' => $winfo['dayprice'],
                'dayprice' => $arr['dayprice'],
                'createtime' => date("Y-m-d H:i:s"),
                'type' => 1, //修改用户信息
            ];
            $drid = $m_dr->insinfo($insertarr);
        }
        return $rcount;
    }

    /*
     * 20210502
     *
     */
    public function getlocationlist() {
        $t_ul = new userlocationmodel();
        $list = $t_ul->sellist();
        return $list;
    }

    /*
     * 20210430
     * 存储地址
     * 20210510
     * 修改为先保存历史
     * 当前表如果有，就
     */
    public function saveuserlocation($arr) {
        $t_ul = new userlocationmodel();
        $openid = $arr['openid'];
        $arr['createdate'] = date('Y-m-d H:i:s');
        $t_ulh = new userlocationhismodel();
        $t_ulh->insinfo($arr);
        $rinfo = $t_ul->selinfobyopenid($openid);
        if (empty($rinfo)) {
            $rid = $t_ul->insinfo($arr);
            return $rid;
        } else {
            $rid = $rinfo['id'];
            $nid = $t_ul->updinfobyid($rid, $arr);
            return $nid;
        }
    }

    /*
     * 20210511
     */
    public function getlocationgroupprovince() {
        $t_ul = new userlocationmodel();
        $tlist = $t_ul->selgroupby_p();
        return $tlist;
    }

    /*
     * 20210511
     */
    public function getlocationgroupcity() {
        $t_ul = new userlocationmodel();
        $tlist = $t_ul->selgroupby_c();
        return $tlist;
    }

    /*
     * 20210515
     * 获得项目的地址
     */
    public function getprjlocation() {
        $t_pl = new prjlocationmodel();
        $t_p = new pinfomodel();
        $plocationlist = $t_pl->sellist();
        $plist = $t_p->getallpinfo();
        foreach ($plist as $prj) {
            foreach ($plocationlist as &$prjlocation) {
                if ($prjlocation['prjid'] == $prj['id']) {
                    $prjlocation['prjname'] = $prj['pname'];
                    break;
                }
            }
        }
        return $plocationlist;
    }

    /*
     * 2030215
     * 只获得自己公司的项目位置
     */
    public function getmyprjlocation($arr) {
        $cid = $arr['companyid'];
        $t_pl = new prjlocationmodel();
        $t_p = new pinfomodel();
        $plocationlist = $t_pl->sellist();
        $plist = $t_p->sellistbycid($cid);
        $myplist = array();
        foreach ($plist as $prj) {
            foreach ($plocationlist as &$prjlocation) {
                if ($prjlocation['prjid'] == $prj['id']) {
                    $prjlocation['prjname'] = $prj['pname'];
                    array_push($myplist, $prjlocation);
                    break;
                }
            }
        }
        //return $plocationlist;
        return $myplist;
    }

    /*
     * 20210515
     * 按项目统计人群
     */
    public function getworkerlocationcountgprj() {
        $t_ul = new userlocationmodel();
        $pwlist = $t_ul->selgroupbyprj();
        return $pwlist;
    }
    /*
     * 20230215
     * 获取自己公司项目的统计人数
     */
    public function getworkerlocationcountgprjbycid($arr) {
        $cid = $arr['companyid'];
        $t_ul = new userlocationmodel();
        $t_p = new pinfomodel();
        $plist = $t_p->sellistbycid($cid);
        $pwlist = array();
        foreach ($plist as $pinfo) {
            $pid = $pinfo['id'];
            $pwcount = $t_ul->selgroupcountbypid($pid);
            array_push($pwlist, $pwcount);
        }

        return $pwlist;
    }

    /*
     * 20210516
     * 提交公告
     */
    public function savenewnotice($arr) {
        $arr['createdate'] = date('Y-m-d H:i:s');
        $t_n = new noticemodel();
        $id = $t_n->insinfo($arr);
        return $id;

    }

    /*
     * 20210517
     *
     */
    public function getallnotice() {
        $t_n = new noticemodel();
        $rlist = $t_n->selalllist();
        return $rlist;
    }

    /*
     * 20230215
     */
    public function getnoticebycid($arr) {
        $cid = $arr['companyid'];
        $t_n = new noticemodel();
        $rlist = $t_n->sellistbycid($cid);
        return $rlist;
    }

    /*
     * 20210517
     * 获得最新消息
     */
    public function getnewnotice($arr) {
        $t_n = new noticemodel();
        $wid = $arr['wid'];
        $t_w = new gworkermodel();
        $winfo = $t_w->getinfobyid($wid);
        $pid = $winfo['cprojectid'];
        $t_p = new pinfomodel();
        $pinfo = $t_p->getbyid($pid);
        $cid = $pinfo['companyid'];
        $res_info = array();
        if (!$cid) {
            return $res_info;
        }

        $rlist = $t_n->selnewmsgbycid($cid);
        $t_nr = new noticereadmodel();
        $readinfo = $t_nr->selnewinfobywid($wid);

        if (!empty($rlist)) {
            if (empty($readinfo)) {
                $res_info = $rlist[0];
            } else {
                if ($readinfo[0]['readnoticeid'] < $rlist[0]['id']) {
                    $res_info = $rlist[0];
                }
            }
        }

        return $res_info;
    }

    /*
     * 20210517
     */
    public function updatenoticereadstatus($arr) {
        $readinfo['wid'] = $arr['wid'];
        $update = true;
        if (array_key_exists('noticeid', $arr)) {
            $readinfo['readnoticeid'] = $arr['noticeid'];
        } else {
            $t_n = new noticemodel();
            $rlist = $t_n->selnewmsg();
            if (!empty($rlist)) {
                $readinfo['readnoticeid'] = $rlist[0]['id'];
            } else {
                $update = false;
            }

        }
        $readinfo['readtime'] = date("Y-m-d H:i:s");
        if ($update) {
            $t_nr = new noticereadmodel();
            $rid = $t_nr->insinfo($readinfo);
        } else {
            $rid = 0;
        }

        return $rid;

    }

    /*
     * 20210517
     *
     */
    public function getpaylistbyuplogid($arr) {
        $logid = $arr['logid'];
        $t_p = new paydetailmodel();
        $list = $t_p->sellistbyuplogid($logid);
        return $list;
    }

    /*
     * 20230130
     * 从水猫那里增加工人
     */
    public function savenewworkerfromwatercat($worker) {
        $t_worker = new gworkermodel();
        $datenow = date('Y-m-d ');
        $worker['create_date'] = $datenow;
        $worker['cprojectid'] = 1;
        if (isset($arr['prjtelno']) || !empty($arr['prjtelno'])) {
            $worker['workclass'] = $arr['prjtelno'];
        }
        $newid = $t_worker->saveworkerinfo($worker);
        //如果重复，直接返回
        if ($newid == -1) {
            return $newid;
        }

        //如果插入正常
        //项目id设置为1
        if ($newid > 0) {
            $r_table = new pwrelationmodel();
            $relation['pid'] = $worker['cprojectid'];
            $relation['wid'] = $newid;
            $relation['startdate'] = $datenow;
            $relation['checkstatus'] = 1;
            $rid = $r_table->savenewrelation($relation);
        }

        return $newid;
    }

    /*
     * 20230126
     */

    public function getworkerinfobytel($arr) {
        $tel = $arr['tel'];
        $t_m = new gworkermodel();
        $rec = $t_m->seluinfobytel($tel);
        $rworker = array();
        if ($rec) {
            $rworker['wid'] = $rec['id'];
            $rworker['wage'] = $rec['wage'];
            $rworker['wname'] = $rec['wname'];
            $rworker['workclass'] = $rec['workclass'];
        }

        return $rworker;
    }

    /*
     * 20230131
     */
    public function addnewinvent($arr) {
        $t_i = new inventrecmodel();
        $arr['inventdate'] = date('Y-m-d');
        $rid = $t_i->insinfo($arr);
        return $rid;

    }

    /*
     * 20230131
     */
    public function getinventinfobywid($arr) {
        $t_i = new inventrecmodel();
        $wid = $arr['wid'];
        $rec = $t_i->selinfobywid($wid);
        return $rec;
    }

    /*
     * 20230131
     */
    public function updateprjbyinvent($arr) {
        $newdate = date('Y-m-d ');
        $pid = $arr['pid'];
        $r_tale = new pwrelationmodel();
        $w_table = new gworkermodel();
        $i_table = new inventrecmodel();
        //检验GPS
        $iinfo = $i_table->selinfobyid($arr['id']);
        $p_lng = $iinfo['prj_lng'];
        $p_lat = $iinfo['prj_lat'];
        $m_lat = $arr['my_lat'];
        $m_lng = $arr['my_lng'];

        if ((abs($p_lng - $m_lng) > 0.1) || (abs($p_lat - $m_lat) > 0.1)) {
            return 0;
        }
        $icount = 0;
        //var_dump($worker_arr);

        //组织数据
        $relation['pid'] = $pid;
        $relation['wid'] = $arr['wid'];
        $relation['startdate'] = $newdate;
        $relation['checkstatus'] = 1;
        $w_table->updateprj($pid, $arr['wid']);
        $rid = $r_tale->addnewrelation($relation);
        //更新inventrec

        if ($rid > 0) {
            $i_table->updateacceptbyid($arr['id']);
            $icount++;
        }

        return $icount;

    }

}