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

import java.util.HashMap;
import java.util.Map;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
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.conn.RecordSet;
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_EASIntermediaryCheckAction")
public class EASIntermediaryCheckAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_1e13fc37 = LoggerFactory.getLogger(EASIntermediaryCheckAction.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();
            try {
                logger_1e13fc37.error(String.valueOf("EASIntermediaryCheckAction：--" + requestid));
                logger_1e13fc37.error("requestIdTemp == " +requestIdTemp);
                logger_1e13fc37.error("userIdTemp == " +userIdTemp);
                Property[] properties = request.getMainTableInfo().getProperty();
                Map<String, String> map = new HashMap<String, String>();
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                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);
                }
                // 是否备案
                String sfba = map.get("CWZJJGSFYBA");
                if ("0".equals(sfba)) {
                    String name = "";
                    String yxx = "";
                    String ywlx = "";
                    // 财务中介机构id
                    String cwzjjgxz = map.get("CWZJJGXZ");
                    // 财务中介名称
                    String cwzjjgmc = map.get("CWZJJGMC");
                    // 财务中介机构业务类型
                    String jgywlx = map.get("YWLX");
                    // 有效性
                    String sfyx = map.get("yxx");
                    String sql = "select mc,yxx,ywlx from e10_common.dbo.uf_intermediaries where id='" + cwzjjgxz + "'";

                    logger_1e13fc37.error("sql == " + sql );
                    String poolname_cn = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs.executeSql(sql, poolname_cn);
                    logger_1e13fc37.error("b == " + b );
                    if (rs.next()) {
                        name = Util.null2String(rs.getString("mc"));
                        yxx = Util.null2String(rs.getString("yxx"));
                        ywlx = Util.null2String(rs.getString("ywlx"));

                        logger_1e13fc37.error("name == " + name );
                        logger_1e13fc37.error("yxx == " + yxx );
                        logger_1e13fc37.error("ywlx == " + ywlx );
                    }
                    if (name.equals(cwzjjgmc) && yxx.equals(sfyx) && jgywlx.equals(ywlx)) {
                        logger_1e13fc37.error(String.valueOf("EASIntermediaryCheckAction：--" + requestid + "已备案中介机构信息未调整"));
                        request.getRequestManager().setMessageid("ERROR");
                        request.getRequestManager().setMessagecontent("已备案中介机构需修改机构名称或者有效性才能提交申请！");
                    }
                } else {
                    // 新增中介机构名称
                    String cwzjjgmcxz = map.get("CWZJJGMCXZ");
                    // 纳税识别号
                    String nssbhxz = map.get("NSSBHXZ");
                    String check = "select id from e10_common.dbo.uf_intermediaries where mc='" + cwzjjgmcxz + "' or nssbh='" + nssbhxz + "'";
                    logger_1e13fc37.error("check == " + check );
                    String poolname = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs.executeSql(check, poolname);
                    logger_1e13fc37.error("b == " + b );
                    if (rs.next()) {
                        logger_1e13fc37.error(String.valueOf("EASIntermediaryCheckAction：--" + requestid + "该中介机构已备案！"));
                        request.getRequestManager().setMessageid("ERROR");
                        request.getRequestManager().setMessagecontent("该中介机构已备案！");
                    }
                }
                logger_1e13fc37.error(String.valueOf("EASIntermediaryCheckAction：--" + requestid + "校验结束"));
            } catch (Exception e) {
                logger_1e13fc37.error(String.valueOf("EASIntermediaryCheckAction：--" + requestid + " 校验出错：" + e.toString()));
                return  WeaResult.fail(500, "EASIntermediaryCheckAction：--" + requestid + " 校验出错：" + e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));

            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_1e13fc37.error("EASIntermediaryCheckAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "EASIntermediaryCheckAction 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;
    }
}
