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

import com.weaver.seconddev.interfaces.htjt.util.TransUtil;
import  com.weaver.seconddev.interfaces.gdwmsoa.ZMES_OA_SUBMIT_SERVICE;
import  com.weaver.seconddev.interfaces.gdwmsoa.ZMES_OA_SUBMIT_SERVICEStub;
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 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.soa.workflow.request.Property;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

@org.springframework.stereotype.Service("workflow_action_OaWmsReturnAction")
public class OaWmsReturnAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private  ZMES_OA_SUBMIT_SERVICE service;


    private static final Logger logger_824c0f2 = LoggerFactory.getLogger(OaWmsReturnAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_824c0f2.error("OaWmsReturnAction 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 requestinfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            String requestid = requestinfo.getRequestid();
            logger_824c0f2.error(String.valueOf(requestid + "--OaWmsReturnAction--start"));
            String createrid = requestinfo.getCreatorid();
            String workflowid = requestinfo.getWorkflowid();
            TransUtil tu = new TransUtil();
//            String tableName = tu.getTableName(workflowid);
            String tableName = requestinfo.getRequestManager().getBillTableName();
            String[] str = new String[2];
            String requestname = requestinfo.getRequestManager().getRequestname();
            Property[] properties = requestinfo.getMainTableInfo().getProperty();
            String spjg = "";
            // 触发系统 0 吴江 1内蒙
            String cfxt = "";
            RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
            String sql = "";
            for (int i = 0; i < properties.length; i++) {
                String name = properties[i].getName().toUpperCase();
                String value = Util.null2String(properties[i].getValue());
                if ("SPJG".equals(name)) {
                    spjg = value;
                }
                if ("CFXT".equals(name)) {
                    cfxt = value;
                }
            }
            // 获取申请人直接上级
            String zjsj = "";
            sql = "select (select lastname from eteams.dbo.employee as  hrmresource where id=b.superior) as zjsj from e10_core_business.dbo." + tableName + " a,employee b where a.sqr=b.id  and a.id =" + requestid;
            logger_824c0f2.error("sql--" + sql);
            String poolname = CONN_TYPE.workflow.getType();
            // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
            boolean b = rs.executeSql(sql, poolname);
            logger_824c0f2.error(String.valueOf("b-->" + b));
            if (rs.next()) {
                zjsj = Util.null2String(rs.getString("zjsj"));
            }
            logger_824c0f2.error(String.valueOf(requestid + "-" + createrid + "-" + requestid + "-" + requestname + "-" + workflowid + "-" + spjg + "-" + zjsj));
            try {
                ZMES_OA_SUBMIT_SERVICEStub.ArrayOfZMES_OA_SUBMIT_ITEM_TYPE param = new ZMES_OA_SUBMIT_SERVICEStub.ArrayOfZMES_OA_SUBMIT_ITEM_TYPE();
                ZMES_OA_SUBMIT_SERVICEStub.ZMES_OA_SUBMIT_ITEM_TYPE[] paramItem = new ZMES_OA_SUBMIT_SERVICEStub.ZMES_OA_SUBMIT_ITEM_TYPE[1];
                ZMES_OA_SUBMIT_SERVICEStub.ZMES_OA_SUBMIT_ITEM_TYPE item = new ZMES_OA_SUBMIT_SERVICEStub.ZMES_OA_SUBMIT_ITEM_TYPE();
                item.setCreator(createrid);
                item.setRequestid(requestid);
                item.setRequestName(requestname);
                item.setWorkflowid(workflowid);
                item.setZzpdjg(spjg);
                item.setPlaceholder1(zjsj);
                paramItem[0] = item;
                param.setZMES_OA_SUBMIT_ITEM_TYPE(paramItem);
//                ZMES_OA_SUBMIT_SERVICE service = new ZMES_OA_SUBMIT_SERVICE();
                str = service.call(param, requestid, cfxt);
                logger_824c0f2.error(String.valueOf(requestid + "--OaWmsReturnAction--result:" + str[0] + "-" + str[1]));
                sql = "update e10_core_business.dbo." + tableName + " set state='" + str[0] + "',message='" + str[1] + "' where id=" + requestid;
                logger_824c0f2.error("sql--" + sql);
                String poolname_et = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b1 = rs.executeSql(sql, poolname_et);
                logger_824c0f2.error(String.valueOf("b-->" + b1));
                if (!"S".equals(str[0])) {
                    requestinfo.getRequestManager().setMessagecontent(requestid + "光导WMS系统返回信息：" + str[1]);
                    return  WeaResult.fail(500, "光导WMS系统返回信息：" + str[1], getResultMapForAction(weaverResultMap, "result","光导WMS系统返回信息：" + str[1], requestinfo.getRequestManager()));
                }
            } catch (Exception e) {
                sql = "update e10_core_business.dbo." + tableName + " set state='E',message='" + e.toString() + "' where id=" + requestid;
                logger_824c0f2.error("sql--"+sql);
                String poolname_bm = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b1 = rs.executeSql(sql, poolname_bm);
                logger_824c0f2.error(String.valueOf("b-->" + b1));
                logger_824c0f2.error(String.valueOf(requestid + "--OaWmsReturnAction--result:" + e.toString()));
                requestinfo.getRequestManager().setMessagecontent("审批结果返回光导WMS系统出错：" + e.toString());
                return  WeaResult.fail(500, "审批结果返回光导WMS系统出错：" + e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), requestinfo.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_824c0f2.error("OaWmsReturnAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "OaWmsReturnAction 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;
    }
}
