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

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
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_CheckAction")
public class ZSRM_PUR_INFO_CheckAction extends BaseBean implements Action, EsbServerlessRpcRemoteInterface {

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

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

        logger_9ad0d62b.error("----- workflow_action_ZSRM_PUR_INFO_CheckAction new -----");
        logger_9ad0d62b.error("requestIdTemp -----"+ requestIdTemp);
        logger_9ad0d62b.error("userIdTemp -----"+ userIdTemp);
        try {
            String requestid = requestinfo.getRequestid();
            try {
                logger_9ad0d62b.error(String.valueOf("ZSRM_PUR_INFO_CheckAction-start-" + requestid));
                String xxjllx = "";
                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 ("XXJLLX".equals(name)) {
                        xxjllx = value;
                    }
                }
                logger_9ad0d62b.error("xxjllx -----"+ xxjllx);
                DetailTable[] detailtable1 = requestinfo.getDetailTableInfo().getDetailTable();
                DetailTable dtq1 = detailtable1[1];
                Row[] sq1 = dtq1.getRow();

                logger_9ad0d62b.error("sq1 -----"+ sq1.toString());
                if ("4".equals(xxjllx)) {
                    logger_9ad0d62b.error("sq1.length -----"+ sq1.length);
                    if (sq1.length == 0) {
                        requestinfo.getRequestManager().setMessageid("ERROR");
                        requestinfo.getRequestManager().setMessagecontent("请填写黄金实际价信息明细！");
                        return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
                    }
                    Set<String> set = new HashSet<String>();
                    for (int k = 0; k < sq1.length; k++) {
                        Row r = sq1[k];
                        Cell[] c = r.getCell();
                        for (int l = 0; l < c.length; l++) {
                            Cell mc = c[l];
                            String named = mc.getName().toUpperCase();
                            String valued = Util.null2String(mc.getValue());
                            if ("GSDM".equals(named)) {
                                set.add(valued);
                            }
                        }
                    }
                    if (set.size() > 1) {
                        requestinfo.getRequestManager().setMessageid("ERROR");
                        requestinfo.getRequestManager().setMessagecontent("黄金实际价信息多行明细里的公司代码要保持一致！");
                        return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
                    }
                } else {
                    if (sq1.length > 0) {
                        requestinfo.getRequestManager().setMessageid("ERROR");
                        requestinfo.getRequestManager().setMessagecontent("信息记录类型不是黄金实际价时，不需要填写黄金实际价信息明细！");
                        return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
                    }
                }
                DetailTable[] detailtable = requestinfo.getDetailTableInfo().getDetailTable();
                DetailTable dtq = detailtable[0];
                logger_9ad0d62b.error("dtq -----"+ dtq.toString());
                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());
                            map.put(nameq, valueq);
                        }
                        str[j][0] = requestid + "_" + j;
                        str[j][1] = map.get("MXCGZZ");
                        str[j][2] = map.get("CGZ");
                        str[j][3] = map.get("MXWL");
                        str[j][4] = map.get("GYSZHH");
                        str[j][5] = map.get("DDDW");
                        str[j][6] = map.get("JBDW");
                        str[j][7] = map.get("JBDWFZ");
                        str[j][8] = map.get("ZSDLS");
                        str[j][9] = map.get("BZDLS");
                        str[j][10] = map.get("YTJJHSJ");
                        str[j][11] = map.get("XSGMSDM");
                        str[j][12] = map.get("JLFL");
                        str[j][13] = map.get("YXQSRQ").replaceAll("-", "");
                        str[j][14] = map.get("YXJZRQ").replaceAll("-", "");
                        str[j][15] = map.get("JJG");
                        str[j][16] = map.get("JGDW");
                        str[j][17] = 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_9ad0d62b.error(String.valueOf("ZSRM_PUR_INFO_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_9ad0d62b.error(String.valueOf("ZSRM_PUR_INFO_CheckAction-error-" + requestid + "-[" + e.toString() + "]"));
                requestinfo.getRequestManager().setMessageid("ERROR");
                requestinfo.getRequestManager().setMessagecontent("调用SAP接口校验数据出错：" + e.toString());
                return  WeaResult.fail(500, "调用SAP接口校验数据出错：" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));

            }
            logger_9ad0d62b.error(String.valueOf("ZSRM_PUR_INFO_CheckAction-end-" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_9ad0d62b.error("ZSRM_PUR_INFO_CheckAction e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZSRM_PUR_INFO_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;
    }
}
