<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/2/3
 * Time: 1:45
 */

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

class daycheckinfo extends Model {
    protected $table = 't_daycheckinfo';

    //  是否存在相庆的记录
    /*
     * 20200416
     * 修改
     * 因为更换工地当天，存在误操作
     * 因在源头解决，所以此修改取消
     */
    public function getinfobywid($pid, $checkdate, $wid) {
        $where_arr['wid'] = $wid;
        //$where_arr['pid']=$pid;//20200416,取消此条件，同一个，同一天只能有一个记录，这样可能导致工地记录不准确，
        $where_arr['checkdate'] = $checkdate;
        $rec = $this->where($where_arr)->find();
        return $rec;
    }

    //新增
    public function addnewcheck($checkinfo) {
        //判断是否存在
        $pid = $checkinfo['pid'];
        $checkdate = $checkinfo['checkdate'];
        $wid = $checkinfo['wid'];

        $rec = $this->getinfobywid($pid, $checkdate, $wid);

        if (empty($rec)) {

            $this->isUpdate(false)->setAttr('id', null)->save($checkinfo);
            $rid = $this->id;

        } else {
            $rid = $rec['id'];
            $this->updatebycheck($checkinfo, $rid);

        }
        $this->addtwoshift($rid);
        return $rid;

    }

    public function updatebycheck($checkinfo, $id) {
        $where_arr['id'] = $id;
        $rid = $this->where($where_arr)->update($checkinfo);
        return $rid;
    }

    private function addtwoshift($id) {
        $stradd = 'update t_daycheckinfo ';
        $stradd = $stradd . " set dayscore=if(isnull(score_1),0,score_1)+if(isnull(score_2),0,score_2)";
        $stradd = $stradd . " where id=" . $id;
        $this->execute($stradd);

    }

    public function getusercheckinfobywid($wid) {
        $where_arr['wid'] = $wid;
        $list = $this->where($where_arr)->order('checkdate')->select();
        return $list;
    }
    /*
     * 20200216
     * steelxu
     * 按月，按人查询的端口
     */
    public function getusercheckinfobywidwithmonth($wid, $strmonth) {
        $strsql = "select *";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where wid=" . $wid . " and checkdate like '" . $strmonth . "%'";
        $strsql = $strsql . " order by checkdate";
        $list = $this->query($strsql);
        return $list;
    }

    public function getchecklistbyheadwithdate($headid, $checkdate) {
        $strsql = "select *";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where checkdate='" . $checkdate . "' and ((headid_1=" . $headid . ") or (headid_2=" . $headid . "))";
        $rlist = $this->query($strsql);
        return $rlist;

    }

    /*
     * 20200206
     * steelxu
     * 汇总统计记工数据
     * param $formatmoth 是格式化的月份，格式化工作在上一级完成
     * 20200401
     * 修改，测试项目记录的不导出
     */
    public function countcheckbymonth($formatmonth, $start, $len) {
        $strsql = "select wid,wname,count(dayscore) as daycount,sum(dayscore) as monthsum ";
        $strsql = $strsql . " from t_daycheckinfo ";
        $strsql = $strsql . " where checkdate like '" . $formatmonth . "%' and pid>3 and dayscore>0"; //
        $strsql = $strsql . " group by wid";
        if ($len > 0) {
            //导出到execl时，传入 $len参数为0，不加此行，导出所有数据
            $strsql = $strsql . " limit " . $start . "," . $len;
        }
        $rlist = $this->query($strsql);
        return $rlist;
    }
    /*
     * 20200421
     * 在原方法上，增加返回的推荐奖励数据的一个方法
     * 从架构上而言，这是一种临时的省事的做法，
     * 再有新的奖励，就必须创建一个奖励类来处理业务逻辑，再合并到输出中
     */
    public function countcheckwithawardbymonth($formatmonth, $start, $len) {
        $strsql = "select a.wid,a.wname,count(a.dayscore) as daycount,sum(a.dayscore) as monthsum,b.sumaward";
        $strsql = $strsql . " from t_daycheckinfo a left join t_recuitaward b on a.wid=b.recuituser and b.summonth='" . $formatmonth . "'";
        $strsql = $strsql . " where checkdate like '" . $formatmonth . "%' and pid>3 and dayscore>0"; //
        $strsql = $strsql . " group by wid";
        if ($len > 0) {
            //导出到execl时，传入 $len参数为0，不加此行，导出所有数据
            $strsql = $strsql . " limit " . $start . "," . $len;
        }
        $rlist = $this->query($strsql);
        return $rlist;
    }

