| | |
| | | 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; |
| | |
| | | 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 |
| | |
| | | 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); |
| | |
| | | .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<>(); |
| | |
| | | orderOutItemDto.setStaNos(maps); |
| | | //默认获取第一站点 |
| | | DeviceSite deviceSite = deviceSites.stream().findFirst().get(); |
| | | orderOutItemDto.setSiteNo(deviceSite.getSite()); |
| | | orderOutItemDto.setSitesNo(deviceSite.getSite()); |
| | | } |
| | | |
| | | list.add(orderOutItemDto); |