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.Esb2EsignApplyRequest4OrgainzeUtil;
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("Esb2EsignCreateRequest4OrganizeAction")
public class Esb2EsignCreateRequest4OrganizeAction implements EsbServerlessRpcRemoteInterface {
    private final static Logger log = LoggerFactory.getLogger(Esb2EsignCreateRequest4OrganizeAction.class);
    @Autowired
    Esb2EsignApplyRequest4OrgainzeUtil esb2EsignApplyRequest4OrgainzeUtil;

//    @Autowired
//    private Esb2EsignPreviewUrlUtil esb2EsignPreviewUrlUtil;

    @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 qsrJobNum = String.valueOf(params.get("qsrJobNum"));
        String templateId = String.valueOf(params.get("templateId"));
        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("qsrJobNum:{}",qsrJobNum);
        log.error("templateId:{}",templateId);

        if(StringUtils.isBlank(templateId)){
            return WeaResult.fail(500,"参数模板ID为空");
        }

        if(StringUtils.isBlank(creditCode)){
            return WeaResult.fail(500,"参数统一社会信用代码为空");
        }

        if(StringUtils.isBlank(docFilekey)){
            return WeaResult.fail(500,"参数签署文件为空");
        }

        if(StringUtils.isBlank(sealId)){
            return WeaResult.fail(500,"参数签署文件印章为空");
        }

        if(StringUtils.isBlank(qsrJobNum)){
            return WeaResult.fail(500,"参数签署人工号为空");
        }

        String signFlowId = "";
        String signUrls = "";
        String accountIds = "";
        String uniqueIds = "";
        String fileKeys = "";

        if(StringUtils.isNotBlank(templateId)){
//            JSONObject dataJson = esb2EsignApplyRequest4OrgainzeUtil.applyRequest(requestid,requestname,templateId,sealId,docFilekey,docName,creditCode,qsrJobNum);
            OkHttpDto okHttpDto = esb2EsignApplyRequest4OrgainzeUtil.applyRequest(requestid,requestname,templateId,sealId,docFilekey,docName,creditCode,qsrJobNum);
            if(okHttpDto.getResponseCode() == 200){
                if(okHttpDto.getDataCode() == 0){
                    JSONObject dataJson = okHttpDto.getDataJson();
                    log.error("dataJson:{}",dataJson.toJSONString());
                    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,"获取签署文件失败");
        }

//        String preViewUrl = "";
//        if(StringUtils.isNotBlank(fileKeys)){
////            preViewUrl = esb2EsignPreviewUrlUtil.queryPreviewUrl(fileKeys);
//            OkHttpDto preOkHttpDto = esb2EsignPreviewUrlUtil.queryPreviewUrl(fileKeys);
//            if(preOkHttpDto.getResponseCode() == 200){
//                if(preOkHttpDto.getDataCode() == 0){
//                    JSONObject preJson = preOkHttpDto.getDataJson();
//                    if(preJson.containsKey("preViewUrl")){
//                        preViewUrl = preJson.getString("preViewUrl");
//                    }
//                }else{
//                    return WeaResult.fail(500,"获取预览地址异常,"+preOkHttpDto.getDataMsg());
//                }
//            }else{
//                return WeaResult.fail(500,"文件预览地址接口异常,"+preOkHttpDto.getDataMsg());
//            }
//        }
//        log.error("preViewUrl:{}",preViewUrl);

        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);
//        actionMap.put("preViewUrl",preViewUrl);

        return WeaResult.success(actionMap);
    }
}
