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

import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;
import com.weaver.common.i18n.tool.util.I18nContextUtil;
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.RecordSetTrans;
import com.weaver.verupgrade.general.BaseBean;
import com.weaver.verupgrade.interfaces.workflow.action.Action;

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

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

    private static final Logger logger_16612b22 = LoggerFactory.getLogger(ZOA_FI_CUSTOMER_MAINTAIN.class);

    private JCoDestination destination;

    private String[][] retstr;

    public ZOA_FI_CUSTOMER_MAINTAIN() {
        new SAPConn();
        this.destination = SAPConn.getJCoDestination();
        this.retstr = null;
    }

    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 paramRequestInfo = requestInfoTemp.getRequestInfo(requestIdTemp, userIdTemp);
        try {
            return WeaResult.success(getResultMapForAction(weaverResultMap, "result", "1", paramRequestInfo.getRequestManager()));
        } catch (Exception e) {
            e.printStackTrace();
            logger_16612b22.error("ZOA_FI_CUSTOMER_MAINTAIN e: {}", e.getMessage());
            return  WeaResult.fail(500, "ZOA_FI_CUSTOMER_MAINTAIN e {}"+ e, getResultMapForAction(weaverResultMap, "result",e.getMessage(), paramRequestInfo.getRequestManager()));
        }
    }

    public String[][] Getexecute(String[] paramString) {
        this.retstr = new String[2][40];
        logger_16612b22.error("ZOA_FI_CUSTOMER_MAINTAIN start");
        logger_16612b22.error("ZOA_FI_CUSTOMER_MAINTAIN paramString {}:" , String.join(",",paramString));
        try {
            RecordSetTrans localRecordSetTrans = I18nContextUtil.getBean(RecordSetTrans.class);
            localRecordSetTrans.setAutoCommit(false);
            JCoFunction localJCoFunction = this.destination.getRepository().getFunction("ZOA_FI_CUSTOMER_MAINTAIN");
            if (localJCoFunction == null) {
                System.out.println("ZOA_FI_CUSTOMER_MAINTAIN not found in SAP.");
                this.retstr[0][1] = "ZOA_FI_CUSTOMER_MAINTAIN not found in SAP.";
            } else {
                // writeLog("start log" + paramString[0][0]);
                JCoStructure localJCoStructure = localJCoFunction.getImportParameterList().getStructure("IN");
                // 动作类型
                localJCoStructure.setValue("EDIT_FLAG", paramString[0]);
                // 公司代码
                localJCoStructure.setValue("BUKRS", paramString[1]);
                // 客户编号
                localJCoStructure.setValue("KUNNR", paramString[2]);
                // 科目组
                localJCoStructure.setValue("KTOKD", paramString[3]);
                // 称谓
                localJCoStructure.setValue("ANRED", paramString[4]);
                // 名称
                localJCoStructure.setValue("NAME1", paramString[5]);
                // 简称-搜索项
                localJCoStructure.setValue("SORTL", paramString[6]);
                // 电话
                localJCoStructure.setValue("TELF1", paramString[7]);
                // 邮件
                localJCoStructure.setValue("SMTP_ADDR", paramString[8]);
                // 手机
                localJCoStructure.setValue("TELF2", paramString[9]);
                // 传真
                localJCoStructure.setValue("TELFX", paramString[10]);
                // 国家
                localJCoStructure.setValue("LAND1", paramString[11]);
                // 地区
                localJCoStructure.setValue("REGIO", paramString[12]);
                // 语言
                localJCoStructure.setValue("SPRAS", paramString[13]);
                // 城市
                localJCoStructure.setValue("ORT01", paramString[14]);
                // 地址
                localJCoStructure.setValue("STRAS", paramString[15]);
                // 邮政编号
                localJCoStructure.setValue("PSTLZ", paramString[16]);
                // 开户行行号
                localJCoStructure.setValue("BANKL", paramString[17]);
                // 开户行名称
                localJCoStructure.setValue("BANKA", paramString[18]);
                // 开户行国家
                localJCoStructure.setValue("BANKS", paramString[19]);
                // 开户行账号
                localJCoStructure.setValue("KOINH", paramString[20]);
                // 纳税登记证代码
                localJCoStructure.setValue("STCD5", paramString[21]);
                // 银行开户行账号
                localJCoStructure.setValue("ACCOUNTHOLDER", paramString[37]);
                // 统驭科目
                localJCoStructure.setValue("AKONT", paramString[22]);
                // 付款条件
                localJCoStructure.setValue("ZTERM", paramString[23]);
                // 排序码
                localJCoStructure.setValue("ZUAWA", paramString[24]);
                // 付款方式
                localJCoStructure.setValue("ZWELS", paramString[25]);
                // 批准组
                localJCoStructure.setValue("FRGRP", paramString[26]);
                // 付款冻结
                localJCoStructure.setValue("ZAHLS", paramString[27]);
                try {
                    localJCoFunction.execute(this.destination);
                    this.retstr[0][0] = localJCoFunction.getExportParameterList().getValue("E_STATUS").toString();
                    this.retstr[0][1] = localJCoFunction.getExportParameterList().getValue("E_MESSAGE").toString();
                    this.retstr[0][2] = localJCoFunction.getExportParameterList().getValue("E_KUNNR").toString();
                    JCoTable localJCoTable1 = localJCoFunction.getTableParameterList().getTable("T_CUSTOMER_INFO");
                    if (localJCoTable1.getNumRows() > 0) {
                        localJCoTable1.setRow(1);
                        // 客户编号
                        this.retstr[1][0] = localJCoTable1.getString("KUNNR").trim();
                        // 公司代码
                        this.retstr[1][1] = localJCoTable1.getString("BUKRS").trim();
                        // 科目组
                        this.retstr[1][2] = localJCoTable1.getString("KTOKD").trim();
                        // 科目组名称
                        this.retstr[1][3] = localJCoTable1.getString("TXT30").trim();
                        // 称谓
                        this.retstr[1][4] = localJCoTable1.getString("ANRED").trim();
                        // 名称
                        this.retstr[1][5] = localJCoTable1.getString("NAME1").trim();
                        // 简称-搜索项
                        this.retstr[1][6] = localJCoTable1.getString("SORTL").trim();
                        // 电话
                        this.retstr[1][7] = localJCoTable1.getString("TELF1").trim();
                        // 邮件
                        this.retstr[1][8] = localJCoTable1.getString("SMTP_ADDR").trim();
                        // 手机
                        this.retstr[1][9] = localJCoTable1.getString("TELF2").trim();
                        // 传真
                        this.retstr[1][10] = localJCoTable1.getString("TELFX").trim();
                        // 国家
                        this.retstr[1][11] = localJCoTable1.getString("LAND1").trim();
                        // 国家描述
                        this.retstr[1][12] = localJCoTable1.getString("LANDX50").trim();
                        // 地区
                        this.retstr[1][13] = localJCoTable1.getString("BLAND").trim();
                        // 地区描述
                        this.retstr[1][14] = localJCoTable1.getString("BEZEI").trim();
                        // 语言
                        this.retstr[1][15] = localJCoTable1.getString("SPRAS").trim();
                        // 语言描述
                        this.retstr[1][16] = localJCoTable1.getString("SPTXT").trim();
                        // 城市
                        this.retstr[1][17] = localJCoTable1.getString("ORT01").trim();
                        // 地址
                        this.retstr[1][18] = localJCoTable1.getString("STRAS").trim();
                        // 邮政编号
                        this.retstr[1][19] = localJCoTable1.getString("PSTLZ").trim();
                        // 开户行行号
                        this.retstr[1][20] = localJCoTable1.getString("BANKL").trim();
                        // 开户行名称
                        this.retstr[1][21] = localJCoTable1.getString("BANKA").trim();
                        // 开户行国家
                        this.retstr[1][22] = localJCoTable1.getString("BANKS").trim();
                        // 开户行国家描述
                        this.retstr[1][23] = localJCoTable1.getString("LANDX").trim();
                        // 开户行账号
                        this.retstr[1][24] = localJCoTable1.getString("KOINH").trim();
                        // 纳税登记证代码
                        this.retstr[1][25] = localJCoTable1.getString("STCD5").trim();
                        // 银行账号持有人 accountholder
                        this.retstr[1][37] = localJCoTable1.getString("ACCOUNTHOLDER").trim();
                        // 统驭科目
                        this.retstr[1][26] = localJCoTable1.getString("AKONT").trim();
                        // 统驭科目描述
                        this.retstr[1][27] = localJCoTable1.getString("TXT20").trim();
                        // 付款条件
                        this.retstr[1][28] = localJCoTable1.getString("ZTERM").trim();
                        // 付款条件描述
                        this.retstr[1][29] = localJCoTable1.getString("TEXT1").trim();
                        // 排序码
                        this.retstr[1][30] = localJCoTable1.getString("ZUAWA").trim();
                        // 排序码描述
                        this.retstr[1][31] = localJCoTable1.getString("TTEXT").trim();
                        // 付款方式
                        this.retstr[1][32] = localJCoTable1.getString("ZWELS").trim();
                        // 付款方式描述
                        this.retstr[1][33] = localJCoTable1.getString("TEXT2").trim();
                        // 批准组
                        this.retstr[1][34] = localJCoTable1.getString("FRGRP").trim();
                        // 批准组描述
                        this.retstr[1][35] = localJCoTable1.getString("FRGRT").trim();
                        // 付款冻结
                        this.retstr[1][36] = localJCoTable1.getString("ZAHLS").trim();
                    }
                } catch (Exception localException2) {
                    this.retstr[0][1] = localException2.toString();
                    logger_16612b22.error("Exception--", localException2);
                }
            }
            return this.retstr;
        } catch (Exception localException1) {
            this.retstr[0][1] = localException1.getMessage();
            logger_16612b22.error("Exception--", localException1);
            return this.retstr;
        }
    }

    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;
    }
}
