From 9230c17bf226582a0d1637b92b6504e20e8c6af5 Mon Sep 17 00:00:00 2001
From: fuliqi <fuliqi@qq.com>
Date: 星期二, 23 七月 2024 16:47:52 +0800
Subject: [PATCH] 人脸阈值检测,检测代码优化

---
 ycl-server/src/main/java/com/ycl/platform/service/impl/YwThresholdServiceImpl.java |  182 ++++++++++++++++++++++++---------------------
 1 files changed, 98 insertions(+), 84 deletions(-)

diff --git a/ycl-server/src/main/java/com/ycl/platform/service/impl/YwThresholdServiceImpl.java b/ycl-server/src/main/java/com/ycl/platform/service/impl/YwThresholdServiceImpl.java
index 549e453..78339cd 100644
--- a/ycl-server/src/main/java/com/ycl/platform/service/impl/YwThresholdServiceImpl.java
+++ b/ycl-server/src/main/java/com/ycl/platform/service/impl/YwThresholdServiceImpl.java
@@ -2,23 +2,21 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
-import com.ycl.platform.domain.entity.WorkOrder;
 import com.ycl.platform.domain.entity.YwThreshold;
+import com.ycl.platform.domain.result.HK.FaceDeviceInspectionResult;
 import com.ycl.platform.domain.result.HK.VehicleDeviceInspectionResult;
-import com.ycl.platform.mapper.WorkOrderMapper;
 import com.ycl.platform.mapper.YwThresholdMapper;
 import com.ycl.platform.service.IYwThresholdService;
-import constant.CompareConstant;
 import constant.YwThreadConstants;
+import enumeration.CompareType;
 import enumeration.general.BusinessTypeEnum;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import utils.DateUtils;
 
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.function.Function;
 import java.util.stream.Collectors;
 
 /**
@@ -28,6 +26,7 @@
  * @date 2024-07-19
  */
 @Service
