| | |
| | | import com.alibaba.druid.support.json.JSONUtils; |
| | | import com.alibaba.fastjson2.JSONArray; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.mongodb.client.result.DeleteResult; |
| | | import com.ycl.platform.domain.entity.TMonitor; |
| | | import com.ycl.platform.domain.entity.YwPoint; |
| | | import com.ycl.platform.domain.result.HK.VehicleDeviceInspectionResult; |
| | | import com.ycl.platform.domain.result.SYS.TMonitorResult; |
| | | 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 com.ycl.platform.service.ITMonitorService; |
| | | import com.ycl.platform.service.YwPointService; |
| | | import com.ycl.system.entity.SysDictData; |
| | | import com.ycl.system.service.ISysDictDataService; |
| | | import com.ycl.utils.DateUtils; |
| | | import com.ycl.utils.StringUtils; |
| | | import constant.ApiConstants; |
| | | import constant.CheckConstants; |
| | | import constant.RedisConstant; |
| | | import enumeration.general.AreaDeptEnum; |
| | | import enumeration.general.ImportantTagEnum; |
| | | import enumeration.general.PointStatus; |
| | | import enumeration.general.ProvinceTagEnum; |
| | | 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; |
| | |
| | | @Autowired |
| | | private ITMonitorService monitorService; |
| | | @Autowired |
| | | private TMonitorMapper monitorMapper; |
| | | @Autowired |
| | | private ISysDictDataService dictDataService; |
| | | @Autowired |
| | | private YwPointService ywPointService; |
| | | @Autowired |
| | | private YwPointMapper ywPointMapper; |
| | | @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<OneMachineFileResult> oneMachineFileResults = mongoTemplate.find(query, OneMachineFileResult.class); |
| | | //数据库monitor表数据 |
| | | Map<String, TMonitorVO> monitorVOMap = monitorService.selectTMonitorList(null).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<SysDictData> DictDataList = dictDataService.selectDictDataList(sysDictData); |
| | | List<String> importantSite = DictDataList.stream().map(SysDictData::getDictValue).collect(Collectors.toList()); |
| | | //同步点位在线率到mongo(弃用) |
| | | // public void siteOnline() { |
| | | // log.info("开始同步点位在线率到mongo"); |
| | | // List<TMonitorResult> tMonitorResults = monitorMapper.selectMonitorResult(null); |
| | | // if (!CollectionUtils.isEmpty(tMonitorResults)) { |
| | | // //如果存在之前的数据先删除 |
| | | // Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(new Date())).lt(DateUtils.getDayEnd(new Date()))); |
| | | // DeleteResult result = mongoTemplate.remove(query, TMonitorResult.class); |
| | | // //存放在mongo中 |
| | | // mongoTemplate.insertAll(tMonitorResults); |
| | | // log.info("结束同步点位在线率到mongo"); |
| | | // } |
| | | // } |
| | | |
| | | //TODO:全年留存还是单次留存 |
| | | for (OneMachineFileResult result : oneMachineFileResults) { |
| | | TMonitor monitor = getMonitor(result,monitorVOMap); |
| | | YwPoint point = getPoint(result, pointMap, importantSite); |
| | | monitorList.add(monitor); |
| | | ywPointList.add(point); |
| | | //比对筛选出新的数据 |
| | | if (!CollectionUtils.isEmpty(monitorVOMap) && !monitorVOMap.containsKey(result.getSBBM())) { |
| | | newMonitorList.add(monitor); |
| | | //同步mongodb一机一档到数据库 |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void synchronize() { |
| | | log.info("开始同步mongodb一机一档到数据库"); |
| | | // Date yesterday = DateUtils.addDays(new Date(), -1); |
| | | Query query = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(new Date())).lt(DateUtils.getDayEnd(new Date()))); |
| | | List<MonitorQualifyResult> oneMachineFileResults = mongoTemplate.find(query, MonitorQualifyResult.class); |
| | | if(!CollectionUtils.isEmpty(oneMachineFileResults)) { |
| | | //mongo品牌数据 |
| | | Query OSDQuery = new Query(Criteria.where("mongoCreateTime").gte(DateUtils.getDayStart(new Date())).lt(DateUtils.getDayEnd(new Date()))); |
| | | 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())); |
| | | //点位数据 |
| | | 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<SysDictData> DictDataList = dictDataService.selectDictDataList(sysDictData); |
| | | //采集区域为重点点位的集合 |
| | | List<String> importantSite = DictDataList.stream().map(SysDictData::getDictValue).collect(Collectors.toList()); |
| | | |
| | | //准备插入设备表的数据 |
| | | List<TMonitor> monitorList = new ArrayList<>(); |
| | | //准备插入点位表的数据 |
| | | List<YwPoint> ywPointList = new ArrayList<>(); |
| | | |
| | | |
| | | //全年留存 |
| | | for (MonitorQualifyResult result : oneMachineFileResults) { |
| | | TMonitor monitor = getMonitor(result, monitorVOMap, osdMap); |
| | | YwPoint point = getPoint(result, pointMap, importantSite); |
| | | monitorList.add(monitor); |
| | | ywPointList.add(point); |
| | | } |
| | | //添加老数据 |
| | | 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)) { |
| | | value.setExamineStatus(Boolean.FALSE); |
| | | //单位设置为空(TODO:为啥) |
| | | value.setUnitId(null); |
| | | ywPointList.add(value); |
| | | } |
| | | }); |
| | | |
| | | log.info("result集合{},设备集合{},点位集合{}", oneMachineFileResults.size(), monitorList.size(), ywPointList.size()); |
| | | //插入数据库 |
| | | if (!CollectionUtils.isEmpty(monitorList)) { |
| | | monitorMapper.deleteAll(); |
| | | monitorService.saveBatch(monitorList); |
| | | } |
| | | if (!CollectionUtils.isEmpty(ywPointList)) { |
| | | ywPointMapper.deleteAll(); |
| | | ywPointService.saveBatch(ywPointList); |
| | | } |
| | | //新的数据放入Redis中等待考核指标任务使用(弃用,改为标签形式) |
| | | // redisTemplate.opsForValue().set(RedisConstant.New_Monitor_Set, JSONArray.toJSONString(newMonitorList)); |
| | | } |
| | | //插入数据库 |
| | | if (!CollectionUtils.isEmpty(monitorList)) { |
| | | monitorService.saveOrUpdateBatch(monitorList); |
| | | } |
| | | if(!CollectionUtils.isEmpty(ywPointList)){ |
| | | ywPointService.saveOrUpdateBatch(ywPointList); |
| | | } |
| | | //新的数据放入Redis中等待考核指标任务使用 |
| | | redisTemplate.opsForValue().set(RedisConstant.New_Monitor_Set, JSONArray.toJSONString(newMonitorList)); |
| | | log.info("结束同步mongodb一机一档到数据库"); |
| | | } |
| | | |
| | | private YwPoint getPoint(OneMachineFileResult result, Map<String, YwPoint> pointMap, List<String> importantSite) { |
| | | private YwPoint getPoint(MonitorQualifyResult result, Map<String, YwPoint> pointMap, List<String> importantSite) { |
| | | YwPoint ywPoint = new YwPoint(); |
| | | if (pointMap.containsKey(result.getSBBM())) { |
| | | ywPoint = pointMap.get(result.getSBBM()); |
| | | }else { |
| | | ywPoint.setPointName(result.getSBMC()); |
| | | if (pointMap.containsKey(result.getSerialNumber().getValue())) { |
| | | ywPoint = pointMap.get(result.getSerialNumber().getValue()); |
| | | } else { |
| | | ywPoint.setPointName(result.getName().getValue()); |
| | | ywPoint.setStatus(PointStatus.WAIT.getDesc()); |
| | | ywPoint.setSerialNumber(result.getSBBM()); |
| | | ywPoint.setImportantTag(ImportantTagEnum.Normal); |
| | | ywPoint.setProvinceTag(ProvinceTagEnum.Normal); |
| | | ywPoint.setSerialNumber(result.getSerialNumber().getValue()); |
| | | ywPoint.setImportantTag(Boolean.FALSE); |
| | | ywPoint.setProvinceTagVideo(Boolean.FALSE); |
| | | ywPoint.setProvinceTagCar(Boolean.FALSE); |
| | | ywPoint.setProvinceTagFace(Boolean.FALSE); |
| | | ywPoint.setImportantCommandImageTag(Boolean.FALSE); |
| | | ywPoint.setCreateTime(new Date()); |
| | | ywPoint.setUpdateTime(new Date()); |
| | | ywPoint.setOnline(ApiConstants.UY_OnlineSite_Unknown); |
| | | } |
| | | //状态设置为考核设备 |
| | | ywPoint.setExamineStatus(Boolean.TRUE); |
| | | //比对是否是重点点位 |
| | | if (importantSite.contains(result.getSBBM())) { |
| | | ywPoint.setImportantTag(ImportantTagEnum.Important); |
| | | if (importantSite.contains(result.getSxjcjqy().getValue())) { |
| | | ywPoint.setImportantTag(Boolean.TRUE); |
| | | } |
| | | //解析deptId |
| | | //区域行政编码 |
| | | String areaCode = result.getSBBM().substring(0, 6); |
| | | AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromCode(areaCode); |
| | | //如果解析不出区域deptId为-1 |
| | | String serialNumber = result.getSerialNumber().getValue(); |
| | | Integer deptId = -1; |
| | | if(areaDeptEnum!=null){ |
| | | deptId = areaDeptEnum.getDeptId(); |
| | | if (!StringUtils.isEmpty(serialNumber)) { |
| | | String areaCode = serialNumber.substring(0, 6); |
| | | AreaDeptEnum areaDeptEnum = AreaDeptEnum.fromCode(areaCode); |
| | | //如果解析不出区域deptId为-1 |
| | | if (areaDeptEnum != null) { |
| | | deptId = areaDeptEnum.getDeptId(); |
| | | } |
| | | } |
| | | ywPoint.setDeptId(Long.valueOf(deptId + "")); |
| | | |
| | | return ywPoint; |
| | | } |
| | | |
| | | private TMonitor getMonitor(OneMachineFileResult 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.getSBBM())) { |
| | | monitor.setId(monitorVOMap.get(result.getSBBM()).getId()); |
| | | 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.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.setSerialNumber(result.getSerialNumber().getValue()); |
| | | monitor.setName(result.getName().getValue()); |
| | | String siteType = result.getJkdwlx().getValue(); |
| | | if (!StringUtils.isEmpty(siteType)) { |
| | | monitor.setSiteType(Long.valueOf(siteType)); |
| | | } |
| | | monitor.setMacAddr(result.getMacdz().getValue()); |
| | | monitor.setIp(result.getIp().getValue()); |
| | | monitor.setCameraFunType(result.getSxjgnlx().getValue()); |
| | | monitor.setLongitude(result.getLongitude().getValue()); |
| | | monitor.setLatitude(result.getLatitude().getValue() + ""); |
| | | monitor.setCameraCaptureArea(result.getSxjcjqy().getValue()); |
| | | String onState = result.getSbzt().getValue(); |
| | | if (!StringUtils.isEmpty(onState)) { |
| | | monitor.setOnState(Long.valueOf(onState)); |
| | | } |
| | | //国标码前八位为行政编码 |
| | | monitor.setCivilCode(result.getSBBM().substring(0, 8)); |
| | | String serialNumber = result.getSerialNumber().getValue(); |
| | | if (!StringUtils.isEmpty(serialNumber)) { |
| | | monitor.setCivilCode(serialNumber.substring(0, 8)); |
| | | } |
| | | return monitor; |
| | | } |
| | | } |