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

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 java.util.Map;
import java.util.HashMap;
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_DeleteSapGuidAction")
public class DeleteSapGuidAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_5b06e417 = LoggerFactory.getLogger(DeleteSapGuidAction.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 {
            logger_5b06e417.error(String.valueOf("do DeleteSapGuidAction on request:" + request.getRequestid()));
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            try {
                // 获取主表数据
                // GUID
                String guid = "";
                // sqlx
                String sqlx = "";
                // hh
                String hh = "";
                // 表单名称
                String tablename = request.getRequestManager().getBillTableName();
                String requestid = request.getRequestid();
                String workflowid = request.getWorkflowid();
                String xxid = "";
                Property[] aproperty = request.getMainTableInfo().getProperty();
                for (int i = 0; i < aproperty.length; i++) {
                    String name = aproperty[i].getName().toUpperCase();
                    String value = Util.null2String(aproperty[i].getValue());
                    if (name.equals("GUID")) {
                        // GUID
                        guid = value;
                        logger_5b06e417.error(String.valueOf("得到GUID：" + guid));
                    }
                    if (name.equals("SQLX")) {
                        // 申请类型
                        sqlx = value;
                        logger_5b06e417.error(String.valueOf("得到SQLX：" + sqlx));
                    }
                    if (name.equals("HH")) {
                        // 行号
                        hh = value;
                        logger_5b06e417.error(String.valueOf("得到HH：" + hh));
                    }
                    if (name.equals("XXID")) {
                        xxid = value;
                        logger_5b06e417.error(String.valueOf("得到消息GUID：" + xxid));
                    }
                }
                if ("0".equals(sqlx) && !"1878".equals(workflowid)) {
                    guid = xxid.replaceAll("<br>", "").replaceAll("&nbsp;", "");
                }
                guid = guid.trim();
                if ("".equals(guid)) {
                    request.getRequestManager().setMessageid("ERROR");
                    request.getRequestManager().setMessagecontent("guid不能为空");
                    return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
                }
                ZITF_OA_DELETE oa_DELETE = new ZITF_OA_DELETE();
                String[] result = oa_DELETE.Getexecute(guid, sqlx, hh);
                String state = result[0];
                String message = result[1];
                // 把返回的值存到流程中
                String sql = (new StringBuilder()).append("update e10_core_business.dbo.").append(tablename).append(" set sta ='").append(state).append("', meg = '").append(message).append("' where id = ").append(requestid).toString();
                logger_5b06e417.error(String.valueOf((new StringBuilder()).append("logsql:").append(sql).toString()));
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                logger_5b06e417.error(String.valueOf("b2 -----"+ b));
            } catch (Exception e) {
                logger_5b06e417.error(String.valueOf(e.getMessage()));
                request.getRequestManager().setMessageid("ERROR");
                request.getRequestManager().setMessagecontent(e.getMessage());
                return  WeaResult.fail(500, "DeleteSapGuidAction ERROR"+ e, getResultMapForAction(weaverResultMap, "result", e.getMessage(), request.getRequestManager()));
            }
            logger_5b06e417.error(String.valueOf("do DeleteSapGuidAction success!"));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_5b06e417.error("DeleteSapGuidAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "DeleteSapGuidAction 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;
    }
}
