/**
 * @Author: Kane Wang <wangkane@qq.com>
 * @Date: 2023-05-26 18:40:36
 * @LastEditors: Kane Wang
 * @LastModified: 2025-05-08 18:55:36
 * @FilePath: src/main/java/com/cpic/xim/utils/newcitizen/NewCitizenUitls.java
 * @Description:
 * 
 *               Copyright (c) 2025 by Kane All rights reserved
 */
package com.cpic.xim.utils.newcitizen;

import java.util.HashMap;
import java.util.Map;

import com.cpic.xim.utils.config.EncryptionParameters;
import com.cpic.xim.utils.http.HttpUtils;
import com.cpic.xim.utils.security.decode.DecryptionUtils;
import com.cpic.xim.utils.security.encode.EncryptionUtils;
import com.cpic.xim.utils.security.sign.SignUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class NewCitizenUitls
{
    // 请求地址
    private static String identifyURL = "https://api.xmcic.cn:8443/government/economic/352";

    /**
     * 新市民验证
     * 
     * @param idCardNo 身份证号码
     * @param name     姓名
     * @return
     * @throws Exception
     */
    public static IdentifyResult identifyNewCitizen( String idCardNo, String name )
                                                                                    throws Exception
    {
        String secretKey = EncryptionParameters.getSecretKey();
        String key       = EncryptionParameters.getKey();
        String pushURL   = "http://222.76.244.118:11101/NewCitizen/save_score.do";

        Map<String, String> param = new HashMap<String, String>();

        param.put( "authorized", EncryptionUtils.aesEncrypt( "1", secretKey ) );
        param.put( "idcard", EncryptionUtils.aesEncrypt( idCardNo, secretKey ) );
        param.put( "name", EncryptionUtils.aesEncrypt( name, secretKey ) );
        param.put( "pushurl", EncryptionUtils.aesEncrypt( pushURL, secretKey ) );
        param.put( "timestamp",
                   EncryptionUtils.aesEncrypt( System.currentTimeMillis() + "", secretKey ) );

        String              sign   = SignUtils.sign( param );
        Map<String, String> params = new HashMap<>();

        params.putAll( param );
        params.put( "key", key );
        params.put( "sign", sign );

        String         resultJSON     = HttpUtils.sendPost( identifyURL, params );
        ObjectMapper   mapper         = new ObjectMapper();
        IdentifyResult identifyResult = null;
        ResponseResult response       = null;

        response = mapper.readValue( resultJSON, ResponseResult.class );

        // 请求结果解密，应为一个json
        // 如果请求参数有误，data为null
        String identifyJSON = DecryptionUtils.aesDecrypt( response.getData(), secretKey );

        if ( identifyJSON != null )
        {
            identifyResult = mapper.readValue( identifyJSON, IdentifyResult.class );
        }

        return identifyResult;
    }
}


class ResponseResult
{
    public ResponseResult()
    {
    }

    public String getCode()
    {
        return code;
    }

    public void setCode( String code )
    {
        this.code = code;
    }

    public String getMessage()
    {
        return message;
    }

    public void setMessage( String message )
    {
        this.message = message;
    }

    public String getData()
    {
        return data;
    }

    public void setData( String data )
    {
        this.data = data;
    }

    public String getSeqNo()
    {
        return seqNo;
    }

    public void setSeqNo( String seqNo )
    {
        this.seqNo = seqNo;
    }

    @JsonProperty( "code" )
    private String code;

    @JsonProperty( "message" )
    private String message;

    @JsonProperty( "data" )
    private String data;

    @JsonProperty( "seqNo" )
    private String seqNo;
}

