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

namespace app\index\controller;
use app\index\logic\checkmanger;
use app\index\logic\workercheck;
use think\Controller;

class Headcheck extends Controller{
    /*
     * 20200304
     * edit
     * 返回cprojectid,作为pid
     */
    public function getgid(){
        $userinfo = request() -> param();

        $logicworker=new workercheck();
        $res=$logicworker->isheaderbytel($userinfo['tel']);
        if ($res['r_code']===0){
            $res['errmsg']='非员工';
        }
        if ($res['r_code']===9902){
            $res['errmsg']='非班组长';
        }

        $res_r['code']=200;
        $res_r['resultData']=$res;

        return json_encode($res_r,320);
    }

    public function getgidV2(){
        $userinfo = request() -> param();

        $logicworker=new workercheck();
        $res=$logicworker->isheaderbytelV2($userinfo);
        if ($res['r_code']===0){
            $res['errmsg']='非员工';
        }
        if ($res['r_code']===9902){
            $res['errmsg']='非班组长';
        }

        $res_r['code']=200;
        $res_r['resultData']=$res;

        return json_encode($res_r,320);
    }

    public function getgidforapp(){
        $userinfo = request() -> param();

        $logicworker=new workercheck();
        $res=$logicworker->isheaderbytel($userinfo['tel']);
        $res_r['msg']="班组长";
        if ($res['r_code']===0){
            $res_r['msg']='非员工';
        }
        if ($res['r_code']===9902){
            $res_r['msg']='非班组长';
        }

        $res_r['code']=200;
        $res_r['data']=$res;

        return json_encode($res_r,320);
    }


    public function getgidforappV2(){
        $userinfo = request() -> param();

        $logicworker=new workercheck();
        $res=$logicworker->isheaderbytelV2($userinfo);
        $res_r['msg']="班组长";
        if ($res['r_code']===0){
            $res_r['msg']='非员工';
        }
        if ($res['r_code']===9902){
            $res_r['msg']='非班组长';
        }

        $res_r['code']=200;
        $res_r['data']=$res;

        return json_encode($res_r,320);
    }

    public function getshift(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
       $res=$logicworker->getshiftbywid($userinfo['wid']);

        $res_r['code']=200;
        $res_r['resultData']=$res;
        return json_encode($res_r);
    }
/*
 * 20200729
 */
    public function getshiftwithcount(){

        $userinfo = request() -> param();
        $logicworker=new workercheck();
       $res=$logicworker->getshiftandcountbywid($userinfo['wid'],$userinfo['gid']);

        $res_r['code']=200;
        $res_r['resultData']=$res;
        return json_encode($res_r);
    }

    /*
     * 20230218
     */
    public function getshiftwithcountforapp(){

        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $res=$logicworker->getshiftandcountbywid($userinfo['wid'],$userinfo['gid']);

        $res_r['code']=200;
        $res_r['msg']='查询成功';
        $res_r['data']=$res;
        return json_encode($res_r);
    }

    /*
     * 获取七天的日期
     */
    public function getshiftdate(){
        //$userinfo = request() -> param();
        $logicworker=new workercheck();
        $res=$logicworker->getcheckdate();
       // $res=$logicworker->getshiftbywid($userinfo['wid']);
        $res_r['code']=200;
        $res_r['resultData']=$res;
        return json_encode($res_r);
    }

    /*20210504
 * 获取七天的日期,以及相应的数据
 */
    public function getshiftdatecheckinfo(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $res=$logicworker->getcheckdate();
        // $res=$logicworker->getshiftbywid($userinfo['wid']);
        $l_opr=new checkmanger();
        foreach($res as &$date){
            $recordinfo=$l_opr->getheadcheckoprwithdate($userinfo['wid'],$date['date']);
            if(!empty($recordinfo)){
                $date['checkoprlist']=$recordinfo;
            }else{
                $date['checkoprlist']=array();
            }

        }
        $res_r['code']=200;
        $res_r['resultData']=$res;
        return json_encode($res_r,320);
    }

