| | |
| | | import com.ycl.platform.domain.entity.YwPoint; |
| | | import com.ycl.platform.domain.result.UY.MonitorQualifyResult; |
| | | import com.ycl.platform.domain.result.UY.OneMachineFileResult; |
| | | import com.ycl.platform.domain.result.UY.OsdCheckResult; |
| | | import com.ycl.platform.domain.vo.TMonitorVO; |
| | | import com.ycl.platform.mapper.TMonitorMapper; |
| | | import com.ycl.platform.mapper.YwPointMapper; |
| | |
| | | import enumeration.general.AreaDeptEnum; |
| | | import enumeration.general.PointStatus; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.mongodb.core.MongoTemplate; |
| | | import org.springframework.data.mongodb.core.query.Criteria; |
| | |
| | | private RedisTemplate redisTemplate; |
| | | |
| | | //同步mongodb一机一档到数据库 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void synchronize() { |
| | | // 获取Calendar实例,默认使用当前时区和语言环境 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | // 清除Calendar中的所有字段值,以避免它们对当前日期时间有影响 |
| | | calendar.clear(); |
| | | // 设置年份、月份(注意:月份是从0开始的,所以8月是7)、日期 |
| | | calendar.set(Calendar.YEAR, 2024); |
| | | calendar.set(Calendar.MONTH, Calendar.AUGUST); |
| | | calendar.set(Calendar.DAY_OF_MONTH, 13); |
| | | // Calendar.getTime()方法返回一个表示此Calendar时间值的Date对象 |
| | | Date date = calendar.getTime(); |
| | | 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()))); |
| | | log.info("开始同步mongodb一机一档到数据库"); |
| | | Date yesterday = DateUtils.addDays(new Date(), -1); |
| | | Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(yesterday)).lt(DateUtils.getDayEnd(yesterday))); |
| | | List<MonitorQualifyResult> oneMachineFileResults = mongoTemplate.find(query, MonitorQualifyResult.class); |
| | | //mongo品牌数据 |
| | | Query OSDQuery = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(yesterday)).lt(DateUtils.getDayEnd(yesterday))); |
| | | Map<String, OsdCheckResult> osdMap = mongoTemplate.find(query, OsdCheckResult.class).stream().collect(Collectors.toMap(OsdCheckResult::getDeviceNo, Function.identity())); |
| | | //数据库monitor表数据 |
| | | Map<String, TMonitorVO> monitorVOMap = monitorMapper.selectMonitorVOList().stream().collect(Collectors.toMap(TMonitorVO::getSerialNumber, Function.identity())); |
| | | //准备插入设备表的数据 |
| | | List<TMonitor> monitorList = new ArrayList<>(); |
| | | //准备插入点位表的数据 |
| | | List<YwPoint> ywPointList = new ArrayList<>(); |
| | | //新的数据,原数据库中不存在的数据 |
| | | Set<TMonitor> newMonitorList = new HashSet<>(); |
| | | //点位数据 |
| | | Map<String, YwPoint> pointMap = ywPointService.list(new QueryWrapper<YwPoint>()).stream().collect(Collectors.toMap(YwPoint::getSerialNumber, Function.identity())); |
| | | |
| | | //重点点位集合字典(解析SXJCJQY字段) |
| | | SysDictData sysDictData = new SysDictData(); |
| | | sysDictData.setDictType("platform_important_site"); |
| | |
| | | //采集区域为重点点位的集合 |
| | | List<String> importantSite = DictDataList.stream().map(SysDictData::getDictValue).collect(Collectors.toList()); |
| | | |
| | | //准备插入设备表的数据 |
| | | List<TMonitor> monitorList = new ArrayList<>(); |
| | | //准备插入点位表的数据 |
| | | List<YwPoint> ywPointList = new ArrayList<>(); |
| | | //新的数据,原数据库中不存在的数据 |
| | | Set<TMonitor> newMonitorList = new HashSet<>(); |
| | | |
| | | //全年留存 |
| | | for (MonitorQualifyResult result : oneMachineFileResults) { |
| | | TMonitor monitor = getMonitor(result, monitorVOMap); |
| | | TMonitor monitor = getMonitor(result, monitorVOMap,osdMap); |
| | | YwPoint point = getPoint(result, pointMap, importantSite); |
| | | monitorList.add(monitor); |
| | | ywPointList.add(point); |
| | |
| | | newMonitorList.add(monitor); |
| | | } |
| | | } |
| | | //添加老数据 |
| | | List<String> numbers = CollectionUtils.isEmpty(monitorList) ? new ArrayList<>() : monitorList.stream().map(TMonitor::getSerialNumber).collect(Collectors.toList()); |
| | | monitorVOMap.forEach((key, value) -> { |
| | | if (!numbers.contains(key)){ |
| | | TMonitor monitor = new TMonitor(); |
| | | BeanUtils.copyProperties(value,monitor); |
| | | //填补品牌 |
| | | OsdCheckResult osdCheckResult = osdMap.get(key); |
| | | if(osdCheckResult!=null) { |
| | | monitor.setDeviceType(osdCheckResult.getDeviceBrand()); |
| | | } |
| | | monitorList.add(monitor); |
| | | } |
| | | }); |
| | | List<String> points = CollectionUtils.isEmpty(ywPointList) ? new ArrayList<>() : ywPointList.stream().map(YwPoint::getSerialNumber).collect(Collectors.toList()); |
| | | pointMap.forEach((key, value) -> { |
| | | if (!points.contains(key)){ |
| | | ywPointList.add(value); |
| | | } |
| | | }); |
| | | |
| | | log.info("result集合{},设备集合{},点位集合{}", oneMachineFileResults.size(), monitorList.size(), ywPointList.size()); |
| | | //插入数据库 |
| | | if (!CollectionUtils.isEmpty(monitorList)) { |
| | |
| | | } |
| | | //新的数据放入Redis中等待考核指标任务使用 |
| | | redisTemplate.opsForValue().set(RedisConstant.New_Monitor_Set, JSONArray.toJSONString(newMonitorList)); |
| | | log.info("结束同步mongodb一机一档到数据库"); |
| | | } |
| | | |
| | | private YwPoint getPoint(MonitorQualifyResult result, Map<String, YwPoint> pointMap, List<String> importantSite) { |
| | |
| | | return ywPoint; |
| | | } |
| | | |
| | | private TMonitor getMonitor(MonitorQualifyResult result, Map<String, TMonitorVO> monitorVOMap) { |
| | | private TMonitor getMonitor(MonitorQualifyResult result, Map<String, TMonitorVO> monitorVOMap, Map<String, OsdCheckResult> osdMap) { |
| | | TMonitor monitor = new TMonitor(); |
| | | if (monitorVOMap.containsKey(result.getSerialNumber().getValue())) { |
| | | monitor.setId(monitorVOMap.get(result.getSerialNumber().getValue()).getId()); |
| | | //更新品牌 |
| | | OsdCheckResult osdCheckResult = osdMap.get(result.getSerialNumber().getValue()); |
| | | if(osdCheckResult!=null) { |
| | | monitor.setDeviceType(osdCheckResult.getDeviceBrand()); |
| | | } |
| | | } |
| | | |
| | | monitor.setSerialNumber(result.getSerialNumber().getValue()); |
| | | monitor.setName(result.getName().getValue()); |
| | | String siteType = result.getJkdwlx().getValue(); |