    /*
     * 20230215
     * 在原有的方式上，加上一个公司id来处理
     */
    public function countcompanycheckwithawardbymonth($formatmonth, $start, $len, $cid) {
        $strsql = "select a.wid,a.wname,count(a.dayscore) as daycount,sum(a.dayscore) as monthsum,b.sumaward";
        $strsql = $strsql . " from t_daycheckinfo a left join t_recuitaward b on a.wid=b.recuituser and b.summonth='" . $formatmonth . "' left join t_pinfo c on a.pid=c.id";
        $strsql = $strsql . " where checkdate like '" . $formatmonth . "%' and c.companyid=" . $cid . " and dayscore>0"; //
        $strsql = $strsql . " group by wid";
        if ($len > 0) {
            //导出到execl时，传入 $len参数为0，不加此行，导出所有数据
            $strsql = $strsql . " limit " . $start . "," . $len;
        }
        $rlist = $this->query($strsql);
        return $rlist;
    }

    /*
     * 20200208
     * steelxu
     * 获取某员工某月的记工记录
     */
    public function getcheckinfobywidwithmonth($wid, $strmonth) {
        $strsql = "select * ";
        $strsql = $strsql . " from t_daycheckinfo ";
        $strsql = $strsql . " where wid=" . $wid . " and checkdate like '" . $strmonth . "%' and pid>3"; //
        $strsql = $strsql . " order by checkdate ";
        $rlist = $this->query($strsql);
        return $rlist;
    }

    /*
     * 20200208
     * steelxu
     * 取消某员工某班次的记录
     * 本函数未更新操作记录
     */
    public function revokecheckshift($checkdate, $wid, $shift, $id) {
        $strsql = "update t_daycheckinfo";
        $strsql = $strsql . " set score_" . $shift . "=0,headid_" . $shift . "=0,gid_" . $shift . "=0";
        $strsql = $strsql . " where checkdate='" . $checkdate . "' and wid=" . $wid;
        $res = $this->execute($strsql);
        $this->addtwoshift($id);
        return $res;
    }

    /*
     * 20200208 获取某公司某班组长记工的记录
     * steelxu
     * 解决班组长可以撤销操作记录的功能
     */
    public function getlistbyheadwithshift($checkdate, $shift, $headid, $gid) {
        $strsql = "select id,wid,wname,checkdate,score_" . $shift . " as score";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where checkdate='" . $checkdate . "' and headid_" . $shift . "=" . $headid . " and gid_" . $shift . "=" . $gid;
        $rlist = $this->query($strsql);

        return $rlist;

    }

    /*
     * 20200215
     * steelxu
     * 按月给工人汇总工分
     */
    public function totalcheckbymonthwithwid($wid) {
        $strsql = "select wid,wname,left(checkdate,7) as strmonth,count(*) as daycount,sum(dayscore) as monthsum";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where wid=" . $wid;
        $strsql = $strsql . " group by left(checkdate,7)";
        $strsql = $strsql . " order by left(checkdate,7) desc";
        $rlist = $this->query($strsql);
        return $rlist;
    }

    /*
     * 20200307
     * 做一个查加班的记录
     * 此记录是单独记录，但不是
     *
     */

    public function selscorewithprjbyscore($start, $len) {
        $strsql = "select a.wid,a.wname,a.checkdate,a.score_1,b.wname as mname_1,a.score_2,c.wname as mname_2,d.pname as prjname ";
        $strsql = $strsql . " from t_daycheckinfo a INNER JOIN t_workerinfo b on a.headid_1=b.id INNER JOIN t_workerinfo c on a.headid_2=c.id INNER JOIN t_pinfo d on a.pid=d.id";
        $strsql = $strsql . " where score_1>5 or score_2>5 ";
        $strsql = $strsql . " order by a.pid,a.wid";
        $strsql = $strsql . " limit $start,$len";

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

    }

    /*
     * 20230215
     * 修改查加班的记录的功能 ，只能查自己公司的
     */
    public function selscorewithprjbyscorecid($start, $len, $cid) {
        $strsql = "select a.wid,a.wname,a.checkdate,a.score_1,b.wname as mname_1,a.score_2,c.wname as mname_2,d.pname as prjname ";
        $strsql = $strsql . " from t_daycheckinfo a INNER JOIN t_workerinfo b on a.headid_1=b.id INNER JOIN t_workerinfo c on a.headid_2=c.id INNER JOIN t_pinfo d on a.pid=d.id";
        $strsql = $strsql . " where (a.score_1>5 or a.score_2>5) and d.companyid=" . $cid;
        $strsql = $strsql . " order by a.pid,a.wid";
        $strsql = $strsql . " limit $start,$len";

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

    }

    /*
     * 20200308 完成按班组统计的数据
     */
    public function selsuminfobygid($gid, $strmonth) {
        // $datenow=date('Y-m-d');
        $strsql = "select checkdate,sum(case when gid_1=$gid then score_1 else 0 end) as d_score_1,sum(case  when gid_2=$gid then score_2 else 0 end) as d_score_2";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where checkdate like '$strmonth%'";
        $strsql = $strsql . " group by checkdate";

//        echo $strsql;
//        echo '<br>';
        $rlist = $this->query($strsql);
        return $rlist;

    }

