<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/3/8
 * Time: 16:54
 */

namespace app\index\logic;
use app\index\model\gworkermodel;
use app\index\model\pinfomodel;
use app\index\model\ginfomodel;
use app\index\model\contactmodel;
use app\index\model\gcmodel;
use app\index\model\daycheckinfo;
use app\index\model\recuitmodel;

class contactlogic{
    /*
     * 20200308 存储新合同
     */
    public function savecontact($arr){
        $t_contact=new contactmodel();
        $res=$t_contact->insinfo($arr);
        return $res;
    }

    /*
 * 20200308 获取合同清单
 */
    public function getclistbypid($arr){
        $pid=$arr['pid'];
        $t_contact=new contactmodel();
        $res=$t_contact->selclistbypid($pid);
        return $res;
    }

    /*
     * 20200320
     * 获取所有合同清单，只为导出服务，该方案中去年验证合同
     */

    public function getclist(){
        $t_contact=new contactmodel();
        $clist=$t_contact->selectclistbyacitve();
        return $clist;
    }
    /*
     * 20230215
     * 获取公司的合同列表
     */
    public function getcompanyclist($cid){
        $t_contact=new contactmodel();
        $clist=$t_contact->sellistbycid($cid);
        return $clist;
    }

    /*
     * 20200309
     * 存入合同关系
     */

    public function savegcrelation($gcinfo){
        $t_gc=new gcmodel();
        $newid=$t_gc->insinfo($gcinfo);
        return $newid;

    }
/*
 * 20200310
 * 按项目获取合同及各合同的汇总记分
 * 当前先实现返回合同，后期加按合同汇总分
 */
    public function getctotalbypid($arr){
        //预留翻页,用$arr可实现controller层不受影响
        $pid=$arr['pid'];
        $t_contact=new contactmodel();
        $res=$t_contact->selclistbypid($pid);

        //todo 条件成熟后 加上所有项目的总分
        return $res;
    }
   /*
    * 20200310
    * 获取合同下的班组考勤
    *
    */

    public function getgroupsumbycid($arr){
        $cid=$arr['cid'];
        $month=$arr['monthinfo'];
        $year=$arr['yearinfo'];
        if (strlen($month)==1){
            $month='0'.$month;
        }
        $strmonth=$year.'-'.$month;
        $t_gc=new gcmodel();
        $t_ginfo=new ginfomodel();
        $t_ds=new daycheckinfo();
        //获取班组清单
        $g_list=$t_gc->selgidbycid($cid);

        foreach($g_list as &$v){
            //获取班组名称
            $gid=$v['gid'];
            $g_info=$t_ginfo->selginfobygid($gid);
            $v['gname']=$g_info['gname'];
            //获取月度计分详情
            $score_list=$t_ds->selsuminfobygid($gid,$strmonth);
           if ($score_list) {
               $summonth = 0;
               foreach ($score_list as $k) {
                  // $day_str = strval(substr(strval($k['checkdate']), -2));
                   $int_score = intval($k['d_score_1']) + intval($k['d_score_2']);

                   if ($int_score > 0) {
                     //  $v[$day_str] = $int_score;
                       $v[$k['checkdate']] = $int_score;
                   }
                   $summonth += $int_score;
               }
               $v['summonth'] = $summonth;
               // print_r($v);
           }
        }
       // echo json_encode($g_list);
        return $g_list;
    }

    /*
     * 20200315
     * 获取同一个合同下班组下所有工人的考勤，
     * 另外还可以按个人对考勤一样，给一个分组的考勤
     * 20200818 将处理过程改到
     */

