<?php

namespace App\Http\Controllers;


use App\Services\Admin\YeWu\AppointmentService;
use DateTime;
use Illuminate\Http\Request;
use App\Lib\HSM;
use Illuminate\Support\Facades\DB;

class TestController extends Controller
{
    public function DBtest(){
     //echo  DB::table('users')->count();



//        $SendData='<com:queryCheckUp>
//         <!--Optional:-->
//         <![CDATA[<xm>王新书</xm>
//         <!--Optional:-->
//         <sfz>142423194705202719</sfz>]]>
//      </com:queryCheckUp>';
//
//        $url = config('app.globals.GongWeiBaseUrl');
     //   return \Yz::XmlHttp($SendData,$url);

        $name =request('name');
        $sfz =request('sfz');

        $info=AppointmentService::CheckGongWei($name,$sfz);
        return  json_encode($info, JSON_UNESCAPED_UNICODE);


    }
    public function mm_test()
    {
        $a= HSM::Hmac("");//测试Hmac摘要
       // $a= HSM::HsmEncrypt("dsf");
       // $a= HSM::HsmDecrypt("fd01ede26c7ba22caa1b86b3941b652c");

       return json_encode( $a, JSON_UNESCAPED_UNICODE);
    }
    public function lqs_test()
    {
        $a=[];
        $neirong =request('neirong');

       // return DB::select($neirong);
        $a=DB::select($neirong);
        //return $a;
        foreach ($a as $key=>$item){
            $HSM_sfz =\App\Lib\HSM::HsmDecrypt($item->id_card_num);
            if($HSM_sfz['status']!=true){
                $item->id_card_num='解密失败';
            }else{
                $item->id_card_num=$HSM_sfz['data'];
            }

        }

        return redirect()->back()->with('data', $a);
    }

    //批量加密信息
    public function jiami()
    {
        $count=DB::select("select count(*) as c from appointment_record where LENGTH(id_card_num)=18 ");
        $list=DB::select("select * from appointment_record where LENGTH(id_card_num)=18 limit 10");
        $shu=0;
        if(count($list)>0){

            foreach ($list as $item){
                $HSM_sfz =\App\Lib\HSM::HsmEncrypt($item->id_card_num);
                if($HSM_sfz['status']!=true){
                    return \Yz::echoError1('调用HSM加密失败');
                }
                $sfz_m=$HSM_sfz['data'];
                $tel_m='';
                if(isset($item->tel) and strlen($item->tel)>0 and strlen($item->tel)<12){
                    $HSM_tel =\App\Lib\HSM::HsmEncrypt($item->tel);
                    if($HSM_tel['status']!=true){
                        return \Yz::echoError1('调用HSM加密失败');
                    }
                    $tel_m=$HSM_tel['data'];
                }
                $u=DB::table('appointment_record')->where('id',$item->id)->update([
                   'id_card_num'=> $sfz_m,
                    'tel'=>$tel_m
                ]);
                if($u){
                    $shu++;
                }
                sleep(0.5);
            }


        }

        echo"执行完毕,共有待处理:".$count[0]->c."条,共更新".$shu."条";
    }
    public function jiemi_age()
    {
        $count=DB::select("select count(*) as c from appointment_record where age is null ");
        $list=DB::select("select * from appointment_record where   age is null    limit 10");
        $shu=0;
        if(count($list)>0){

            foreach ($list as $item){
                $HSM_sfz =\App\Lib\HSM::HsmDecrypt($item->id_card_num);
                if($HSM_sfz['status']!=true){
                    return \Yz::echoError1('调用HSM解密失败');
                }
                $sfz=$HSM_sfz['data'];

                $u=DB::table('appointment_record')->where('id',$item->id)->update([
                    'age'=> \App\Lib\Tools::calculateAgeFromID($sfz,$item->created_at),
                    'birthday'=>\App\Lib\Tools::getBirthdayFromIDCard($sfz)
                ]);
                if($u){
                    $shu++;
                }
                sleep(0.5);
            }


        }


        echo"执行完毕,共有待处理:".$count[0]->c."条,共更新".$shu."条";
    }

}
