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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * oeM成本加工费
 */
public class ZOEM_PROCESSING_CHARGE {

    private static final Logger logger_499f401d = LoggerFactory.getLogger(ZOEM_PROCESSING_CHARGE.class);

    private JCoDestination destination;

    private String[] retstr;

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

    public String[] Getexecute(String[][] as) {
        retstr = new String[2];
        try {
            JCoFunction jcofunction = destination.getRepository().getFunction("ZOEM_PROCESSING_CHARGE");
            if (jcofunction == null) {
                System.out.println("ZOEM_PROCESSING_CHARGE not found in SAP.");
                retstr[0] = "E";
                retstr[1] = "ZOEM_PROCESSING_CHARGE not found in SAP.";
            } else {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < as.length; i++) {
                    // JCoStructure structure = jcofunction.getImportParameterList().getStructure("IN");
                    // structure.setValue("ITFID", "SRM007");
                    // JCoTable jcotable = structure.getTable("DATAS");
                    JCoTable jcotable = jcofunction.getTableParameterList().getTable("IT_TAB");
                    jcotable.clear();
                    jcotable.appendRow();
                    // 采购组织
                    jcotable.setValue("EKORG", as[i][0]);
                    // 物料编号
                    jcotable.setValue("MATNR", as[i][1]);
                    // 供应商ERP编号
                    jcotable.setValue("LIFNR", as[i][2]);
                    // 订单单位
                    jcotable.setValue("MEINS", as[i][3]);
                    // 价格单位
                    jcotable.setValue("KPEIN", as[i][4]);
                    // 价格
                    jcotable.setValue("KBETR", as[i][5]);
                    // 开始生效日期
                    jcotable.setValue("DATAB", as[i][6]);
                    // 有效截至日期
                    jcotable.setValue("DATBI", as[i][7]);
                    try {
                        jcofunction.execute(destination);
                        // JCoStructure out = jcofunction.getExportParameterList().getStructure("OUT");
                        // JCoTable outtable = out.getTable("RETURN");
                        JCoTable outtable = jcofunction.getTableParameterList().getTable("ET_RETURN");
                        String state = outtable.getValue("TYPE").toString();
                        String message = outtable.getValue("MESSAGE").toString();
                        if (!"S".equals(state)) {
                            retstr[0] = "E";
                            sb.append("第" + (i + 1) + "行数据有误，错误信息：" + message + "；\r\n");
                        }
                    } catch (Exception exception1) {
                        retstr[0] = "E";
                        sb.append("第" + (i + 1) + "行数据有误，错误信息：" + exception1.toString() + "；\r\n");
                    }
                }
                if (sb.length() > 0) {
                    retstr[0] = "E";
                    retstr[1] = sb.toString();
                } else {
                    retstr[0] = "S";
                    retstr[1] = "执行成功；\r\n";
                }
            }
        } catch (Exception exception) {
            retstr[0] = "E";
            retstr[1] = exception.getMessage();
        }
        return retstr;
    }
}
