zhangxin 1 month ago
parent
commit
231f4e0527

+ 2 - 2
prod-line-imes/src/main/java/com/huaxia/imes/controller/MesLineController.java

@@ -89,7 +89,7 @@ public class MesLineController extends BaseController {
      * @return
      * @return
      */
      */
     @GetMapping("/count_day")
     @GetMapping("/count_day")
-    public R<Map<String, Map<String, Long>>> list() {
+    public R< Map<String,Map<String,Map<String,Object>>>> list() {
         return mesLineService.countDay();
         return mesLineService.countDay();
     }
     }
 
 
@@ -122,7 +122,7 @@ public class MesLineController extends BaseController {
      * @return
      * @return
      */
      */
     @GetMapping("/pass_rate")
     @GetMapping("/pass_rate")
-    public R<Map<String,List<Map<String,Map<String,Object>>>>> getPassRate() {
+    public R< Map<String, Map<String, Map<String, Object>>>> getPassRate() {
         return mesLineService.getPassRate();
         return mesLineService.getPassRate();
     }
     }
 
 

+ 71 - 115
prod-line-imes/src/main/java/com/huaxia/imes/service/MesLineService.java

@@ -135,7 +135,7 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
      *
      *
      * @return
      * @return
      */
      */
-    public R<Map<String, Map<String, Long>>> countDay() {
+    public R<Map<String, Map<String, Map<String, Object>>>> countDay() {
 
 
         //获取当前时间
         //获取当前时间
         LocalDate now = LocalDate.now();
         LocalDate now = LocalDate.now();
@@ -147,35 +147,46 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
         wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
         wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
         List<MesLine> boy = mesLineMapper.selectList(wr);
         List<MesLine> boy = mesLineMapper.selectList(wr);
-        Map<String, Map<String, Long>> map = new LinkedHashMap<>();//格式 key:星期几 value:数量
+        //格式 key:产线名称 value:星期几 key:产量 value:数量
+        Map<String, Map<String, Map<String, Object>>> newMap = new LinkedHashMap<>();
         // 初始化Map中的键,并设置默认值为0
         // 初始化Map中的键,并设置默认值为0
         for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
         for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
             String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
             String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
-            map.put(dayOfWeekName, new HashMap<>());
-        }
-        //判断集合
-        if (boy != null && !boy.isEmpty()) {
-            for (MesLine mesLine : boy) {
-                try {
-                    //获取星期几
-                    LocalDate date = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
-                    String week = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
-                    Long count = mesLine.getCurrentQty();//获取产量
-                    String lineName = mesLine.getLineName();//获取产线名称
+            if (boy != null && !boy.isEmpty()) {
+                for (MesLine mesLine : boy) {
+                    String lineName = mesLine.getLineName();
                     if (StringUtils.isBlank(lineName)) {
                     if (StringUtils.isBlank(lineName)) {
                         continue;
                         continue;
                     }
                     }
-                    // 获取内层Map
-                    Map<String, Long> innerMap = map.get(week);
-                    // 更新内层Map中的产量
-                    innerMap.put(lineName, innerMap.getOrDefault(lineName, 0L) + count);
-                } catch (Exception e) {
-                    log.warn("数据转换异常", e);
+                    Map<String, Map<String, Object>> dayMap = newMap.computeIfAbsent(lineName, k -> new LinkedHashMap<>());
+                    Map<String, Object> statsMap = dayMap.computeIfAbsent(dayOfWeekName, k -> new HashMap<String, Object>() {{
+                        put("产量", 0L);
+                    }});
                 }
                 }
             }
             }
         }
         }
+        //遍历集合
+        if (boy != null && !boy.isEmpty()) {
+            for (MesLine mesLine : boy) {
+                // 获取星期几
+                LocalDate date = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
+                String week = date.getDayOfWeek().getDisplayName(TextStyle.FULL, Locale.CHINA);
+                Long count = mesLine.getCurrentQty(); // 获取产量
+                String lineName = mesLine.getLineName(); // 获取产线名称
+                if (StringUtils.isBlank(lineName)) {
+                    continue;
+                }
+                //获取内层
+                Map<String, Map<String, Object>> dayMap = newMap.get(lineName);
+                Map<String, Object> statsMap = dayMap.get(week);
+                // 更新内层Map中的产量
+                Long totalOutPut = (Long) statsMap.getOrDefault("产量", 0L);
+                totalOutPut += count;
+                statsMap.put("产量", totalOutPut);
+            }
 
 
-        return R.ok(map);
+        }
+        return R.ok(newMap);
     }
     }
 
 
 
 
@@ -243,7 +254,7 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
      *
      *
      * @return
      * @return
      */
      */
