| | |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; |
| | |
| | | list.forEach(item -> { if (item.getNextProcessId() != null) processIds.add(item.getNextProcessId()); }); |
| | | Map<Long, MesProProcessDO> processMap = convertMap( |
| | | processService.getProcessList(new ArrayList<>(processIds)), MesProProcessDO::getId); |
| | | // 2. 批量获取产出产品物料信息 |
| | | Set<Long> outputItemIds = convertSet(list, MesProRouteProcessDO::getOutputItemId); |
| | | outputItemIds.remove(null); |
| | | Map<Long, MesMdItemDO> outputItemMap = outputItemIds.isEmpty() |
| | | ? Collections.emptyMap() |
| | | : mdItemService.getItemMap(outputItemIds); |
| | | // 2. 批量获取产出产品物料信息(解析逗号分隔的字符串) |
| | | Set<Long> allOutputItemIds = new HashSet<>(); |
| | | Map<Long, List<Long>> voOutputItemIdsMap = new HashMap<>(); |
| | | for (int i = 0; i < list.size(); i++) { |
| | | String outputItemIdStr = list.get(i).getOutputItemId(); |
| | | if (outputItemIdStr != null && !outputItemIdStr.isBlank()) { |
| | | List<Long> ids = Arrays.stream(outputItemIdStr.split(",")) |
| | | .map(String::trim).filter(s -> !s.isEmpty()) |
| | | .map(Long::valueOf).collect(Collectors.toList()); |
| | | voOutputItemIdsMap.put((long) i, ids); |
| | | allOutputItemIds.addAll(ids); |
| | | } |
| | | } |
| | | Map<Long, MesMdItemDO> outputItemMap = allOutputItemIds.isEmpty() |
| | | ? Collections.emptyMap() : mdItemService.getItemMap(allOutputItemIds); |
| | | // 3. 拼接 VO |
| | | return BeanUtils.toBean(list, MesProRouteProcessRespVO.class, vo -> { |
| | | List<MesProRouteProcessRespVO> result = BeanUtils.toBean(list, MesProRouteProcessRespVO.class); |
| | | for (int i = 0; i < result.size(); i++) { |
| | | MesProRouteProcessRespVO vo = result.get(i); |
| | | MapUtils.findAndThen(processMap, vo.getProcessId(), |
| | | process -> vo.setProcessCode(process.getCode()).setProcessName(process.getName())); |
| | | MapUtils.findAndThen(processMap, vo.getNextProcessId(), |
| | | nextProcess -> vo.setNextProcessName(nextProcess.getName())); |
| | | MapUtils.findAndThen(outputItemMap, vo.getOutputItemId(), |
| | | item -> vo.setOutputItemCode(item.getCode()).setOutputItemName(item.getName())); |
| | | }); |
| | | // 产出产品(支持多选) |
| | | List<Long> ids = voOutputItemIdsMap.getOrDefault((long) i, Collections.emptyList()); |
| | | if (!ids.isEmpty()) { |
| | | vo.setOutputItemIds(ids); |
| | | List<String> names = ids.stream() |
| | | .map(id -> { |
| | | MesMdItemDO item = outputItemMap.get(id); |
| | | return item != null ? item.getName() : String.valueOf(id); |
| | | }).collect(Collectors.toList()); |
| | | vo.setOutputItemNames(String.join(", ", names)); |
| | | // 向后兼容:第一个产出产品 |
| | | Long firstId = ids.get(0); |
| | | vo.setOutputItemId(firstId); |
| | | MesMdItemDO firstItem = outputItemMap.get(firstId); |
| | | if (firstItem != null) { |
| | | vo.setOutputItemCode(firstItem.getCode()); |
| | | vo.setOutputItemName(firstItem.getName()); |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private MesProRouteProcessRespVO buildRouteProcessRespVO(MesProRouteProcessDO routeProcess) { |