    /*
     * 20230222
     */
    public function getshiftdatecheckinfoforapp(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $res=$logicworker->getcheckdate();
        // $res=$logicworker->getshiftbywid($userinfo['wid']);
        $l_opr=new checkmanger();
        foreach($res as &$date){
            $recordinfo=$l_opr->getheadcheckoprwithdate($userinfo['wid'],$date['date']);
            if(!empty($recordinfo)){
                $date['checkoprlist']=$recordinfo;
            }else{
                $date['checkoprlist']=array();
            }

        }
        $res_r['code']=200;
        $res_r['msg']="请求成功";
        $res_r['data']=$res;
        return json_encode($res_r,320);
    }
/*
 * 20200304
 * 增加了一个wid参数，
 * @todo 因为前端未上线，所以后端的新参数先进行判断，并给默认值，待前端上线后再修改
 */
    public function getworkerlist(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $shift=$userinfo['shift'];
        $checkdate=$userinfo['checkdate'];
        $gid=$userinfo['gid'];
        //todo 前端上线后取消判断和默认值
        if (array_key_exists('wid',$userinfo)){
            $wid=$userinfo['wid'];
        }else{
            $wid=0;
        }
        $res=$logicworker->getworkerlistbyhead($gid,$checkdate,$shift,$wid);
        $res_r['code']=200;
        $res_r['resultData']=$res;
        return json_encode($res_r,320);
    }

    /*
     * 20230218
     * 考勤
     */
    public function getworkerlistforapp(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $shift=$userinfo['shift'];
        $checkdate=$userinfo['checkdate'];
        $gid=$userinfo['gid'];
        //todo 前端上线后取消判断和默认值
        if (array_key_exists('wid',$userinfo)){
            $wid=$userinfo['wid'];
        }else{
            $wid=0;
        }
        $res=$logicworker->getworkerlistbyhead($gid,$checkdate,$shift,$wid);
        $res_r['code']=200;
        $res_r['msg']="查询成功";
        $res_r['data']=$res;
        return json_encode($res_r,320);
    }

    public function savechecklist(){
        //
        $userinfo = request() -> param();
    //   var_dump($userinfo);
       // $checklist=json_decode($userinfo['checklist'],true);
       // $checklist=json_decode($userinfo['checklist']);
       // $checklist=array();
        $userinfo['checklist']=htmlspecialchars_decode($userinfo['checklist']);
        $checklist=json_decode($userinfo['checklist'],true);
//        var_dump($checklist1);
//        $checklist=$userinfo['checklist'];
       // var_dump($checklist);
        $checkoprinfo['checkdate']=$userinfo['checkdate'];
        $checkoprinfo['checkorder']=$userinfo['checkorder'];
        $checkoprinfo['headid']=$userinfo['headid'];
        if (array_key_exists('gid',$userinfo)){
            $checkoprinfo['gid']=$userinfo['gid'];
        }else{
            $checkoprinfo['gid']=0;
        }
        $checkoprinfo['isopr']=1;

        for($i=0;$i<count($checklist);$i++){
            $checklist[$i]['oprtime']=date('Y:m:d H:i:s');
        }

        $logicworker=new workercheck();
//        $scount=$logicworker->oprcheckinfo($checklist,$checkoprinfo);
//        $res_r['code']=200;
//        $res_r['resultData']=$scount;

        $scount=$logicworker->savedaycheckinfo($checklist,$checkoprinfo);
        $res_r['code']=200;
        $res_r['resultData']=$scount;
        return json_encode($res_r);

    }

