<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/4/15
 * Time: 16:27
 */

namespace app\index\logic;
use app\index\model\recuitmodel;
use app\index\model\recuitawardmodel;
use app\index\model\daycheckinfo;
use app\index\model\gworkermodel;

class recuitlogic{
    public function savenewrecuit($arr){
        $t_r=new recuitmodel();
        $rid=$t_r->insinfo($arr);
        return $rid;

    }

    public function getulistbyrecuit($arr){
        $recuitid=$arr['recuituser'];
        $t_r=new recuitmodel();
        $rlist=$t_r->selulistbyrecuit($recuitid);
        return $rlist;
    }

    /*
     * 20200416
     *修改数据结构，直接存储
     */
    public function getallrecuit($arr){
        $t_r=new recuitmodel();
        $rlist=$t_r->selrlist();
//        $t_m= new gworkermodel();
//        foreach($rlist as &$k){
//            $recuitid=$k['recuituserid'];
//            $uid=$k['userid'];
//            $recuitinfo=$t_m->getinfobyid($recuitid);
//            $uinfo=$t_m->getinfobyid($uid);
//            $k['recuitname']=$recuitinfo['wname'];
//            $k['wname']=$uinfo['wname'];
//
//        }

        return $rlist;
    }

    /*
     * 20200417
     */

    public function getnamebytl($arr){
        $tel=$arr['tel'];
        $t_m=new gworkermodel();
        $rec=$t_m->seluinfobytel($tel);
        return $rec;
    }

    /*
     * 20200418
     * 计算
     */
    private function countaward($uid){
        //调用时已确认此用户数据需要重新计算，所以不再判断
        //获取推荐人清单,同进要读取最早的入职日期
        $t_r=new recuitmodel();
        $ulist=$t_r->selulistbyrecuit($uid);
        if (!$ulist){
            //直接返回空，什么也不做
            return 0;
        }
        $firstindate=$ulist[0]['indate'];//获取第一条入职记录，以确认是否在需要处理上个月的记录
        //获取已有的记录
        $t_ra=new recuitawardmodel();
        $recordlist=$t_ra->selinfolistbywid($uid,1);
        //循环计算准备
        $t_ds=new daycheckinfo();
        $edate=date('Y-m-d',strtotime('-2 day'));
        $smonth=date('Y-m',strtotime('-2 day'));//计算的月份
       // echo $edate;
      //  echo $smonth;
        //如果有记录，则获取最后一条记录的月份
        $lastrecmonth='';
        if ($recordlist){
            $lastrecmonth=$recordlist[0]['summonth'];

        }
        //确定开始日期
        //如果确定开始日期，如果已有记录，则开始日期从最后一条记录月份的第一天开始，如果没有记录，则从内推人员的第一个入职日期开始
        if ($lastrecmonth>''){
            $bdate=$lastrecmonth.'-01';
            $commonth=$lastrecmonth;
        }else{
            $bdate=$firstindate;
            $commonth=substr($bdate,0,7);
        }
       // echo $commonth;
        //从确定的开始日期开始，循环计算,循环结束条件，下一个计算月份超出当前月份
        while ($commonth<=$smonth){
            //设置初始变量
            $recuitworks=0;
            $sumcounts=0;
            $tmpbdate=$commonth.'-01';
            $commothendate=date('Y-m-d',strtotime("$tmpbdate +1 month -1 day"));
            //如果超出，则设置实际日期,避免最后两天被统计进去
            if ($commothendate>=$edate){
                $commothendate=$edate;
            }
            //循环遍历每个推荐的员工，获取阶段考勤日
            foreach($ulist as &$kv ){
                $kid=$kv['userid'];
                $indate=$kv['indate'];
                if ($indate>$bdate){
                    $combdate=$indate;
                }else{
                    $combdate=$bdate;
                }

                $sumw=$t_ds->selcountbyidwithdate($kid,$combdate,$commothendate);

                $sumcounts+=$sumw;
                if ($indate<=$commothendate){
                    $recuitworks+=1;
                }
                //echo "kid=$kid,combdate=$combdate,commonthenddate=$commothendate,sumw=$sumw,sumcounts=$sumcounts"."</br>";
            }
         //存储
            //新增或更新组装数据
            $awardinfo['recuituser']=$uid;
            $awardinfo['recuitworks']=$recuitworks;
            $awardinfo['summonth']=$commonth;
            $awardinfo['sumdate']=$commothendate;
            $awardinfo['sumcounts']=$sumcounts;
            $awardinfo['sumaward']=$sumcounts*5;
            if ($commonth==$lastrecmonth){
                //更新
                $lastrecid=$recordlist[0]['id'];
                $t_ra->updateinfobyid($lastrecid,$awardinfo);
            }else{
                //新增
                $rid=$t_ra->insinfo($awardinfo);
            }
          //准备计算下一月
            $bdate=date('Y-m-01',strtotime("$bdate +1 month"));
            $commonth=substr($bdate,0,7);

        };
        return 1;
    }

