龚焕茏
2024-08-02 5e859a25ddf7d61753e5849b3c6ef36cb0cbf68c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.ycl.platform.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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.YwThresholdMapper;
import com.ycl.platform.service.IYwThresholdService;
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.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * 运维阈值Service业务层处理
 *
 * @author gonghl
 * @date 2024-07-19
 */
@Service
@Slf4j
public class YwThresholdServiceImpl extends ServiceImpl<YwThresholdMapper, YwThreshold> implements IYwThresholdService {
    @Autowired
    private YwThresholdMapper ywThresholdMapper;
 
    /**
     * 查询运维阈值
     *
     * @param id 运维阈值主键
     * @return 运维阈值
     */
    @Override
    public YwThreshold selectYwThresholdById(Long id) {
        return ywThresholdMapper.selectYwThresholdById(id);
    }
 
    /**
     * 查询运维阈值列表
     *
     * @param ywThreshold 运维阈值
     * @return 运维阈值
     */
    @Override
    public Map<String, List<YwThreshold>> selectYwThresholdList(YwThreshold ywThreshold) {
        List<YwThreshold> ywThresholds = ywThresholdMapper.selectYwThresholdList(ywThreshold);
        Map<String, List<YwThreshold>> map = ywThresholds.stream().collect(Collectors.groupingBy(YwThreshold::getMonitorType));
        return map;
    }
 
    /**
     * 新增运维阈值
     *
     * @param ywThreshold 运维阈值
     * @return 结果
     */
    @Override
    public int insertYwThreshold(YwThreshold ywThreshold) {
        ywThreshold.setCreateTime(DateUtils.getNowDate());
        return ywThresholdMapper.insertYwThreshold(ywThreshold);
    }
 
    /**
     * 修改运维阈值
     *
     * @param list 运维阈值
     * @return 结果
     */
    @Override
    public Boolean updateYwThreshold(List<YwThreshold> list) {
        list.forEach(item -> item.setCreateTime(new Date()));
        return updateBatchById(list);
    }
 
    /**
     * 批量删除运维阈值
     *
     * @param ids 需要删除的运维阈值主键
     * @return 结果
     */
    @Override
    public int deleteYwThresholdByIds(Long[] ids) {
        return ywThresholdMapper.deleteYwThresholdByIds(ids);
    }
 
    /**
     * 删除运维阈值信息
     *
     * @param id 运维阈值主键
     * @return 结果
     */
    @Override
    public int deleteYwThresholdById(Long id) {
        return ywThresholdMapper.deleteYwThresholdById(id);
    }
 
    /**
     * 判断视频阈值是否满足下发条件
     *
     * @param list
     */
    @Override
    public void videoCheck(List list) {
 
    }
 
    /**
     * 判断人脸阈值是否满足下发条件
     *
     * @param list
     */
    @Override
    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:待处理集合添加工单
    }
 
    /**
     * 判断车辆阈值是否满足下发条件
     *
     * @param list
     */
    @Override
    public void carCheck(List<VehicleDeviceInspectionResult> list) {
        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) {
                log.error("车辆对象数据为空");
                continue;
            }
            //检查持续无数据天数
            Integer continueNoDataCount = result.getContinueNoDataCount();
            check(YwThreadConstants.Car_ContinueNoDataCount, continueNoDataCount, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
            //检查时钟准确率
            Float clockPercent = result.getSnapClock().getClockPercent();
            check(YwThreadConstants.Car_ClockPercent, clockPercent, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
            //检查数据及时率
            Float timelyPercentResult = result.getSnapTimely().getTimelyPercent();
            check(YwThreadConstants.Car_TimelyPercent, timelyPercentResult, result, thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ);
            //检查不唯一数据量
            Integer nouniqueCountResult = result.getSnapUnique().getNouniqueCount();
            check(YwThreadConstants.Car_NouniqueCount, nouniqueCountResult, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
            //检查白天未识别量
            Integer dayNoNumberCountResult = result.getSnapPlate().getDayNoNumberCount();
            check(YwThreadConstants.Car_DayNoNumberCount, dayNoNumberCountResult, result, thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ);
            //车辆六项属性不完整量
            Integer noIntegrityCountResult = result.getIntegrity().getNoIntegrityCount();
            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 <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 {
            throw new IllegalArgumentException("不支持转换类型" + type);
        }
    }
 
}