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

import java.util.*;
import java.rmi.RemoteException;
import java.text.*;
import java.math.*;
import  com.weaver.seconddev.interfaces.swfa.*;
import  com.weaver.seconddev.interfaces.swfa.sl.*;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
import com.weaver.verupgrade.conn.CONN_TYPE;
import java.lang.*;
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.*;
import com.weaver.verupgrade.general.TimeUtil;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.interfaces.workflow.action.Action;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.conn.RecordSet;

/**
 * 出差申请--安全级别判断--qingf20170214
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_ZOA_CCSQ_AQJB_Action")
public class ZOA_CCSQ_AQJB_Action extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_31bb84de = LoggerFactory.getLogger(ZOA_CCSQ_AQJB_Action.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_31bb84de.error("ZOA_CCSQ_AQJB_Action 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 {
            try {
                String sqr = "";
                String aqjb = "90";
                // 流程请求ID
                String reid = request.getRequestid();
                // 出差随行人员
                String ccsxry = "";
                logger_31bb84de.error(String.valueOf("ZOA_CCSQ_AQJB_Action-Request: " + reid + "开始"));
                RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
                // ResourceComInfo hr = new ResourceComInfo();
                BillFieldUtil unit = new BillFieldUtil();
                // 封装表单信息
                // 获取表单主字段信息
                Property[] properties = request.getMainTableInfo().getProperty();
                for (int i = 0; i < properties.length; i++) {
                    // 主表数据
                    // 字段名
                    String name = properties[i].getName().toLowerCase();
                    // 值
                    String value = Util.null2String(properties[i].getValue());
                    if (name.equals("sqr")) {
                        // 出差人
                        sqr = value;
                    }
                    if (name.equals("ccsxry")) {
                        // 出差随行人员
                        ccsxry = value;
                    }
                }
                String tablename = "";
                String wfid = request.getWorkflowid();
//                tablename = unit.getTablename(wfid);
                tablename = request.getRequestManager().getBillTableName();
                String logsql = "";
                if (ccsxry.equals("")) {
                    ccsxry = "0";
                }
                if (sqr.equals("")) {
                    sqr = "0";
                }
                if (!tablename.equals("")) {
                    logsql = "update e10_core_business.dbo." + tablename + " set aqjb = (select max(sec_level) from eteams.dbo.employee as  HrmResource where id in (" + sqr + ") or id in (" + ccsxry + ") ) where id = " + reid;
                    logger_31bb84de.error(String.valueOf("logsql:" + logsql));
                    String poolname = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs1.executeSql(logsql, poolname);
                    logger_31bb84de.error(String.valueOf("b--"+b));
                }
                logger_31bb84de.error(String.valueOf("ZOA_CCSQ_AQJB_Action-Request: " + reid + "完成"));
            } catch (Exception e) {
                logger_31bb84de.error("Exception:" , e);
                return  WeaResult.fail(500, "ZOA_CCSQ_AQJB_Action err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
                // e.printStackTrace();
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_31bb84de.error("ZOA_CCSQ_AQJB_Action e: {}", e);
            return  WeaResult.fail(500, "ZOA_CCSQ_AQJB_Action 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;
    }
}
