| | |
| | | import com.vincent.rsf.framework.exception.CoolException; |
| | | import com.vincent.rsf.server.api.utils.LocUtils; |
| | | import com.vincent.rsf.server.common.constant.Constants; |
| | | import com.vincent.rsf.server.common.domain.BaseParam; |
| | | import com.vincent.rsf.server.common.domain.PageParam; |
| | | import com.vincent.rsf.server.manager.controller.dto.ExistDto; |
| | | import com.vincent.rsf.server.manager.controller.dto.OrderOutItemDto; |
| | | import com.vincent.rsf.server.manager.controller.params.*; |
| | | import com.vincent.rsf.server.manager.enums.*; |
| | | import com.vincent.rsf.server.manager.entity.Matnr; |
| | | import com.vincent.rsf.server.manager.entity.*; |
| | | import com.vincent.rsf.server.manager.mapper.AsnOrderMapper; |
| | | import com.vincent.rsf.server.manager.mapper.LocItemMapper; |
| | | import com.vincent.rsf.server.manager.service.*; |
| | | import com.vincent.rsf.server.manager.utils.LocManageUtil; |
| | | import com.vincent.rsf.server.manager.utils.OptimalAlgorithmUtil; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | |
| | | |
| | | public Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | /** 出库剩余量容差:小于等于此值视为已分配完,避免浮点误差产生多余“库存不足”行 */ |
| | | private static final BigDecimal ISSUED_TOLERANCE = new BigDecimal("0.000001"); |
| | | |
| | | @Autowired |
| | | private AsnOrderItemService asnOrderItemService; |
| | |
| | | private LocService locService; |
| | | @Autowired |
| | | private WaveOrderRelaServiceImpl waveOrderRelaService; |
| | | @Autowired |
| | | private TaskItemService taskItemService; |
| | | @Autowired |
| | | private LocItemMapper locItemMapper; |
| | | |
| | | @Override |
| | | public PageParam<Matnr, BaseParam> pageMatnrForOutStock(PageParam<Matnr, BaseParam> pageParam, Map<String, Object> params) { |
| | | // 在 getMatnrPage 之前取出 locUseStatus:getMatnrPage 会从 where.map 中 remove 掉该键(与 params 同引用),导致后续取不到 |
| | | String locUseStatus = params.get("locUseStatus") != null ? params.get("locUseStatus").toString().trim() : null; |
| | | if (locUseStatus != null && locUseStatus.isEmpty()) locUseStatus = null; |
| | | |
| | | PageParam<Matnr, BaseParam> page = matnrService.getMatnrPage(pageParam, params); |
| | | List<Matnr> records = page.getRecords(); |
| | | if (records == null || records.isEmpty()) { |
| | | return page; |
| | | } |
| | | List<Long> matnrIds = records.stream().map(Matnr::getId).collect(Collectors.toList()); |
| | | List<Map<String, Object>> stockByLocList = locItemMapper.listStockByMatnrIdsGroupByStatusAndLoc(matnrIds, locUseStatus); |
| | | Map<Long, List<Map<String, Object>>> rowsByMatnr = buildRowsByMatnrFromPerLoc(stockByLocList); |
| | | List<Matnr> expanded = new ArrayList<>(); |
| | | for (Matnr record : records) { |
| | | List<Map<String, Object>> statusRows = rowsByMatnr.get(record.getId()); |
| | | if (statusRows == null || statusRows.isEmpty()) { |
| | | record.setStockQty(0d); |
| | | record.setLocUseStatus$(null); |
| | | record.setLocCodes$(null); |
| | | expanded.add(record); |
| | | continue; |
| | | } |
| | | for (Map<String, Object> row : statusRows) { |
| | | double v = row.get("stockQty") instanceof Number ? ((Number) row.get("stockQty")).doubleValue() : 0d; |
| | | String useStatus = getStr(row, "useStatus", "usestatus"); |
| | | String statusDesc = useStatus != null ? LocStsType.getDescByType(useStatus) : null; |
| | | String locCodesWithQty = getStr(row, "locCodes$", "loccodes$"); |
| | | Matnr copy = cloneMatnrForRow(record); |
| | | copy.setStockQty(v); |
| | | copy.setLocUseStatus$(statusDesc); |
| | | copy.setLocCodes$(locCodesWithQty); |
| | | expanded.add(copy); |
| | | } |
| | | } |
| | | page.setRecords(expanded); |
| | | return page; |
| | | } |
| | | |
| | | /** 复制物料用于按状态展开行(仅复制展示用字段,id 保持原样供前端选行用) */ |
| | | private static Matnr cloneMatnrForRow(Matnr source) { |
| | | Matnr copy = new Matnr(); |
| | | BeanUtils.copyProperties(source, copy, "stockQty", "locUseStatus$", "locCodes$"); |
| | | return copy; |
| | | } |
| | | |
| | | /** |
| | | * 仅用「按库位明细」查询结果在内存中分组汇总:按 (matnrId, useStatus) 聚合, |
| | | * 得到 stockQty、locCodes$(库位(数量),...),减轻数据库压力。 |
| | | */ |
| | | private static Map<Long, List<Map<String, Object>>> buildRowsByMatnrFromPerLoc(List<Map<String, Object>> stockByLocList) { |
| | | Map<String, List<Map<String, Object>>> perLocByMatnrAndStatus = new HashMap<>(); |
| | | for (Map<String, Object> locRow : stockByLocList) { |
| | | Long mid = getLong(locRow, "matnrId", "matnrid"); |
| | | String us = getStr(locRow, "useStatus", "usestatus"); |
| | | if (mid == null || us == null) continue; |
| | | String key = mid + ":" + us; |
| | | perLocByMatnrAndStatus.computeIfAbsent(key, k -> new ArrayList<>()).add(locRow); |
| | | } |
| | | Map<Long, List<Map<String, Object>>> rowsByMatnr = new HashMap<>(); |
| | | for (Map.Entry<String, List<Map<String, Object>>> e : perLocByMatnrAndStatus.entrySet()) { |
| | | String[] parts = e.getKey().split(":", 2); |
| | | if (parts.length != 2) continue; |
| | | Long matnrId; |
| | | try { matnrId = Long.parseLong(parts[0]); } catch (NumberFormatException ex) { continue; } |
| | | String useStatus = parts[1]; |
| | | List<Map<String, Object>> locs = e.getValue(); |
| | | double stockQty = 0d; |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (Map<String, Object> locRow : locs) { |
| | | Object q = getAny(locRow, "locQty", "locqty"); |
| | | double qty = q instanceof Number ? ((Number) q).doubleValue() : 0d; |
| | | stockQty += qty; |
| | | String code = getStr(locRow, "locCode", "loccode"); |
| | | if (sb.length() > 0) sb.append(","); |
| | | sb.append(code != null ? code : "").append("(").append(formatQtyForLoc(qty)).append(")"); |
| | | } |
| | | Map<String, Object> statusRow = new HashMap<>(); |
| | | statusRow.put("matnrId", matnrId); |
| | | statusRow.put("useStatus", useStatus); |
| | | statusRow.put("stockQty", stockQty); |
| | | statusRow.put("locCodes$", sb.toString()); |
| | | rowsByMatnr.computeIfAbsent(matnrId, k -> new ArrayList<>()).add(statusRow); |
| | | } |
| | | return rowsByMatnr; |
| | | } |
| | | |
| | | private static String formatQtyForLoc(double qty) { |
| | | if (qty == (long) qty) return String.valueOf((long) qty); |
| | | String s = BigDecimal.valueOf(qty).setScale(6, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString(); |
| | | return s; |
| | | } |
| | | |
| | | private static Long getLong(Map<String, Object> map, String... keys) { |
| | | Object v = getAny(map, keys); |
| | | if (v == null) return null; |
| | | if (v instanceof Long) return (Long) v; |
| | | if (v instanceof Number) return ((Number) v).longValue(); |
| | | try { return Long.parseLong(v.toString()); } catch (NumberFormatException e) { return null; } |
| | | } |
| | | |
| | | private static String getStr(Map<String, Object> map, String... keys) { |
| | | Object v = getAny(map, keys); |
| | | return v != null ? v.toString() : null; |
| | | } |
| | | |
| | | private static Object getAny(Map<String, Object> map, String... keys) { |
| | | for (String key : keys) { |
| | | Object v = map.get(key); |
| | | if (v != null) return v; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * @param |
| | |
| | | * @time 2025/4/29 13:47 |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public R updateOrderItem(AsnOrderAndItemsParams params, Long loginUserId) { |
| | | WkOrder orders = params.getOrders(); |
| | | if (Objects.isNull(orders)) { |
| | |
| | | if (Objects.isNull(params.getItems()) || params.getItems().isEmpty()) { |
| | | throw new CoolException("明细参数不能为空!!"); |
| | | } |
| | | // 删除/修改明细前收集当前单据下的明细 id,用于校验与同步库位状态 |
| | | List<Long> existingIds = asnOrderItemService.list( |
| | | new LambdaQueryWrapper<WkOrderItem>().eq(WkOrderItem::getOrderId, orders.getId())) |
| | | .stream().map(WkOrderItem::getId).collect(Collectors.toList()); |
| | | Set<Long> requestedIds = params.getItems().stream() |
| | | .map(item -> item.get("id")) |
| | | .filter(Objects::nonNull) |
| | | .map(id -> Long.valueOf(id.toString())) |
| | | .collect(Collectors.toSet()); |
| | | // 已生成工作档的明细不允许删除 |
| | | for (Long existingId : existingIds) { |
| | | if (!requestedIds.contains(existingId) && hasGeneratedTask(orders.getId(), existingId)) { |
| | | throw new CoolException("该明细已生成工作档,不能删除"); |
| | | } |
| | | } |
| | | // 已生成工作档的明细不允许修改 |
| | | for (Map<String, Object> item : params.getItems()) { |
| | | Object idObj = item.get("id"); |
| | | if (idObj != null && hasGeneratedTask(orders.getId(), Long.valueOf(idObj.toString()))) { |
| | | throw new CoolException("该明细已生成工作档,不能修改"); |
| | | } |
| | | } |
| | | try { |
| | | svaeOrUpdateOrderItem(params, loginUserId); |
| | | } catch (Exception e) { |
| | | throw new CoolException(e.getMessage()); |
| | | } |
| | | // 对本次被删除的明细(仅初始化状态):先同步库位状态,再删除明细记录 |
| | | for (Long existingId : existingIds) { |
| | | if (!requestedIds.contains(existingId)) { |
| | | syncLocStatusOnOrderItemRemoved(orders.getId(), existingId, loginUserId); |
| | | outStockItemService.removeById(existingId); |
| | | } |
| | | } |
| | | // 重新汇总主单数量(删除明细后) |
| | | List<WkOrderItem> afterItems = asnOrderItemService.list(new LambdaQueryWrapper<WkOrderItem>() |
| | | .eq(WkOrderItem::getOrderId, orders.getId())); |
| | | Double sum = afterItems.stream().mapToDouble(WkOrderItem::getAnfme).sum(); |
| | | orders.setAnfme(sum); |
| | | this.updateById(orders); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 判断出库单明细是否已生成工作档(存在关联的任务明细) |
| | | */ |
| | | private boolean hasGeneratedTask(Long orderId, Long orderItemId) { |
| | | return taskItemService.count(new LambdaQueryWrapper<TaskItem>() |
| | | .eq(TaskItem::getSourceId, orderId) |
| | | .eq(TaskItem::getOrderItemId, orderItemId)) > 0; |
| | | } |
| | | |
| | | /** |
| | | * 出库单明细被删除时同步库位状态:释放该明细关联的库位预约、回滚 LocItem.workQty、恢复库位为在库(F) |
| | | */ |
| | | private void syncLocStatusOnOrderItemRemoved(Long orderId, Long orderItemId, Long loginUserId) { |
| | | List<TaskItem> taskItems = taskItemService.list(new LambdaQueryWrapper<TaskItem>() |
| | | .eq(TaskItem::getSourceId, orderId) |
| | | .eq(TaskItem::getOrderItemId, orderItemId)); |
| | | if (taskItems.isEmpty()) { |
| | | return; |
| | | } |
| | | Set<Long> affectedLocIds = new HashSet<>(); |
| | | Date now = new Date(); |
| | | for (TaskItem taskItem : taskItems) { |
| | | if (taskItem.getSource() == null) { |
| | | continue; |
| | | } |
| | | LocItem locItem = locItemService.getById(taskItem.getSource()); |
| | | if (locItem == null) { |
| | | continue; |
| | | } |
| | | Double anfme = taskItem.getAnfme() != null ? taskItem.getAnfme() : 0.0; |
| | | Double newWorkQty = Math.round((locItem.getWorkQty() - anfme) * 1000000) / 1000000.0; |
| | | locItem.setWorkQty(newWorkQty >= 0 ? newWorkQty : 0) |
| | | .setOrderId(null) |
| | | .setOrderItemId(null) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(now); |
| | | locItemService.updateById(locItem); |
| | | affectedLocIds.add(locItem.getLocId()); |
| | | } |
| | | for (Long locId : affectedLocIds) { |
| | | long stillReserved = locItemService.count(new LambdaQueryWrapper<LocItem>() |
| | | .eq(LocItem::getLocId, locId) |
| | | .isNotNull(LocItem::getOrderId)); |
| | | if (stillReserved == 0) { |
| | | Loc loc = locService.getById(locId); |
| | | if (loc != null && LocStsType.LOC_STS_TYPE_R.type.equals(loc.getUseStatus())) { |
| | | loc.setUseStatus(LocStsType.LOC_STS_TYPE_F.type) |
| | | .setUpdateBy(loginUserId) |
| | | .setUpdateTime(now); |
| | | locService.updateById(loc); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | |
| | | if (StringUtils.isNotBlank(locItem.getFieldsIndex())) { |
| | | orderItemWrapper.eq(WkOrderItem::getFieldsIndex, locItem.getFieldsIndex()); |
| | | } |
| | | WkOrderItem orderItem = outStockItemService.getOne(orderItemWrapper); |
| | | // 同一出库单下同一物料可能有多条明细(如多行合并),用 list 取仍有剩余数量的第一条,避免 getOne 返回多条抛 TooManyResultsException |
| | | List<WkOrderItem> orderItemCandidates = outStockItemService.list(orderItemWrapper); |
| | | WkOrderItem orderItem = orderItemCandidates.stream() |
| | | .filter(o -> o.getAnfme() != null && o.getWorkQty() != null && o.getAnfme().compareTo(o.getWorkQty()) > 0) |
| | | .findFirst() |
| | | .orElse(null); |
| | | |
| | | // 如果找不到单据明细,且LocItem来自库存调整,则自动创建WkOrderItem |
| | | if (Objects.isNull(orderItem)) { |
| | |
| | | |
| | | locItem.setOutQty(param.getOutQty()) |
| | | .setBatch(param.getBatch()) |
| | | .setOrderId(outId) |
| | | .setOrderItemId(orderItem.getId()) |
| | | .setSourceId(outId) |
| | | .setSourceCode(orderItem.getOrderCode()) |
| | | .setSource(orderItem.getId()); |
| | | locItems.add(locItem); |
| | | |
| | | LocToTaskParams taskParams = new LocToTaskParams(); |
| | | // 出库单下发任务时,出库口未传则默认 1001 |
| | | String siteNo = StringUtils.isNotBlank(param.getSiteNo()) ? param.getSiteNo() : "1001"; |
| | | taskParams.setType(Constants.TASK_TYPE_ORDER_OUT_STOCK) |
| | | .setOrgLoc(loc.getCode()) |
| | | .setItems(locItems) |
| | | .setSourceId(outId) |
| | | .setSiteNo(param.getSiteNo()); |
| | | .setSiteNo(siteNo); |
| | | try { |
| | | //生成出库任务 |
| | | locItemService.generateTask(TaskResouceType.TASK_RESOUCE_ORDER_TYPE.val, taskParams, loginUserId); |
| | |
| | | TaskType.TASK_TYPE_OUT.type, |
| | | TaskType.TASK_TYPE_MERGE_OUT.type, |
| | | TaskType.TASK_TYPE_PICK_AGAIN_OUT.type); |
| | | List<DeviceSite> sites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>().in(DeviceSite::getType, list).groupBy(DeviceSite::getSite)); |
| | | // 先查全部再按 site 去重,避免 GROUP BY 与 only_full_group_by 冲突 |
| | | List<DeviceSite> all = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>().in(DeviceSite::getType, list)); |
| | | List<DeviceSite> sites = all.stream() |
| | | .collect(Collectors.toMap(DeviceSite::getSite, d -> d, (a, b) -> a)) |
| | | .values().stream() |
| | | .collect(Collectors.toList()); |
| | | return R.ok(sites); |
| | | } |
| | | |
| | |
| | | Set<ExistDto> existDtos = new HashSet<>(); |
| | | for (WkOrderItem wkOrderItem : wkOrderItems) { |
| | | BigDecimal issued = new BigDecimal(wkOrderItem.getAnfme().toString()) |
| | | .subtract(new BigDecimal(wkOrderItem.getWorkQty().toString()) |
| | | ); |
| | | if (issued.doubleValue() <= 0) { |
| | | .subtract(new BigDecimal(wkOrderItem.getWorkQty().toString())); |
| | | if (issued.compareTo(ISSUED_TOLERANCE) <= 0) { |
| | | continue; |
| | | } |
| | | List<LocItem> locItems = new ArrayList<>(); |
| | |
| | | for (LocItem locItem : locItems) { |
| | | Loc loc = locService.getById(locItem.getLocId()); |
| | | List<LocItem> itemList = locItemService.list(new LambdaQueryWrapper<LocItem>().eq(LocItem::getLocCode, locItem.getLocCode())); |
| | | if (issued.doubleValue() > 0) { |
| | | ExistDto existDto = new ExistDto().setBatch(locItem.getBatch()).setMatnr(locItem.getMatnrCode()).setLocNo(locItem.getLocCode()); |
| | | if (existDtos.add(existDto)) { |
| | | locItem.setOutQty(issued.doubleValue() >= locItem.getAnfme() ? locItem.getAnfme() : issued.doubleValue()); |
| | | locItem.setBarcode(loc.getBarcode()); |
| | | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); |
| | | orderOutItemDto.setLocItem(locItem); |
| | | if (issued.compareTo(ISSUED_TOLERANCE) <= 0) { |
| | | break; |
| | | } |
| | | // 该库位可分配数量:取本行待分配与库位库存的较小值 |
| | | double allocatable = Math.min(issued.doubleValue(), locItem.getAnfme() != null ? locItem.getAnfme() : 0); |
| | | // 当分配量等于库位库存时,使用库位库存精度作为出库数量,避免截断导致界面显示/库存校验不一致(如库存15.123457被截成15.123) |
| | | double outQtyToSet = (locItem.getAnfme() != null && Math.abs(allocatable - locItem.getAnfme()) < 1e-6) |
| | | ? locItem.getAnfme() : allocatable; |
| | | ExistDto existDto = new ExistDto().setBatch(locItem.getBatch()).setMatnr(locItem.getMatnrCode()).setLocNo(locItem.getLocCode()); |
| | | if (existDtos.add(existDto)) { |
| | | // 首次使用该库位:加入列表并扣减 issued |
| | | locItem.setOutQty(outQtyToSet); |
| | | locItem.setBarcode(loc.getBarcode()); |
| | | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); |
| | | orderOutItemDto.setLocItem(locItem); |
| | | |
| | | List<DeviceSite> deviceSites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>() |
| | | .eq(DeviceSite::getChannel, loc.getChannel()) |
| | | .eq(DeviceSite::getType, issued.doubleValue() >= locItem.getAnfme() && itemList.size() == 1 ? TaskType.TASK_TYPE_OUT.type : TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) |
| | | ); |
| | | List<DeviceSite> deviceSites = deviceSiteService.list(new LambdaQueryWrapper<DeviceSite>() |
| | | .eq(DeviceSite::getChannel, loc.getChannel()) |
| | | .eq(DeviceSite::getType, issued.doubleValue() >= locItem.getAnfme() && itemList.size() == 1 ? TaskType.TASK_TYPE_OUT.type : TaskType.TASK_TYPE_PICK_AGAIN_OUT.type) |
| | | ); |
| | | // 出库口列表排序:1001 排第一,作为默认 |
| | | deviceSites.sort((a, b) -> { |
| | | boolean a1001 = "1001".equals(a.getSite()); |
| | | boolean b1001 = "1001".equals(b.getSite()); |
| | | if (a1001 && !b1001) return -1; |
| | | if (!a1001 && b1001) return 1; |
| | | return 0; |
| | | }); |
| | | |
| | | if (!deviceSites.isEmpty()) { |
| | | List<OrderOutItemDto.staListDto> maps = new ArrayList<>(); |
| | | for (DeviceSite sta : deviceSites) { |
| | | OrderOutItemDto.staListDto staListDto = new OrderOutItemDto.staListDto(); |
| | | staListDto.setStaNo(sta.getSite()); |
| | | staListDto.setStaName(sta.getSite()); |
| | | maps.add(staListDto); |
| | | } |
| | | orderOutItemDto.setStaNos(maps); |
| | | //默认获取第一站点 |
| | | DeviceSite deviceSite = deviceSites.stream().findFirst().get(); |
| | | orderOutItemDto.setSiteNo(deviceSite.getSite()); |
| | | if (!deviceSites.isEmpty()) { |
| | | List<OrderOutItemDto.staListDto> maps = new ArrayList<>(); |
| | | for (DeviceSite sta : deviceSites) { |
| | | OrderOutItemDto.staListDto staListDto = new OrderOutItemDto.staListDto(); |
| | | staListDto.setStaNo(sta.getSite()); |
| | | staListDto.setStaName(sta.getSite()); |
| | | maps.add(staListDto); |
| | | } |
| | | |
| | | list.add(orderOutItemDto); |
| | | |
| | | issued = issued.subtract(new BigDecimal(locItem.getAnfme().toString())); |
| | | orderOutItemDto.setStaNos(maps); |
| | | //默认获取第一站点 |
| | | DeviceSite deviceSite = deviceSites.stream().findFirst().get(); |
| | | orderOutItemDto.setSitesNo(deviceSite.getSite()); |
| | | } |
| | | |
| | | list.add(orderOutItemDto); |
| | | issued = issued.subtract(new BigDecimal(locItem.getAnfme().toString())); |
| | | } else { |
| | | // 该库位已被前序订单行占用:只扣减 issued,不重复加入列表,避免产生“库存不足”脏数据 |
| | | issued = issued.subtract(new BigDecimal(String.valueOf(allocatable))); |
| | | } |
| | | } |
| | | if (issued.doubleValue() > 0) { |
| | | if (issued.compareTo(ISSUED_TOLERANCE) > 0) { |
| | | double remaining = issued.setScale(6, RoundingMode.HALF_UP).doubleValue(); |
| | | LocItem locItem = new LocItem() |
| | | .setId(new Random().nextLong()) |
| | | .setMatnrCode(wkOrderItem.getMatnrCode()) |
| | | .setMaktx(wkOrderItem.getMaktx()) |
| | | .setAnfme(0.00) |
| | | .setWorkQty(issued.doubleValue()) |
| | | .setOutQty(issued.doubleValue()) |
| | | .setWorkQty(remaining) |
| | | .setOutQty(remaining) |
| | | .setUnit(wkOrderItem.getStockUnit()) |
| | | .setBatch(wkOrderItem.getSplrBatch()); |
| | | OrderOutItemDto orderOutItemDto = new OrderOutItemDto(); |