package com.ycl.calculate; import com.alibaba.fastjson2.JSONArray; import com.ycl.platform.domain.entity.CheckIndexCar; import com.ycl.platform.domain.entity.TMonitor; import com.ycl.platform.domain.result.HK.DataIntegrityMonitoringResult; import com.ycl.platform.domain.result.HK.VehicleDeviceInspectionResult; import com.ycl.platform.domain.result.UY.MonitorQualifyResult; import com.ycl.platform.domain.vo.TMonitorVO; import com.ycl.platform.mapper.CheckIndexCarMapper; import com.ycl.platform.mapper.TMonitorMapper; import com.ycl.platform.service.ICheckIndexCarService; import constant.ApiConstants; import constant.CheckConstants; import constant.RedisConstant; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import utils.DateUtils; import utils.StringUtils; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; import java.util.stream.Collectors; /** * 计算车辆目录一致率 * 获取分省厅、区域的map k为deptId或者Province_deptId * 更新或新增 */ @Component @Slf4j public class CarConsistentCalculation extends IndexCalculationServe implements CalculationStrategy { @Autowired private CheckIndexCarMapper checkIndexCarMapper; @Autowired private TMonitorMapper monitorMapper; @Autowired private ICheckIndexCarService checkIndexCarService; @Autowired private RedisTemplate redisTemplate; //区域车辆目录一致率的内部类 protected static class AreaStats { int totalSites = 0; int newSites = 0; } @Override public void calculate(List list) { //获取分区域的指标数量 Map areaStatsMap = getAreaStatsMap(list); if (areaStatsMap == null) return; // 查询是否index表已经存在今日数据 List checkIndexCarList = checkIndexCarMapper.selectToday(DateUtils.getDate()); List checkIndexCars = new ArrayList<>(); areaStatsMap.forEach((deptId, stats) -> { if (stats.totalSites > 0) { CheckIndexCar checkIndexCar = createOrUpdateCheckIndexCar(deptId, stats, checkIndexCarList); if (checkIndexCar != null) { checkIndexCars.add(checkIndexCar); } } }); checkIndexCarService.saveOrUpdateBatch(checkIndexCars); } /** * 累计车辆目录一致率 */ @Override public void updateAreaStats(Map areaStatsMap, String key, MonitorQualifyResult result) { //返回对象的引用,如果不存在会放入新的key,value AreaStats stats = areaStatsMap.computeIfAbsent(key, k -> new AreaStats()); stats.totalSites++; if(result.getNewDevice()!=null && result.getNewDevice()){ stats.newSites++; } } /** * 车辆信息采集正确率 */ private CheckIndexCar createOrUpdateCheckIndexCar(String key, AreaStats stats, List checkIndexCarList) { CheckIndexCar checkIndexCar = getCheckIndex(key, checkIndexCarList, CheckIndexCar.class); if (checkIndexCar == null) { return null; } //调用目录一致率计算方法 Map param = new HashMap<>(); param.put("totalSites", stats.totalSites); param.put("newSites", stats.newSites); BigDecimal directoryConstant = directoryConstant(param); checkIndexCar.setDeviceDirectoryConsistent(directoryConstant); return checkIndexCar; } }