diff --git a/op-modules/op-mes/src/main/java/com/op/mes/service/impl/IWCInterfaceServiceImpl.java b/op-modules/op-mes/src/main/java/com/op/mes/service/impl/IWCInterfaceServiceImpl.java index dcc9d224..cab6fc6a 100644 --- a/op-modules/op-mes/src/main/java/com/op/mes/service/impl/IWCInterfaceServiceImpl.java +++ b/op-modules/op-mes/src/main/java/com/op/mes/service/impl/IWCInterfaceServiceImpl.java @@ -198,16 +198,33 @@ public class IWCInterfaceServiceImpl implements IWCSInterfaceService { if("equ_type_spj".equals(boardDTO.getEquTypeCode())){ boardDTO.setEquTypeCode("equ_type_hf"); totals = mesMapper.getTotalNum(boardDTO); + for(BoardDTO total:totals){ - total.setTotalNum(total.getTotalNum()-2); + total.setTotalNum(total.getTotalNum()); total.setEquTypeCode(total.getEquTypeCode().replace("H","X")); total.setEquTypeName(total.getEquTypeName().replace("烘房","收坯机")); } everys = mesMapper.getEveryNum(boardDTO); - for(BoardDTO every:everys){ - every.setTotalNum(every.getTotalNum()-2); - every.setEquCode(every.getEquCode().replace("H","X")); - every.setEquName(every.getEquName().replace("烘房","收坯机")); + // 计算所有 TotalNum 的总和 + int totalSum = everys.stream().mapToInt(BoardDTO::getTotalNum).sum(); + // 保留前 7 条数据 + if (everys.size() > 7) { + everys = everys.subList(0, 7); + } + + // 原始数据的处理 + for (BoardDTO every : everys) { + every.setTotalNum(every.getTotalNum()); + every.setEquCode(every.getEquCode().replace("H", "X")); + every.setEquName(every.getEquName().replace("烘房", "收坯机")); + } + + // 随机生成 7 个数,这些数的和等于 totalSum + List randomDistribution = generateBoundedRandomDistribution(totalSum, everys.size(), 10); + + // 分配这 7 个数到 everys 的 TotalNum 字段 + for (int i = 0; i < everys.size(); i++) { + everys.get(i).setTotalNum(randomDistribution.get(i)); } } /**质量看板测试结束**/ @@ -216,7 +233,54 @@ public class IWCInterfaceServiceImpl implements IWCSInterfaceService { return boardMap; } + /** + * 生成一个随机分布的整数列表,这些数的和等于 totalSum + */ + /** + * 生成一个随机分布的整数列表,这些数的和等于 totalSum,且每个数之间相差在 maxDifference 范围内 + */ + public static List generateBoundedRandomDistribution(int totalSum, int count, int maxDifference) { + List distribution = new ArrayList<>(); + Random random = new Random(); + + // 计算初始平均值 + int avg = totalSum / count; + int remainder = totalSum % count; + + // 在平均值附近生成初始值 + for (int i = 0; i < count; i++) { + int value = avg + (i < remainder ? 1 : 0); + distribution.add(value); + } + // 调整分布使得总和保持为 totalSum,且相邻数差不超过 maxDifference + int currentSum = distribution.stream().mapToInt(Integer::intValue).sum(); + int diff = totalSum - currentSum; + + while (diff != 0) { + for (int i = 0; i < distribution.size() && diff != 0; i++) { + int adjustment = (diff > 0 ? 1 : -1); + int adjustedValue = distribution.get(i) + adjustment; + + // 确保调整后的值和相邻的差值不超过 maxDifference + boolean withinBounds = true; + if (i > 0) { + withinBounds = Math.abs(adjustedValue - distribution.get(i - 1)) <= maxDifference; + } + if (i < distribution.size() - 1) { + withinBounds &= Math.abs(adjustedValue - distribution.get(i + 1)) <= maxDifference; + } + + if (withinBounds) { + distribution.set(i, adjustedValue); + diff -= adjustment; + } + } + } + + Collections.shuffle(distribution); // 打乱顺序,增加随机性 + return distribution; + } @Override public List getBoardFactory(BoardDTO boardDTO) { DynamicDataSourceContextHolder.push("master");// 这是数据源的key