package com.ycl.calculate; import com.ycl.platform.base.CheckIndex; import com.ycl.platform.domain.entity.TMonitor; import com.ycl.platform.domain.entity.YwPoint; import com.ycl.platform.domain.result.UY.MonitorQualifyResult; import com.ycl.platform.domain.vo.TMonitorVO; import com.ycl.platform.mapper.TMonitorMapper; import com.ycl.platform.mapper.YwPointMapper; import com.ycl.utils.DateUtils; import constant.ApiConstants; import constant.CheckConstants; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * 计算公式 */ @Component @Slf4j public class IndexCalculationServe { @Autowired private YwPointMapper pointMapper; @Autowired private TMonitorMapper monitorMapper; @Autowired private MongoTemplate mongoTemplate; //一机一档合格率 public BigDecimal monitorQualify(Map param) { BigDecimal totalMonitors = new BigDecimal((Integer) param.get("totalMonitors")); BigDecimal qualifyMonitor = new BigDecimal((Integer) param.get("qualifyMonitor")); return qualifyMonitor.divide(totalMonitors, 4, RoundingMode.HALF_UP); } //一机一档注册率率 public BigDecimal monitorRegistration(Map param) { //新+正在用的档案数 BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); //正在用的档案数 BigDecimal useSites = new BigDecimal((Integer) param.get("totalSites") - (Integer) param.get("newSites")); return useSites.divide(totalSites, 4, RoundingMode.HALF_UP); } //档案考核比 public BigDecimal archivesRate(Map param) { BigDecimal allFiles = new BigDecimal((Integer) param.get("allFiles")); BigDecimal todayFiles = new BigDecimal((Integer) param.get("todayFiles")); return todayFiles.divide(allFiles, 4, RoundingMode.HALF_UP); } //点位在线率 public BigDecimal siteOnline(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal onlineSites = new BigDecimal((Integer) param.get("onlineSites")); return onlineSites.divide(totalSites, 4, RoundingMode.HALF_UP); } //视频Osd准确率 public BigDecimal osdAccuracy(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal osdAccuracySites = new BigDecimal((Integer) param.get("osdAccuracySites")); return osdAccuracySites.divide(totalSites, 4, RoundingMode.HALF_UP); } //视频时间合格率 public BigDecimal osdTimeAccuracy(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal timeAccuracySites = new BigDecimal((Integer) param.get("timeAccuracySites")); return timeAccuracySites.divide(totalSites, 4, RoundingMode.HALF_UP); } //目录一致率,每超过百分之1扣10% public BigDecimal directoryConstant(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal newSites = new BigDecimal((Integer) param.get("newSites")); // 计算newSites占totalSites的百分比倍数 BigDecimal percentage = newSites.divide(totalSites, 2, RoundingMode.DOWN).multiply(BigDecimal.valueOf(100)); BigDecimal result = BigDecimal.ONE; if (BigDecimal.ONE.compareTo(percentage) <= 0) { result = result.subtract(percentage.multiply(new BigDecimal("0.1"))); } return result.max(BigDecimal.ZERO); } //视图库对接稳定性 public BigDecimal viewConnectStability(Map param) { BigDecimal totalDataSum = new BigDecimal((Integer) param.get("totalDataSum")); BigDecimal avgCount = new BigDecimal((Integer) param.get("avgCount")); return avgCount.divide(totalDataSum, 4, RoundingMode.HALF_UP); } //标注正确率 public BigDecimal infoAccuracy(Map param) { BigDecimal totalSitesCount = new BigDecimal((Integer) param.get("totalSites")); BigDecimal qualifySiteCount = new BigDecimal((Integer) param.get("qualifySite")); return qualifySiteCount.divide(totalSitesCount, 4, RoundingMode.HALF_UP); } //数据上传及时性 public BigDecimal snapshopDelay(Map param) { BigDecimal totalCount = new BigDecimal((Integer) param.get("totalCount")); BigDecimal delayCount1 = new BigDecimal((Integer) param.get("delayCount1")); BigDecimal delayCount2 = new BigDecimal((Integer) param.get("delayCount2")); BigDecimal delayCount3 = new BigDecimal((Integer) param.get("delayCount3")); BigDecimal result = delayCount1.divide(totalCount, 10, RoundingMode.HALF_UP).multiply(BigDecimal.ONE) .add(delayCount2.divide(totalCount, 10, RoundingMode.HALF_UP).multiply(new BigDecimal("0.8"))) .add(delayCount3.divide(totalCount, 10, RoundingMode.HALF_UP).multiply(new BigDecimal("0.6"))); return result.setScale(4, RoundingMode.HALF_UP); } //车辆数据完整性 public BigDecimal dataIntegrity(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal importantTotalSites = new BigDecimal((Integer) param.get("importantTotalSites")); BigDecimal integritySites = new BigDecimal((Integer) param.get("integritySites")); BigDecimal importantIntegritySites = new BigDecimal((Integer) param.get("importantIntegritySites")); BigDecimal result = integritySites.divide(totalSites, 10, RoundingMode.HALF_UP) .add(importantIntegritySites.divide(importantTotalSites, 10, RoundingMode.HALF_UP)); return result.setScale(4, RoundingMode.HALF_UP); } //车辆数据完整性 public BigDecimal dataAccuracy(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal importantTotalSites = new BigDecimal((Integer) param.get("importantTotalSites")); BigDecimal accuracySites = new BigDecimal((Integer) param.get("accuracySites")); BigDecimal importantAccuracySites = new BigDecimal((Integer) param.get("importantAccuracySites")); BigDecimal result = accuracySites.divide(totalSites, 10, RoundingMode.HALF_UP) .add(importantAccuracySites.divide(importantTotalSites, 10, RoundingMode.HALF_UP)); return result.setScale(4, RoundingMode.HALF_UP); } //时钟准确性 public BigDecimal clockAccuracy(Map param) { BigDecimal totalSitesCount = new BigDecimal((Integer) param.get("totalSites")); BigDecimal accuracySites = new BigDecimal((Integer) param.get("accuracySites")); return accuracySites.divide(totalSitesCount, 4, RoundingMode.HALF_UP); } //url可用性 public BigDecimal urlUsability(Map param) { BigDecimal totalSitesCount = new BigDecimal((Integer) param.get("totalSites")); BigDecimal urlQualifySites = new BigDecimal((Integer) param.get("urlQualifySites")); return urlQualifySites.divide(totalSitesCount, 4, RoundingMode.HALF_UP); } //大图可用性 public BigDecimal picUsability(Map param) { BigDecimal totalSitesCount = new BigDecimal((Integer) param.get("totalSites")); BigDecimal picUsabilitySites = new BigDecimal((Integer) param.get("picUsabilitySites")); return picUsabilitySites.divide(totalSitesCount, 4, RoundingMode.HALF_UP); } //图片合格率 public BigDecimal dataQualify(Map param) { BigDecimal totalSitesCount = new BigDecimal((Integer) param.get("totalSites")); BigDecimal picQualifySites = new BigDecimal((Integer) param.get("picQualifySites")); return picQualifySites.divide(totalSitesCount, 4, RoundingMode.HALF_UP); } //录像可用率 public BigDecimal videoUsability(Map param) { BigDecimal totalSites = new BigDecimal((Integer) param.get("totalSites")); BigDecimal usabilitySites = new BigDecimal((Integer) param.get("usabilitySites")); return usabilitySites.divide(totalSites, 4, RoundingMode.HALF_UP); } //返回省厅国标码集合 public List getProvince() { List province = pointMapper.selectByTag(null, Boolean.TRUE, null); return CollectionUtils.isEmpty(province) ? new ArrayList<>() : province.stream().map(YwPoint::getSerialNumber).collect(Collectors.toList()); } //返回重点点位集合 public List getImportant() { List important = pointMapper.selectByTag(Boolean.TRUE, null, null); return CollectionUtils.isEmpty(important) ? new ArrayList<>() : important.stream().map(YwPoint::getSerialNumber).collect(Collectors.toList()); } //返回重点指挥图像集合 public List getCommandImage() { List commandImages = pointMapper.selectByTag(null, null, Boolean.TRUE); return CollectionUtils.isEmpty(commandImages) ? new ArrayList<>() : commandImages.stream().map(YwPoint::getSerialNumber).collect(Collectors.toList()); } //返回monitor //TODO:时间写死了 public List getMonitorFromMongo() { Date date = DateUtils.getDay(2024, 7, 13); Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(date)).lt(DateUtils.getDayEnd(date))); // Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(new Date())).lt(DateUtils.getDayEnd(new Date()))); List oneMachineFileResults = mongoTemplate.find(query, MonitorQualifyResult.class); return CollectionUtils.isEmpty(oneMachineFileResults) ? new ArrayList<>() : oneMachineFileResults.stream().map(result -> result.getSerialNumber().getValue()).collect(Collectors.toList()); } //检查是否存在当日数据 public T getCheckIndex(String key, List checkIndexList, Class clazz) { T checkIndex; // 检查是否已存在今日数据 Optional existingIndex = checkIndexList.stream() .filter(index -> key.startsWith(ApiConstants.Province) ? CheckConstants.Examine_Tag_Province.equals(index.getExamineTag()) && key.split("_")[1].equals(index.getDeptId().toString()) : CheckConstants.Examine_Tag_County.equals(index.getExamineTag()) && key.equals(index.getDeptId().toString())) .findFirst(); if (existingIndex.isPresent()) { checkIndex = existingIndex.get(); } else { try { checkIndex = clazz.getDeclaredConstructor().newInstance(); checkIndex.setDeptId(key.startsWith(ApiConstants.Province) ? Long.parseLong(key.split("_")[1]) : Long.parseLong(key)); checkIndex.setExamineTag(key.startsWith(ApiConstants.Province) ? CheckConstants.Examine_Tag_Province : CheckConstants.Examine_Tag_County); checkIndex.setCreateTime(new Date()); } catch (Exception e) { checkIndex = null; log.error("无法创建 checkIndex 实例", e); } } return checkIndex; } }