    public function getworkersumbycid($arr){
        //准备
        $cid=$arr['cid'];
        $month=$arr['monthinfo'];
        $year=$arr['yearinfo'];
        if (strlen($month)==1){
            $month='0'.$month;
        }
        $strmonth=$year.'-'.$month;
        $t_gc=new gcmodel();
        $t_ginfo=new ginfomodel();
        $t_ds=new daycheckinfo();

        $t_re=new recuitmodel();
        //取出ulist 存储所有的被推荐人，减少每一个用户都要访问数据表的问题，在单独一条记录低于就可以
        $ulist=$t_re->seluserlist();
        $recuituserlist=array();
        foreach($ulist as $kv){
            array_push($recuituserlist,$kv['userid']);
        }
        $gwlist=array();

        //先获取班组
        $g_list=$t_gc->selgidbycid($cid);
        foreach($g_list as &$v) {
            //按班组获得员工记分清单
            $gid=$v['gid'];
            $g_info=$t_ginfo->selginfobygid($gid);
            $v['gname']=$g_info['gname'];
            $v['wid']='g'.strval($gid);
            //再按班组组装员工数据
            $workerscore_list=$t_ds->selworksumbygidwithdate($gid,$strmonth);

            $newscorelist=$this->countgroupdaysum($workerscore_list);

            //获取班组汇总数据
            //$score_list=$t_ds->selsuminfobygid($gid,$strmonth);
            //20200823 用新方法，不访问数据表，最后再合并
            $score_list=$this->countgroupdaysum($workerscore_list);
            if($workerscore_list){
                //如果该项目日期内有员工记录，则建立员工记录
                $summonth = 0;
                foreach ($score_list as $k) {
                    // $day_str = strval(substr(strval($k['checkdate']), -2));
                    $int_score = intval($k['d_score_1']) + intval($k['d_score_2']);

                    if ($int_score > 0) {
                        //  $v[$day_str] = $int_score;
                        $v[$k['checkdate']] = $int_score;
                    }
                    $summonth += $int_score;
                }
                $v['summonth'] = $summonth;
                //以上处理班组总数据
                //以下处理班组个人数据
                $opredwlist=$this->opreworkerlist($workerscore_list,$recuituserlist);
                $v['detail']=$opredwlist;

//                }
            }else{
                //没有员工被记工，则返回一条记录，只有组名
                $v['summonth']=0;
                $v['detail']=array();
            }

        }
        return $g_list;
    }
    /*
     * 20200823
     * 根据问题，推动内部获取分组的相关数据
     *
     */
    private function countgroupdaysum($workerlist){
        $wlist=array();
        $datelist=array();
        foreach($workerlist as $kv){
            $cdate=$kv['checkdate'];
            if (in_array($cdate,$datelist)){
                //已存在，取得位置
                $dateindex=array_flip($datelist);
                $dindex=$dateindex[$cdate];
                $wlist[$dindex]['d_score_1']+=$kv['d_score_1'];
                $wlist[$dindex]['d_score_2']+=$kv['d_score_2'];
            }else{
                //不存在，创造一个新元素，同步插入
                $nitem=array();
                $nitem['checkdate']=$cdate;
                $nitem['d_score_1']=$kv['d_score_1'];
                $nitem['d_score_2']=$kv['d_score_2'];
                array_push($wlist,$nitem);
                array_push($datelist,$cdate);
            }
        }
        return $wlist;

    }

    /*
     * edit
     * 20200503
     * 增加，如果是被推荐的工人，则每天到10分累加5元的成本
     * 20200818 被推荐的工人处理到上一级处理，一次处理只访问一次数据表，节约时间
     */

    private function opreworkerlist($arr,$ulist){
        $wlist=array();
        //初始化


        foreach($arr as $k){
            $wname=$k['wname'];

                $wlist[$wname]['wid']=$k['wid'];

                $wlist[$wname]['gname']=$wname;
                $checkdate=$k['checkdate'];
                $int_score = intval($k['d_score_1']) + intval($k['d_score_2']);
                $wlist[$wname][$checkdate]=$int_score;
                if (array_key_exists('summonth',$wlist[$wname])){
                    $wlist[$wname]['summonth']+=$int_score;
                }else{
                    $wlist[$wname]['summonth']=$int_score;
                }
                //累加超过10分的天数
               if ($int_score>=10){
                   if (array_key_exists('countdays',$wlist[$wname])){
                       $wlist[$wname]['countdays']+=1;
                   }else{
                       $wlist[$wname]['countdays']=1;
                   }
               }

        }
        $key_list=array_keys($wlist);
        $workertotal=array();
        foreach($key_list as $kv){
            $wid=$wlist[$kv]['wid'];
            //获取是否是推荐关系
         //   $recuitinfo=$t_re->selrecuitinfobyuserid($wid);  //20200817 ,test废弃,比较时间
          // if ($recuitinfo){
            if (in_array($wid,$ulist)){                       //20200817 测试使用
                if(array_key_exists('countdays',$wlist[$kv])){
                    $wlist[$kv]['recuitcost']=5*$wlist[$kv]['countdays'];
                }
            }

            array_push($workertotal,$wlist[$kv]);
        }
        //补充身份证号
        $t_w=new gworkermodel();

        foreach($workertotal as &$worker){
            $wid=$worker['wid'];
            $winfo=$t_w->getinfobyid($wid);
            if($winfo){
                $worker['sfz']=$winfo['shengfengid'];
            }

        }
        return $workertotal;

    }
}
