|
@@ -0,0 +1,214 @@
|
|
|
+package cn.iocoder.yudao.module.pms.sap;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.ObjUtil;
|
|
|
+import cn.iocoder.yudao.module.system.api.saporg.SapOrgApi;
|
|
|
+import cn.iocoder.yudao.module.system.api.saporg.dto.SapOrgRespDTO;
|
|
|
+import com.sap.conn.jco.*;
|
|
|
+import io.swagger.v3.oas.annotations.Operation;
|
|
|
+import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Tag(name = "管理后台 - SAP同步接口 物料主数据 领料单 SAP库存")
|
|
|
+@RequestMapping("/pms/sapSync")
|
|
|
+@RestController
|
|
|
+@Validated
|
|
|
+public class SapController {
|
|
|
+ @Autowired
|
|
|
+ private SapConnector sapConnector;
|
|
|
+ @Autowired
|
|
|
+ private SapOrgApi sapOrgApi;
|
|
|
+
|
|
|
+ @GetMapping("/callSapStock")
|
|
|
+ @Operation(summary = "同步sap 库存接口")
|
|
|
+ public String callSapStock() {
|
|
|
+ try {
|
|
|
+ JCoDestination destination = sapConnector.getDestination();
|
|
|
+ JCoFunction function = destination.getRepository().getFunction("ZPMS_002");
|
|
|
+
|
|
|
+ // 设置输入参数
|
|
|
+ JCoParameterList input = function.getImportParameterList();
|
|
|
+ input.setValue("IV_WERKS", "6011");
|
|
|
+
|
|
|
+ // 执行 RFC 调用
|
|
|
+ function.execute(destination);
|
|
|
+
|
|
|
+ // 获取输出参数
|
|
|
+
|
|
|
+ JCoParameterList output = function.getTableParameterList();
|
|
|
+ JCoTable etStockTable = function.getTableParameterList().getTable("ET_STOCK");
|
|
|
+ // JCoParameterList output = function.getExportParameterList();
|
|
|
+ if (CollUtil.isNotEmpty(output)) {
|
|
|
+ output.forEach(material -> {
|
|
|
+ System.out.println(material.getName());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (etStockTable != null && etStockTable.getNumRows() > 0) {
|
|
|
+ for (int i = 0; i < etStockTable.getNumRows(); i++) {
|
|
|
+ etStockTable.setRow(i);
|
|
|
+ System.out.println(String.format("行号 %d: 工厂=%s, 物料=%s, 数量=%s\n",
|
|
|
+ i,
|
|
|
+ etStockTable.getString("WERKS"),
|
|
|
+ etStockTable.getString("MATNR"),
|
|
|
+ etStockTable.getString("LABST")
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "Material Info: " + output.getString("ET_STOCK");
|
|
|
+ } catch (JCoException e) {
|
|
|
+ return "Error calling SAP Stock: " + e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步SAP领料单 数据只有新增的情况
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/callSapPickingList")
|
|
|
+ @Operation(summary = "同步sap 领料单接口")
|
|
|
+ public String callSapPickingList() {
|
|
|
+ List<String> factoryCodes = factoryCodes();
|
|
|
+ if (CollUtil.isEmpty(factoryCodes)) {
|
|
|
+ return "No SAP Factory";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ JCoDestination destination = sapConnector.getDestination();
|
|
|
+ JCoFunction function = destination.getRepository().getFunction("ZPMS_006");
|
|
|
+ if (ObjUtil.isEmpty(function)) {
|
|
|
+ return "No SAP Function";
|
|
|
+ }
|
|
|
+ // 设置输入参数
|
|
|
+ JCoParameterList input = function.getImportParameterList();
|
|
|
+ input.setValue("I_WERKS", "6011");
|
|
|
+ // 暂时只设置同步 2025年后的 领料单 2025年之前的领料单 手动入库
|
|
|
+ input.setValue("I_DATUM_F", "20250101"); // 开始日期
|
|
|
+ // input.setValue("I_DATUM_T", "20251231"); // 结束日期
|
|
|
+
|
|
|
+ // 执行 RFC 调用
|
|
|
+ function.execute(destination);
|
|
|
+
|
|
|
+ // 获取输出参数
|
|
|
+ JCoParameterList output = function.getTableParameterList();
|
|
|
+ JCoTable headTable = function.getTableParameterList().getTable("T_HEAD");
|
|
|
+ // JCoParameterList output = function.getExportParameterList();
|
|
|
+ if (CollUtil.isNotEmpty(output)) {
|
|
|
+ output.forEach(material -> {
|
|
|
+ // System.out.println(material.getName());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (headTable != null && headTable.getNumRows() > 0) {
|
|
|
+ for (int i = 0; i < headTable.getNumRows(); i++) {
|
|
|
+ headTable.setRow(i);
|
|
|
+ System.out.println(String.format("行号 %d: 工厂=%s, 名称 1=%s, 领料/退料编号=%s, ZLLLX=%s, " +
|
|
|
+ "日期=%tT, 项目定义=%s, WBS 元素=%s, 描述=%s, 所属项目部=%s, 描述=%s, 成本中心=%s, 描述=%s\n",
|
|
|
+ i,
|
|
|
+ headTable.getString("WERKS"),
|
|
|
+ headTable.getString("TXTMD"),
|
|
|
+ headTable.getString("ZLTNO"),
|
|
|
+ headTable.getString("ZLLLX"),
|
|
|
+ headTable.getString("DATUM"),
|
|
|
+ headTable.getString("PSPID"),
|
|
|
+ headTable.getString("POSID"),
|
|
|
+ headTable.getString("POST1"),
|
|
|
+ headTable.getString("ZSSXMB"),
|
|
|
+ headTable.getString("LGOBE"),
|
|
|
+ headTable.getString("KOSTL"),
|
|
|
+ headTable.getString("LTEXT")
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ JCoTable itemTable = function.getTableParameterList().getTable("T_ITEM");
|
|
|
+ if (itemTable != null && itemTable.getNumRows() > 0) {
|
|
|
+ for (int i = 0; i < itemTable.getNumRows(); i++) {
|
|
|
+ itemTable.setRow(i);
|
|
|
+ System.out.println(String.format("行号 %d: ZNUMC=%s, 物料=%s, 描述=%s, 数量=%f, " +
|
|
|
+ "基本单位=%s, 特殊库存=%s, 移动原因=%s, 原因=%s, 库存地点=%s, 描述=%s, 库存仓位=%s, 批次=%s, 转入工厂=%s, BEIZHU=%s\n",
|
|
|
+ i,
|
|
|
+ headTable.getString("ZNUMC"),
|
|
|
+ headTable.getString("MATNR"),
|
|
|
+ headTable.getString("MAKTX"),
|
|
|
+ headTable.getString("MENGE"),
|
|
|
+ headTable.getString("MEINS"),
|
|
|
+ headTable.getString("SOBKZ"),
|
|
|
+ headTable.getString("GRUND"),
|
|
|
+ headTable.getString("GRTXT"),
|
|
|
+ headTable.getString("LGORT"),
|
|
|
+ headTable.getString("LGOBE"),
|
|
|
+ headTable.getString("LGPBE"),
|
|
|
+ headTable.getString("CHARG"),
|
|
|
+ headTable.getString("WERKS_ZR"),
|
|
|
+ headTable.getString("BEIZHU")
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return "Material Info: " + output.getString("T_HEAD");
|
|
|
+ } catch (JCoException e) {
|
|
|
+ return "Error calling SAP Picking List: " + e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @GetMapping("/callSapMaterialMasterData")
|
|
|
+ @Operation(summary = "同步sap 物料主数据接口")
|
|
|
+ public String callSapMaterialMasterData() {
|
|
|
+ //
|
|
|
+ try {
|
|
|
+ JCoDestination destination = sapConnector.getDestination();
|
|
|
+ JCoFunction function = destination.getRepository().getFunction("ZPMS_002");
|
|
|
+
|
|
|
+ // 设置输入参数
|
|
|
+ JCoParameterList input = function.getImportParameterList();
|
|
|
+ input.setValue("IV_WERKS", "6011");
|
|
|
+
|
|
|
+ // 执行 RFC 调用
|
|
|
+ function.execute(destination);
|
|
|
+
|
|
|
+ // 获取输出参数
|
|
|
+
|
|
|
+ JCoParameterList output = function.getTableParameterList();
|
|
|
+ JCoTable etStockTable = function.getTableParameterList().getTable("ET_STOCK");
|
|
|
+ // JCoParameterList output = function.getExportParameterList();
|
|
|
+ if (CollUtil.isNotEmpty(output)) {
|
|
|
+ output.forEach(material -> {
|
|
|
+ System.out.println(material.getName());
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (etStockTable != null && etStockTable.getNumRows() > 0) {
|
|
|
+ for (int i = 0; i < etStockTable.getNumRows(); i++) {
|
|
|
+ etStockTable.setRow(i);
|
|
|
+ System.out.println(String.format("行号 %d: 工厂=%s, 物料=%s, 数量=%s\n",
|
|
|
+ i,
|
|
|
+ etStockTable.getString("WERKS"),
|
|
|
+ etStockTable.getString("MATNR"),
|
|
|
+ etStockTable.getString("LABST")
|
|
|
+ ));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return "Material Info: " + output.getString("ET_STOCK");
|
|
|
+ } catch (JCoException e) {
|
|
|
+ return "Error calling SAP Material Master Data: " + e.getMessage();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询所有SAP工厂
|
|
|
+ */
|
|
|
+ private List<String> factoryCodes(){
|
|
|
+ List<SapOrgRespDTO> sapOrgs = sapOrgApi.getSapOrgByType(1);
|
|
|
+ if (CollUtil.isEmpty(sapOrgs)) {
|
|
|
+ return CollUtil.empty(String.class);
|
|
|
+ }
|
|
|
+ List<String> factoryCodes = sapOrgs.stream()
|
|
|
+ .map(SapOrgRespDTO::getFactoryCode)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ return CollUtil.isEmpty(factoryCodes) ? CollUtil.empty(String.class) : factoryCodes;
|
|
|
+ }
|
|
|
+}
|