package com.weaver.seconddev.interfaces.workflow.action;

import java.util.HashMap;
import java.util.Map;
import  com.weaver.seconddev.interfaces.crmoa.CRM_SyncTempUnfreeze;
import  com.weaver.seconddev.interfaces.crmoa.CRM_SyncTempUnfreezeStub.CRM_TEMPUNFREEZE_HEAD;
import  com.weaver.seconddev.interfaces.crmoa.CRM_SyncTempUnfreezeStub.CRM_TEMPUNFREEZE_RETURN;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.lang.*;
import java.util.*;
import com.weaver.verupgrade.workflow.request.RequestManager;
import com.weaver.esb.api.rpc.EsbServerlessRpcRemoteInterface;
import com.weaver.common.base.entity.result.WeaResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.weaver.verupgrade.soa.workflow.request.RequestInfo;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_BlackListTempThawSyncAction")
public class BlackListTempThawSyncAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

    // 对应E9的请求信息对象
    @Autowired
    private RequestInfo requestInfoTemp;

    @Autowired
    private CRM_SyncTempUnfreeze service;

    private static final Logger logger_e39d526d = LoggerFactory.getLogger(BlackListTempThawSyncAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        Map<String, Object> weaverResultMap = new HashMap<>();
        Long requestIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("requestid", -1)));
        Long userIdTemp = Long.parseLong(String.valueOf(requestInfoMap.getOrDefault("userid", -1)));
        RequestInfo request = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = request.getRequestid();
            logger_e39d526d.error(String.valueOf("BlackListTempThawSyncAction--start--" + requestid));
            Property[] properties = request.getMainTableInfo().getProperty();
            Map<String, String> map = new HashMap<String, String>();
            BillFieldUtil util = new BillFieldUtil();
            for (int i = 0; i < properties.length; i++) {
                String name = properties[i].getName().toUpperCase();
                String value = Util.null2String(properties[i].getValue());
                map.put(name, value);
            }
            logger_e39d526d.error("BlackListTempThawSyncAction map--" + map);
            try {
                String sqlx = map.get("SQLX");
                String jdksrq = "";
                String jdjsrq = "";
                if ("0".equals(sqlx)) {
                    jdksrq = map.get("JDKSRQ");
                    jdjsrq = map.get("JDJSRQ");
                }
                CRM_TEMPUNFREEZE_HEAD[] arr = new CRM_TEMPUNFREEZE_HEAD[1];

                CRM_TEMPUNFREEZE_HEAD head = new CRM_TEMPUNFREEZE_HEAD();
                logger_e39d526d.error("jdksrq"+jdksrq);
                // 公司编码
                head.setBusinessCode(map.get("GSDM"));
                // 编号
                head.setCode(map.get("BH"));
                // 解冻截止时间
                head.setEndDate(jdjsrq);
                // 法人/自然人名称
                head.setLegalPerson(map.get("HMDMC"));
                // OA流程号
                head.setOaCode(requestid);
                // oa流程操作
                head.setOaOperateFlag("2");
                // 录入操作人
                head.setOperator(map.get("LRCZR"));
                // 撤回的oa流程号 x
                head.setRecallform(map.get("CHSQD"));
                // 解冻开始时间
                head.setStartDate(jdksrq);
                // 标识状态
                logger_e39d526d.error("SQLX" + map.get("SQLX"));
                head.setStatusFlag(Integer.parseInt(map.get("SQLX")) + 1 + "");
                // 简要原因
                head.setSummary(map.get("YWGK"));
                // 社会信用代码/身份证号码
                head.setTaxno(map.get("SHXYDM"));
                // 类型
                logger_e39d526d.error("lx" + map.get("LX") );
                head.setType(util.getselectName(map.get("LX"), "100003720000056151")); //50151 -> 56151
                arr[0] = head;
                logger_e39d526d.error("arr" + arr.toString() );
                CRM_TEMPUNFREEZE_RETURN[] rets = service.call(arr);
                String state = "";
                String mes = "";
                for (CRM_TEMPUNFREEZE_RETURN ret : rets) {
                    String status = ret.getState();
                    String message = ret.getMessage();
                    logger_e39d526d.error(String.valueOf("调用ARS同步黑名单返回值：" + status + "--" + message));
                    if ("S".equals(status)) {
                        state = "S";
                        mes = "执行成功";
                    } else {
                        state = "E";
                        mes += message + "；";
                    }
                }
                if (!"S".equals(state)) {
                    logger_e39d526d.error(String.valueOf("BlackListTempThawCheckAction--error--" + requestid + "--" + mes));
                    request.getRequestManager().setMessageid("ERROR");
                    request.getRequestManager().setMessagecontent("ARS系统返回信息：" + mes);
                    return  WeaResult.fail(500, "ARS系统返回信息：" + mes, getResultMapForAction(weaverResultMap, "result", "", request.getRequestManager()));
                }
            } catch (Exception e) {
                logger_e39d526d.error(String.valueOf(requestid + "调用ARS临时解冻接口同步数据出错" + e.toString()));
                request.getRequestManager().setMessageid("ERROR");
                request.getRequestManager().setMessagecontent("ARS系统返回信息：" + e.toString());
                return  WeaResult.fail(500, "调用ARS临时解冻接口同步数据出错" + e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));
            }
            logger_e39d526d.error(String.valueOf("BlackListTempThawSyncAction--end--" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_e39d526d.error("BlackListTempThawSyncAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "BlackListTempThawSyncAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));
        }
    }

    Map<String, Object> getResultMapForAction(Map<String, Object> map, String key, Object value, RequestManager requestManager) {
        if (key != null && !key.isEmpty()) {
            map.put(key, value);
        }
        String msgContent = requestManager.getMessagecontent();
        if (msgContent != null && !msgContent.isEmpty()) {
            map.put("msgContent", msgContent);
        }
        String msgId = requestManager.getMessageid();
        if (msgId != null && !msgId.isEmpty()) {
            map.put("msgId", msgId);
        }
        return map;
    }
}
