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.formmode.setup.ModeRightInfo;
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_UpdateFwInfoAction")
public class UpdateFwInfoAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_af68f31d = LoggerFactory.getLogger(UpdateFwInfoAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_af68f31d.error("UpdateFwInfoAction start");
        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_af68f31d.error(String.valueOf("do UpdateFwInfoAction on request:" + request.getRequestid()));
            // 默认状态
            String state = "S";
            // 默认信息
            String message = "操作成功";
            String requestid = request.getRequestid();
            Map<String, String> map = new HashMap<String, String>();
            // 表单名称
            String tablename = request.getRequestManager().getBillTableName();
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            try {
                // 获取主表数据
                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());
                    map.put(name, value);
                }
                // 案件信息
                String ajxx = String.valueOf(map.get("AJXX"));
                // 审批状态	1(案件处理)=未通过	 3(结案)=通过
                String spzt = String.valueOf(map.get("SPZT"));
                // 把状态更新到表单建模中
                String sql = "update uf_FW01301 set xajzt = '" + spzt + "' where id = '" + ajxx+"'";
                logger_af68f31d.error(String.valueOf("UpdateFwInfoAction sql:" + sql));
                // rs.execute(sql);
                // 状态更新成功后 对单条数据进行重构
//                ModeRightInfo info = I18nContextUtil.getBean(ModeRightInfo.class);
                // 创建人ID <= 0时，创建人ID为原来该条数据的创建人
                /*创建人ID，表单模块ID，数据ID
 TODO 方法参数:0 类型发生变化 e9类型:int e10类型:long
 TODO 方法参数:108 类型发生变化 e9类型:int e10类型:long
 TODO 方法参数:Integer.parseInt(ajxx) 类型发生变化 e9类型:int e10类型:long
 TODO // 创建人字段已废弃 默认是用billid 数据对应的数据创建人*/
//                info.rebuildModeDataShareByEdit(0, 108, Integer.parseInt(ajxx));
            } catch (Exception e) {
                logger_af68f31d.error("UpdateFwInfoAction e: {}", e.getMessage());
                state = "E";
                message = e.getMessage();
                return  WeaResult.fail(500, "UpdateFwInfoAction err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
            } finally {
                // 重构成功后 更新状态
                String sql2 = (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_af68f31d.error(String.valueOf((new StringBuilder()).append("UpdateFwInfoAction 更新状态 logsql:").append(sql2).toString()));
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql2, poolname);
                logger_af68f31d.error("b -----"+ b);
            }
            logger_af68f31d.error(String.valueOf("do UpdateFwInfoAction success!"));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_af68f31d.error("UpdateFwInfoAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "UpdateFwInfoAction 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;
    }
}