    /*
     * 20200419
     *获取某人的分数
     * 2020-04-20
     * 返回数据不同了，要返回所有的数据
     */
    public function getawardbyrid($arr){
        //是否有推荐的人,没有直接退出
        $uid=$arr['uid'];
        $t_r=new recuitmodel();
        $ulist=$t_r->selulistbyrecuit($uid);
        if (!$ulist){
            //直接返回空，什么也不做
            return 0;
        }
        //是否有已经到的计算日的数据

        $edate=date('Y-m-d',strtotime('-2 day'));
        $t_ra=new recuitawardmodel();
        $rec=$t_ra->selinfobyiddate($uid,$edate);
        if (!$rec){
            $rec=$this->countaward($uid);
            if ($rec==0){
                return 0;
            }
        }
        //获取所有数据
        $rlist=$t_ra->selinfolistbywid($uid,0);
        //获取这两天的临时数据
        $temprec=$this->counttmpaward($uid);
        array_push($rlist,$temprec);
        return $rlist;
    }

    /*
     * 20200419
     * 计算这两天的临时数据
     */

    private function counttmpaward($uid){
        //获取推荐人清单
        $t_r=new recuitmodel();
        $t_ds=new daycheckinfo();
        $ulist=$t_r->selulistbyrecuit($uid);
        $bdate=date('Y-m-d',strtotime('-1 day'));
        $edate=date('Y-m-d');
        $sumcounts=0;
        $recuitworks=0;
        foreach($ulist as &$kv ){
            $kid=$kv['userid'];
            $indate=$kv['indate'];
            //确定开始日期

                if ($indate>$bdate){
                    $bdate=$indate;
                }

            $sumw=$t_ds->selcountbyidwithdate($kid,$bdate,$edate);
            $sumcounts+=$sumw;
            $recuitworks+=1;
        }

        //新增或更新组装数据
        $awardinfo['recuituser']=$uid;
        $awardinfo['recuitworks']=$recuitworks;
        $awardinfo['summonth']='近两日';
        $awardinfo['somdate']=$edate;
        $awardinfo['sumcounts']=$sumcounts;
        $awardinfo['sumaward']=$sumcounts*5;
        return $awardinfo;
    }

    /*
     * 20200421
     * 轮询处理
     */

    public function oprrecuitaward(){
        // 获取所有关系：
        $t_r=new recuitmodel();
        $ulist=$t_r->selrlist();
        //批量处理
        $edate=date('Y-m-d',strtotime('-2 day'));
        $t_ra=new recuitawardmodel();
        foreach ($ulist as $kv) {
            $uid=$kv['recuituserid'];
            $rec=$t_ra->selinfobyiddate($uid,$edate);
            if (!$rec){
                $this->countaward($uid);
            }
        }

    }
}