/*
 * @Author: Kane
 * 
 * @Date: 2023-05-25 15:26:09
 * 
 * @LastEditors: Kane
 * 
 * @FilePath: /NewCitizenQueryResult/src/main/java/com/cpic/xim/utils/newcitizen/NewCitizenUitls.java
 * 
 * @Description: 新市民认证相关的工具方法
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved.
 */
package com.cpic.xim.utils.newcitizen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
// import com.cdp.product.security.decode.CdpDecryptUtil;
// import com.cdp.product.security.encode.EncryptionUtils;
// import com.cdp.product.security.sign.CdpSignUtil;
import com.cpic.xim.utils.security.encode.EncryptionUtils;
import com.cpic.xim.utils.security.decode.DecryptionUtils;
import com.cpic.xim.utils.security.sign.SignUtils;
import com.cpic.xim.utils.config.EncryptionParameters;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParseException;
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     = 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;
    }

    public static String sendPost( String url, Map<String, String> bodyMap )
    {
        HttpPost post = new HttpPost( url );

        try
        {
            // 创建参数集合
            List<BasicNameValuePair> list = new ArrayList<>();
            // 添加参数
            if ( bodyMap != null )
            {
                for ( String str : bodyMap.keySet() )
                {
                    list.add( new BasicNameValuePair( str, bodyMap.get( str ) ) );
                }
            }
            // 把参数放入请求对象，post发送的参数list，指定格式
            post.setEntity( new UrlEncodedFormEntity( list, "UTF-8" ) );
            CloseableHttpClient client = HttpClients.createDefault();
            // 启动执行请求，并获得返回值
            CloseableHttpResponse response = client.execute( post );
            // 得到返回的entity对象
            HttpEntity entity = response.getEntity();
            // 把实体对象转换为string
            return EntityUtils.toString( entity, "UTF-8" );
        }
        catch ( Exception e1 )
        {
            e1.printStackTrace();
            return "";
        }
    }
}


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;
}

