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 java.lang.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.weaver.verupgrade.file.Prop;
import com.weaver.verupgrade.general.BaseBean;

public class ZSRM_PUR_INFO {

    private static final Logger logger_741408ac = LoggerFactory.getLogger(ZSRM_PUR_INFO.class);

    private JCoDestination destination;

    private String[] retstr;

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

    public String[] Getexecute(String[][] as) {

        logger_741408ac.error("ZSRM_PUR_INFO  Getexecute as = " + as);
        retstr = new String[2];
        try {
            JCoFunction jcofunction = destination.getRepository().getFunction("ZSRM_PUR_INFO");
            if (jcofunction == null) {
                System.out.println("ZSRM_PUR_INFO not found in SAP.");
                retstr[0] = "E";
                retstr[1] = "ZSRM_PUR_INFO 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.clear();
                    jcotable.appendRow();
                    jcotable.setValue("PRICE_NO", as[i][0]);
                    jcotable.setValue("EKORG", as[i][1]);
                    jcotable.setValue("EKGRP", as[i][2]);
                    jcotable.setValue("MATNR", as[i][3]);
                    jcotable.setValue("LIFNR", as[i][4]);
                    jcotable.setValue("MEINS", as[i][5]);
                    jcotable.setValue("UMREN", as[i][6]);
                    jcotable.setValue("UMREZ", as[i][7]);
                    jcotable.setValue("MINBM", as[i][8]);
                    jcotable.setValue("NORBM", as[i][9]);
                    jcotable.setValue("APLFZ", as[i][10]);
                    jcotable.setValue("MWSKZ", as[i][11]);
                    jcotable.setValue("ESOKZ", as[i][12]);
                    jcotable.setValue("DATAB", as[i][13]);
                    jcotable.setValue("DATAI", as[i][14]);
                    jcotable.setValue("KPEIN", as[i][16]);
                    jcotable.setValue("WGLIF", as[i][17]);
                    jcotable.setValue("TESTRUN", as[i][18]);
                    String WAERS = as[i][19];
                    String KBETR = as[i][15];
                    // 新增货币
                    jcotable.setValue("WAERS", WAERS);
                    // String hbpz = Prop.getPropValue("htsysjcmt","gy105hb");
                    // if(!"".equals(WAERS)&&!"".equals(hbpz)){
                    // WAERS = ","+WAERS.toUpperCase()+",";
                    // hbpz = ","+hbpz.toUpperCase()+",";
                    // if(hbpz.indexOf(WAERS)>=0){
                    // KBETR = KBETR.substring(0,KBETR.indexOf("."));
                    // }
                    // }
                    // 采购凭证中的净价格(以凭证货币计)BTCI
                    jcotable.setValue("KBETR", KBETR);
                    jcotable.setValue("MESNO", as[i][20]);
                    logger_741408ac.error(String.valueOf("ZSRM_PUR_INFO-PRICE_NO=" + as[i][0] + "||EKORG=" + as[i][1] + "||EKGRP=" + as[i][2] + "||MATNR=" + as[i][3] + "||LIFNR=" + as[i][4] + "||MEINS=" + as[i][5] + "||UMREN=" + as[i][6] + "||UMREZ=" + as[i][7] + "||MINBM=" + as[i][8] + "||NORBM=" + as[i][9] + "||APLFZ=" + as[i][10] + "||MWSKZ=" + as[i][11] + "||ESOKZ=" + as[i][12] + "||DATAB=" + as[i][13] + "||DATAI=" + as[i][14] + "||KBETR=" + KBETR + "||KPEIN=" + as[i][16] + "||WGLIF=" + as[i][17] + "||TESTRUN=" + as[i][18] + "||HB=" + as[i][19] + "||MESNO=" + as[i][20]));
                    try {
                        jcofunction.execute(destination);
                        JCoStructure out = jcofunction.getExportParameterList().getStructure("OUT");
                        JCoTable outtable = out.getTable("RETURN");
                        String state = outtable.getValue("ISTAT").toString();
                        String message = outtable.getValue("SRMSAGE").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();
            logger_741408ac.error(String.valueOf("ZSRM_PUR_INFO=" + "state=" + retstr[0] + "||" + exception.getMessage()));

        }
        return retstr;
    }
}
