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

import java.util.HashMap;
import java.util.Map;
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.general.BaseBean;
import com.weaver.verupgrade.general.Util;
import com.weaver.verupgrade.soa.workflow.request.*;
import com.weaver.verupgrade.soa.workflow.request.Property;
import com.weaver.verupgrade.soa.workflow.request.DetailTable;
import com.weaver.verupgrade.soa.workflow.request.Row;
import com.weaver.verupgrade.soa.workflow.request.Cell;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    private static final Logger logger_69d789ca = LoggerFactory.getLogger(ZSRM_PUR_INFO_GY132_CheckAction.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);


        logger_69d789ca.error("----- workflow_action_ZSRM_PUR_INFO_GY132_CheckAction new -----");
        logger_69d789ca.error("requestIdTemp -----"+ requestIdTemp);
        logger_69d789ca.error("userIdTemp -----"+ userIdTemp);
        try {
            String requestid = requestinfo.getRequestid();
            try {
                logger_69d789ca.error(String.valueOf("ZSRM_PUR_INFO_GY132_CheckAction-start-" + requestid));
                String sync = "";
                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 ("SFXYWHJGXXJL".equals(name)) {
                        sync = value;
                    }
                }
                logger_69d789ca.error("sync -----" + sync);
                if ("0".equals(sync)) {
                    DetailTable[] detailtable = requestinfo.getDetailTableInfo().getDetailTable();
                    DetailTable dtq = detailtable[1];
                    Row[] sq = dtq.getRow();
                    if (sq.length > 0) {
                        String[][] str = new String[sq.length][21];
                        for (int j = 0; j < sq.length; j++) {
                            Row rq = sq[j];
                            Cell[] cq = rq.getCell();
                            Map<String, String> map = new HashMap<String, String>();
                            for (int k = 0; k < cq.length; k++) {
                                Cell cq1 = cq[k];
                                String nameq = cq1.getName().toUpperCase();
                                String valueq = Util.null2String(cq1.getValue());
                                logger_69d789ca.error("workflow_action_ZSRM_PUR_INFO_GY132_CheckAction nameq --" + nameq);
                                logger_69d789ca.error("workflow_action_ZSRM_PUR_INFO_GY132_CheckAction valueq --" + valueq);
                                map.put(nameq, valueq);
                            }
                            str[j][0] = requestid + "_" + j;
                            str[j][1] = null2String(map.get("CGZZ"));
                            str[j][2] = null2String(map.get("CGZ"));
                            str[j][3] = null2String(map.get("WL"));
                            str[j][4] = null2String(map.get("GYSZHH"));
                            str[j][5] = null2String(map.get("DDDW"));
                            str[j][6] = null2String(map.get("JBDW"));
                            str[j][7] = null2String(map.get("JBDWFZ"));
                            str[j][8] = null2String(map.get("ZSDLS"));
                            str[j][9] = null2String(map.get("BZDLS"));
                            str[j][10] = null2String(map.get("YTJJHSJ"));
                            str[j][11] = null2String(map.get("XSGMSDM"));
                            str[j][12] = null2String(map.get("JLFL"));
                            str[j][13] = null2String(map.get("YXQSRQ")).replaceAll("-", "");
                            str[j][14] = null2String(map.get("YXJZRQ")).replaceAll("-", "");
                            str[j][15] = null2String(map.get("JJG"));
                            str[j][16] = null2String(map.get("JGDW"));
                            str[j][17] = null2String(map.get("GYSWLZ"));
                            str[j][18] = "X";
                            str[j][19] = map.get("HB");
                            str[j][20] = requestid;
                        }
                        ZSRM_PUR_INFO info = new ZSRM_PUR_INFO();
                        String[] ret = info.Getexecute(str);
                        logger_69d789ca.error(String.valueOf("ZSRM_PUR_INFO_GY132_CheckAction-return-" + requestid + "-[" + ret[0] + "," + ret[1] + "]"));
                        if (!"S".equals(ret[0])) {
                            requestinfo.getRequestManager().setMessageid("ERROR");
                            requestinfo.getRequestManager().setMessagecontent("SAP返回错误信息：" + ret[1]);
                            return  WeaResult.fail(500, "SAP返回错误信息：" + ret[1], getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
                        }
                    }
                }
            } catch (Exception e) {
                logger_69d789ca.error("ZSRM_PUR_INFO_GY132_CheckAction-error-" + requestid + "--",e);
                requestinfo.getRequestManager().setMessageid("ERROR");
                requestinfo.getRequestManager().setMessagecontent("调用SAP接口校验数据出错：" + e.toString());
                return  WeaResult.fail(500, "调用SAP接口校验数据出错：" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
            }
            logger_69d789ca.error(String.valueOf("ZSRM_PUR_INFO_GY132_CheckAction-end-" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_69d789ca.error("ZSRM_PUR_INFO_GY132_CheckAction e: {}", e);
            return  WeaResult.fail(500, "ZSRM_PUR_INFO_GY132_CheckAction e: {}"+ e, getResultMapForAction(weaverResultMap, "result", "", 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;
    }

    public static String null2String(String s){
        return s == null ? "" : s;
    }

    public static String null2String(Object o){
        return o == null ? "" : o.toString();
    }

    public static String null2String(String s1,String s2){
        return s1 == null ? (s2 == null ? "" : s2) : s1;
    }
}
