<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/1/27
 * Time: 15:35
 */
namespace app\index\logic;
use app\index\model\daycheckinfo;
use app\index\model\gworkermodel;
use app\index\model\pinfomodel;
use app\index\model\prjlocationmodel;
use app\index\model\pwrelationmodel;
use app\index\model\ginfomodel;
use app\index\model\wheadmodel;
use app\index\model\contactmodel;
use app\index\model\gcmodel;
use app\index\model\transfermodel;

class prjmanger {
    public function getallprj() {
        $t_prj = new pinfomodel();
        $prj_list = $t_prj->getallpinfo();
        return $prj_list;
    }

    /*
     * 20230214
     * 根据 web管理员身份获取接口
     */
    public function getallprjbycid($arr) {
        $cid = $arr['companyid'];
        $t_prj = new pinfomodel();
        $prj_list = $t_prj->sellistbycid($cid);
        return $prj_list;
    }

    public function addnewprj($prj) {
        $t_prj = new pinfomodel();
        $datenow = date('Y-m-d ');
        $prj['create_date'] = $datenow;
        $id = $t_prj->saveprjinfo($prj);
        return $id;
    }

    public function getbyid($id) {
        $t_prj = new pinfomodel();
        $prjinfo = $t_prj->getbyid($id);
        return $prjinfo;
    }

    //批量设置修改项目关系接口
    /*
     * 20200204
     * steelxu
     * 修改用户的对应项目，以批量更改用户项目
     * 特别说明，对班组长的迁移目前不自动处理，不在此方法中默认解决
     */

    public function changerelation($pid, $worker_arr) {
        $newdate = date('Y-m-d ');
        $r_tale = new pwrelationmodel();
        $w_table = new gworkermodel();
        $icount = 0;
        //var_dump($worker_arr);
        foreach ($worker_arr as $k => $v) {
            //组织数据
            $relation['pid'] = $pid;
            $relation['wid'] = $v['wid'];
            $relation['startdate'] = $newdate;
            $relation['checkstatus'] = 1;
            $w_table->updateprj($pid, $v['wid']);
            $rid = $r_tale->addnewrelation($relation);
            if ($rid > 0) {
                $icount++;
            }
        }
        return $icount;

    }
    /*
     * 20200211
     * steelxu
     * 创建班组
     */
    public function creategroup($arr) {
        $g_tabel = new ginfomodel();
        $res = $g_tabel->addnewgroup($arr);
        return $res;
    }

    /*
     * 20200211
     * steelxu
     * 获取项目下所有的班组
     * 20200309修改加合同关系
     * 20200530
     * 增加反回班组长WID
     */
    public function getglist($pid) {
        $g_talbe = new ginfomodel();
        $gheadinfo = new wheadmodel();
        $work_table = new gworkermodel();
        $t_gc = new gcmodel();
        $t_ctct = new contactmodel();

        //获取所有班组
        $res = $g_talbe->getglistbypid($pid);
        foreach ($res as &$gitem) {
            //取班组长名称
            $gid = $gitem['id'];
            $gheader = $gheadinfo->getwidbygid($gid);
            if ($gheader) {
                $wid = $gheader['gwid'];
                $winfo = $work_table->getinfobyid($wid);
                $gitem['wname'] = $winfo['wname'];
                $gitem['gwid'] = $wid;
            } else {
                $gitem['wname'] = '';
            }
            //取合同名称
            $gcinfo = $t_gc->selcinfobygid($gid);
            if ($gcinfo) {
                $cid = $gcinfo['cid'];
                $cinfo = $t_ctct->selcinfobycid($cid);
                $gitem['contactname'] = $cinfo['contactname'];
            } else {
                $gitem['contactname'] = '';
            }
        }
        return $res;
    }

    /*
     * 20200331
     * 更新班组名称，详情
     */
    public function updategroupinfo($arr) {
        $g_tabel = new ginfomodel();
        $res = $g_tabel->updatenamebyid($arr);
        return $res;
    }

    /*
     * 20210228
     * steelxu
     * 将用户离职，放到人才库，prjid=3中
     * 所有的组长权限清零
     */

    public function leavejob($worker_arr) {
        $newdate = date('Y-m-d ');
        $r_tale = new pwrelationmodel();
        $w_table = new gworkermodel();
        $t_gheader = new wheadmodel();
        $icount = 0;

        //var_dump($worker_arr);
        foreach ($worker_arr as $k => $v) {
            //组织数据
            $relation['pid'] = 3;
            $relation['wid'] = $v['wid'];
            $relation['startdate'] = $newdate;
            $relation['checkstatus'] = 1;
            $w_table->updateprj(3, $v['wid']);
            $rid = $r_tale->addnewrelation($relation);
            if ($rid > 0) {
                $icount++;
            }
            //如果是组长去掉组长权限
            $change = $t_gheader->changeghead($v['wid']);
        }
        return $icount;

    }

