package com.ycl.calculate; import com.ycl.platform.domain.entity.CheckIndexCar; import com.ycl.platform.domain.result.HK.SnapshotDataMonitorResult; import com.ycl.platform.mapper.CheckIndexCarMapper; import com.ycl.platform.service.ICheckIndexCarService; import com.ycl.system.entity.SysDictData; import com.ycl.system.mapper.SysDictDataMapper; import com.ycl.utils.DateUtils; import constant.ApiConstants; import constant.CheckConstants; import constant.CheckThreadConstants; import constant.RedisConstant; import enumeration.general.AreaDeptEnum; 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 java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDate; import java.time.temporal.TemporalAdjusters; import java.util.*; /** * 计算车辆点位在线率、视图库对接稳定性 * 抓拍数据量监测结果接口数据 * 获取分省厅、区域的map k为deptId或者Province_deptId * 循环map计算点位在线率 * 更新或新增 */ @Component @Slf4j public class CarSnapshopDataCalculation extends IndexCalculationServe implements CalculationStrategy { @Autowired private CheckIndexCarMapper checkIndexCarMapper; @Autowired private ICheckIndexCarService checkIndexCarService; @Autowired private RedisTemplate redisTemplate; @Autowired private SysDictDataMapper dictDataMapper; //区域车辆点位在线指标的内部类 protected static class AreaStats { int totalSites = 0; int onlineSites = 0; int totalDataSum = 0; } @Override public void calculate(List list) { //获取分区域的指标数量 Map areaStatsMap = getAreaStatsMap(list); if (areaStatsMap == null) return; // 获取当前日期 LocalDate today = LocalDate.now(); // 获取本月的第一天 LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 判断今天是否是本月的第一天 if (today.equals(firstDayOfMonth)) { // 如果是,则清除Redis中记录中断次数的数据 redisTemplate.delete(RedisConstant.Check_Car_ViewConnect); } //从字典获取同期抓拍量 List carSnap = dictDataMapper.selectDictDataByType(CheckConstants.DICT_CAR_SNAP); // 查询是否index表已经存在今日数据 List checkIndexCarList = checkIndexCarMapper.selectToday(DateUtils.getDate()); List checkIndexCars = new ArrayList<>(); areaStatsMap.forEach((key, stats) -> { if (stats.totalSites > 0) { CheckIndexCar checkIndexCar = createOrUpdateCheckIndexCar(key, stats, checkIndexCarList,carSnap); if (checkIndexCar != null) { checkIndexCars.add(checkIndexCar); } } }); checkIndexCarService.saveOrUpdateBatch(checkIndexCars); } /** * 累计总点位数、离线数、总抓拍量 */ @Override public void updateAreaStats(Map areaStatsMap, String key, SnapshotDataMonitorResult result) { //返回对象的引用,如果不存在会放入新的key,value AreaStats stats = areaStatsMap.computeIfAbsent(key, k -> new AreaStats()); stats.totalSites++; if (ApiConstants.HK_SnapCount_ResultType_Null != result.getResultType()) { stats.onlineSites++; stats.totalDataSum += result.getDataCount(); } } /** * 车辆点位在线率和视图库对接稳定性 */ private CheckIndexCar createOrUpdateCheckIndexCar(String key, AreaStats stats, List checkIndexCarList,List carSnap) { CheckIndexCar checkIndexCar = getCheckIndex(key, checkIndexCarList, CheckIndexCar.class); if (checkIndexCar == null) { return null; } //调用点位在线计算方法 if (stats.totalSites >= CheckThreadConstants.Check_Car_SiteOnline) { Map siteOnlineParam = new HashMap<>(); siteOnlineParam.put("totalSites", stats.totalSites); siteOnlineParam.put("onlineSites", stats.onlineSites); BigDecimal siteOnline = siteOnline(siteOnlineParam); checkIndexCar.setSiteOnline(siteOnline.min(BigDecimal.ONE)); } else { checkIndexCar.setSiteOnline(BigDecimal.ZERO); } //视图库对接稳定性 //Redis记录该区县当月无数据上传次数 Integer noDateCount = (Integer) redisTemplate.opsForHash().get(RedisConstant.Check_Car_ViewConnect, key); // 如果值为null,则初始化为0 if (noDateCount == null) { noDateCount = 0; } Double deductScore = 0.1 * noDateCount; if (stats.totalDataSum != 0) { Integer deptId = key.startsWith(ApiConstants.Province) ? Integer.parseInt(key.split("_")[1]) : Integer.parseInt(key); String dictKey = key.startsWith(ApiConstants.Province) ? ApiConstants.Province+AreaDeptEnum.fromDept(deptId).getCode() : ApiConstants.County+AreaDeptEnum.fromDept(deptId).getCode(); Optional first = carSnap.stream().filter(sysDictData -> sysDictData.getDictLabel().equals(dictKey)).findFirst(); if(first.isPresent()){ String value = first.get().getDictValue(); Map viewConnectParam = new HashMap<>(); viewConnectParam.put("totalDataSum", stats.totalDataSum); viewConnectParam.put("avgCount", new BigDecimal(value).multiply(new BigDecimal(CheckConstants.MultiplyNum)).divide(new BigDecimal(LocalDate.now().getDayOfMonth()), 0, RoundingMode.HALF_UP)); BigDecimal viewConnectStability = viewConnectStability(viewConnectParam); viewConnectStability = viewConnectStability.subtract(new BigDecimal(deductScore)).max(BigDecimal.ZERO).min(BigDecimal.ONE); checkIndexCar.setViewConnectStability(viewConnectStability); } } else { noDateCount++; } // 将新的值放回Hash中 redisTemplate.opsForHash().put(RedisConstant.Check_Car_ViewConnect, key, noDateCount); return checkIndexCar; } }