-    public R<Map<String,Long>> getMonthTotal() {
+    public R<Map<String, Long>> getMonthTotal() {
         //获取当前时间
         //获取当前时间
         LocalDate now = LocalDate.now();
         LocalDate now = LocalDate.now();
         //获取本月的开始时间和结束时间
         //获取本月的开始时间和结束时间
@@ -255,13 +266,13 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         List<MesLine> list = mesLineMapper.selectList(wr);
         List<MesLine> list = mesLineMapper.selectList(wr);
         // 开始累加本月各产线总产量
         // 开始累加本月各产线总产量
         Map<String, Long> totalByLine = new HashMap<>();
         Map<String, Long> totalByLine = new HashMap<>();
-        if (list != null && !list.isEmpty()){
+        if (list != null && !list.isEmpty()) {
             for (MesLine mesLine : list) {
             for (MesLine mesLine : list) {
                 String lineName = mesLine.getLineName();
                 String lineName = mesLine.getLineName();
                 Long currentQty = mesLine.getCurrentQty();
                 Long currentQty = mesLine.getCurrentQty();
-                if (totalByLine.containsKey(lineName)){
+                if (totalByLine.containsKey(lineName)) {
                     totalByLine.put(lineName, totalByLine.get(lineName) + currentQty);
                     totalByLine.put(lineName, totalByLine.get(lineName) + currentQty);
-                }else {
+                } else {
                     totalByLine.put(lineName, currentQty);
                     totalByLine.put(lineName, currentQty);
                 }
                 }
 
 
@@ -275,7 +286,7 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
      *
      *
      * @return
      * @return
      */
      */
-    public R<Map<String, List<Map<String, Map<String, Object>>>>> getPassRate() {
+    public R< Map<String, Map<String, Map<String, Object>>>> getPassRate() {
         //获取当前日期
         //获取当前日期
         LocalDate now = LocalDate.now();
         LocalDate now = LocalDate.now();
         //计算本周的星期一
         //计算本周的星期一
@@ -286,18 +297,30 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
         LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
         wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
         wr.between(MesLine::getCreateTime, mondayOfWeek, sundayOfWeek);
         List<MesLine> boy = mesLineMapper.selectList(wr);
         List<MesLine> boy = mesLineMapper.selectList(wr);
-        //创建Map 用于存储统计结果  星期一:合格率
-        Map<String, Map<String, Object>> weekMap = new LinkedHashMap<>();
-        //存储的数据格式 key:产线名称 value:星期几 value:{产量:0,合格数量:0,不合格数量:0,合格率}
-        Map<String, List<Map<String, Map<String, Object>>>> map = new LinkedHashMap<>();
-
+        //存储的数据格式 key:产线名称  value:星期几  key:产量,value:数量
+        Map<String, Map<String, Map<String, Object>>> map = new LinkedHashMap<>();
 
 
-        //添加星期一-星期日
-       /* for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
+        //初始化每周的数据
+        for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
             String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
             String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
-            weekMap.put(dayOfWeekName, new HashMap<>());
+            if (boy != null && !boy.isEmpty()) {
+                for (MesLine mesLine : boy) {
+                    //获取产线名称
+                    String lineName = mesLine.getLineName();
+                    if (StringUtils.isBlank(lineName)) {
+                        continue;
+                    }
+                    Map<String, Map<String, Object>> dayMap = map.computeIfAbsent(lineName, k -> new LinkedHashMap<>());
+                    dayMap.putIfAbsent(dayOfWeekName, new HashMap<String, Object>() {{
+                        put("产量", 0L);
+                        put("不合格数量", 0L);
+                        put("合格数量", 0L);
+                        put("合格率", "0.00%");
+                    }});
+                }
+            }
         }
         }
-        //遍历集合,统计每条产线的数据
+        // 遍历集合,统计每条产线的数据
         if (boy != null && !boy.isEmpty()) {
         if (boy != null && !boy.isEmpty()) {
             for (MesLine mesLine : boy) {
             for (MesLine mesLine : boy) {
                 // 获取创建日期
                 // 获取创建日期
@@ -305,105 +328,38 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
                 if (createTime.isAfter(mondayOfWeek.minusDays(1)) && createTime.isBefore(sundayOfWeek.plusDays(1))) {
                 if (createTime.isAfter(mondayOfWeek.minusDays(1)) && createTime.isBefore(sundayOfWeek.plusDays(1))) {
                     DayOfWeek dayOfWeek = createTime.getDayOfWeek();
                     DayOfWeek dayOfWeek = createTime.getDayOfWeek();
                     String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
                     String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
-                    Map<String, Object> dayStats = weekMap.get(dayOfWeekName);
-
+                    String lineName = mesLine.getLineName();
+                    if (StringUtils.isBlank(lineName)) {
+                        continue;
+                    }
+                    Map<String, Map<String, Object>> dayMap = map.get(lineName);
+                    Map<String, Object> stats = dayMap.get(dayOfWeekName);
                     // 累加总产量
                     // 累加总产量
-                    Long totalOutput = (Long) dayStats.getOrDefault("产量", 0L);
+                    Long totalOutput = (Long) stats.getOrDefault("产量", 0L);
                     Long count = mesLine.getCurrentQty();//产量+不合格数
                     Long count = mesLine.getCurrentQty();//产量+不合格数
                     Long poorQty = mesLine.getPoorQty();//不合格数量
                     Long poorQty = mesLine.getPoorQty();//不合格数量
                     totalOutput += (count + poorQty);
                     totalOutput += (count + poorQty);
-                    dayStats.put("产量", totalOutput);
+                    stats.put("产量", totalOutput);
 
 
                     // 累加不合格数量
                     // 累加不合格数量
-                    Long totalPoorQty = (Long) dayStats.getOrDefault("不合格数量", 0L);
+                    Long totalPoorQty = (Long) stats.getOrDefault("不合格数量", 0L);
                     totalPoorQty += mesLine.getPoorQty();
                     totalPoorQty += mesLine.getPoorQty();
-                    dayStats.put("不合格数量", totalPoorQty);
+                    stats.put("不合格数量", totalPoorQty);
 
 
                     //计算合格数量
                     //计算合格数量
-                    Long totalQualifiedQty = (Long) dayStats.getOrDefault("合格数量", 0L);
+                    Long totalQualifiedQty = (Long) stats.getOrDefault("合格数量", 0L);
                     totalQualifiedQty += count;
                     totalQualifiedQty += count;
-                    dayStats.put("合格数量", totalQualifiedQty);
+                    stats.put("合格数量", totalQualifiedQty);
 
 
                     // 更新合格率
                     // 更新合格率
                     if (totalOutput > 0) {
                     if (totalOutput > 0) {
                         double qualificationRate = ((double) totalQualifiedQty / (double) totalOutput) * 100;
                         double qualificationRate = ((double) totalQualifiedQty / (double) totalOutput) * 100;
-                        dayStats.put("合格率", String.format(Locale.US, "%.2f%%", qualificationRate));
+                        stats.put("合格率", String.format(Locale.US, "%.2f%%", qualificationRate));
                     } else {
                     } else {
-                        dayStats.put("合格率", "0.00%");
-                    }
-                }
-            }
-
-        }
-        return R.ok(weekMap);*/
-
-
-        //初始化每周的数据
-        for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
-            String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
-            if (boy != null && !boy.isEmpty()) {
-                for (MesLine mesLine : boy) {
-                    //获取产线名称
-                    String lineName = mesLine.getLineName();
-                    if (!map.containsKey(lineName)) {
-                        map.put(lineName, new ArrayList<>());
-                    }
-                    //通过key 获取对应的list
-                    List<Map<String, Map<String, Object>>> mapList = map.get(lineName);
-                    boolean found = false;
-                    for (Map<String, Map<String, Object>> dayData : mapList) {
-                        if (dayData.containsKey(dayOfWeekName)) {
-                            found = true;
-                            break;
-                        }
-                    }
-                    if (!found) {
-                        Map<String, Map<String, Object>> dayData = new HashMap<>();
-                        dayData.put(dayOfWeekName, new HashMap<>());
-                        mapList.add(dayData);
+                        stats.put("合格率", "0.00%");
                     }
                     }
-                }
-            }
-        }
-        // 遍历集合,统计每条产线的数据
-        if (boy != null && !boy.isEmpty()) {
-            for (MesLine mesLine : boy) {
-                // 获取创建日期
-                LocalDate createTime = mesLine.getCreateTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
-                if (createTime.isAfter(mondayOfWeek.minusDays(1)) && createTime.isBefore(sundayOfWeek.plusDays(1))) {
-                    DayOfWeek dayOfWeek = createTime.getDayOfWeek();
-                    String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
-                    String lineName = mesLine.getLineName();
-                    List<Map<String, Map<String, Object>>> lineData = map.get(lineName);
-                    for (Map<String, Map<String, Object>> dayData : lineData) {
-                        if (dayData.containsKey(dayOfWeekName)) {
-                            Map<String, Object> stats = dayData.get(dayOfWeekName);
-                            // 累加总产量
-                            Long totalOutput = (Long) stats.getOrDefault("产量", 0L);
-                            Long count = mesLine.getCurrentQty();//产量+不合格数
-                            Long poorQty = mesLine.getPoorQty();//不合格数量
-                            totalOutput += (count + poorQty);
-                            stats.put("产量", totalOutput);
-
-                            // 累加不合格数量
-                            Long totalPoorQty = (Long) stats.getOrDefault("不合格数量", 0L);
-                            totalPoorQty += mesLine.getPoorQty();
-                            stats.put("不合格数量", totalPoorQty);
 
 
-                            //计算合格数量
-                            Long totalQualifiedQty = (Long) stats.getOrDefault("合格数量", 0L);
-                            totalQualifiedQty += count;
-                            stats.put("合格数量", totalQualifiedQty);
 
 
-                            // 更新合格率
-                            if (totalOutput > 0) {
-                                double qualificationRate = ((double) totalQualifiedQty / (double) totalOutput) * 100;
-                                stats.put("合格率", String.format(Locale.US, "%.2f%%", qualificationRate));
-                            } else {
-                                stats.put("合格率", "0.00%");
-                            }
-                        }
-                    }
                 }
                 }
             }
             }
         }
         }