<?php
/**
 * Created by PhpStorm.
 * User: sicilon_IT
 * Date: 2020/5/10
 * Time: 20:32
 */

namespace app\index\controller;
use app\index\model\indatarec;
use app\index\model\gworkermodel;

use think\Controller;

class Enterprise extends Controller{
    /*
 * 20200423 来自网上文档
 */
    protected function curlPost($url,$data)
    {
        $ch = curl_init();
        $params[CURLOPT_URL] = $url;    //请求url地址
        $params[CURLOPT_HEADER] = FALSE; //是否返回响应头信息
        $params[CURLOPT_SSL_VERIFYPEER] = false;
        $params[CURLOPT_SSL_VERIFYHOST] = false;
        $params[CURLOPT_RETURNTRANSFER] = true; //是否将结果返回
        $params[CURLOPT_POST] = true;
        $params[CURLOPT_POSTFIELDS] = $data;
        curl_setopt_array($ch, $params); //传入curl参数
        $content = curl_exec($ch); //执行
        curl_close($ch); //关闭连接
        return $content;
    }

    private function queryworkerbyeid($cid){
        $url="https://iws.4bpoc.com/wapsenyi/wap/getPersonnelList";
        $pdata=array();
        $pdata['conid']=$cid;
        $wliststr=$this->curlPost($url,$pdata);

        $wlist=json_decode($wliststr,true);
        return $wlist;
    }

    public function oprallworker(){
        ini_set('max_execution_time','0');
        //初始化员工表
        $t_w=new gworkermodel();
        //获取当前的数据
        $t_indata=new indatarec();
        $rlist=$t_indata->all();
        $rlist=collection($rlist)->toArray();

        //每个公司读取
        foreach($rlist as &$kv){
            $cid=$kv['contractid'];
            $maxid=$kv['workrecid'];
            $workerlist=$this->queryworkerbyeid($cid);
           $wlist=$workerlist['Rows'];
            foreach($wlist as $worker){
                //先比较ID,只有大于才处理
                if ($worker['id']>$kv['workrecid']){
                    if ($worker['id']>$maxid){
                        //更换id
                        $maxid=$worker['id'];
                    }

                    //比较身份证
                    $sfz=$worker['card'];
                    $rec=$t_w->selinfobysfz($sfz);
                    if (!$rec){
                        //todo 去存储此数据
                        //
                        $arr['wname']=$worker['name'];
                        $arr['telno']=$worker['Mobile'];
                        $arr['shengfengid']=$sfz;
                        $arr['cprojectid']=3;
                        $arr['workclass']=$worker['TypeOfWork'];
                        $arr['wage']=$worker['age'];
                        $arr['cardno']=$worker['EMP_BANK_CARD'];

                        $t_w->saveworkerinfo($arr);
                    }


                }
            }
            //更新最大的id
            $t_indata->updrecbycid($cid,$maxid);

        }



    }
}