package org.jeecg.modules.sap.service.impl; import com.alibaba.fastjson.JSONObject; import com.sap.conn.jco.*; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.jeecg.common.aspect.annotation.ApiLog; import org.jeecg.common.constant.ApiLogCategoryEnum; import org.jeecg.config.sap.SapRfcConnectionManager; import org.jeecg.modules.sap.FunctionConst; import org.jeecg.modules.sap.dto.OrderBomDTO; import org.jeecg.modules.sap.dto.OrderProcessDTO; import org.jeecg.modules.sap.service.OrderProcessSync; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.*; @Service @Slf4j public class OrderProcessSyncImpl implements OrderProcessSync { @Autowired private SapRfcConnectionManager connectionManager; @Override @ApiLog(apiName = "产品工艺路线同步(ZPPF_033_2)", apiCategory = ApiLogCategoryEnum.SAP) public Map syncOrderProcess(String factoryCode, String orderCode) throws Exception { Map resultMap = new HashMap<>(); JCoDestination destination = connectionManager.getDestination(); JCoRepository repository = destination.getRepository(); JCoFunction function = repository.getFunction(FunctionConst.ZPPF_033_2); if (function == null) { throw new RuntimeException("RFC 函数 ZPPF_033_2 未找到!"); } // 设置输入参数 List items = new ArrayList<>(); if(StringUtils.isNotBlank(factoryCode)){ //新火炬 JCoTable WERKSTable = function.getTableParameterList().getTable("ZTAB_WERKS"); String[] split = factoryCode.split(","); for(String code : split){ WERKSTable.appendRow(); WERKSTable.setValue("WERKS", code); } //组装请求参数 JSONObject item = new JSONObject(); item.put("WERKS", factoryCode); items.add(item); } if(StringUtils.isNotBlank(orderCode)) { //新火炬 JCoTable AUFNRTable = function.getTableParameterList().getTable("ZTAB_AUFNR"); String[] split = orderCode.split(","); for(String code : split){ AUFNRTable.appendRow(); AUFNRTable.setValue("AUFNR", code); } //组装请求参数 JSONObject item = new JSONObject(); item.put("AUFNR", orderCode); items.add(item); } // 执行调用 function.execute(destination); //获取返回结果 String zmess = function.getExportParameterList().getValue("ZMESS").toString(); String ztype = function.getExportParameterList().getValue("ZTYPE").toString();//S 标识 成功 // 获取输出参数 JCoTable outputTable = function.getTableParameterList().getTable("ZTAB_OUT"); int numRows = outputTable.getNumRows(); List resultList = new ArrayList<>(); OrderProcessDTO dto; for (int i = 0; i < numRows; i++) { outputTable.setRow(i); dto = new OrderProcessDTO(); dto.setAUFNR(outputTable.getString("AUFNR")); dto.setMATNR(outputTable.getString("MATNR")); dto.setWERKS(outputTable.getString("WERKS")); dto.setVORNR(outputTable.getString("VORNR")); dto.setLTXA1(outputTable.getString("LTXA1")); dto.setSTEUS(outputTable.getString("STEUS")); //添加结果集 resultList.add(dto); log.info(dto.toString()); } resultMap.put("zmess", zmess); resultMap.put("ztype", ztype); resultMap.put("importParameters", items); resultMap.put("result", resultList); return resultMap; } }