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

import org.json.JSONObject;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
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 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_NonStandardAction")
public class HTGD_MES_NonStandardAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private ReturnMesWLPS_SYService service;
    private static final Logger logger_c8d15d65 = LoggerFactory.getLogger(HTGD_MES_NonStandardAction.class);

    @Override
    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();
            logger_c8d15d65.error(String.valueOf("HTGD_MES_NonStandardAction--" + requestId + "开始执行"));
            logger_c8d15d65.error("requestIdTemp == " +requestIdTemp);
            logger_c8d15d65.error("userIdTemp == " +userIdTemp);
            // 工作流ID
            String workFlowId = requestInfo.getWorkflowid();
            BillFieldUtil util = new BillFieldUtil();
//            String tablename = util.getTablename(workFlowId);
            String tablename = requestInfo.getRequestManager().getBillTableName();
            try {
                String requestName = requestInfo.getRequestManager().getRequestname();
                String creatorId = requestInfo.getCreatorid();
                Map<String, String> map = new HashMap<String, String>();
                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());
                    map.put(name, value);
                }
                ResourceComInfo rsCom = I18nContextUtil.getBean(ResourceComInfo.class);
                // 判断结果
                String zzpdjg = map.get("ZZPDJG");
                // 市场意见
                String opinion = map.get("SCYJ");
                // 业务员
                String ywy = map.get("YWY");
                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.setZzpdjg(zzpdjg);
                param.setWorkflowid(workFlowId);
                param.setRequestName(requestName);
                param.setRequestid(requestId);
                param.setCreator(rsCom.getWorkcode(creatorId));
                JSONObject obj = new JSONObject();
                obj.put("opinion", opinion);
                obj.put("judgeusername", rsCom.getWorkcode(ywy));
                obj.put("judgerealname", rsCom.getLastname(ywy));
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                obj.put("judgmentdate", sdf.format(new Date()));
                logger_c8d15d65.error(String.valueOf(obj.toString()));
                param.setPlaceholder1(obj.toString());
                mainData.addWLPS_SY_Main(param);
                req.setSYSID("SYS052");
                req.setSENDID(uuid);
                req.setITFID("HT01ETS009");
                req.setDATAS(mainData);
                String[] ret = service.returnRes(req);
                if ("E".equals(ret[0])) {
                    RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                    String sql = "update e10_core_business.dbo." + tablename + " set state='" + ret[0] + "',message='" + ret[1] + "' where id=" + requestId;
                    String poolname_ar = CONN_TYPE.workflow.getType();
                    // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                    boolean b = rs.executeSql(sql, poolname_ar);
                    logger_c8d15d65.error("b -----"+ b);
                }
                logger_c8d15d65.error(String.valueOf("HTGD_MES_NonStandardAction--" + requestId + "执行结束"));
            } catch (Exception exception) {
                logger_c8d15d65.error(String.valueOf("HTGD_MES_NonStandardAction--" + requestId + "执行出错：" + exception.getMessage()));
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                String sql = "update e10_core_business.dbo." + tablename + " set state='E',message='" + exception.getMessage() + "' where id=" + requestId;
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                logger_c8d15d65.error("b2 -----"+ b);
                return  WeaResult.fail(500, "HTGD_MES_NonStandardAction " +exception, getResultMapForAction(weaverResultMap, "result", exception.getMessage(), requestInfo.getRequestManager()));

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