    /*
     * 20250212
     * 自动离职，增加调出记录
     */

    public function autoleavejob($worker_arr) {
        $newdate = date('Y-m-d ');
        $r_tale = new pwrelationmodel();
        $w_table = new gworkermodel();
        $t_gheader = new wheadmodel();
        $icount = 0;

        //var_dump($worker_arr);
        foreach ($worker_arr as $k => $v) {
            //组织数据
            $relation['pid'] = 3;
            $relation['wid'] = $v['wid'];
            $relation['startdate'] = $newdate;
            $relation['checkstatus'] = 1;
            $w_table->updateprj(3, $v['wid']);
            $rid = $r_tale->addnewrelation($relation);
            if ($rid > 0) {
                $icount++;
            }
            //如果是组长去掉组长权限
            $change = $t_gheader->changeghead($v['wid']);
            //插入调动表记录
            $trans['wname'] = $v['wname'];
            $trans['wid'] = $v['wid'];
            $trans['rectime'] = date('Y-m-d H:i:s');
            $trans['telno'] = $v['telno'];
            $trans['transoutman'] = '两日未记工';
            $trans['transoutid'] = 0;
            $rid = $t_trans->insinfo($trans);
        }
        return $icount;

    }

    /*
     * 20210228
     * steelxu
     * 将用户离职，放到人才库，prjid=3中
     * 所有的组长权限清零
     *并插入调动表中
     */

    public function transferprj($headid, $worker_arr) {
        $newdate = date('Y-m-d ');
        $r_tale = new pwrelationmodel();
        $w_table = new gworkermodel();
        $t_gheader = new wheadmodel();
        $t_trans = new transfermodel();
        $icount = 0;

        //获取名字
        $headinfo = $w_table->getinfobyid($headid);
        $transoutname = $headinfo['wname'];

        //var_dump($worker_arr);
        foreach ($worker_arr as $k => $v) {
            //组织数据
            $relation['pid'] = 3;
            $relation['wid'] = $v['wid'];
            $relation['startdate'] = $newdate;
            $relation['checkstatus'] = 1;
            $w_table->updateprj(3, $v['wid']);
            $rid = $r_tale->addnewrelation($relation);
            if ($rid > 0) {
                $icount++;
            }
            //如果是组长去掉组长权限
            $change = $t_gheader->changeghead($v['wid']);
            //插入调动表记录
            $trans['wname'] = $v['wname'];
            $trans['wid'] = $v['wid'];
            $trans['rectime'] = date('Y-m-d H:i:s');
            $trans['telno'] = $v['telno'];
            $trans['transoutman'] = $transoutname;
            $trans['transoutid'] = $v['transoutid'];
            $rid = $t_trans->insinfo($trans);
        }
        return $icount;

    }

    /*
     * 20210725
     * 每个项目的人员统计
     */
    public function getworkercountbypid() {
        $t_p = new pinfomodel();
        $plist = $t_p->getallpinfo();
        $t_w = new gworkermodel();
        $wlist = $t_w->selwcountbyprj();
        foreach ($wlist as &$pwinfo) {
            $pid = $pwinfo['cprojectid'];
            foreach ($plist as $prjinfo) {
                if ($pid == $prjinfo['id']) {
                    $pwinfo['prjname'] = $prjinfo['pname'];
                }
            }
        }
        return $wlist;
    }

    /*
     * 20210807
     * 七天自动离职
     * edit by steelxu5
     * 改为2天未记工，自动离开项目
     */
    public function autoleavejobwithweek() {
        $t_gw = new gworkermodel();
        $wlist = $t_gw->selinfolistwithprjid();
        $unworkerlist = array();
        $bdate = date('Y-m-d', strtotime('-2 day')); //20250211 改为2天未记工自动离开，原为7天
        $t_d = new daycheckinfo();
        foreach ($wlist as $worker) {
            $wid = $worker['id'];
            $weekcount = $t_d->selscorebyidwithdays($wid, $bdate);
            if ($weekcount == 0) {
                $nworker = array();
                $nworker['wid'] = $wid;
                $nworker['tel'] = $worker['telno'];
                $nworker['wname'] = $worker['wname'];
                array_push($unworkerlist, $nworker);
            }
        }
        if (count($unworkerlist) > 0) {
            $this->autoleavejob($unworkerlist);
        }
    }

    /*
     * 20230302
     */
    public function subnewprjlocation($arr) {
        $t_pl = new prjlocationmodel();
        $rec = $t_pl->insinfo($arr);
        return $rec;
    }

    /*
     * 20230302
     */
    public function getprjlocationinfo($arr) {
        $prjid = $arr['prjid'];
        $t_pl = new prjlocationmodel();
        $rec = $t_pl->selinfobyprjid($prjid);
        return $rec;
    }

    /*
 *
 */

}
