// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3)
// Source File Name:   ZOA_MM_PR_CREATE_BpAction.java
package com.weaver.seconddev.interfaces.workflow.action;

import java.text.SimpleDateFormat;
import java.util.Date;
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;

// Referenced classes of package  com.weaver.seconddev.interfaces.workflow.action:
// ZOA_MM_PR_CREATE, Action
@org.springframework.stereotype.Service("workflow_action_JiaBanCheckAction")
public class JiaBanCheckAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_f2eef612 = LoggerFactory.getLogger(JiaBanCheckAction.class);

    public JiaBanCheckAction() {
    }

    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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            try {
                Map<String, String> map = new HashMap<String, String>();
                String id = requestinfo.getRequestid();
                String tablename = requestinfo.getRequestManager().getBillTableName();
                Property[] aproperty = requestinfo.getMainTableInfo().getProperty();
                for (int j = 0; j < aproperty.length; j++) {
                    String name = aproperty[j].getName().toUpperCase();
                    String value = Util.null2String(aproperty[j].getValue());
                    map.put(name, value);
                }
                String ksrq = map.get("KSRQ");
                String kssj = map.get("KSSJ");
                String jsrq = map.get("JSRQ");
                String jssj = map.get("JSSJ");
                String applicant = map.get("XM");
                String startLaststr = ksrq + " " + kssj;
                String endLaststr = jsrq + " " + jssj;
                SimpleDateFormat __dateMinuteFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                String result = "";
                String sql = "select t.requestId, t.ksrq+' '+t.kssj as starttime,t.jsrq+' '+t.jssj as endtime from e10_core_business.dbo." + tablename + " t where t.requestId != " + id + " and t.xm = " + applicant;
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                logger_f2eef612.error("b -----"+ b);
                while (rs.next()) {
                    String requestId = Util.null2String(rs.getString("requestId"));
                    String startstr = Util.null2String(rs.getString("starttime"));
                    String endstr = Util.null2String(rs.getString("endtime"));
                    Date startLast = null;
                    Date endLast = null;
                    Date start = null;
                    Date end = null;
                    if (startLaststr != null && endLaststr != null && startstr != null && endstr != null) {
                        startLast = __dateMinuteFormatter.parse(startLaststr);
                        endLast = __dateMinuteFormatter.parse(endLaststr);
                        start = __dateMinuteFormatter.parse(startstr);
                        end = __dateMinuteFormatter.parse(endstr);
                        if (!(start.after(endLast) || end.before(startLast) || start.compareTo(endLast) == 0 || end.compareTo(startLast) == 0)) {
                            // 结果字符串
                            result += "," + requestId;
                        }
                    }
                }
                if (result != null && !result.equals("")) {
                    result = result.replaceFirst(",", "");
                    requestinfo.getRequestManager().setMessageid("ERROR");
                    requestinfo.getRequestManager().setMessagecontent("本次申请和流程(" + result + "有重复)");
                    return  WeaResult.fail(500, "本次申请和流程(" + result + "有重复)", getResultMapForAction(weaverResultMap, "result","本次申请和流程(" + result + "有重复)", requestinfo.getRequestManager()));

                }
            } catch (Exception exception) {
                logger_f2eef612.error(String.valueOf("JiaBanCheckAction error:" + exception.getMessage()));
                requestinfo.getRequestManager().setMessageid("ERROR");
                requestinfo.getRequestManager().setMessagecontent(exception.getMessage());
                return  WeaResult.fail(500, "JiaBanCheckAction err"+ exception, getResultMapForAction(weaverResultMap, "result",exception.getMessage(), requestinfo.getRequestManager()));

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