zhangxin 1 месяц назад
Родитель
Сommit
198667b4e8

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

@@ -111,7 +111,7 @@ public class MesLineController extends BaseController {
      * @return
      */
     @GetMapping("/month_total")
-    public R<Long> getMonthTotal() {
+    public R<Map<String,Long>> getMonthTotal() {
         return mesLineService.getMonthTotal();
     }
 
@@ -122,13 +122,13 @@ public class MesLineController extends BaseController {
      * @return
      */
     @GetMapping("/pass_rate")
-    public R<Map<String, Map<String, Object>>> getPassRate() {
+    public R<Map<String,List<Map<String,Map<String,Object>>>>> getPassRate() {
         return mesLineService.getPassRate();
     }
 
 
     /**
-     * 获取人均产出(星期1-7)
+     * 获取人均产出(按月)
      *
      * @return
      */

+ 122 - 14
prod-line-imes/src/main/java/com/huaxia/imes/service/MesLineService.java

@@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import org.springframework.util.Assert;
+
 import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.time.*;
@@ -71,6 +72,9 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         //判断创建时间是否为空
         if (boy.getCreateTime() == null) {
             boy.setCreateTime(new Date());
+        } else {
+            //如果创建时间不为空,则判断时间是否在今天之后 如果是则提醒 创建时间只能小于等于今天
+            Assert.isTrue(boy.getCreateTime().after(new Date()), "500-创建时间不能大于今天");
         }
         //通过产线名称和创建如期去查询产线数据是否存在
         LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
@@ -91,6 +95,11 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
      * @param boy
      */
     public void edit(MesLine boy) {
+        //检验数据
+        this.check(boy);
+        //判断一下时间
+        Assert.isTrue(boy.getCreateTime() != null, "500-时间不能为空");
+        Assert.isTrue(boy.getCreateTime().after(new Date()), "500-创建时间不能大于今天");
         boy.setUpdateTime(new Date())
                 .setUpdateBy(SecurityUtils.getUsername());
         this.updateById(boy);
@@ -234,7 +243,7 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
      *
      * @return
      */
-    public R<Long> getMonthTotal() {
+    public R<Map<String,Long>> getMonthTotal() {
         //获取当前时间
         LocalDate now = LocalDate.now();
         //获取本月的开始时间和结束时间
@@ -244,14 +253,21 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         LambdaQueryWrapper<MesLine> wr = new LambdaQueryWrapper<>();
         wr.between(MesLine::getCreateTime, startOfMonth, endOfMonth);
         List<MesLine> list = mesLineMapper.selectList(wr);
-        //开始累加本月总产量
-        Long total = 0L;
-        if (list != null && !list.isEmpty()) {
+        // 开始累加本月各产线总产量
+        Map<String, Long> totalByLine = new HashMap<>();
+        if (list != null && !list.isEmpty()){
             for (MesLine mesLine : list) {
-                total += mesLine.getCurrentQty();
+                String lineName = mesLine.getLineName();
+                Long currentQty = mesLine.getCurrentQty();
+                if (totalByLine.containsKey(lineName)){
+                    totalByLine.put(lineName, totalByLine.get(lineName) + currentQty);
+                }else {
+                    totalByLine.put(lineName, currentQty);
+                }
+
             }
         }
-        return R.ok(total);
+        return R.ok(totalByLine);
     }
 
     /**
@@ -259,7 +275,7 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
      *
      * @return
      */
-    public R<Map<String, Map<String, Object>>> getPassRate() {
+    public R<Map<String, List<Map<String, Map<String, Object>>>>> getPassRate() {
         //获取当前日期
         LocalDate now = LocalDate.now();
         //计算本周的星期一
@@ -272,8 +288,12 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         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<>();
+
+
         //添加星期一-星期日
-        for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
+       /* for (DayOfWeek dayOfWeek : DayOfWeek.values()) {
             String dayOfWeekName = dayOfWeek.getDisplayName(TextStyle.FULL, Locale.CHINA);
             weekMap.put(dayOfWeekName, new HashMap<>());
         }
@@ -315,12 +335,85 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
             }
 
         }
-        return R.ok(weekMap);
+        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);
+                    }
+                }
+            }
+        }
+        // 遍历集合,统计每条产线的数据
+        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%");
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
+        return R.ok(map);
     }
 
 
     /**
-     * 获取人均产出(星期1-7)
+     * 获取人均产出(按月)
      *
      * @return
      */
@@ -337,6 +430,23 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
         List<MesLine> boy = mesLineMapper.selectList(wr);
         //创建Map 用于存储统计结果 日期:产线 每日产量 每日工作人数 人均产出
         Map<String, Map<String, Map<String, Object>>> weekMap = new LinkedHashMap<>();
+        //初始化 每月1号到当前时间
+        LocalDate currentDate = firstDayOfMonth;
+        while (currentDate.isBefore(now.plusDays(1))) {
+            String dateString = currentDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+            if (boy != null && !boy.isEmpty()) {
+                for (MesLine mesLine : boy) {
+                    String lineName = mesLine.getLineName();
+                    Map<String, Map<String, Object>> dayStats = weekMap.computeIfAbsent(lineName, k -> new TreeMap<>(Comparator.naturalOrder()));
+                    dayStats.putIfAbsent(dateString, new HashMap<String, Object>() {{
+                        put("产量", 0L);
+                        put("每日工作人数", 0);
+                        put("人均产出", BigDecimal.ZERO);
+                    }});
+                }
+                currentDate = currentDate.plusDays(1);
+            }
+        }
         //遍历集合 获取某条线产量
         if (boy != null && !boy.isEmpty()) {
             for (MesLine mesLine : boy) {
@@ -347,8 +457,8 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
                 //序列化日期格式 年月日
                 String dateString = createTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                 //初始化数据
-                Map<String, Map<String, Object>> dayStats = weekMap.computeIfAbsent(lineName, k -> new HashMap<>());
-                Map<String, Object> lineStats = dayStats.computeIfAbsent(dateString, k -> new HashMap<>());
+                Map<String, Map<String, Object>> dayStats = weekMap.get(lineName);
+                Map<String, Object> lineStats = dayStats.get(dateString);
                 //累加产量
                 Long totalOutPut = (Long) lineStats.getOrDefault("产量", 0L);
                 Long count = mesLine.getCurrentQty();//产量
@@ -378,8 +488,6 @@ public class MesLineService extends ServiceImpl<MesLineMapper, MesLine> implemen
             }
 
         }
-
-
         return R.ok(weekMap);
     }
 }