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

namespace app\index\logic;
use app\index\model\checkinfomodel;
use app\index\model\checkoprmodel;
use app\index\model\pwrelationmodel;
use app\index\model\daycheckinfo;
use app\index\model\ginfomodel;
use app\index\model\wheadmodel;
use app\index\model\gworkermodel;

class checkmanger {
    //20200203 日起停止使用，改从daycheckinfo中读取
    public function getchecklistbyhead($checkhead, $checkdate, $checkshift) {
        $check_table = new checkinfomodel();
        $list = $check_table->getchecklistbyheader($checkhead, $checkdate, $checkshift);
        return $list;

    }

    //20200203 日起使用，从daycheckinfo中读取
    public function getchecklistbyheadwithdate($checkhead, $checkdate) {
        $daycheck_table = new daycheckinfo();
        $list = $daycheck_table->getchecklistbyheadwithdate($checkhead, $checkdate);
        return $list;

    }

    /*
     *   getprjcheckoprbywid  于20200203日停止使用，仅保留代码
     * */

    public function getprjcheckoprbywid($wid, $checkdate, $shift) {
        //获得项目ID
        $pw_tabel = new pwrelationmodel();
        $pid = $pw_tabel->getpidbywid($wid);
        if ($pid > 0) {
            //获取项目下，当天班次所有打分记录
            $opr_t = new checkoprmodel();
            $list = $opr_t->getoprinfobypid($pid, $checkdate, $shift);
            return $list;
        } else {
            //此人无项目
            $list = array();
            return $list;
        }

    }
    //20200203开始使用，按日汇总考勤,参数中去掉了班次参数
    public function getheadcheckoprwithdate($wid, $checkdate) {
        //获得项目ID
        $pw_tabel = new pwrelationmodel();
        $pid = $pw_tabel->getpidbywid($wid);
        if ($pid > 0) {
            //获取项目下，当天班次所有打分记录
            $opr_t = new checkoprmodel();
            $list = $opr_t->getoprinfobypid($pid, $checkdate);
            return $list;
        } else {
            //此人无项目
            $list = array();
            return $list;
        }

    }
    /*
     * 20200403
     * 获取项目经理个人所有的记工记录
     */
    public function getoprinfobyhid($arr) {
        $hid = $arr['hid'];
        $startdate = date('Y-m-d', strtotime('-60 day'));
        $opr_t = new checkoprmodel();
        $list = $opr_t->seloprlistbyhid($hid, $startdate);

        return $list;
    }

    /*
     * 20200206
     * steelxu
     * 按人汇总表的数据,汇人总的天数，总记分
     * 先直接应用daycheckinfo实现
     * 20200325
     * 加身份证号
     */
    public function getsumcheckinfo($query_arr) {
        $month = $query_arr['monthinfo'];
        $year = $query_arr['yearinfo'];
        $pageNum = $query_arr['pageNum'];
        $pageSize = $query_arr['pageSize'];
        $start = ($pageNum - 1) * $pageSize;
        if (strlen($month) == 1) {
            $month = '0' . $month;
        }
        $strmonth = $year . '-' . $month;
        $dc_table = new daycheckinfo();
        //20200421 为统计推荐奖金，启用新方法，
        //$r_list=$dc_table->countcheckbymonth($strmonth,$start,$pageSize);
        //20200421 含 奖励计算的新方法
        $r_list = $dc_table->countcheckwithawardbymonth($strmonth, $start, $pageSize);

        //获取所有工人信息
        //todo 此处在数据量大时可优化，可重写model层查询，只返回id和身份证，这样效率更高，现在暂时不用
        $t_w = new gworkermodel();
        $w_list = $t_w->selshengfengzhenglist();
        $sfzlist = array();
        foreach ($w_list as $k) {
            $id = $k['id'];
            if ($k['shengfengid']) {
                $sfzlist[$id] = $k['shengfengid'];
            } else {
                $sfzlist[$id] = '';
            }

        }

        //获得了总体数据,获取每日数据
        foreach ($r_list as &$v) {
            $wid = $v['wid'];
            $v['sfz'] = $sfzlist[$wid];
            $detail_arr = $dc_table->getcheckinfobywidwithmonth($wid, $strmonth);
            foreach ($detail_arr as $k) {
                $daystr = substr($k['checkdate'], -2);
//                echo $k['checkdate'];
//                echo $daystr;
                $v[$daystr] = $k['dayscore'];
            }
        }
        return $r_list;
    }