    /*
     * 20200312
     * 按项目，起始日期，统计出勤记录
     *
     */
    public function selcinfowithdate($startday, $endday, $pid) {
        $strsql = "select checkdate,pid,count(*) as workernumber ";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where checkdate>='" . $startday . "' and checkdate<='" . $endday . "' and pid=" . $pid;
        $strsql = $strsql . " group by checkdate";

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

    /*
     * 20200315
     * 实现按班组按人的统计
     */
    public function selworksumbygidwithdate($gid, $strmonth) {
        // $datenow=date('Y-m-d');
        $strsql = "select checkdate,wid,wname,sum(case when gid_1=$gid then score_1 else 0 end) as d_score_1,sum(case  when gid_2=$gid then score_2 else 0 end) as d_score_2";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where checkdate like '$strmonth%'";
        $strsql = $strsql . " group by checkdate,wid";
        $strsql = $strsql . " having d_score_1>0 or d_score_2>0";
//        echo $strsql;
//        echo '<br>';
        $rlist = $this->query($strsql);
        return $rlist;

    }

    /*
     * 20200419
     * 获取某员工某一时间段的工作记录
     *
     */
    public function selcountbyidwithdate($id, $bdate, $edate) {
        $strsql = "select wid,checkdate, sum(dayscore) as sumds";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where wid=$id and checkdate>='" . $bdate . "' and checkdate<='" . $edate . "'";
        $strsql = $strsql . " group by checkdate";
        $strsql = $strsql . " having sumds>=10";

        $rlist = $this->query($strsql);
        if ($rlist) {
            $rlist = collection($rlist)->toArray();
            return count($rlist);
        } else {
            return 0;
        }
    }

    /*
     * 20200729
     * 根据GID获取已取班次已经记工的人数
     */
    public function selcountbygid($gid, $checkdate, $shift) {
        $strsql = "select count(id) as checkcount";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where gid_$shift=$gid and checkdate='" . $checkdate . "' and score_$shift>0";
        $count = $this->query($strsql);
        $count = collection($count)->toArray();
        return $count;
    }

    /*
     * 20210807
     */
    public function selscorebyidwithdays($id, $bdate) {
        $strsql = "select wid,checkdate, sum(dayscore) as sumds";
        $strsql = $strsql . " from t_daycheckinfo";
        $strsql = $strsql . " where wid=$id and checkdate>='" . $bdate . "'";

        $rlist = $this->query($strsql);
        if ($rlist) {
            $rlist = collection($rlist)->toArray();
            return count($rlist);
        } else {
            return 0;
        }
    }

    /*
     * 20230215
     */
    public function selinfobyid($id) {
        $where['id'] = $id;
        $rec = $this->where($where)->find();
        return $rec;

    }
    /**
     * N天内有记工人员的清单
     *
     * @return void
     * @author wj
     * @date 2025-02-17
     */
    public function hasreocdeuserlistwithdays($day_num, $iscount = false, $page = 1, $size = 10) {
        $index = ($page - 1) * $size;
        $day = date('Y-m-d', strtotime('-' . $day_num . ' day'));
        $today = date('Y-m-d');
        $sql1 = "select max(dc_id) from (select dc.id as dc_id,dc.wid,p.id,w.wname,w.shengfengid,w.telno,p.pname from t_daycheckinfo as dc join t_workerinfo as w on w.id=dc.wid join t_pinfo as p on dc.pid = p.id where checkdate BETWEEN '" . $day . "' and '" . $today . "' ORDER BY dc.checkdate desc) as tab
            GROUP BY wname,pname";
        if ($iscount) {
            $sql = "
            select count(dc.id) as count from t_daycheckinfo as dc join t_workerinfo as w on w.id=dc.wid join t_pinfo as p on dc.pid = p.id where checkdate BETWEEN '" . $day . "' and '" . $today . "' and dc.id in (" . $sql1 . ")";
            $count = $this->query($sql);
            $data = $count[0]['count'];
        } else {
            $sql = "
            select dc.id as dc_id,dc.wid,dc.checkdate,p.id,w.wname,w.shengfengid,w.telno,p.pname from t_daycheckinfo as dc join t_workerinfo as w on w.id=dc.wid join t_pinfo as p on dc.pid = p.id where checkdate BETWEEN '" . $day . "' and '" . $today . "' and dc.id in ("
                . $sql1 .
                ") ORDER BY dc.checkdate desc limit " . $index . "," . $size;
            $list = $this->query($sql);
            if ($list) {
                $data = collection($list)->toArray();
            } else {
                $data = [];
            }
        }
        return $data;
    }

}
