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

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

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

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

    private static final Logger logger_78218549 = LoggerFactory.getLogger(ZSRM_PUR_INFO_Action.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_78218549.error("----- workflow_action_ZSRM_PUR_INFO_Action new -----");
        logger_78218549.error("requestIdTemp -----"+ requestIdTemp);
        logger_78218549.error("userIdTemp -----"+ userIdTemp);


        try {
            String requestid = requestinfo.getRequestid();
            String wfid = requestinfo.getWorkflowid();
            try {
                logger_78218549.error(String.valueOf("ZSRM_PUR_INFO_Action-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_78218549.error("xxjllx -----"+ xxjllx);
                DetailTable[] detailtable = requestinfo.getDetailTableInfo().getDetailTable();
                DetailTable dtq = detailtable[0];
                Row[] sq = dtq.getRow();
                logger_78218549.error("sq -----"+ sq.toString());
                StringBuilder sb = new StringBuilder();
                String state = "";
                if (sq.length > 0) {
                    logger_78218549.error("sqlength");
                    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] = "";
                        str[j][19] = map.get("HB");
                        str[j][20] = requestid;
                    }
                    ZSRM_PUR_INFO info = new ZSRM_PUR_INFO();
                    logger_78218549.error("info ---- " + info.toString());
                    String[] ret = info.Getexecute(str);
                    state = ret[0];
                    logger_78218549.error(String.valueOf("ZSRM_PUR_INFO_Action-return-" + requestid + "-[" + ret[0] + "," + ret[1] + "]"));
                    sb.append("信息记录新增结果：" + ret[1]);
                }
                if ("4".equals(xxjllx)) {
                    DetailTable[] dt = requestinfo.getDetailTableInfo().getDetailTable();
                    DetailTable dtm = dt[1];
                    Row[] rows = dtm.getRow();
                    String[][] param = new String[rows.length][9];
                    String gsdm = "";
                    for (int l = 0; l < rows.length; l++) {
                        Row row = rows[l];
                        Cell[] cell = row.getCell();
                        Map<String, String> map1 = new HashMap<String, String>();
                        for (int m = 0; m < cell.length; m++) {
                            Cell cl = cell[m];
                            String nameI = cl.getName().toUpperCase();
                            String valueI = Util.null2String(cl.getValue());
                            map1.put(nameI, valueI);
                        }
                        param[l][0] = map1.get("GYSWLZ");
                        param[l][1] = map1.get("YXQZ").replaceAll("-", "");
                        param[l][2] = map1.get("YXQQ").replaceAll("-", "");
                        param[l][3] = map1.get("JE");
                        param[l][4] = map1.get("JLDW");
                        param[l][5] = map1.get("DWKDBS");
                        param[l][6] = map1.get("HBDW");
                        param[l][7] = "";
                        param[l][8] = "";
                        gsdm = map1.get("GSDM");
                    }
                    ZOA_MM_CONDREC_RFC service = new ZOA_MM_CONDREC_RFC();
                    logger_78218549.error("service ---- " + service.toString());
                    String[] res = service.Getexecute(gsdm, param);
                    logger_78218549.error(String.valueOf("ZSRM_PUR_INFO_Action-CONDREC_return-" + requestid + "-[" + res[0] + "," + res[1] + "]"));
                    sb.append("黄金实际价新增结果：" + res[1]);
                    if (!"S".equals(res[0])) {
                        state = res[0];
                    }
                }
                RecordSet rs = I18nContextUtil.getBean(RecordSet.class);
                BillFieldUtil util = new BillFieldUtil();
//                String tablename = util.getTablename(wfid);
                String tablename = requestinfo.getRequestManager().getBillTableName();
                String sql = "update e10_core_business.dbo." + tablename + " set state='" + state + "',message='" + sb.toString() + "' where id=" + requestid;
                logger_78218549.error("sql -----"+ sql);
                String poolname = CONN_TYPE.workflow.getType();
                // TODO E10执行sql方法第二参数必须指定源，默认使用流程源，单体客户无需修改，微服务/组合客户需根据查询表所属服务切换源，或使用外部数据源
                boolean b = rs.executeSql(sql, poolname);
                logger_78218549.error("b -----"+ b);
            } catch (Exception e) {
                logger_78218549.error(String.valueOf("ZSRM_PUR_INFO_Action-error-" + requestid + "-[" + e.toString() + "]"));
                requestinfo.getRequestManager().setMessagecontent("调用SAP接口同步数据出错：" + e.toString());
                return  WeaResult.fail(500, "调用SAP接口同步数据出错：" + e, getResultMapForAction(weaverResultMap, "result", "", requestinfo.getRequestManager()));
            }
            logger_78218549.error(String.valueOf("ZSRM_PUR_INFO_Action-end-" + requestid));
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", Action.SUCCESS, requestinfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_78218549.error("ZSRM_PUR_INFO_Action e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZSRM_PUR_INFO_Action 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;
    }
}
