add:增加物料主数据同步
This commit is contained in:
@@ -23,6 +23,7 @@ import org.nl.wms.basedata_manage.service.dao.mapper.MdMeMaterialbaseMapper;
|
||||
import org.nl.wms.ext_manage.service.WmsToErpService;
|
||||
import org.nl.wms.ext_manage.service.dto.ErpMaterial;
|
||||
import org.nl.wms.ext_manage.service.impl.ErpSyncService;
|
||||
import org.nl.wms.pm_manage.sap.outBoundDelivery.service.impl.OutboundDeliveryServiceImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -49,9 +50,9 @@ public class MdMeMaterialbaseServiceImpl extends ServiceImpl<MdMeMaterialbaseMap
|
||||
*/
|
||||
@Autowired
|
||||
private WmsToErpService wmsToErpService;
|
||||
|
||||
@Autowired
|
||||
private ErpSyncService erpSyncService;
|
||||
private OutboundDeliveryServiceImpl sapSyncService;
|
||||
|
||||
|
||||
/**
|
||||
* wms调用erp服务
|
||||
@@ -128,45 +129,45 @@ public class MdMeMaterialbaseServiceImpl extends ServiceImpl<MdMeMaterialbaseMap
|
||||
@Override
|
||||
@Transactional
|
||||
public void materialSync(JSONObject whereJson) {
|
||||
List<ErpMaterial> list = erpSyncService.list();
|
||||
// 查询是否有相同物料
|
||||
List<MdMeMaterialbase> likeMaterList = this.list(
|
||||
new QueryWrapper<MdMeMaterialbase>().lambda()
|
||||
.in(MdMeMaterialbase::getMaterial_code, list.stream()
|
||||
.map(ErpMaterial::getSkuCode)
|
||||
.collect(Collectors.toList())
|
||||
)
|
||||
);
|
||||
// 需插入的物料集合
|
||||
List<MdMeMaterialbase> materDaoList = new ArrayList<>();
|
||||
for (ErpMaterial json : list) {
|
||||
boolean is_like = likeMaterList.stream()
|
||||
.anyMatch(row -> row.getMaterial_code().equals(json.getSkuCode()));
|
||||
if (is_like) {
|
||||
continue;
|
||||
}
|
||||
// 组织数据
|
||||
MdMeMaterialbase dao = new MdMeMaterialbase();
|
||||
dao.setMaterial_id(IdUtil.getStringId());
|
||||
dao.setMaterial_code(json.getSkuCode());
|
||||
dao.setMaterial_name(json.getSkuName());
|
||||
if (!StrUtil.isEmptyIfStr(json.getModel()) && json.getModel().length() > 100){
|
||||
dao.setMaterial_spec(json.getModel().substring(0, 100));
|
||||
}else{
|
||||
dao.setMaterial_spec(json.getModel());
|
||||
}
|
||||
|
||||
dao.setQty_unit_id("1");
|
||||
dao.setQty_unit_name("Pcs");
|
||||
dao.setCreate_id(SecurityUtils.getCurrentUserId());
|
||||
dao.setCreate_name(SecurityUtils.getCurrentNickName());
|
||||
dao.setCreate_time(DateUtil.now());
|
||||
dao.setUpdate_optid(SecurityUtils.getCurrentUserId());
|
||||
dao.setUpdate_optname(SecurityUtils.getCurrentNickName());
|
||||
dao.setUpdate_time(DateUtil.now());
|
||||
materDaoList.add(dao);
|
||||
}
|
||||
this.saveBatch(materDaoList);
|
||||
sapSyncService.syncMaterialFromSap("");
|
||||
// // 查询是否有相同物料
|
||||
// List<MdMeMaterialbase> likeMaterList = this.list(
|
||||
// new QueryWrapper<MdMeMaterialbase>().lambda()
|
||||
// .in(MdMeMaterialbase::getMaterial_code, list.stream()
|
||||
// .map(ErpMaterial::getSkuCode)
|
||||
// .collect(Collectors.toList())
|
||||
// )
|
||||
// );
|
||||
// // 需插入的物料集合
|
||||
// List<MdMeMaterialbase> materDaoList = new ArrayList<>();
|
||||
// for (ErpMaterial json : list) {
|
||||
// boolean is_like = likeMaterList.stream()
|
||||
// .anyMatch(row -> row.getMaterial_code().equals(json.getSkuCode()));
|
||||
// if (is_like) {
|
||||
// continue;
|
||||
// }
|
||||
// // 组织数据
|
||||
// MdMeMaterialbase dao = new MdMeMaterialbase();
|
||||
// dao.setMaterial_id(IdUtil.getStringId());
|
||||
// dao.setMaterial_code(json.getSkuCode());
|
||||
// dao.setMaterial_name(json.getSkuName());
|
||||
// if (!StrUtil.isEmptyIfStr(json.getModel()) && json.getModel().length() > 100){
|
||||
// dao.setMaterial_spec(json.getModel().substring(0, 100));
|
||||
// }else{
|
||||
// dao.setMaterial_spec(json.getModel());
|
||||
// }
|
||||
//
|
||||
// dao.setQty_unit_id("1");
|
||||
// dao.setQty_unit_name("Pcs");
|
||||
// dao.setCreate_id(SecurityUtils.getCurrentUserId());
|
||||
// dao.setCreate_name(SecurityUtils.getCurrentNickName());
|
||||
// dao.setCreate_time(DateUtil.now());
|
||||
// dao.setUpdate_optid(SecurityUtils.getCurrentUserId());
|
||||
// dao.setUpdate_optname(SecurityUtils.getCurrentNickName());
|
||||
// dao.setUpdate_time(DateUtil.now());
|
||||
// materDaoList.add(dao);
|
||||
// }
|
||||
// this.saveBatch(materDaoList);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,6 +20,17 @@
|
||||
| `POST /insertSapWlpz` | 物料凭证同步 | `pm_manage.sap.materialDocument` |
|
||||
| `POST /insertSapMm021` | 销售退货出库同步 | `pm_manage.sap.salesReturnOut` |
|
||||
|
||||
## SAP 物料主数据同步(MDG012,WMS 主动拉取)
|
||||
|
||||
| 接口 | 说明 | 实现 |
|
||||
|------|------|------|
|
||||
| `POST /api/sapOutboundDelivery/syncMaterial?date=yyyy-MM-dd` | 从 SAP 拉取物料主数据落库 | `OutboundDeliveryServiceImpl#syncMaterialFromSap` |
|
||||
|
||||
- 传 `date` 为增量:已存在物料更新名称/规格/单位;不传为全量:已存在跳过,只补新增。
|
||||
- 分页拉取,每页 `COUNT=2000`;响应体 `body` 为对象(`{sum, data}`),与写入类接口的 `body` 数组不同,故 `SapApi` 单独提供 `materialQuery`,不走 S/E 校验。
|
||||
- 字段映射:`matnr→material_code`、`maktx→material_name`、`atwrt→material_spec`、`meins→qty_unit_name`(并按 `unit_code` 反查 `qty_unit_id`)。
|
||||
|
||||
|
||||
## 业务模块位置
|
||||
|
||||
业务 CRUD、下推、回传策略等均在 `org.nl.wms.pm_manage.sap` 下:
|
||||
|
||||
@@ -76,6 +76,35 @@ public class SapApi {
|
||||
return postInbound(bizLabel, request);
|
||||
}
|
||||
|
||||
/**
|
||||
* MDG012 物料主数据查询
|
||||
* <p>
|
||||
* 查询类接口与写入类接口不同:响应体 body 是「对象」而非数组,
|
||||
* 形如 {@code {"body":{"sum":"100","data":[{...}]}}},不能复用 postInbound 的 S/E 校验。
|
||||
*/
|
||||
public JSONObject materialQuery(JSONObject request) {
|
||||
return postQuery("SAP物料查询[" + SapConstant.INTF_MDG012 + "]", request);
|
||||
}
|
||||
|
||||
private JSONObject postQuery(String bizLabel, Object request) {
|
||||
String param = request instanceof String ? (String) request : JSON.toJSONString(request);
|
||||
log.info("{}请求:{}", bizLabel, param);
|
||||
try {
|
||||
String resultMsg = HttpRequest.post(SapConstant.SAP_INBOUND_URL)
|
||||
.basicAuth(BASIC_AUTH_USER, BASIC_AUTH_PASSWORD)
|
||||
.body(param)
|
||||
.execute()
|
||||
.body();
|
||||
log.info("{}响应:{}", bizLabel, resultMsg);
|
||||
if (!StringUtils.hasText(resultMsg)) {
|
||||
return null;
|
||||
}
|
||||
return JSONObject.parseObject(resultMsg);
|
||||
} catch (Exception e) {
|
||||
throw new BadRequestException(bizLabel + "失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject postInbound(String bizLabel, Object request) {
|
||||
String param = request instanceof String ? (String) request : JSON.toJSONString(request);
|
||||
log.info("{}请求:{}", bizLabel, param);
|
||||
|
||||
@@ -33,6 +33,15 @@ public class SapConstant {
|
||||
/** 接口编号:生产入库 */
|
||||
public static final String INTF_PP066 = "PP066";
|
||||
|
||||
/** 接口编号:物料主数据查询(SAP → WMS 物料同步) */
|
||||
public static final String INTF_MDG012 = "MDG012";
|
||||
|
||||
/** 物料查询同步来源系统 */
|
||||
public static final String MATERIAL_QUERY_SRC_SYSTEM = "WMS";
|
||||
|
||||
/** 物料查询同步目标系统 */
|
||||
public static final String MATERIAL_QUERY_DEST_SYSTEM = "sap";
|
||||
|
||||
public static final String SRC_SYSTEM = "WMS5";
|
||||
public static final String DEST_SYSTEM = "SAP";
|
||||
|
||||
|
||||
@@ -83,4 +83,10 @@ public class OutboundDeliveryController {
|
||||
outboundDeliveryService.reverse(dto);
|
||||
return ResponseData.build();
|
||||
}
|
||||
|
||||
@PostMapping("/syncMaterial")
|
||||
@Log("从SAP同步物料主数据(MDG012)")
|
||||
public ResponseEntity<Object> syncMaterial(@RequestParam(required = false) String date) {
|
||||
return ResponseData.build(outboundDeliveryService.syncMaterialFromSap(date), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,14 @@ public interface OutboundDeliveryService extends IService<OutboundDelivery> {
|
||||
|
||||
JSONObject insertSapWwjhd(DeliveryOrderDTO deliveryOrderDTO);
|
||||
|
||||
/**
|
||||
* 从 SAP 同步物料主数据(MDG012)
|
||||
*
|
||||
* @param date 增量同步起始日期(yyyy-MM-dd);为空表示全量同步(已存在的物料跳过,只补新增)
|
||||
* @return 同步结果:{countInsert=新增数, countUpdate=更新数}
|
||||
*/
|
||||
JSONObject syncMaterialFromSap(String date);
|
||||
|
||||
IPage<OutboundDelivery> queryPage(OutboundDeliveryQuery query, PageQuery page);
|
||||
|
||||
OutboundDeliveryOrderDto getDetail(Long id);
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.nl.wms.pm_manage.sap.outBoundDelivery.service.impl;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
@@ -11,6 +12,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.common.exception.BadRequestException;
|
||||
import org.nl.common.utils.IdUtil;
|
||||
import org.nl.common.utils.SecurityUtils;
|
||||
import org.nl.wms.ext_manage.sap.api.SapApi;
|
||||
import org.nl.wms.ext_manage.sap.api.dto.SubcontractConfirmBody;
|
||||
@@ -21,7 +23,9 @@ import org.nl.wms.ext_manage.sap.api.dto.SubcontractReverseBody;
|
||||
import org.nl.wms.ext_manage.sap.api.dto.SubcontractReverseRequest;
|
||||
import org.nl.wms.ext_manage.sap.enums.SapConstant;
|
||||
import org.nl.wms.basedata_manage.service.IMdMeMaterialbaseService;
|
||||
import org.nl.wms.basedata_manage.service.IMdPbMeasureunitService;
|
||||
import org.nl.wms.basedata_manage.service.dao.MdMeMaterialbase;
|
||||
import org.nl.wms.basedata_manage.service.dao.MdPbMeasureunit;
|
||||
import org.nl.wms.pm_manage.sap.sapDelivery.service.dto.DeliveryOrderDTO;
|
||||
import org.nl.wms.pm_manage.sap.sapDelivery.service.dto.SapBody;
|
||||
import org.nl.wms.pm_manage.sap.sapDelivery.service.dto.SapHeader;
|
||||
@@ -82,6 +86,8 @@ public class OutboundDeliveryServiceImpl extends ServiceImpl<OutboundDeliveryMap
|
||||
@Autowired
|
||||
private IMdMeMaterialbaseService materialbaseService;
|
||||
@Autowired
|
||||
private IMdPbMeasureunitService measureunitService;
|
||||
@Autowired
|
||||
private SapApi sapApi;
|
||||
@Autowired
|
||||
private ISapSubcontractReverseRecordService reverseRecordService;
|
||||
@@ -111,6 +117,192 @@ public class OutboundDeliveryServiceImpl extends ServiceImpl<OutboundDeliveryMap
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject syncMaterialFromSap(String date) {
|
||||
// 传了 date 才做「已存在则更新」,否则全量只补新增
|
||||
boolean incremental = StrUtil.isNotBlank(date);
|
||||
String changeDate = incremental ? date.trim() : null;
|
||||
int pageSize = 2000;
|
||||
int page = 0;
|
||||
boolean hasMore = true;
|
||||
int countInsert = 0;
|
||||
int countUpdate = 0;
|
||||
String now = DateUtil.now();
|
||||
String operatorId = SecurityUtils.getCurrentUserId();
|
||||
String operatorName = SecurityUtils.getCurrentNickName();
|
||||
Set<String> existCodes = new HashSet<>();
|
||||
List<MdMeMaterialbase> existList = materialbaseService.list(
|
||||
new LambdaQueryWrapper<MdMeMaterialbase>()
|
||||
.select(MdMeMaterialbase::getMaterial_id, MdMeMaterialbase::getMaterial_code));
|
||||
if (!CollectionUtils.isEmpty(existList)) {
|
||||
for (MdMeMaterialbase material : existList) {
|
||||
if (material != null && StrUtil.isNotBlank(material.getMaterial_code())) {
|
||||
existCodes.add(material.getMaterial_code());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 同一次同步内 SAP 可能返回重复物料编码,需去重
|
||||
Set<String> handledCodes = new HashSet<>();
|
||||
while (hasMore) {
|
||||
JSONObject ret = sapApi.materialQuery(buildMaterialQueryRequest(changeDate, page, pageSize));
|
||||
if (ret == null) {
|
||||
break;
|
||||
}
|
||||
JSONObject body = ret.getJSONObject("body");
|
||||
if (body == null) {
|
||||
break;
|
||||
}
|
||||
JSONArray data = body.getJSONArray("data");
|
||||
if (data == null || data.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
JSONObject one = data.getJSONObject(i);
|
||||
if (one == null) {
|
||||
continue;
|
||||
}
|
||||
String materialCode = StrUtil.trim(one.getString("matnr"));
|
||||
if (StrUtil.isBlank(materialCode) || !handledCodes.add(materialCode)) {
|
||||
continue;
|
||||
}
|
||||
String materialName = resolveMaterialName(one);
|
||||
// 规格 = atwrt
|
||||
String materialSpec = StrUtil.trim(one.getString("atwrt"));
|
||||
String unitCode = StrUtil.trim(one.getString("meins"));
|
||||
boolean exists = existCodes.contains(materialCode);
|
||||
if (exists) {
|
||||
//不做更新
|
||||
if (!incremental) {
|
||||
continue;
|
||||
}
|
||||
MdMeMaterialbase update = new MdMeMaterialbase();
|
||||
if (StrUtil.isNotBlank(materialName)) {
|
||||
update.setMaterial_name(materialName);
|
||||
}
|
||||
if (StrUtil.isNotBlank(materialSpec)) {
|
||||
update.setMaterial_spec(materialSpec);
|
||||
}
|
||||
if (unitCode != null) {
|
||||
update.setQty_unit_name(unitCode);
|
||||
}
|
||||
update.setUpdate_optid(operatorId);
|
||||
update.setUpdate_optname(operatorName);
|
||||
update.setUpdate_time(now);
|
||||
materialbaseService.update(update, new LambdaQueryWrapper<MdMeMaterialbase>()
|
||||
.eq(MdMeMaterialbase::getMaterial_code, materialCode));
|
||||
countUpdate++;
|
||||
} else {
|
||||
MdMeMaterialbase insert = new MdMeMaterialbase();
|
||||
insert.setMaterial_id(IdUtil.getStringId());
|
||||
insert.setMaterial_code(materialCode);
|
||||
insert.setMaterial_name(StrUtil.blankToDefault(materialName, materialCode));
|
||||
insert.setMaterial_spec(materialSpec);
|
||||
if (unitCode != null) {
|
||||
insert.setQty_unit_name(unitCode);
|
||||
}
|
||||
insert.setCreate_id(operatorId);
|
||||
insert.setCreate_name(operatorName);
|
||||
insert.setCreate_time(now);
|
||||
insert.setUpdate_optid(operatorId);
|
||||
insert.setUpdate_optname(operatorName);
|
||||
insert.setUpdate_time(now);
|
||||
insert.setIs_used("1");
|
||||
insert.setIs_delete("0");
|
||||
materialbaseService.save(insert);
|
||||
existCodes.add(materialCode);
|
||||
countInsert++;
|
||||
}
|
||||
}
|
||||
// 总页数(sum/COUNT)
|
||||
int total = 0;
|
||||
String sumStr = StrUtil.trim(body.getString("sum"));
|
||||
if (StrUtil.isNotBlank(sumStr)) {
|
||||
try {
|
||||
total = Integer.parseInt(sumStr);
|
||||
} catch (NumberFormatException e) {
|
||||
log.warn("SAP物料查询返回的总数非法: {}", sumStr);
|
||||
}
|
||||
}
|
||||
if (total > 0) {
|
||||
int totalPages = (total + pageSize - 1) / pageSize;
|
||||
if (page + 1 >= totalPages) {
|
||||
hasMore = false;
|
||||
}
|
||||
}
|
||||
page++;
|
||||
}
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("countInsert", countInsert);
|
||||
result.put("countUpdate", countUpdate);
|
||||
result.put("incremental", incremental);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建 MDG012 物料查询请求报文
|
||||
*/
|
||||
private JSONObject buildMaterialQueryRequest(String changeDate, int page, int pageSize) {
|
||||
JSONObject header = new JSONObject();
|
||||
header.put("intf_id", SapConstant.INTF_MDG012);
|
||||
header.put("src_system", SapConstant.MATERIAL_QUERY_SRC_SYSTEM);
|
||||
header.put("dest_system", SapConstant.MATERIAL_QUERY_DEST_SYSTEM);
|
||||
header.put("src_msgid", "wsx");
|
||||
header.put("requesttime", "");
|
||||
header.put("backup1", "1");
|
||||
header.put("backup2", "2");
|
||||
|
||||
JSONObject item = new JSONObject();
|
||||
item.put("MATNR", "");
|
||||
item.put("ERSDA", "");
|
||||
item.put("CREATED_AT_TIME", "");
|
||||
item.put("LAEDA", StrUtil.blankToDefault(changeDate, ""));
|
||||
item.put("LAST_CHANGED_TIME", "");
|
||||
item.put("ZRSV03", "");
|
||||
item.put("ZRSV04", "");
|
||||
item.put("ZRSV05", "");
|
||||
item.put("ZRSV06", "");
|
||||
item.put("ZRSV07", "");
|
||||
item.put("PAGE", String.valueOf(page));
|
||||
item.put("COUNT", String.valueOf(pageSize));
|
||||
|
||||
JSONArray bodyArr = new JSONArray();
|
||||
bodyArr.add(item);
|
||||
|
||||
JSONObject requestJson = new JSONObject();
|
||||
requestJson.put("header", header);
|
||||
requestJson.put("body", bodyArr);
|
||||
return requestJson;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取物料名称:优先顶层 maktx,其次取 item1 多语言数组的第一条
|
||||
*/
|
||||
private String resolveMaterialName(JSONObject one) {
|
||||
String materialName = StrUtil.trim(one.getString("maktx"));
|
||||
if (StrUtil.isNotBlank(materialName)) {
|
||||
return materialName;
|
||||
}
|
||||
JSONArray item1 = one.getJSONArray("item1");
|
||||
if (item1 != null && !item1.isEmpty()) {
|
||||
JSONObject lang = item1.getJSONObject(0);
|
||||
if (lang != null) {
|
||||
return StrUtil.trim(lang.getString("maktx"));
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按单位编码(meins)反查计量单位,查不到返回 null(不阻断同步)
|
||||
*/
|
||||
private MdPbMeasureunit findUnitByCode(String unitCode) {
|
||||
if (StrUtil.isBlank(unitCode)) {
|
||||
return null;
|
||||
}
|
||||
return measureunitService.getOne(new LambdaQueryWrapper<MdPbMeasureunit>()
|
||||
.eq(MdPbMeasureunit::getUnit_code, unitCode).last("limit 1"));
|
||||
}
|
||||
|
||||
private void validateSyncHeader(SapHeader header) {
|
||||
if (header == null || StrUtil.isBlank(header.getIntfId())) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user