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

import  com.weaver.seconddev.interfaces.oames.ReturnMesWLPS_SYService;
import  com.weaver.seconddev.interfaces.oames.SI_OA_HTOAApproFlowSync_OUTServiceStub;
import  com.weaver.seconddev.interfaces.swfa.BillFieldUtil;
import java.util.UUID;
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.hrm.resource.ResourceComInfo;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

/**
 * 亨通光电替代料审批流程
 */
@org.springframework.stereotype.Service("workflow_action_HTGD_MES_TDLApproveActivon")
public class HTGD_MES_TDLApproveActivon extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private ReturnMesWLPS_SYService service;
    private static final Logger logger_674a8052 = LoggerFactory.getLogger(HTGD_MES_TDLApproveActivon.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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestId = requestinfo.getRequestid();
            try {
                String requestName = requestinfo.getRequestManager().getRequestname();
                String creatorId = requestinfo.getCreatorid();
                // 工作流ID
                String workFlowId = requestinfo.getWorkflowid();
                logger_674a8052.error(String.valueOf("HTGD_MES_TDLApproveActivon-start-" + requestId));
                logger_674a8052.error("requestIdTemp == " +requestIdTemp);
                logger_674a8052.error("userIdTemp == " +userIdTemp);
                String fxzt = "";
                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());
                    if ("SPJG".equals(name)) {
                        fxzt = value;
                    }
                }
                ResourceComInfo rsCom = I18nContextUtil.getBean(ResourceComInfo.class);
                String uuid = UUID.randomUUID().toString();
//                ReturnMesWLPS_SYService service = new ReturnMesWLPS_SYService();
                SI_OA_HTOAApproFlowSync_OUTServiceStub.WLPS_SY_Request req = new SI_OA_HTOAApproFlowSync_OUTServiceStub.WLPS_SY_Request();
                SI_OA_HTOAApproFlowSync_OUTServiceStub.ArrayOfWLPS_SY_Main mainData = new SI_OA_HTOAApproFlowSync_OUTServiceStub.ArrayOfWLPS_SY_Main();
                SI_OA_HTOAApproFlowSync_OUTServiceStub.WLPS_SY_Main param = new SI_OA_HTOAApproFlowSync_OUTServiceStub.WLPS_SY_Main();
                param.setWorkflowid(workFlowId);
                param.setFxzt(fxzt);
                param.setRequestName(requestName);
                param.setRequestid(requestId);
                param.setCreator(rsCom.getWorkcode(creatorId));
                // param.setPlaceholder1(mjson.toJSONString());
                // writeLog(mjson.toJSONString());
                mainData.addWLPS_SY_Main(param);
                req.setSYSID("SYS052");
                req.setSENDID(uuid);
                req.setITFID("HT01ETS009");
                req.setDATAS(mainData);
                String[] ret = service.returnRes(req);
                logger_674a8052.error(String.valueOf(requestId + "-HTGD_MES_TDLApproveActivon info:" + ret[0] + "-" + ret[1]));
                BillFieldUtil util = new BillFieldUtil();
//                String tablename = util.getTablename(workFlowId);
                String tablename = requestinfo.getRequestManager().getBillTableName();
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql("update e10_core_business.dbo." + tablename + " set state='" + ret[0] + "',message='" + ret[1] + "' where id=" + requestId, poolname);
                if (b) {
                    logger_674a8052.error(String.valueOf(requestId + "-HTGD_MES_TDLApproveActivon update success"));
                } else {
                    logger_674a8052.error(String.valueOf(requestId + "-HTGD_MES_TDLApproveActivon update fail"));
                    return  WeaResult.fail(500, requestId + "-HTGD_MES_TDLApproveActivon update fail", getResultMapForAction(weaverResultMap, "result",requestId + "-HTGD_MES_TDLApproveActivon update fail", requestinfo.getRequestManager()));

                }
            } catch (Exception exception) {
                logger_674a8052.error(String.valueOf(requestId + "-HTGD_MES_TDLApproveActivon error:" + exception.getMessage()));
                return  WeaResult.fail(500, "HTGD_MES_TDLApproveActivon error"+ exception, getResultMapForAction(weaverResultMap, "result",exception.getMessage(), requestinfo.getRequestManager()));

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