    /*
     * 20230215
     * 加上公司的
     */
    public function getsumcompanycheckinfo($query_arr) {
        $month = $query_arr['monthinfo'];
        $year = $query_arr['yearinfo'];
        $pageNum = $query_arr['pageNum'];
        $pageSize = $query_arr['pageSize'];
        $cid = $query_arr['companyid'];
        $start = ($pageNum - 1) * $pageSize;
        if (strlen($month) == 1) {
            $month = '0' . $month;
        }
        $strmonth = $year . '-' . $month;
        $dc_table = new daycheckinfo();
        //20200421 为统计推荐奖金，启用新方法，
        //$r_list=$dc_table->countcheckbymonth($strmonth,$start,$pageSize);
        //20200421 含 奖励计算的新方法
        // $r_list=$dc_table->countcheckwithawardbymonth($strmonth,$start,$pageSize);
        //20230215 加上公司
        $r_list = $dc_table->countcompanycheckwithawardbymonth($strmonth, $start, $pageSize, $cid);
        //获取所有工人信息
        //todo 此处在数据量大时可优化，可重写model层查询，只返回id和身份证，这样效率更高，现在暂时不用
        //身份证还是取全部的，因为人员会流动，无论那种方式都会有问题，最后再看如何优化
        // 问题在于，如果工人数量较多，再和工人身份证做连接，将是a*b的数量级的运算，
        //如果取出来，则是a+b的运算，
        $t_w = new gworkermodel();
        $w_list = $t_w->selshengfengzhenglist();
        // $w_list=$t_w->selcompanyshengfengzhenglist($cid);
        $sfzlist = array();
        foreach ($w_list as $k) {
            $id = $k['id'];
            if ($k['shengfengid']) {
                $sfzlist[$id] = $k['shengfengid'];
            } else {
                $sfzlist[$id] = '';
            }

        }

        //获得了总体数据,获取每日数据
        foreach ($r_list as &$v) {
            $wid = $v['wid'];
            if ($sfzlist[$wid]) {
                $v['sfz'] = $sfzlist[$wid];
                $detail_arr = $dc_table->getcheckinfobywidwithmonth($wid, $strmonth);
                foreach ($detail_arr as $k) {
                    $daystr = substr($k['checkdate'], -2);
//                echo $k['checkdate'];
//                echo $daystr;
                    $v[$daystr] = $k['dayscore'];
                }
            }
        }
        return $r_list;
    }

    /*
     * 20200208
     * steelxu
     * 取消某员工，某班次考勤
     * 20200209 增加对操作记录的处理,在opr 表中增加一条 -1的记录
     * 20200307,增加对撤销时gid的撤销操作
     */
    public function invokecheckbywid($query_arr) {
        $wid = $query_arr['wid'];
        $checkdate = $query_arr['checkdate'];
        $shift = $query_arr['shift'];
        $id = $query_arr['id'];
        $headid = $query_arr['headid'];
        if (array_key_exists('gid', $query_arr)) {
            $gid = $query_arr['gid'];
        } else {
            $t_head = new wheadmodel();
            $gid = $t_head->getgidbywid($wid);
        }
        $dc_table = new daycheckinfo();
        $res = $dc_table->revokecheckshift($checkdate, $wid, $shift, $id);
        $ginfo = new ginfomodel();
        $pid = $ginfo->getpidbygid($gid);
        $opr_arr['pid'] = $pid;
        $opr_arr['checkdate'] = $checkdate;
        $opr_arr['checkorder'] = $shift;
        $opr_arr['headid'] = $headid;
        $opr_arr['isopr'] = 1;
        $opr_arr['gid'] = $gid;
        $opr_arr['wnum'] = -1;
        $opr_arr['oprtime'] = date('Y:m:d H:i:s');
        $oprtable = new checkoprmodel();
        $oprtable->saveOprRecord($opr_arr);

        return $res;
    }

    /*
     * 20200208
     * steelxu
     * 获取班组长某班次的记工记录，
     * 以便班组长取消
     *20200416
     * 取出时只有当时所在的班组才能修改，换了班组则不可以
     *
     */
    public function getheadoprwithshift($query_arr) {
        $headid = $query_arr['wid'];
        $checkdate = $query_arr['checkdate'];
        $shift = $query_arr['shift'];
        //获取班组
        $ghead = new wheadmodel();
        $gid = $ghead->getgidbywid($headid);

        $dc_table = new daycheckinfo();
        $rlist = $dc_table->getlistbyheadwithshift($checkdate, $shift, $headid, $gid);
        return $rlist;
    }

    public function getworkovertime($pagenum, $pagesize) {
        $start = ($pagenum - 1) * $pagesize;
        $t_dayscore = new daycheckinfo();
        $rlist = $t_dayscore->selscorewithprjbyscore($start, $pagesize);
        return $rlist;
    }

    /*
     * 20230215
     * 修改查加班的功能，只能查本公司的
     */
    public function getworkovertimebycid($pagenum, $pagesize, $cid) {
        $start = ($pagenum - 1) * $pagesize;
        $t_dayscore = new daycheckinfo();
        $rlist = $t_dayscore->selscorewithprjbyscorecid($start, $pagesize, $cid);
        return $rlist;
    }
    /**
     * 5天内有记工人员列表
     *
     * @return void
     * @author wj
     * @date 2025-02-18
     */
    public function hasreocdeuserlist($iscount = false, $page = 1, $size = 10) {
        $day_num = 5;
        $m_dc = new daycheckinfo();
        $data = $m_dc->hasreocdeuserlistwithdays($day_num, $iscount, $page, $size);
        return $data;
    }

}