package com.dgtly.wxportal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dgtly.common.annotation.Log;
import com.dgtly.system.domain.ConfigParams;
import com.dgtly.system.domain.ContextInfo;
import com.dgtly.wxportal.config.ESignConfig;
import com.dgtly.wxportal.service.IEsignOrganRealVerifyService;
import com.dgtly.wxportal.utils.ESign.ESignHttpUtil;
import com.dgtly.wxportal.utils.ESign.ESignUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description:企业认证
 * @author:qxm
 * @date:2020/10/12 16:08
 */
@Slf4j
@Service
public class EsignOrganRealVerifyServiceImpl implements IEsignOrganRealVerifyService {

    @Autowired
    private ESignHttpUtil eSignHttpUtil;
    @Autowired
    private ESignConfig eSignConfig;
    /**
     * @description: 企业认证
     * @param: [accountId, agentAccountId]
     * @return: java.lang.String
     * @author: qxm
     * @date: 2020/10/12 15:13
     */
    @Override
    public String organRealVerify(String accountId, String agentAccountId,String redirectUrl) {
        JSONObject json = buildParam(accountId,agentAccountId,redirectUrl);
        log.info("开始调用E请求参数{}", json);
        JSONObject jsonObject = eSignHttpUtil.doPostGetJson(ESignUrl.OrgIdentityUrl, json, accountId);
        log.info("E返回结果{}", jsonObject);
        JSONObject result = (JSONObject)jsonObject.get("data");
        return result.get("shortLink").toString();
    }

    private JSONObject buildParam(String accountId, String agentAccountId,String redirectUrl) {
        //企业认证参数
        String notifyUrl = eSignConfig.getOrganNotifyUrl();
        ContextInfo contextInfo = new ContextInfo(notifyUrl, redirectUrl);
        //认证配置信息
        String[] arr = {"name", "certNo", "legalRepName", "legalRepCertNo","agentName","agentIdNo"};
        ConfigParams configParams = new ConfigParams(null,arr);
        //拼接参数
        return buildOneStepFlowParam(accountId,agentAccountId,contextInfo,configParams);
    }

    private static JSONObject buildOneStepFlowParam(String accountId, String agentAccountId, ContextInfo contextInfo, ConfigParams configParams) {
        JSONObject json = new JSONObject();
        json.put("accountId", accountId);
        json.put("agentAccountId", agentAccountId);
        json.put("availableAuthTypes", new String[]{"ORG_BANK_TRANSFER"} );
        json.put("contextInfo", contextInfo);
        json.put("configParams", configParams);
        return json;
    }
}
