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

import java.util.*;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.text.*;
import java.math.*;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
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.workflow.workflow.WorkflowBillComInfo;
import com.weaver.verupgrade.workflow.workflow.WorkflowComInfo;
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;

/**
 * 最后审批取赋值--qingf20151223
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_LastOperateAction")
public class LastOperateAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    private static final Logger logger_e95a7685 = LoggerFactory.getLogger(LastOperateAction.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);
        logger_e95a7685.error("workflow_action_LastOperateAction start");
        try {
            try {
                RecordSet rs1 = I18nContextUtil.getBean(RecordSet.class);
                ResourceComInfo hr = I18nContextUtil.getBean(ResourceComInfo.class);
                WorkflowBillComInfo wkbl = I18nContextUtil.getBean(WorkflowBillComInfo.class);
                WorkflowComInfo wkfl = I18nContextUtil.getBean(WorkflowComInfo.class);
                // 流程请求ID
                String reid = request.getRequestid();
                String workflowid = request.getWorkflowid();

                // 获取审批人姓名
                String spr = hr.getResourcename(String.valueOf(userIdTemp));
//                String tablename = wkbl.getTablename(wkfl.getFormId(workflowid));
                String tablename = request.getRequestManager().getBillTableName();
                logger_e95a7685.error("workflow_action_LastOperateAction tablename==>",tablename);
                String logsql = "update e10_core_business.dbo." + tablename + " set spr = '" + spr + "' where id = " + reid;
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs1.executeSql(logsql, poolname);
                logger_e95a7685.error(String.valueOf("b--"+b));
                logger_e95a7685.error(String.valueOf("start log"));
                logger_e95a7685.error(String.valueOf("------------------------------------------------------------------------"));
                logger_e95a7685.error(String.valueOf("LastOperateAction:" + logsql));
                logger_e95a7685.error(String.valueOf("------------------------------------------------------------------------"));
                logger_e95a7685.error(String.valueOf("end log"));
            } catch (Exception e) {
                // BaseBean baseBean = new BaseBean();
                logger_e95a7685.error(String.valueOf("start log"));
                logger_e95a7685.error(String.valueOf("------------------------------------------------------------------------"));
                logger_e95a7685.error(String.valueOf("LastOperateAction error:" + e.toString()));
                logger_e95a7685.error(String.valueOf("------------------------------------------------------------------------"));
                logger_e95a7685.error(String.valueOf("end log"));
                return  WeaResult.fail(500, "LastOperateAction err "+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_e95a7685.error("LastOperateAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "LastOperateAction 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;
    }
}