    /*
     * 20230218
     * forapp
     */
    public function savechecklistforapp(){
        //
        $userinfo = request() -> param();
        //   var_dump($userinfo);
        // $checklist=json_decode($userinfo['checklist'],true);
        // $checklist=json_decode($userinfo['checklist']);
        // $checklist=array();
        $userinfo['checklist']=htmlspecialchars_decode($userinfo['checklist']);
        $checklist=json_decode($userinfo['checklist'],true);
//        var_dump($checklist1);
//        $checklist=$userinfo['checklist'];
        // var_dump($checklist);
        $checkoprinfo['checkdate']=$userinfo['checkdate'];
        $checkoprinfo['checkorder']=$userinfo['checkorder'];
        $checkoprinfo['headid']=$userinfo['headid'];
        if (array_key_exists('gid',$userinfo)){
            $checkoprinfo['gid']=$userinfo['gid'];
        }else{
            $checkoprinfo['gid']=0;
        }
        $checkoprinfo['isopr']=1;

        for($i=0;$i<count($checklist);$i++){
            $checklist[$i]['oprtime']=date('Y:m:d H:i:s');
        }

        $logicworker=new workercheck();
//        $scount=$logicworker->oprcheckinfo($checklist,$checkoprinfo);
//        $res_r['code']=200;
//        $res_r['resultData']=$scount;

        $scount=$logicworker->savedaycheckinfo($checklist,$checkoprinfo);
        $res_r['code']=200;
        $res_r['msg']="提交成功";
        $res_r['data']=$scount;
        return json_encode($res_r);

    }
    /*
     * 2020215 此方法待修改，增加月参数，按月获取，不改此接口，但增加一个，封闭开放原则
     */
    public function getusercheckrecord(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $wid=$userinfo['wid'];
        $r_list=$logicworker->getworkercheckinfobyid($wid);
        $res_r['code']=200;
        $res_r['resultData']=$r_list;

        return json_encode($res_r,320);
    }

    /*
 * 2020215 按封闭开放原则，增加一个按月获取工人记工详情的控制，原控制逻辑暂停
 */
    public function getusercheckrecordwihtmonth(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $wid=$userinfo['wid'];
        $strmonth=$userinfo['strmonth'];
        $r_list=$logicworker->getworkercheckinfobyidwithmonth($wid,$strmonth);
        $res_r['code']=200;
        $res_r['resultData']=$r_list;

        return json_encode($res_r,320);
    }

    /*
     * 20230222
     */
    public function getusercheckrecordwihtmonthforapp(){
        $userinfo = request() -> param();
        $logicworker=new workercheck();
        $wid=$userinfo['wid'];
        $strmonth=$userinfo['strmonth'];
        $r_list=$logicworker->getworkercheckinfobyidwithmonth($wid,$strmonth);
        $res_r['code']=200;
        $res_r['msg']="请求成功";
        $res_r['data']=$r_list;

        return json_encode($res_r,320);
    }

    /*
     * 20200208
     * steelxu
     * 取消考勤记录
     */
    public function revokecheck(){
        $userinfo = request() -> param();
        $logiccheck=new checkmanger();

        $result=$logiccheck->invokecheckbywid($userinfo);
        if (!$result){
            $res_r['code']=0;
            $res_r['resultData']=$result;
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$result;
        }


        return json_encode($res_r,320);
    }

    /*
     * 20230218
     */
    public function revokecheckforapp(){
        $userinfo = request() -> param();
        $logiccheck=new checkmanger();

        $result=$logiccheck->invokecheckbywid($userinfo);
        if (!$result){
            $res_r['code']=0;
            $res_r['msg']="撤销失败";
            $res_r['data']=$result;
        }else{
            $res_r['code']=200;
            $res_r['msg']="撤销成功";
            $res_r['data']=$result;
        }


        return json_encode($res_r,320);
    }

    /*
     * 20200208
     * steelxu
     * 获取某班工长记工的记录
     */

    public function getlistbyheadwithshift(){
        $userinfo = request() -> param();
        $logiccheck=new checkmanger();

        $result=$logiccheck->getheadoprwithshift($userinfo);
        if (!$result){
            $res_r['code']=0;
            $res_r['resultData']=$result;
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$result;
        }


        return json_encode($res_r,320);
    }

    /*
     * 20230218
     */
    public function getlistbyheadwithshiftforapp(){
        $userinfo = request() -> param();
        $logiccheck=new checkmanger();

        $result=$logiccheck->getheadoprwithshift($userinfo);
        if (!$result){
            $res_r['code']=0;
            $res_r['msg']="本班次未记工";
            $res_r['data']=$result;
        }else{
            $res_r['code']=200;
            $res_r['msg']="已记工人员";
            $res_r['data']=$result;
        }


        return json_encode($res_r,320);
    }

