package com.ycl.task; import com.alibaba.druid.support.json.JSONUtils; import com.alibaba.fastjson2.JSONArray; import com.ycl.platform.domain.entity.TMonitor; import com.ycl.platform.domain.result.UY.OneMachineFileResult; import com.ycl.platform.domain.vo.TMonitorVO; import com.ycl.platform.service.ITMonitorService; import com.ycl.system.entity.SysDictData; import com.ycl.system.service.ISysDictDataService; import com.ycl.utils.DateUtils; import constant.RedisConstant; 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.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.CollectionUtils; import java.util.*; import java.util.function.Function; import java.util.stream.Collectors; /** * 当天晚上 同步mongodb一机一档到数据库 */ @Slf4j @Component("monitorTask") public class MonitorTask { @Autowired private MongoTemplate mongoTemplate; @Autowired private ITMonitorService monitorService; @Autowired private ISysDictDataService dictDataService; @Autowired private RedisTemplate redisTemplate; //同步mongodb一机一档到数据库 public void synchronize() { Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(new Date())).lt(DateUtils.getDayEnd(new Date()))); List oneMachineFileResults = mongoTemplate.find(query, OneMachineFileResult.class); List serialNumberInBase = monitorService.selectTMonitorList(null).stream().map(TMonitorVO::getSerialNumber).collect(Collectors.toList()); //准备插入数据库的数据 List monitorList = new ArrayList<>(); //新的数据,原数据库中不存在的数据 Set newMonitorList = new HashSet<>(); //TODO:解析区域、补充deptId List areaCodeList = new ArrayList<>(); //TODO:解析重点点位 //重点点位集合字典(解析SXJCJQY字段) SysDictData sysDictData = new SysDictData(); sysDictData.setDictType("platform_important_site"); List DictDataList = dictDataService.selectDictDataList(sysDictData); List importantSite = DictDataList.stream().map(SysDictData::getDictValue).collect(Collectors.toList()); for (OneMachineFileResult result : oneMachineFileResults) { TMonitor monitor = setMonitor(result); monitorList.add(monitor); //比对筛选出新的数据 if(!CollectionUtils.isEmpty(serialNumberInBase) && !serialNumberInBase.contains(result.getSBBM())){ newMonitorList.add(monitor); } } //新的数据放入Redis中等待考核指标任务使用 redisTemplate.opsForValue().set(RedisConstant.New_Monitor_Set, JSONArray.toJSONString(newMonitorList)); } private TMonitor setMonitor(OneMachineFileResult result) { TMonitor monitor = new TMonitor(); monitor.setSerialNumber(result.getSBBM()); monitor.setName(result.getSBMC()); monitor.setSiteType(Long.valueOf(result.getJKDWLX())); monitor.setMacAddr(result.getMACDZ()); monitor.setIp(result.getIP()); monitor.setCameraFunType(result.getSXJGNLX()); monitor.setLongitude(result.getJD() + ""); monitor.setLatitude(result.getWD() + ""); monitor.setCameraCaptureArea(result.getSXJCJQY()); monitor.setOnState(Long.valueOf(result.getSBZT())); //国标码前八位为行政编码 monitor.setCivilCode(result.getSBBM().substring(0, 8)); return monitor; } }