package com.vincent.rsf.server.manager.service; import com.vincent.rsf.server.api.controller.erp.params.SyncOrdersItem; import com.vincent.rsf.server.manager.entity.Matnr; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Objects; /** * 主库 man_matnr 按视图结果写入;独立事务,与副库视图查询分离 */ @Slf4j @Service public class CusBarcodeSyncMatnrApplyService { @Autowired private MatnrService matnrService; @Transactional(propagation = Propagation.REQUIRES_NEW) public void applyFromViewRows(List> viewItems, Map orderItemByCode, Long loginUserId) { if (viewItems == null || viewItems.isEmpty()) { return; } for (Map row : dedupeCusBarcodeViewRows(viewItems)) { String matCode = CusBarcodeSyncViewQueryService.matnrCodeFromBarcode(Objects.toString(row.get("barcode"), null)); if (matCode == null) { continue; } SyncOrdersItem syncItem = resolveSyncOrderItem(orderItemByCode, matCode); String viewSpec = StringUtils.trimToEmpty(Objects.toString(row.get("item_spec"), "")); String viewUnit = StringUtils.trimToNull(Objects.toString(row.get("unit_no"), null)); String viewItemName = StringUtils.trimToNull(Objects.toString(row.get("item_name"), null)); String incomingName = syncItem == null ? null : StringUtils.trimToNull(syncItem.getMaktx()); Matnr local = matnrService.getOneByCodeAndBatch(matCode, ""); if (local == null) { Matnr matnr = new Matnr(); matnr.setCode(matCode) .setBatch("") .setName(incomingName != null ? incomingName : (viewItemName != null ? viewItemName : matCode)) .setSpec(viewSpec) .setUnit(viewUnit) .setStockUnit(viewUnit) .setStatus(1) .setCreateBy(loginUserId) .setUpdateBy(loginUserId) .setCreateTime(new Date()) .setUpdateTime(new Date()); matnrService.save(matnr); log.info("[cus_barcode_sync] man_matnr 新增 code={}", matCode); continue; } boolean nameDiff = incomingName != null && !StringUtils.equals(StringUtils.trimToEmpty(local.getName()), incomingName); boolean specDiff = !StringUtils.equals(StringUtils.trimToEmpty(local.getSpec()), viewSpec); boolean unitDiff = viewUnit != null && (!StringUtils.equals(StringUtils.trimToEmpty(local.getUnit()), viewUnit) || !StringUtils.equals(StringUtils.trimToEmpty(local.getStockUnit()), viewUnit)); if (!nameDiff && !specDiff && !unitDiff) { log.debug("[cus_barcode_sync] man_matnr 已存在且无变更 code={} id={}", matCode, local.getId()); continue; } Matnr update = new Matnr(); update.setId(local.getId()); if (nameDiff) { update.setName(incomingName); } if (specDiff) { update.setSpec(viewSpec); } if (unitDiff) { update.setUnit(viewUnit).setStockUnit(viewUnit); } update.setUpdateBy(loginUserId).setUpdateTime(new Date()); matnrService.updateById(update); log.info("[cus_barcode_sync] man_matnr 更新 code={} id={} nameDiff={} specDiff={} unitDiff={}", matCode, local.getId(), nameDiff, specDiff, unitDiff); } } private static SyncOrdersItem resolveSyncOrderItem(Map orderItemByCode, String matCode) { return matCode == null ? null : orderItemByCode.get(matCode); } private static List> dedupeCusBarcodeViewRows(List> viewItems) { Map> firstByCode = new LinkedHashMap<>(); if (viewItems == null) { return Collections.emptyList(); } for (Map row : viewItems) { String matCode = CusBarcodeSyncViewQueryService.matnrCodeFromBarcode(Objects.toString(row.get("barcode"), null)); if (matCode == null) { continue; } firstByCode.putIfAbsent(matCode, row); } return new ArrayList<>(firstByCode.values()); } }