<?php

namespace app\admin\api;

use OneSm\Sm4;

class DataApi{

    public $guid;

    public function __construct()
    {
        $this->getGuid();
    }

    protected function getGuid(){
        $options = [
            'cache_wsdl'     => WSDL_CACHE_NONE,
            'trace'          => 1,
            'stream_context' => stream_context_create(
                [
                    'ssl' => [
                        'verify_peer'       => false,
                        'verify_peer_name'  => false,
                        'allow_self_signed' => true
                    ]
                ]
            )
        ];
        $redis = \app\common\Redis::instance(\think\facade\Config::get("cache.stores.redis.select"));
        $guid= $redis->get("jjdata_guid");
        if(!$guid || empty($guid)) {
            $client = new \SoapClient("http://110.88.153.177:802/ConvergenceServiceBoot/webservice/Authentication?wsdl", $options);
            $param = [
                'userid' => 'jjswzzb_sbxt',
                'password' => 'sbxt@2022'
            ];
            $rst = $client->loginByAccount($param);

            $ret = json_decode($rst->return, true);
            if($ret['code'] == '01'){
                $this->guid = $ret['data'];
                $redis->set("jjdata_guid",$this->guid,1700);
            }else{
                $this->guid = '';
            }
        }else{
            $this->guid = $guid;
        }
    }

    public function getClient($url){
        $this->getGuid();
        $options = [
            'cache_wsdl'     => WSDL_CACHE_NONE,
            'trace'          => 1,
            'stream_context' => stream_context_create(
                [
                    'ssl' => [
                        'verify_peer'       => false,
                        'verify_peer_name'  => false,
                        'allow_self_signed' => true
                    ]
                ]
            )
        ];

        $client = new \SoapClient($url, $options);
        return $client;
    }

    public function queryUnemploymentInsuranceByIdAndTime($card,$start,$end){
        $client = $this->getClient("http://110.88.153.177:802/ConvergenceServiceBoot/webservice/InsuranceRecordSearch?wsdl");
        $param = [
            'guid' => $this->guid,
            'requestid' => 'null_00001',
            'id' => $card,
            'startTime' => $start,
            'endTime' => $end
        ];

        $rst = $client->queryUnemploymentInsuranceByIdAndTime($param);
        $ret = json_decode($rst->return, true);
        if($ret['code'] == '01'){
            return $ret;
        }else{
            return false;
        }
    }

    public function IDNumber($realname,$birthday){
        $client = $this->getClient("http://110.88.153.177:802/ConvergenceServiceBoot/webservice/InsuranceRecordSearchs?wsdl");
        $param = [
            'guid' => $this->guid,
            'requestid' => 'null_00001',
            'aac003' => $realname,
            'bac504' => $birthday
        ];

        $rst = $client->IDNumber($param);
        $ret = json_decode($rst->return, true);
        if($ret['code'] == '01'){
            return $ret;
        }else{
            return false;
        }
    }

    public function queryOldAgeSecurityInsuranceByIdAndTime($card,$start,$end){
        $client = $this->getClient("http://110.88.153.177:802/ConvergenceServiceBoot/webservice/InsuranceRecordSearch?wsdl");
        $param = [
            'guid' => $this->guid,
            'requestid' => 'null_00001',
            'id' => $card,
            'startTime' => $start,
            'endTime' => $end
        ];

        $rst = $client->queryOldAgeSecurityInsuranceByIdAndTime($param);
        $ret = json_decode($rst->return, true);
        if($ret['code'] == '01'){
            return $ret;
        }else{
            return false;
        }
    }

    public function queryJfhbxxByYears($card,$name,$years){
        $client = $this->getClient("http://110.88.153.177:802/ConvergenceServiceBoot/webservice/CityBrain?wsdl");
        $param = [
            'guid' => $this->guid,
            'requestid' => 'null_00001',
            'reason' => "测试",
            'optIDCard' => "350582199006028550",
            'optName' => "郑明炜",
            'optIp' => get_client_ip(),
            'optMac' => "00-D8-61-73-14-EC",
            'IDCard' => $card,
            'name' => $name,
            'years' => $years
        ];

        $rst = $client->queryJfhbxxByYears($param);
        $ret = json_decode($rst->return, true);
        $ret = json_decode($ret, true);
        if($ret['code'] == '01'){
            return $ret;
        }else{
            return false;
        }
    }

    public function untrustworthyPersonnel($card){
        $client = $this->getClient("http://110.88.153.177:802/ConvergenceServiceBoot/webservice/CityBrain?wsdl");
        $param = [
            'guid' => $this->guid,
            'requestid' => 'null_00001',
            'reason' => "测试",
            'optIDCard' => "350582199006028550",
            'optName' => "郑明炜",
            'optIp' => get_client_ip(),
            'optMac' => "00-D8-61-73-14-EC",
            'IDCard' => $card
        ];

        $rst = $client->untrustworthyPersonnel($param);
        $ret = json_decode($rst->return, true);
        $ret = json_decode($ret, true);
        if($ret['code'] == '200'){
            return $ret;
        }else{
            return false;
        }
    }


    public function untrustworthyPersonnelByBaidu($realname,$card)
    {
        $ch = curl_init();

        // 设置URL
        curl_setopt($ch, CURLOPT_URL, "https://sp1.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?resource_id=6899&query=失信被执行人名单&cardNum={$card}&iname={$realname}&areaName=&from_mid=1&ie=utf-8&oe=utf-8&format=json&t=1699341456645&cb=jQuery110207963560485657695_1699341404406&_=1699341404407");

        // 设置返回结果不直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        // 执行请求
        $response = curl_exec($ch);

        // 检查是否有错误发生
        if (curl_errno($ch)) {
            $error = curl_error($ch);
            dump($error);
            curl_close($ch);
            return false;
        }

        // 关闭cURL资源
        curl_close($ch);

        // 提取JSONP格式数据中的JSON部分
        $jsonStart = strpos($response, '(') + 1;
        $jsonEnd = strrpos($response, ')');
        $jsonLength = $jsonEnd - $jsonStart;
        $jsonData = substr($response, $jsonStart, $jsonLength);

        // 解析JSON数据
        $result = json_decode($jsonData, true);

        return $result;
    }
}