package com.weaver.seconddev.chapanda.esign.esb;

import com.alibaba.fastjson.JSONObject;
import com.weaver.common.base.entity.result.WeaResult;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.seconddev.chapanda.esign.entity.OkHttpDto;
import com.weaver.seconddev.chapanda.esign.util.Esb2EsignApplyRequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service("Esb2EsignCreateRequestAction")
public class Esb2EsignCreateRequestAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2EsignCreateRequestAction.class);

    @Autowired
    Esb2EsignApplyRequestUtil esb2EsignApplyRequestUtil;

    @Override
    public WeaResult<Map<String, Object>> execute(Map<String, Object> params) {

        String requestid = String.valueOf(params.get("requestid"));
        String requestname = String.valueOf(params.get("requestname"));
        String sealId = String.valueOf(params.get("sealId"));
        String docFilekey = String.valueOf(params.get("docFilekey"));
        String docName = String.valueOf(params.get("docName"));
        String creditCode = String.valueOf(params.get("creditCode"));

        String employeeMobile = String.valueOf(params.get("employeeMobile"));
        String qsrJobNum = String.valueOf(params.get("qsrJobNum"));

        log.error("requestid:{}",requestid);
        log.error("requestname:{}",requestname);
        log.error("sealId:{}",sealId);
        log.error("docFilekey:{}",docFilekey);
        log.error("docName:{}",docName);
        log.error("creditCode:{}",creditCode);
        log.error("employeeMobile:{}",employeeMobile);
        log.error("qsrJobNum:{}",qsrJobNum);

        String signFlowId = "";
        String signUrls = "";
        String accountIds = "";
        String uniqueIds = "";
        String fileKeys = "";
        String templateId = String.valueOf(params.get("templateId"));
        if(StringUtils.isNotBlank(templateId)){
            OkHttpDto okHttpDto = esb2EsignApplyRequestUtil.applyRequest(requestid,requestname,employeeMobile,templateId,sealId,docFilekey,docName,creditCode,qsrJobNum);
            if(okHttpDto.getResponseCode() == 200){
                if(okHttpDto.getDataCode() == 0){
                    JSONObject dataJson = okHttpDto.getDataJson();
                    signFlowId = dataJson.getString("signFlowId");
                    signUrls = dataJson.getString("signUrls");
                    accountIds = dataJson.getString("accountIds");
                    uniqueIds = dataJson.getString("uniqueIds");
                    fileKeys = dataJson.getString("fileKeys");
                }else{
                    return WeaResult.fail(500,"创建签署流程异常,"+okHttpDto.getDataMsg());
                }
            }else{
                return WeaResult.fail(500,"签署流程接口异常,"+okHttpDto.getDataMsg());
            }

            log.error("signFlowId:{}",signFlowId);
            log.error("signUrls:{}",signUrls);
            log.error("accountIds:{}",accountIds);
            log.error("uniqueIds:{}",uniqueIds);
            log.error("fileKeys:{}",fileKeys);
        }

        if(StringUtils.isBlank(fileKeys)){
            return WeaResult.fail(500,"用印申请失败");
        }

        Map<String, Object> actionMap = new HashMap<String, Object>();
        actionMap.put("code",200);
        actionMap.put("signFlowId",signFlowId);
        actionMap.put("accountIds",accountIds);
        actionMap.put("signUrls",signUrls);
        actionMap.put("uniqueIds",uniqueIds);
        actionMap.put("fileKeys",fileKeys);
        return WeaResult.success(actionMap);
    }
}