    /*
     * 20200215
     * steelxu
     * 获取按月汇总的记录,应用于工人用APP查看自己的记录
     */

    public function getchecktotallistbymonth(){
        $queryinfo=request()->param();
        $logicworker=new workercheck();
        $wid=$queryinfo['wid'];
        $r_list=$logicworker->gettotallistbywid($wid);
        $res_r['code']=200;
        $res_r['resultData']=$r_list;

        return json_encode($res_r,320);


    }


    /*
     * 20230222
     */
    public function getchecktotallistbymonthforapp(){
        $queryinfo=request()->param();
        $logicworker=new workercheck();
        $wid=$queryinfo['wid'];
        $r_list=$logicworker->gettotallistbywid($wid);
        $res_r['code']=200;
        $res_r['msg']="请求成功";
        $res_r['data']=$r_list;
        return json_encode($res_r,320);
    }

    /*
     * 20200317
     * 响应分班组数据
     */

    public function queryscoredetailbyidwithmonth(){
        $queryinfo=request()->param();
        $l_wcheck=new workercheck();
        $rlist=$l_wcheck->getScoreGroupbyWidWithMonth($queryinfo);

        if (!$rlist){
            $res_r['code']=0;
            $res_r['resultData']=$rlist;
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$rlist;
        }

        return json_encode($res_r,320);
    }

    /*
     * 20230222
     */
    public function queryscoredetailbyidwithmonthforapp(){
        $queryinfo=request()->param();
        $l_wcheck=new workercheck();
        $rlist=$l_wcheck->getScoreGroupbyWidWithMonth($queryinfo);

        if (!$rlist){
            $res_r['code']=0;
            $res_r['msg']="请求失败";
            $res_r['data']=$rlist;
        }else{
            $res_r['code']=200;
            $res_r['msg']="请求成功";
            $res_r['data']=$rlist;
        }

        return json_encode($res_r,320);
    }

    /*
     * 20200531
     * WEB端根据项目，请求员工
     */
    public function queryuncheckworklist(){
        $queryinfo=request()->param();
        $logicworker=new workercheck();
        $wlist=$logicworker->getunchecklist($queryinfo);
        if (!$wlist){
            $res_r['code']=0;
            $res_r['resultData']='';
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$wlist;
        }

        return json_encode($res_r,320);

    }


    /*
     * 20210421
     * 提交投诉
     */
    public function subcomplain(){
        $queryinfo=request()->param();
        $l_wcheck=new workercheck();
        $rid=$l_wcheck->savecomplaininf($queryinfo);

        if (!$rid){
            $res_r['code']=0;
            $res_r['resultData']='提交失败';
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$rid;
        }

        return json_encode($res_r,320);
    }

    /*
     * 20230222
     */
    public function subcomplainforapp(){
        $queryinfo=request()->param();
        $l_wcheck=new workercheck();
        $rid=$l_wcheck->savecomplaininf($queryinfo);

        if (!$rid){
            $res_r['code']=0;
            $res_r['msg']="提交投诉失败";
            $res_r['data']=array();
        }else{
            $res_r['code']=200;
            $res_r['msg']="提交投诉成功";
            $res_r['data']=$rid;
        }

        return json_encode($res_r,320);
    }

    /*
     * 20210421
     */
    public function querycomplainlist(){

        $l_wcheck=new workercheck();
        $list=$l_wcheck->getcomplainlist();

        if (!$list){
            $res_r['code']=0;
            $res_r['resultData']='提交失败';
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$list;
        }

        return json_encode($res_r,320);
    }

    /*
     * 20230215
     * 只能获取本公司的投诉
     * getcomplainlistbycid
     */
    public function querycomplainlistbycid(){
        $queryinfo=request()->param();
        $l_wcheck=new workercheck();
        $list=$l_wcheck->getcomplainlistbycid($queryinfo);

        if (!$list){
            $res_r['code']=0;
            $res_r['resultData']='无数据';
        }else{
            $res_r['code']=200;
            $res_r['resultData']=$list;
        }

        return json_encode($res_r,320);
    }

}