+@Slf4j
 public class YwThresholdServiceImpl extends ServiceImpl<YwThresholdMapper, YwThreshold> implements IYwThresholdService {
     @Autowired
     private YwThresholdMapper ywThresholdMapper;
@@ -104,13 +103,48 @@
 
 
     @Override
-    public void faceCheck(List list) {
+    public void videoCheck(List list) {
 
     }
 
+    /**
+     * 鍒ゆ柇浜鸿劯闃堝�兼槸鍚︽弧瓒充笅鍙戞潯浠�
+     *
+     * @param list
+     */
     @Override
-    public void videoCheck(List list) {
-
+    public void faceCheck(List<FaceDeviceInspectionResult> list) {
+        Map<String, YwThreshold> thresholdMap = getYwThresholdMap(BusinessTypeEnum.FACE.name());
+        //鍑嗗涓嬪彂宸ュ崟闆嗗悎
+        List<FaceDeviceInspectionResult> distributeList = new ArrayList<>();
+        //鍑嗗鑷姩鐢熸垚宸ュ崟闆嗗悎
+        List<FaceDeviceInspectionResult> workOrderList = new ArrayList<>();
+        //澶勭悊鎺ュ彛鏁版嵁
+        for (FaceDeviceInspectionResult result : list) {
+            if (result == null) {
+                log.error("浜鸿劯瀵硅薄鏁版嵁涓虹┖");
+                continue;
+            }
+            //妫�鏌ユ椂閽熷噯纭巼
+            Float clockPercent = result.getSnapClock().getClockPercent();
+            check(YwThreadConstants.Face_ClockPercent, clockPercent, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
+            //妫�鏌ユ暟鎹強鏃剁巼
+            Float timelyPercent = result.getSnapTimely().getTimelyPercent();
+            check(YwThreadConstants.Face_TimelyPercent, timelyPercent, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
+            //妫�鏌ユ寔缁棤鏁版嵁澶╂暟
+            Integer continueNoDataCount = result.getContinueNoDataCount();
+            check(YwThreadConstants.Face_ContinueNoDataCount, continueNoDataCount, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
+            //妫�鏌ヤ笉鍞竴鏁版嵁閲�
+            Integer nouniqueCount = result.getSnapUnique().getNouniqueCount();
+            check(YwThreadConstants.Face_NouniqueCount, nouniqueCount, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
+            //妫�鏌ヤ汉鑴镐綆璇勫垎鐜�
+            Float lowScorePercent = result.getSnapValidity().getLowScorePercent();
+            check(YwThreadConstants.Face_LowScorePercent, lowScorePercent, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
+            //妫�鏌ュ缓妯″け璐ョ巼
+            Float failPercent = result.getSnapValidity().getFailPercent();
+            check(YwThreadConstants.Face_FailPercent, failPercent, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
+        }
+        //TODO锛氬緟澶勭悊闆嗗悎娣诲姞宸ュ崟
     }
 
     /**
@@ -120,33 +154,11 @@
      */
     @Override
     public void carCheck(List<VehicleDeviceInspectionResult> list) {
-        QueryWrapper<YwThreshold> wrapper = new QueryWrapper<>();
-        wrapper.eq("monitor_type", BusinessTypeEnum.CAR.name());
-        List<YwThreshold> ywThresholds = ywThresholdMapper.selectList(wrapper);
-        Map<String, YwThreshold> map = new HashMap<>();
-        for (YwThreshold ywThreshold : ywThresholds) {
-            String key = ywThreshold.getKey();
-            map.put(key, ywThreshold);
-        }
-        //鏃堕挓鍑嗙‘鐜囬槇鍊�
-        Float clockPercent = Float.valueOf(map.get(YwThreadConstants.Car_ClockPercent).getValue());
-        Float clockPercentAuto = Float.valueOf(map.get(YwThreadConstants.Car_ClockPercent).getValueAuto());
-        //鏁版嵁鍙婃椂鐜囬槇鍊�
-        Float timelyPercent = Float.valueOf(map.get(YwThreadConstants.Car_TimelyPercent).getValue());
-        Float timelyPercentAuto = Float.valueOf(map.get(YwThreadConstants.Car_TimelyPercent).getValueAuto());
-        //鎸佺画鏃犳暟鎹ぉ鏁伴槇鍊�
-        Integer continueNoDataCount = Integer.valueOf(map.get(YwThreadConstants.Car_ContinueNoDataCount).getValue());
-        Integer continueNoDataCountAuto = Integer.valueOf(map.get(YwThreadConstants.Car_ContinueNoDataCount).getValueAuto());
-        //涓嶅敮涓�鏁版嵁閲忛槇鍊�
-        Integer nouniqueCount = Integer.valueOf(map.get(YwThreadConstants.Car_NouniqueCount).getValue());
-        Integer nouniqueCountAuto = Integer.valueOf(map.get(YwThreadConstants.Car_NouniqueCount).getValueAuto());
-        //鐧藉ぉ鏈瘑鍒噺闃堝��
-        Integer dayNoNumberCount = Integer.valueOf(map.get(YwThreadConstants.Car_DayNoNumberCount).getValue());
-        Integer dayNoNumberCountAuto = Integer.valueOf(map.get(YwThreadConstants.Car_DayNoNumberCount).getValueAuto());
-        //杞﹁締鍏」灞炴�т笉瀹屾暣閲忛槇鍊�
-        Integer noIntegrityCount = Integer.valueOf(map.get(YwThreadConstants.Car_NoIntegrityCount).getValue());
-        Integer noIntegrityCountAuto = Integer.valueOf(map.get(YwThreadConstants.Car_NoIntegrityCount).getValueAuto());
-
+        Map<String, YwThreshold> thresholdMap = getYwThresholdMap(BusinessTypeEnum.CAR.name());
+        //鍑嗗涓嬪彂宸ュ崟闆嗗悎
+        List<VehicleDeviceInspectionResult> distributeList = new ArrayList<>();
+        //鍑嗗鑷姩鐢熸垚宸ュ崟闆嗗悎
+        List<VehicleDeviceInspectionResult> workOrderList = new ArrayList<>();
         //澶勭悊鎺ュ彛鏁版嵁
         for (VehicleDeviceInspectionResult result : list) {
             if (result == null) {
@@ -154,68 +166,70 @@
                 continue;
             }
             //妫�鏌ユ寔缁棤鏁版嵁澶╂暟
-            Integer continueNoDataCountResult = result.getContinueNoDataCount();
-            check(continueNoDataCount, continueNoDataCountAuto, result, continueNoDataCountResult, "杞﹁締鎸佺画鏃犳暟鎹ぉ鏁颁负绌�", CompareConstant.MoreThanEq);
+            Integer continueNoDataCount = result.getContinueNoDataCount();
+            check(YwThreadConstants.Car_ContinueNoDataCount, continueNoDataCount, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
             //妫�鏌ユ椂閽熷噯纭巼
-            Float clockPercentResult = result.getSnapClock().getClockPercent();
-            check(clockPercent, clockPercentAuto, result, clockPercentResult, "杞﹁締鏃堕挓鍑嗙‘鐜囦负绌�", CompareConstant.LessThanEq);
+            Float clockPercent = result.getSnapClock().getClockPercent();
+            check(YwThreadConstants.Car_ClockPercent, clockPercent, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
             //妫�鏌ユ暟鎹強鏃剁巼
             Float timelyPercentResult = result.getSnapTimely().getTimelyPercent();
-            check(timelyPercent, timelyPercentAuto, result, timelyPercentResult, "杞﹁締鏁版嵁鍙婃椂鐜囦负绌�", CompareConstant.LessThanEq);
+            check(YwThreadConstants.Car_TimelyPercent, timelyPercentResult, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
             //妫�鏌ヤ笉鍞竴鏁版嵁閲�
             Integer nouniqueCountResult = result.getSnapUnique().getNouniqueCount();
-            check(nouniqueCount, nouniqueCountAuto, result, nouniqueCountResult, "杞﹁締涓嶅敮涓�鏁版嵁閲忎负绌�", CompareConstant.MoreThanEq);
+            check(YwThreadConstants.Car_NouniqueCount, nouniqueCountResult, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
             //妫�鏌ョ櫧澶╂湭璇嗗埆閲�
             Integer dayNoNumberCountResult = result.getSnapPlate().getDayNoNumberCount();
-            check(dayNoNumberCount, dayNoNumberCountAuto, result, dayNoNumberCountResult, "杞﹁締鐧藉ぉ鏈瘑鍒噺涓虹┖", CompareConstant.MoreThanEq);
+            check(YwThreadConstants.Car_DayNoNumberCount, dayNoNumberCountResult, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
             //杞﹁締鍏」灞炴�т笉瀹屾暣閲�
             Integer noIntegrityCountResult = result.getIntegrity().getNoIntegrityCount();
-            check(noIntegrityCount, noIntegrityCountAuto, result, noIntegrityCountResult, "杞﹁締鍏」灞炴�т笉瀹屾暣閲忎负绌�", CompareConstant.MoreThanEq);
+            check(YwThreadConstants.Car_NoIntegrityCount, noIntegrityCountResult, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
         }
+        //TODO锛氬緟澶勭悊闆嗗悎娣诲姞宸ュ崟
+    }
+
+    //灏佽闃堝�间负map
+    private Map<String, YwThreshold> getYwThresholdMap(String name) {
+        return ywThresholdMapper.selectList(
+                new QueryWrapper<YwThreshold>().eq("monitor_type", name)
+        ).stream().collect(Collectors.toMap(
+                YwThreshold::getKey,
+                Function.identity()
+        ));
+    }
+
+    //妫�鏌ラ槇鍊�
+    private <T extends Comparable<T>> void check(String key, T value, Object result, Map<String, YwThreshold> thresholds, List distributeList, List workOrderList, CompareType compareType) {
+        Optional.ofNullable(value).ifPresentOrElse(
+                v -> {
+                    YwThreshold ywThreshold = thresholds.get(key);
+                    //杞崲绫诲瀷
+                    T thresholdValue = parseThreshold(ywThreshold.getValueAuto(), value.getClass());
+                    T thresholdAutoValue = parseThreshold(ywThreshold.getValue(), value.getClass());
+                    //姣旇緝澶у皬锛屽姞鍏ュ埌瀵瑰簲寰呭鐞嗛泦鍚�
+                    if (compareType.compare(v, thresholdAutoValue)) {
+                        workOrderList.add(result);
+                    } else if (compareType.compare(v, thresholdValue)) {
+                        distributeList.add(result);
+                    }
+                },
+                () -> log.error("{} 涓虹┖: {}", thresholds.get(key).getName(), result)
+        );
+
 
     }
 
-    private void check(Integer threshold, Integer thresholdAuto, VehicleDeviceInspectionResult result, Integer thresholdResult, String message, String command) {
-        if (thresholdResult != null) {
-            //澶т簬绫诲瀷
-            if (CompareConstant.MoreThanEq.equals(command)) {
-                if (thresholdResult >= thresholdAuto) {
-                    //TODO锛氳嚜鍔ㄤ笅鍙戝伐鍗�
-                } else if (thresholdResult >= threshold) {
-                    //TODO锛氳繘鍏ヤ笅鍙戝伐鍗曢〉闈�
-                }
-            } else if (CompareConstant.LessThanEq.equals(command)) {
-                //灏忎簬绫诲瀷
-                if (thresholdResult <= thresholdAuto) {
-                    //TODO锛氳嚜鍔ㄤ笅鍙戝伐鍗�
-                } else if (thresholdResult <= threshold) {
-                    //TODO锛氳繘鍏ヤ笅鍙戝伐鍗曢〉闈�
-                }
-            }
+    private <T extends Comparable<T>> T parseThreshold(String thresholdStr, Class<?> type) {
+        if (Integer.class.equals(type)) {
+            return (T) Integer.valueOf(thresholdStr);
+        } else if (Float.class.equals(type)) {
+            return (T) Float.valueOf(thresholdStr);
+        } else if (Double.class.equals(type)) {
+            return (T) Double.valueOf(thresholdStr);
+        } else if (Long.class.equals(type)) {
+            return (T) Long.valueOf(thresholdStr);
         } else {
-            log.error(message + result);
+            throw new IllegalArgumentException("涓嶆敮鎸佽浆鎹㈢被鍨�" + type);
         }
     }
 
-    private void check(Float threshold, Float thresholdAuto, VehicleDeviceInspectionResult result, Float thresholdResult, String message, String command) {
-        if (thresholdResult != null) {
-            //澶т簬绫诲瀷
-            if (CompareConstant.MoreThanEq.equals(command)) {
-                if (thresholdResult >= thresholdAuto) {
-                    //TODO锛氳嚜鍔ㄤ笅鍙戝伐鍗�
-                } else if (thresholdResult >= threshold) {
-                    //TODO锛氳繘鍏ヤ笅鍙戝伐鍗曢〉闈�
-                }
-            } else if (CompareConstant.LessThanEq.equals(command)) {
-                //灏忎簬绫诲瀷
-                if (thresholdResult <= thresholdAuto) {
-                    //TODO锛氳嚜鍔ㄤ笅鍙戝伐鍗�
-                } else if (thresholdResult <= threshold) {
-                    //TODO锛氳繘鍏ヤ笅鍙戝伐鍗曢〉闈�
-                }
-            }
-        } else {
-            log.error(message + result);
-        }
-    }
 }

--
Gitblit v1.8.0