xiangpei
2024-08-09 39676fc53ae0fce69aadcd5125accc1c67d463a5
ycl-server/src/main/java/com/ycl/platform/service/impl/YwThresholdServiceImpl.java
@@ -1,36 +1,41 @@
package com.ycl.platform.service.impl;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycl.platform.domain.dto.CarDTO;
import com.ycl.platform.domain.dto.FaceDTO;
import com.ycl.platform.domain.dto.VideoDTO;
import com.ycl.platform.domain.entity.WorkOrder;
import com.ycl.platform.domain.entity.YwThreshold;
import com.ycl.platform.domain.form.CarThresholdForm;
import com.ycl.platform.domain.form.FaceThresholdForm;
import com.ycl.platform.domain.form.VideoThresholdForm;
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 com.ycl.utils.DateUtils;
import com.ycl.platform.service.WorkOrderService;
import constant.YwThreadConstants;
import enumeration.CompareType;
import enumeration.general.BusinessTypeEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import enumeration.general.WorkOrderStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.DateUtils;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
 * 运维阈值Service业务层处理
 *
 * @author gonghl
 * @date 2024-03-25
 * @date 2024-07-19
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class YwThresholdServiceImpl extends ServiceImpl<YwThresholdMapper, YwThreshold> implements IYwThresholdService {
    @Autowired
    private YwThresholdMapper ywThresholdMapper;
    private final YwThresholdMapper ywThresholdMapper;
    @Autowired
    private WorkOrderService workOrderService;
    /**
     * 查询运维阈值
@@ -39,7 +44,7 @@
     * @return 运维阈值
     */
    @Override
    public YwThreshold selectYwThresholdById(Integer id) {
    public YwThreshold selectYwThresholdById(Long id) {
        return ywThresholdMapper.selectYwThresholdById(id);
    }
@@ -50,8 +55,10 @@
     * @return 运维阈值
     */
    @Override
    public List<YwThreshold> selectYwThresholdList(YwThreshold ywThreshold) {
        return ywThresholdMapper.selectYwThresholdList(ywThreshold);
    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;
    }
    /**
@@ -69,13 +76,13 @@
    /**
     * 修改运维阈值
     *
     * @param ywThreshold 运维阈值
     * @param list 运维阈值
     * @return 结果
     */
    @Override
    public int updateYwThreshold(YwThreshold ywThreshold) {
        ywThreshold.setUpdateTime(DateUtils.getNowDate());
        return ywThresholdMapper.updateYwThreshold(ywThreshold);
    public Boolean updateYwThreshold(List<YwThreshold> list) {
        list.forEach(item -> item.setCreateTime(new Date()));
        return updateBatchById(list);
    }
    /**
@@ -85,7 +92,7 @@
     * @return 结果
     */
    @Override
    public int deleteYwThresholdByIds(Integer[] ids) {
    public int deleteYwThresholdByIds(Long[] ids) {
        return ywThresholdMapper.deleteYwThresholdByIds(ids);
    }
@@ -96,105 +103,172 @@
     * @return 结果
     */
    @Override
    public int deleteYwThresholdById(Integer id) {
    public int deleteYwThresholdById(Long id) {
        return ywThresholdMapper.deleteYwThresholdById(id);
    }
    /**
     * 判断视频阈值是否满足下发条件
     *
     * @param list
     */
    @Override
    public int editFace(FaceThresholdForm form) {
        YwThreshold ywThreshold = baseMapper.selectById(form.getId());
        if (Objects.isNull(ywThreshold)) {
            throw new RuntimeException("数据不存在");
    public void videoCheck(List list) {
    }
    /**
     * 判断人脸阈值是否满足下发条件
     *
     * @param list
     */
    @Override
    public void faceCheck(List<FaceDeviceInspectionResult> list) {
        Map<String, YwThreshold> thresholdMap = getYwThresholdMap(BusinessTypeEnum.FACE.name());
        //准备下发工单集合
        List<WorkOrder> distributeList = new ArrayList<>();
        //准备自动生成工单集合
        List<WorkOrder> workOrderList = new ArrayList<>();
        //处理接口数据
        for (FaceDeviceInspectionResult result : list) {
            if (result == null) {
                log.error("人脸对象数据为空");
                continue;
            }
            //检查时钟准确率
            Float clockPercent = result.getSnapClock().getClockPercent();
            check(YwThreadConstants.Face_ClockPercent, clockPercent, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ,"");
            //检查数据及时率
            Float timelyPercent = result.getSnapTimely().getTimelyPercent();
            check(YwThreadConstants.Face_TimelyPercent, timelyPercent, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ,"");
            //检查持续无数据天数
            Integer continueNoDataCount = result.getContinueNoDataCount();
            check(YwThreadConstants.Face_ContinueNoDataCount, continueNoDataCount, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ,"");
            //检查不唯一数据量
            Integer nouniqueCount = result.getSnapUnique().getNouniqueCount();
            check(YwThreadConstants.Face_NouniqueCount, nouniqueCount, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ,"");
            //检查人脸低评分率
            Float lowScorePercent = result.getSnapValidity().getLowScorePercent();
            check(YwThreadConstants.Face_LowScorePercent, lowScorePercent, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ,"");
            //检查建模失败率
            Float failPercent = result.getSnapValidity().getFailPercent();
            check(YwThreadConstants.Face_FailPercent, failPercent, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ,"");
        }
        BeanUtils.copyProperties(form, ywThreshold);
//        ywThreshold.setIndicator(JSON.toJSONString(form));
        return baseMapper.updateById(ywThreshold);
        /** 添加工单 */
        workOrderService.innerAddWorkOrder(workOrderList);
        workOrderService.innerAddWorkOrder(distributeList);
    }
    /**
     * 判断车辆阈值是否满足下发条件
     *
     * @param list
     */
    @Override
    public int editCar(CarThresholdForm form) {
        YwThreshold ywThreshold = baseMapper.selectById(form.getId());
        if (Objects.isNull(ywThreshold)) {
            throw new RuntimeException("数据不存在");
    public void carCheck(List<VehicleDeviceInspectionResult> list) {
        Map<String, YwThreshold> thresholdMap = getYwThresholdMap(BusinessTypeEnum.CAR.name());
        //准备下发工单集合
        List<WorkOrder> distributeList = new ArrayList<>();
        //准备自动生成工单集合
        List<WorkOrder> workOrderList = new ArrayList<>();
        //处理接口数据
        for (VehicleDeviceInspectionResult result : list) {
            if (result == null) {
                log.error("车辆对象数据为空");
                continue;
            }
            //检查持续无数据天数
            Integer continueNoDataCount = result.getContinueNoDataCount();
            check(YwThreadConstants.Car_ContinueNoDataCount, continueNoDataCount, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ,"");
            //检查时钟准确率
            Float clockPercent = result.getSnapClock().getClockPercent();
            check(YwThreadConstants.Car_ClockPercent, clockPercent, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ,"");
            //检查数据及时率
            Float timelyPercentResult = result.getSnapTimely().getTimelyPercent();
            check(YwThreadConstants.Car_TimelyPercent, timelyPercentResult, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.LESS_THAN_EQ,"");
            //检查不唯一数据量
            Integer nouniqueCountResult = result.getSnapUnique().getNouniqueCount();
            check(YwThreadConstants.Car_NouniqueCount, nouniqueCountResult, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ,"");
            //检查白天未识别量
            Integer dayNoNumberCountResult = result.getSnapPlate().getDayNoNumberCount();
            check(YwThreadConstants.Car_DayNoNumberCount, dayNoNumberCountResult, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ,"");
            //车辆六项属性不完整量
            Integer noIntegrityCountResult = result.getIntegrity().getNoIntegrityCount();
            check(YwThreadConstants.Car_NoIntegrityCount, noIntegrityCountResult, result.getExternalIndexCode(), thresholdMap, distributeList, workOrderList, CompareType.MORE_THAN_EQ,"");
        }
        BeanUtils.copyProperties(form, ywThreshold);
//        ywThreshold.setIndicator(JSON.toJSONString(form));
        return baseMapper.updateById(ywThreshold);
        /** 添加工单 */
        workOrderService.innerAddWorkOrder(workOrderList);
        workOrderService.innerAddWorkOrder(distributeList);
    }
    @Override
    public int editVideo(VideoThresholdForm form) {
        YwThreshold ywThreshold = baseMapper.selectById(form.getId());
        if (Objects.isNull(ywThreshold)) {
            throw new RuntimeException("数据不存在");
    /**
     * 把阈值条件查出来,转成map
     *
     * @param name
     * @return
     */
    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()
        ));
    }
    /**
     * 检查阈值
     *
     * @param key 某阈值标识
     * @param value 接口获取到的值
     * @param serialNumber 国标码
     * @param thresholds 阈值条件map
     * @param distributeList 要下发的工单容器
     * @param workOrderList 待下发的工单容器
     * @param compareType 比较方式:>=    <=
     * @param errorType 故障类型
     * @param <T>
     */
    private <T extends Comparable<T>> void check(String key, T value, String serialNumber, Map<String, YwThreshold> thresholds, List distributeList, List workOrderList, CompareType compareType,String errorType) {
        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)) {
                        //自动下发工单
                        WorkOrder workOrder = new WorkOrder();
                        workOrder.setSerialNumber(serialNumber);
                        workOrder.setStatus(WorkOrderStatusEnum.WAIT_DISTRIBUTE);
                        workOrder.setErrorType(errorType);
                        workOrderList.add(workOrder);
                    } else if (compareType.compare(v, thresholdValue)) {
                        //进入工单代下发
                        WorkOrder workOrder = new WorkOrder();
                        workOrder.setSerialNumber(serialNumber);
                        workOrder.setStatus(WorkOrderStatusEnum.DISTRIBUTED);
                        workOrder.setErrorType(errorType);
                        distributeList.add(workOrder);
                    }
                },
                () -> log.error("{} 为空: {}", thresholds.get(key).getName(), serialNumber)
        );
    }
    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);
        }
        BeanUtils.copyProperties(form, ywThreshold);
//        ywThreshold.setIndicator(JSON.toJSONString(form));
        return baseMapper.updateById(ywThreshold);
    }
    @Override
    public Boolean checkFace(List<FaceDTO> faceList) {
        YwThreshold faceEntity = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(YwThreshold::getMonitorType, BusinessTypeEnum.FACE)
                .one();
        if (Objects.isNull(faceEntity)) {
            throw new RuntimeException("请配置人脸的工单阈值");
        }
        // todo 遍历条件,生成工单
        return Boolean.FALSE;
    }
    @Override
    public Boolean checkCar(List<CarDTO> faceList) {
        YwThreshold carEntity = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(YwThreshold::getMonitorType, BusinessTypeEnum.CAR)
                .one();
        if (Objects.isNull(carEntity)) {
            throw new RuntimeException("请配置车辆的工单阈值");
        }
        // todo 遍历条件,生成工单
        return Boolean.FALSE;
    }
    @Override
    public Boolean checkVideo(List<VideoDTO> faceList) {
        YwThreshold videoEntity = new LambdaQueryChainWrapper<>(baseMapper)
                .eq(YwThreshold::getMonitorType, BusinessTypeEnum.VIDEO)
                .one();
        if (Objects.isNull(videoEntity)) {
            throw new RuntimeException("请配置视频的工单阈值");
        }
        // todo 遍历条件,生成工单
        return Boolean.FALSE;
    }
    @Override
    public FaceThresholdForm getFace(Integer id) {
        return null;
    }
    @Override
    public CarThresholdForm getCar(Integer id) {
        return null;
    }
    @Override
    public VideoThresholdForm getVideo(Integer id) {
        YwThreshold result = new LambdaQueryChainWrapper<>(baseMapper)
                .select(YwThreshold::getImageQuality,
                        YwThreshold::getId,
                        YwThreshold::getMonitorType,
                        YwThreshold::getImageQualityAuto,
                        YwThreshold::getVideoQuality,
                        YwThreshold::getVideoQualityAuto,
                        YwThreshold::getAnnotationAccuracy,
                        YwThreshold::getAnnotationAccuracyAuto)
                .eq(YwThreshold::getId, id)
                .eq(YwThreshold::getMonitorType, BusinessTypeEnum.VIDEO)
                .one();
        VideoThresholdForm form = new VideoThresholdForm();
        BeanUtils.copyProperties(result, form);
        return form;
    }
}