| | |
| | | import com.vincent.rsf.server.manager.controller.dto.LocStockDto; |
| | | import com.vincent.rsf.server.manager.entity.*; |
| | | import com.vincent.rsf.server.manager.enums.*; |
| | | import com.vincent.rsf.server.manager.mapper.MatnrMapper; |
| | | import com.vincent.rsf.server.manager.service.*; |
| | | import com.vincent.rsf.server.system.constant.DictTypeCode; |
| | | import com.vincent.rsf.server.system.constant.GlobalConfigCode; |
| | |
| | | private TaskItemService taskItemService; |
| | | @Autowired |
| | | private TaskService taskService; |
| | | @Autowired |
| | | private MatnrMapper matnrMapper; |
| | | |
| | | /** |
| | | * 云仓改单/取消前:任务明细已关联该单据且主任务未逻辑删除则不允许 |
| | |
| | | return; |
| | | } |
| | | assertWkOrderExceStatusUnexecuted(order, "取消"); |
| | | } |
| | | |
| | | private CusItemSyncMode resolveCusItemSyncMode() { |
| | | Config c = configService.getOne(new LambdaQueryWrapper<Config>() |
| | | .eq(Config::getFlag, GlobalConfigCode.CUS_ITEM_SYNC_MODE) |
| | | .eq(Config::getDeleted, 0), false); |
| | | if (c == null) { |
| | | return CusItemSyncMode.NONE; |
| | | } |
| | | return CusItemSyncMode.fromConfig(c.getVal()); |
| | | } |
| | | |
| | | private static Map<String, SyncOrdersItem> buildOrderItemByMatnrCode(List<SyncOrdersItem> orderItems) { |
| | | Map<String, SyncOrdersItem> map = new LinkedHashMap<>(); |
| | | if (orderItems == null) { |
| | | return map; |
| | | } |
| | | for (SyncOrdersItem item : orderItems) { |
| | | if (StringUtils.isBlank(item.getMatnr())) { |
| | | continue; |
| | | } |
| | | map.putIfAbsent(item.getMatnr().trim(), item); |
| | | } |
| | | return map; |
| | | } |
| | | |
| | | /** |
| | | * 按视图行更新/插入物料主数据(与通知档无关)。 |
| | | */ |
| | | private void applyCusItemViewRowsToMatnr(List<Map<String, Object>> viewItems, Map<String, SyncOrdersItem> orderItemByCode, Long loginUserId) { |
| | | if (viewItems == null || viewItems.isEmpty()) { |
| | | return; |
| | | } |
| | | for (Map<String, Object> row : viewItems) { |
| | | String itemNo = StringUtils.trimToNull(Objects.toString(row.get("item_no"), null)); |
| | | if (itemNo == null) { |
| | | continue; |
| | | } |
| | | SyncOrdersItem syncItem = orderItemByCode.get(itemNo); |
| | | String viewSpec = StringUtils.trimToEmpty(Objects.toString(row.get("item_spec"), "")); |
| | | String viewUnit = StringUtils.trimToNull(Objects.toString(row.get("unit_no"), null)); |
| | | String incomingName = syncItem == null ? null : StringUtils.trimToNull(syncItem.getMaktx()); |
| | | Matnr local = matnrService.getOneByCodeAndBatch(itemNo, ""); |
| | | if (local == null) { |
| | | Matnr matnr = new Matnr(); |
| | | matnr.setCode(itemNo) |
| | | .setBatch("") |
| | | .setName(incomingName != null ? incomingName : itemNo) |
| | | .setSpec(viewSpec) |
| | | .setUnit(viewUnit) |
| | | .setStockUnit(viewUnit) |
| | | .setStatus(1) |
| | | .setCreateBy(loginUserId) |
| | | .setUpdateBy(loginUserId) |
| | | .setCreateTime(new Date()) |
| | | .setUpdateTime(new Date()); |
| | | matnrService.save(matnr); |
| | | 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) { |
| | | 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); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 按配置处理视图与物料表。 |
| | | */ |
| | | private void syncMatnrFromCusItemSyncViewByConfig(List<SyncOrdersItem> orderItems, Long loginUserId) { |
| | | if (orderItems == null || orderItems.isEmpty()) { |
| | | return; |
| | | } |
| | | List<String> matnrCodes = orderItems.stream() |
| | | .map(SyncOrdersItem::getMatnr) |
| | | .filter(StringUtils::isNotBlank) |
| | | .map(String::trim) |
| | | .distinct() |
| | | .collect(Collectors.toList()); |
| | | if (matnrCodes.isEmpty()) { |
| | | return; |
| | | } |
| | | CusItemSyncMode mode = resolveCusItemSyncMode(); |
| | | Map<String, SyncOrdersItem> orderItemByCode = buildOrderItemByMatnrCode(orderItems); |
| | | |
| | | if (mode == CusItemSyncMode.NONE) { |
| | | syncMatnrNonForceFromView(matnrCodes, orderItemByCode, loginUserId); |
| | | return; |
| | | } |
| | | |
| | | List<Map<String, Object>> viewItems = matnrMapper.selectByCusItemSyncView(matnrCodes); |
| | | Set<String> inView = viewItems == null ? Collections.emptySet() : viewItems.stream() |
| | | .map(r -> StringUtils.trimToNull(Objects.toString(r.get("item_no"), null))) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | |
| | | for (String code : matnrCodes) { |
| | | if (!inView.contains(code)) { |
| | | throw new CoolException("物料未在视图 cus_item_sync_view 中:" + code); |
| | | } |
| | | } |
| | | applyCusItemViewRowsToMatnr(viewItems, orderItemByCode, loginUserId); |
| | | } |
| | | |
| | | /** |
| | | * 不强制:视图能查到则新增/更新物料表;查不到的编码再查物料表,存在则放行。 |
| | | */ |
| | | private void syncMatnrNonForceFromView(List<String> matnrCodes, Map<String, SyncOrdersItem> orderItemByCode, Long loginUserId) { |
| | | List<Map<String, Object>> viewItems = null; |
| | | try { |
| | | viewItems = matnrMapper.selectByCusItemSyncView(matnrCodes); |
| | | } catch (Exception ex) { |
| | | log.warn("查询 cus_item_sync_view 失败,将仅按物料表校验:{}", ex.getMessage()); |
| | | } |
| | | Set<String> inView = viewItems == null ? Collections.emptySet() : viewItems.stream() |
| | | .map(r -> StringUtils.trimToNull(Objects.toString(r.get("item_no"), null))) |
| | | .filter(Objects::nonNull) |
| | | .collect(Collectors.toSet()); |
| | | if (viewItems != null && !viewItems.isEmpty()) { |
| | | try { |
| | | applyCusItemViewRowsToMatnr(viewItems, orderItemByCode, loginUserId); |
| | | } catch (Exception ex) { |
| | | log.warn("按视图写入物料主数据失败:{}", ex.getMessage()); |
| | | } |
| | | } |
| | | for (String code : matnrCodes) { |
| | | if (inView.contains(code)) { |
| | | continue; |
| | | } |
| | | Matnr m = matnrService.getOneByCodeAndBatch(code, ""); |
| | | if (m == null) { |
| | | throw new CoolException("视图无该物料且物料表不存在:" + code); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | } |
| | | // 按配置同步物料主数据(none 时不影响通知档写入主流程) |
| | | syncMatnrFromCusItemSyncViewByConfig(syncOrder.getOrderItems(), loginUserId); |
| | | WkOrder wkOrder = new WkOrder(); |
| | | String wkTypeInput = syncOrder.getWkType(); |
| | | String typeCode = StringUtils.isBlank(wkTypeInput) ? null : orderWorkTypeService.getTypeByLabel(wkTypeInput); |