| | |
| | | import cn.iocoder.yudao.framework.common.util.collection.MapUtils; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.process.MesProProcessBomItemVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.process.MesProProcessOutputItemVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.process.MesProRouteProcessRespVO; |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.route.vo.process.MesProRouteProcessSaveReqVO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO; |
| | |
| | | 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); |
| | | // 3. 拼接 VO |
| | | return BeanUtils.toBean(list, MesProRouteProcessRespVO.class, vo -> { |
| | | // 2. 解析产出配置(JSON 数组 或 旧的逗号分隔),收集全部物料/产品编号 |
| | | List<List<MesProProcessOutputItemVO>> outputsPerRow = new ArrayList<>(); |
| | | Set<Long> allItemIds = new HashSet<>(); |
| | | for (MesProRouteProcessDO row : list) { |
| | | List<MesProProcessOutputItemVO> outs = parseOutputsRaw(row.getOutputItemId()); |
| | | outputsPerRow.add(outs); |
| | | for (MesProProcessOutputItemVO out : outs) { |
| | | if (out.getItemId() != null) { |
| | | allItemIds.add(out.getItemId()); |
| | | } |
| | | if (out.getProductId() != null) { |
| | | allItemIds.add(out.getProductId()); |
| | | } |
| | | } |
| | | } |
| | | Map<Long, MesMdItemDO> outputItemMap = allItemIds.isEmpty() |
| | | ? Collections.emptyMap() : mdItemService.getItemMap(allItemIds); |
| | | // 3. 拼接 VO(先把 output_item_id 置空,避免 String 转 Long 抛 NumberFormatException;产出单独填充) |
| | | List<MesProRouteProcessDO> safeList = BeanUtils.toBean(list, MesProRouteProcessDO.class); |
| | | safeList.forEach(row -> row.setOutputItemId(null)); |
| | | List<MesProRouteProcessRespVO> result = BeanUtils.toBean(safeList, 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<MesProProcessOutputItemVO> outs = outputsPerRow.get(i); |
| | | for (MesProProcessOutputItemVO out : outs) { |
| | | fillOutputItemInfo(out, outputItemMap); |
| | | } |
| | | if (!outs.isEmpty()) { |
| | | List<Long> ids = outs.stream().map(MesProProcessOutputItemVO::getItemId) |
| | | .filter(Objects::nonNull).collect(Collectors.toList()); |
| | | vo.setOutputItemIds(ids); |
| | | vo.setOutputItemNames(outs.stream().map(MesProProcessOutputItemVO::getItemName) |
| | | .filter(Objects::nonNull).collect(Collectors.joining(", "))); |
| | | // 向后兼容:第一个产出物料 |
| | | MesProProcessOutputItemVO first = outs.get(0); |
| | | vo.setOutputItemId(first.getItemId()); |
| | | vo.setOutputItemCode(first.getItemCode()); |
| | | vo.setOutputItemName(first.getItemName()); |
| | | } |
| | | // 前端主用:outputs(含归属产品) |
| | | vo.setOutputs(outs); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | private MesProRouteProcessRespVO buildRouteProcessRespVO(MesProRouteProcessDO routeProcess) { |
| | |
| | | return buildRouteProcessRespVOList(ListUtil.of(routeProcess)).get(0); |
| | | } |
| | | |
| | | /** |
| | | * 解析 output_item_id 为产出配置列表(不含名称) |
| | | * 兼容 JSON 数组 [{"itemId":231,"productId":231}] 与逗号分隔 "231,232" 两种格式 |
| | | */ |
| | | private List<MesProProcessOutputItemVO> parseOutputsRaw(String raw) { |
| | | List<MesProProcessOutputItemVO> result = new ArrayList<>(); |
| | | if (raw == null || raw.isBlank()) { |
| | | return result; |
| | | } |
| | | String text = raw.trim(); |
| | | if (text.startsWith("[")) { |
| | | try { |
| | | cn.hutool.json.JSONArray array = cn.hutool.json.JSONUtil.parseArray(text); |
| | | for (Object element : array) { |
| | | if (!(element instanceof cn.hutool.json.JSONObject)) { |
| | | continue; |
| | | } |
| | | cn.hutool.json.JSONObject m = (cn.hutool.json.JSONObject) element; |
| | | Long itemId = m.getLong("itemId"); |
| | | if (itemId == null) { |
| | | continue; |
| | | } |
| | | MesProProcessOutputItemVO vo = new MesProProcessOutputItemVO(); |
| | | vo.setItemId(itemId); |
| | | vo.setProductId(m.getLong("productId")); |
| | | result.add(vo); |
| | | } |
| | | return result; |
| | | } catch (Exception ignored) { |
| | | // 解析失败按逗号兜底 |
| | | result.clear(); |
| | | } |
| | | } |
| | | for (String s : text.split(",")) { |
| | | String t = s.trim(); |
| | | if (t.isEmpty()) { |
| | | continue; |
| | | } |
| | | try { |
| | | MesProProcessOutputItemVO vo = new MesProProcessOutputItemVO(); |
| | | vo.setItemId(Long.valueOf(t)); |
| | | result.add(vo); |
| | | } catch (NumberFormatException ignored) { |
| | | // 跳过非法值 |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 填充产出物料的编码、名称与归属产品名称 |
| | | */ |
| | | private void fillOutputItemInfo(MesProProcessOutputItemVO vo, Map<Long, MesMdItemDO> itemMap) { |
| | | if (vo.getItemId() == null) { |
| | | return; |
| | | } |
| | | MesMdItemDO item = itemMap.get(vo.getItemId()); |
| | | if (item != null) { |
| | | vo.setItemCode(item.getCode()); |
| | | vo.setItemName(item.getName()); |
| | | } |
| | | if (vo.getProductId() != null) { |
| | | MesMdItemDO product = itemMap.get(vo.getProductId()); |
| | | if (product != null) { |
| | | vo.setProductName(product.getName()); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |