| | |
| | | } |
| | | @Override |
| | | public R addElectronic(ElectronicFenceAddDto dto) { |
| | | try { |
| | | // 获取现有的运营区域(areaType=1) |
| | | LambdaQueryWrapper<XpElectronicFenceArea> xpElectronicFenceAreaLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | xpElectronicFenceAreaLambdaQueryWrapper.eq(XpElectronicFenceArea::getAreaType, 1); |
| | | List<XpElectronicFenceArea> existingOperationAreas = xpElectronicFenceAreaMapper.selectList(xpElectronicFenceAreaLambdaQueryWrapper); |
| | | // 获取现有的运营区域(areaType=1) |
| | | LambdaQueryWrapper<XpElectronicFenceArea> xpElectronicFenceAreaLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | xpElectronicFenceAreaLambdaQueryWrapper.eq(XpElectronicFenceArea::getAreaType, 1); |
| | | List<XpElectronicFenceArea> existingOperationAreas = xpElectronicFenceAreaMapper.selectList(xpElectronicFenceAreaLambdaQueryWrapper); |
| | | |
| | | // 获取待添加的新区域 |
| | | List<XpElectronicFenceArea> newElectronicFenceAreas = dto.getElectronicFenceAreas(); |
| | | // 获取待添加的新区域 |
| | | List<XpElectronicFenceArea> newElectronicFenceAreas = dto.getElectronicFenceAreas(); |
| | | |
| | | |
| | | // 检查新区域与现有运营区域是否有重叠 |
| | | for (XpElectronicFenceArea newArea : newElectronicFenceAreas) { |
| | | if (newArea.getAreaType() == 1) { // 只检查运营区域 |
| | | for (XpElectronicFenceArea existingArea : existingOperationAreas) { |
| | | if (polygonOverlapService.checkPolygonOverlap( |
| | | newArea.getFencePoints(), |
| | | existingArea.getFencePoints())) { |
| | | throw new ServiceException( |
| | | "运营区域{}与现有运营区域{}发生重叠", |
| | | I18nKeyConstant.EDIT_ELECTRONIC_ERROR, |
| | | new Object[]{existingArea.getAreaName(), existingArea.getAreaName()} |
| | | ); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 保存主表数据 |
| | | XpElectronicFence xpElectronicFence = new XpElectronicFence(); |
| | | BeanUtils.copyProperties(dto, xpElectronicFence); |
| | | xpElectronicFence.setStatus(0); |
| | | |
| | | if (dto.getStatus() == 0) { |
| | | xpElectronicFence.setStatus(0); |
| | | } else if (dto.getStatus() == 1) { |
| | | if (dto.getEffectType() == 1) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | xpElectronicFence.setEffectTime(LocalDateTime.now()); |
| | | } else { |
| | | if (dto.getEffectTime().isBefore(LocalDateTime.now())) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | } else { |
| | | xpElectronicFence.setStatus(1); |
| | | } |
| | | } |
| | | } |
| | | xpElectronicFence.setCityCode(dto.getCityCode()); |
| | | // 生成围栏id 规则:城市首字母-类型简写-4位自增流水号(如 SZ-OP-001),支持复制;数据停用或删除后,流水号不回收 |
| | | XpRegion xpRegion = xpRegionMapper.selectOne(new LambdaQueryWrapper<XpRegion>() |
| | | .eq(XpRegion::getCode, dto.getCityCode())); |
| | | String name = xpRegion.getName(); |
| | | this.save(xpElectronicFence); |
| | | // 根据城市中文名,截取前两位的首字母并且大写 |
| | | // 截取前两个字符 |
| | | String prefix = name.substring(0, 2); |
| | | // 获取拼音首字母并大写 |
| | | String result = getPinyinInitials(prefix).toUpperCase(); |
| | | xpElectronicFence.setElectronicNumber(result + "-OP-" + String.format("%04d", xpElectronicFence.getId())); |
| | | if (dto.getStatus() == 0) { |
| | | xpElectronicFence.setStatus(0); |
| | | } else if (dto.getStatus() == 1) { |
| | | if (dto.getEffectType() == 1) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | xpElectronicFence.setEffectTime(LocalDateTime.now()); |
| | | } else { |
| | | if (dto.getEffectTime().isBefore(LocalDateTime.now())) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | } else { |
| | | // 添加定时任务job type=1电子围栏生效 type=2电子围栏失效 |
| | | Map<String, ? extends Object> maps = |
| | | new ImmutableMap.Builder<String, Long>(). |
| | | put("type", 1L) |
| | | .put("id", xpElectronicFence.getId()) |
| | | .build(); |
| | | QuartzManager.addJob( |
| | | ElectronicEffectJob.class, |
| | | (ElectronicEffectJob.effect + xpElectronicFence.getId()).toUpperCase(), |
| | | TimeJobType.ELECTRONIC_EFFECT, |
| | | DateUtils.localDateTimeToDate(dto.getEffectTime()), |
| | | maps |
| | | // 检查新区域与现有运营区域是否有重叠 |
| | | for (XpElectronicFenceArea newArea : newElectronicFenceAreas) { |
| | | if (newArea.getAreaType() == 1) { // 只检查运营区域 |
| | | for (XpElectronicFenceArea existingArea : existingOperationAreas) { |
| | | if (polygonOverlapService.checkPolygonOverlap( |
| | | newArea.getFencePoints(), |
| | | existingArea.getFencePoints())) { |
| | | throw new ServiceException( |
| | | "运营区域{}与现有运营区域{}发生重叠", |
| | | I18nKeyConstant.EDIT_ELECTRONIC_ERROR, |
| | | new Object[]{existingArea.getAreaName(), existingArea.getAreaName()} |
| | | ); |
| | | xpElectronicFence.setStatus(1); |
| | | } |
| | | } |
| | | if (dto.getFailureType() != null && dto.getFailureType() == 2) { |
| | | |
| | | if (dto.getFailureTime().isBefore(LocalDateTime.now())) { |
| | | // 设置待启用 |
| | | xpElectronicFence.setStatus(1); |
| | | } else { |
| | | // 添加定时任务job type=1电子围栏生效 type=2电子围栏失效 |
| | | Map<String, ? extends Object> maps = |
| | | new ImmutableMap.Builder<String, Long>(). |
| | | put("type", 2L) |
| | | .put("id", xpElectronicFence.getId()) |
| | | .build(); |
| | | QuartzManager.addJob( |
| | | ElectronicEffectJob.class, |
| | | (ElectronicEffectJob.failure + xpElectronicFence.getId()).toUpperCase(), |
| | | TimeJobType.ELECTRONIC_FAILURE, |
| | | DateUtils.localDateTimeToDate(dto.getEffectTime()), |
| | | maps |
| | | ); |
| | | xpElectronicFence.setStatus(1); |
| | | } |
| | | } |
| | | } |
| | | this.updateById(xpElectronicFence); |
| | | if (xpElectronicFence.getStatus() == 2) { |
| | | // 设置电子围栏ID并保存区域数据 |
| | | for (XpElectronicFenceArea area : newElectronicFenceAreas) { |
| | | area.setElectronicId(xpElectronicFence.getId()); |
| | | area.setAreaStatus(1); |
| | | area.setArea(area.getArea()); |
| | | area.setCityCode(dto.getCityCode()); |
| | | xpElectronicFenceAreaMapper.insert(area); |
| | | } |
| | | |
| | | }else{ |
| | | // 设置电子围栏ID并保存区域数据 |
| | | for (XpElectronicFenceArea area : newElectronicFenceAreas) { |
| | | area.setElectronicId(xpElectronicFence.getId()); |
| | | area.setAreaStatus(2); |
| | | area.setArea(area.getArea()); |
| | | area.setCityCode(dto.getCityCode()); |
| | | xpElectronicFenceAreaMapper.insert(area); |
| | | |
| | | } |
| | | } |
| | | |
| | | if (xpElectronicFence.getStatus() == 2) { |
| | | for (XpElectronicFenceArea newElectronicFenceArea : newElectronicFenceAreas) { |
| | | if (newElectronicFenceArea.getAreaType() == 1 |
| | | && newElectronicFenceArea.getAreaStatus() == 1) { |
| | | XpGeoFencingPolygon xpGeoFencingPolygon = new XpGeoFencingPolygon(); |
| | | xpGeoFencingPolygon.setFenceId(String.valueOf(xpElectronicFence.getId())); |
| | | xpGeoFencingPolygon.setName(xpElectronicFence.getElectronicName()); |
| | | xpGeoFencingPolygon.setAreaId(String.valueOf(newElectronicFenceArea.getId())); |
| | | xpGeoFencingPolygon.setCityCode(xpElectronicFence.getCityCode()); |
| | | xpGeoFencingPolygon.setOperationStrategy(xpElectronicFence.getOperationStrategy()); |
| | | // 将围栏点转换为GeoJSON多边形 |
| | | GeoJsonPolygon geoJsonPolygon = convertToGeoJsonPolygon(newElectronicFenceArea.getFencePoints()); |
| | | xpGeoFencingPolygon.setPoints(geoJsonPolygon); |
| | | mongoTemplate.insert(xpGeoFencingPolygon); |
| | | } |
| | | if (newElectronicFenceArea.getAreaType() == 2 |
| | | && newElectronicFenceArea.getAreaStatus() == 1) { |
| | | XpGeoFencingProhibitedPolygon xpGeoFencingProhibitedPolygon = new XpGeoFencingProhibitedPolygon(); |
| | | xpGeoFencingProhibitedPolygon.setFenceId(String.valueOf(xpElectronicFence.getId())); |
| | | xpGeoFencingProhibitedPolygon.setName(xpElectronicFence.getElectronicName()); |
| | | xpGeoFencingProhibitedPolygon.setAreaId(String.valueOf(newElectronicFenceArea.getId())); |
| | | // 将围栏点转换为GeoJSON多边形 |
| | | GeoJsonPolygon geoJsonPolygon = convertToGeoJsonPolygon(newElectronicFenceArea.getFencePoints()); |
| | | xpGeoFencingProhibitedPolygon.setPoints(geoJsonPolygon); |
| | | mongoTemplate.save(xpGeoFencingProhibitedPolygon); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return R.ok(); |
| | | } catch (Exception e) { |
| | | log.error("新增电子围栏失败", e); |
| | | throw new ServiceException("新增电子围栏失败", I18nKeyConstant.ADD_ELECTRONIC_ERROR); |
| | | } |
| | | |
| | | // 保存主表数据 |
| | | XpElectronicFence xpElectronicFence = new XpElectronicFence(); |
| | | BeanUtils.copyProperties(dto, xpElectronicFence); |
| | | xpElectronicFence.setStatus(0); |
| | | |
| | | if (dto.getStatus() == 0) { |
| | | xpElectronicFence.setStatus(0); |
| | | } else if (dto.getStatus() == 1) { |
| | | if (dto.getEffectType() == 1) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | xpElectronicFence.setEffectTime(LocalDateTime.now()); |
| | | } else { |
| | | if (dto.getEffectTime().isBefore(LocalDateTime.now())) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | } else { |
| | | xpElectronicFence.setStatus(1); |
| | | } |
| | | } |
| | | } |
| | | xpElectronicFence.setCityCode(dto.getCityCode()); |
| | | // 生成围栏id 规则:城市首字母-类型简写-4位自增流水号(如 SZ-OP-001),支持复制;数据停用或删除后,流水号不回收 |
| | | XpRegion xpRegion = xpRegionMapper.selectOne(new LambdaQueryWrapper<XpRegion>() |
| | | .eq(XpRegion::getCode, dto.getCityCode())); |
| | | String name = xpRegion.getName(); |
| | | this.save(xpElectronicFence); |
| | | // 根据城市中文名,截取前两位的首字母并且大写 |
| | | // 截取前两个字符 |
| | | String prefix = name.substring(0, 2); |
| | | // 获取拼音首字母并大写 |
| | | String result = getPinyinInitials(prefix).toUpperCase(); |
| | | xpElectronicFence.setElectronicNumber(result + "-OP-" + String.format("%04d", xpElectronicFence.getId())); |
| | | if (dto.getStatus() == 0) { |
| | | xpElectronicFence.setStatus(0); |
| | | } else if (dto.getStatus() == 1) { |
| | | if (dto.getEffectType() == 1) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | xpElectronicFence.setEffectTime(LocalDateTime.now()); |
| | | } else { |
| | | if (dto.getEffectTime().isBefore(LocalDateTime.now())) { |
| | | // 设置为启用中 |
| | | xpElectronicFence.setStatus(2); |
| | | } else { |
| | | // 添加定时任务job type=1电子围栏生效 type=2电子围栏失效 |
| | | Map<String, ? extends Object> maps = |
| | | new ImmutableMap.Builder<String, Long>(). |
| | | put("type", 1L) |
| | | .put("id", xpElectronicFence.getId()) |
| | | .build(); |
| | | QuartzManager.addJob( |
| | | ElectronicEffectJob.class, |
| | | (ElectronicEffectJob.effect + xpElectronicFence.getId()).toUpperCase(), |
| | | TimeJobType.ELECTRONIC_EFFECT, |
| | | DateUtils.localDateTimeToDate(dto.getEffectTime()), |
| | | maps |
| | | ); |
| | | xpElectronicFence.setStatus(1); |
| | | } |
| | | } |
| | | if (dto.getFailureType() != null && dto.getFailureType() == 2) { |
| | | |
| | | if (dto.getFailureTime().isBefore(LocalDateTime.now())) { |
| | | // 设置待启用 |
| | | xpElectronicFence.setStatus(1); |
| | | } else { |
| | | // 添加定时任务job type=1电子围栏生效 type=2电子围栏失效 |
| | | Map<String, ? extends Object> maps = |
| | | new ImmutableMap.Builder<String, Long>(). |
| | | put("type", 2L) |
| | | .put("id", xpElectronicFence.getId()) |
| | | .build(); |
| | | QuartzManager.addJob( |
| | | ElectronicEffectJob.class, |
| | | (ElectronicEffectJob.failure + xpElectronicFence.getId()).toUpperCase(), |
| | | TimeJobType.ELECTRONIC_FAILURE, |
| | | DateUtils.localDateTimeToDate(dto.getEffectTime()), |
| | | maps |
| | | ); |
| | | xpElectronicFence.setStatus(1); |
| | | } |
| | | } |
| | | } |
| | | this.updateById(xpElectronicFence); |
| | | if (xpElectronicFence.getStatus() == 2) { |
| | | // 设置电子围栏ID并保存区域数据 |
| | | for (XpElectronicFenceArea area : newElectronicFenceAreas) { |
| | | area.setElectronicId(xpElectronicFence.getId()); |
| | | area.setAreaStatus(1); |
| | | area.setArea(area.getArea()); |
| | | area.setCityCode(dto.getCityCode()); |
| | | xpElectronicFenceAreaMapper.insert(area); |
| | | } |
| | | |
| | | }else{ |
| | | // 设置电子围栏ID并保存区域数据 |
| | | for (XpElectronicFenceArea area : newElectronicFenceAreas) { |
| | | area.setElectronicId(xpElectronicFence.getId()); |
| | | area.setAreaStatus(2); |
| | | area.setArea(area.getArea()); |
| | | area.setCityCode(dto.getCityCode()); |
| | | xpElectronicFenceAreaMapper.insert(area); |
| | | |
| | | } |
| | | } |
| | | |
| | | if (xpElectronicFence.getStatus() == 2) { |
| | | for (XpElectronicFenceArea newElectronicFenceArea : newElectronicFenceAreas) { |
| | | if (newElectronicFenceArea.getAreaType() == 1 |
| | | && newElectronicFenceArea.getAreaStatus() == 1) { |
| | | XpGeoFencingPolygon xpGeoFencingPolygon = new XpGeoFencingPolygon(); |
| | | xpGeoFencingPolygon.setFenceId(String.valueOf(xpElectronicFence.getId())); |
| | | xpGeoFencingPolygon.setName(xpElectronicFence.getElectronicName()); |
| | | xpGeoFencingPolygon.setAreaId(String.valueOf(newElectronicFenceArea.getId())); |
| | | xpGeoFencingPolygon.setCityCode(xpElectronicFence.getCityCode()); |
| | | xpGeoFencingPolygon.setOperationStrategy(xpElectronicFence.getOperationStrategy()); |
| | | // 将围栏点转换为GeoJSON多边形 |
| | | GeoJsonPolygon geoJsonPolygon = convertToGeoJsonPolygon(newElectronicFenceArea.getFencePoints()); |
| | | xpGeoFencingPolygon.setPoints(geoJsonPolygon); |
| | | mongoTemplate.insert(xpGeoFencingPolygon); |
| | | } |
| | | if (newElectronicFenceArea.getAreaType() == 2 |
| | | && newElectronicFenceArea.getAreaStatus() == 1) { |
| | | XpGeoFencingProhibitedPolygon xpGeoFencingProhibitedPolygon = new XpGeoFencingProhibitedPolygon(); |
| | | xpGeoFencingProhibitedPolygon.setFenceId(String.valueOf(xpElectronicFence.getId())); |
| | | xpGeoFencingProhibitedPolygon.setName(xpElectronicFence.getElectronicName()); |
| | | xpGeoFencingProhibitedPolygon.setAreaId(String.valueOf(newElectronicFenceArea.getId())); |
| | | // 将围栏点转换为GeoJSON多边形 |
| | | GeoJsonPolygon geoJsonPolygon = convertToGeoJsonPolygon(newElectronicFenceArea.getFencePoints()); |
| | | xpGeoFencingProhibitedPolygon.setPoints(geoJsonPolygon); |
| | | mongoTemplate.save(xpGeoFencingProhibitedPolygon); |
| | | } |
| | | } |
| | | } |
| | | |
| | | return R.ok(); |
| | | } |
| | | |
| | | @Override |