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

import java.util.*;
import java.text.*;
import java.math.*;
import java.util.Date;
import  com.weaver.seconddev.interfaces.srmoa.*;
import  com.weaver.seconddev.interfaces.swfa.*;
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.RecordSet;
import com.weaver.verupgrade.conn.RecordSetDataSource;
import com.weaver.verupgrade.general.TimeUtil;
import com.weaver.verupgrade.general.Util;
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.hrm.resource.ResourceComInfo;

/**
 * SRM--亨通商城审批接口--libin2017017
 * @author Administrator
 */
@org.springframework.stereotype.Service("workflow_action_OaSrmCheckMallRefuseAction")
public class OaSrmCheckMallRefuseAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

    @Autowired
    private CallSrmCheckMallServices srm;


    private static final Logger logger_6576dfe9 = LoggerFactory.getLogger(OaSrmCheckMallRefuseAction.class);

    public WeaResult<Map<String, Object>> execute(Map<String, Object> requestInfoMap) {
        logger_6576dfe9.error("OaSrmCheckMallRefuseAction 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 {
            int reqid = 0;
            String requestId = "";
            // 商城采购申请号
            String prno = "";
            // 1批准、-1驳回
            int shzt = -1;
            try {
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                BillFieldUtil unit = new BillFieldUtil();
                // 流程请求ID
                requestId = request.getRequestid();
                logger_6576dfe9.error(String.valueOf("OaSrmCheckMallRefuseAction-Request: " + requestId + "开始"));
                MallPurReqChkServiceServiceStub.MallPurReqIN resultin = new MallPurReqChkServiceServiceStub.MallPurReqIN();
                // 封装单据主信息
                // 封装表单信息
                // 获取表单主字段信息
                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("cgsqh")) {
                        // 商城采购申请号
                        prno = value;
                    }
                }
                try {
                    reqid = Integer.parseInt(requestId);
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
                resultin.setRequestId(reqid);
                resultin.setCheckStatus(shzt);
                resultin.setPrNo(prno);
                // writeLog("调用接口开始0 ");
                logger_6576dfe9.error(String.valueOf("调用接口完成RequestId--" + requestId + "--prno--" + prno + "--shzt--" + shzt));
                String sta = "";
                String meg = "";
//                CallSrmCheckMallServices srm = new CallSrmCheckMallServices();
                srm.setOainput(resultin);
                String[] rtInfo = srm.call();
                logger_6576dfe9.error(String.valueOf(requestId + " 调用接口完成prno--" + rtInfo[0] + "--Istat--" + rtInfo[1] + "--Srmsage--" + rtInfo[2]));
                sta = rtInfo[1];
                meg = rtInfo[2].replace("'", "’");
                String tablename = "";
                String wfid = request.getWorkflowid();
//                tablename = unit.getTablename(wfid);
                tablename = request.getRequestManager().getBillTableName();
                String logsql = "update e10_core_business.dbo." + tablename + " set sta ='" + sta + "', meg = '" + meg + "' where id = " + requestId;
                logger_6576dfe9.error(String.valueOf(requestId + " logsql:" + logsql));
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(logsql, poolname);
                logger_6576dfe9.error(String.valueOf("b--"+b));
                logger_6576dfe9.error(String.valueOf("OaSrmCheckMallRefuseAction-Request: " + requestId + "完成"));
                if (!sta.equals("S")) {
                    request.getRequestManager().setMessagecontent("推送SRM失败:" + meg);
                    request.getRequestManager().setMessageid("ERROR:");
                    return  WeaResult.fail(500, "OaSrmCheckMallRefuseAction meg"+ meg, getResultMapForAction(weaverResultMap, "result",meg, request.getRequestManager()));
                }
            } catch (Exception e) {
                logger_6576dfe9.error(String.valueOf(requestId + " error:" + e.getMessage()));
                logger_6576dfe9.error(String.valueOf(e));
                request.getRequestManager().setMessagecontent("推送SRM失败，接口异常请联系管理员");
                request.getRequestManager().setMessageid("ERROR:");
                return  WeaResult.fail(500, "OaSrmCheckMallRefuseAction err"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), request.getRequestManager()));
            }
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", SUCCESS, request.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_6576dfe9.error("OaSrmCheckMallRefuseAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "OaSrmCheckMallRefuseAction 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;
    }
}
