package com.dgtly.wxportal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dgtly.system.domain.ConfigParams;
import com.dgtly.system.domain.ContextInfo;
import com.dgtly.wxportal.config.ESignConfig;
import com.dgtly.wxportal.service.IEsignPersonRealnVerifyService;
import com.dgtly.wxportal.utils.ESign.ESignHttpUtil;
import com.dgtly.wxportal.utils.ESign.ESignUrl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:个人认证
 * @author:qxm
 * @date:2020/10/12 15:12
 */
@Service
public class EsignPersonReaLnVerifyServiceImpl implements IEsignPersonRealnVerifyService {

    @Autowired
    private ESignHttpUtil eSignHttpUtil;
    @Autowired
    private ESignConfig eSignConfig;

    /**
     * @description: 个人认证
     * @param: [accountId]
     * @return: java.lang.String
     * @author: qxm
     * @date: 2020/10/12 15:13
     */
    @Override
    public String personRealnVerify(String accountId,String redirectUrl) {
        JSONObject json = buildParam(accountId, "",redirectUrl);
        JSONObject jsonObject = eSignHttpUtil.doPostGetJson(ESignUrl.IndivIdentityUrl, json, accountId);
        JSONObject result = (JSONObject)jsonObject.get("data");
        return result.get("shortLink").toString();
    }

    private JSONObject buildParam(String accountId, String receiveUrlMobileNo,String redirectUrl) {
        //个人认证参数
        String notifyUrl = eSignConfig.getPersonNotifyUrl();
        ContextInfo contextInfo = new ContextInfo(notifyUrl, redirectUrl);
        //认证配置信息
        String[] arr = {"name", "certNo", "mobileNo", "bankCardNo"};
        ConfigParams configParams = new ConfigParams(arr, null);
        //拼接参数
        return buildOneStepFlowParam(accountId, receiveUrlMobileNo, contextInfo, configParams);
    }

    private static JSONObject buildOneStepFlowParam(String accountId, String receiveUrlMobileNo, ContextInfo contextInfo, ConfigParams configParams) {
        JSONObject json = new JSONObject();
        json.put("accountId", accountId);
        json.put("authType", "PSN_TELECOM_AUTHCODE");
        json.put("availableAuthTypes", new String[]{"PSN_TELECOM_AUTHCODE","PSN_BANK4_AUTHCODE","PSN_FACEAUTH_BYURL"});
        json.put("contextInfo", contextInfo);
        json.put("configParams", configParams);
        return json;
    }

}
