diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java index 1bd66a11..61a33fba 100755 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/DeviceChannelServiceImpl.java @@ -292,21 +292,31 @@ public class DeviceChannelServiceImpl implements IDeviceChannelService { // 存储数据,方便对数据去重 List channels = new ArrayList<>(); - // 存储需要更新的数据 + // 存储需要更新的国标通道 List updateChannelsForInfo = new ArrayList<>(); + // 存储需要更新的通用通道 List updateCommonChannelsForInfo = new ArrayList<>(); - // 存储需要需要新增的数据库 + // 存储需要更新的分组 + List updateGroupForInfo = new ArrayList<>(); + // 存储需要更新的行政区划 + List updateRegionForInfo = new ArrayList<>(); + // 存储需要需要新增的国标通道 List addChannels = new ArrayList<>(); + // 存储需要需要新增的通用通道 List addCommonChannels = new ArrayList<>(); + // 存储需要需要新增的分组 + List addGroups = new ArrayList<>(); + // 存储需要需要新增的行政区划 + List addRegions = new ArrayList<>(); Map subContMap = new HashMap<>(); - // 存储得到的10到13位为215的业务分组数据 - Map businessGroupMap = new HashMap<>(); - // 存储得到的10到13位为216的虚拟组织 数据 - Map virtuallyGroupMap = new HashMap<>(); - // 存储得到的行政区划数据 - Map regionMap = new HashMap<>(); + // 存储得到的10到13位为215的业务分组数据, 默认加载数据库中的所有业务分组 + Map businessGroupMap = groupMapper.queryTopGroupForMap(); + // 存储得到的10到13位为216的虚拟组织 数据, 默认加载数据库中的所有虚拟组织 + Map virtuallyGroupMap = groupMapper.queryNotTopGroupForMap(); + // 存储得到的行政区划数据, 默认加载数据库中的所有行政区划 + Map regionMap = regionMapper.getAllForMap(); // 存储得到的所有行政区划, 后续检验civilCode是否已传输对应的行政区划数据,从而确定是否需要自动创建节点。 Set civilCodeSet = new HashSet<>(); List clearChannels = new ArrayList<>(); @@ -319,6 +329,8 @@ public class DeviceChannelServiceImpl implements IDeviceChannelService { continue; } gbIdSet.add(deviceChannel.getChannelId()); + Gb28181CodeType channelIdType = SipUtils.getChannelIdType(deviceChannel.getChannelId()); + // 处理国标通道相关的判断 if (allChannelMap.containsKey(deviceChannel.getChannelId())) { DeviceChannel channelInDb = allChannelMap.get(deviceChannel.getChannelId()); deviceChannel.setId(channelInDb.getId()); @@ -328,56 +340,91 @@ public class DeviceChannelServiceImpl implements IDeviceChannelService { deviceChannel.setUpdateTime(DateUtil.getNow()); // 同步时发现状态变化 updateChannelsForInfo.add(deviceChannel); - updateCommonChannelsForInfo.add(CommonGbChannel.getInstance(null, deviceChannel)); + if (channelIdType == null) { + updateCommonChannelsForInfo.add(CommonGbChannel.getInstance(null, deviceChannel)); + } // 将需要更新的移除,剩下的都是需要删除的了 allChannelMap.remove(deviceChannel.getChannelId()); }else { deviceChannel.setCreateTime(DateUtil.getNow()); deviceChannel.setUpdateTime(DateUtil.getNow()); addChannels.add(deviceChannel); + if (channelIdType == null) { + addCommonChannels.add(CommonGbChannel.getInstance(null, deviceChannel)); + } + } - Gb28181CodeType channelIdType = SipUtils.getChannelIdType(deviceChannel.getChannelId()); - if (channelIdType != null) { - if ( - ( - channelIdType == Gb28181CodeType.CIVIL_CODE_PROVINCE - || channelIdType == Gb28181CodeType.CIVIL_CODE_CITY - || channelIdType == Gb28181CodeType.CIVIL_CODE_COUNTY - || channelIdType == Gb28181CodeType.CIVIL_CODE_GRASS_ROOTS - ) - && - !regionMap.containsKey(deviceChannel.getChannelId()) - ) { + + if (channelIdType != null) { + if (isRegion(channelIdType)) { + Region region; + if (!regionMap.containsKey(deviceChannel.getChannelId())) { + // 区域不存在记录并新增 CivilCodePo parentCivilCodePo = civilCodeFileConf.getParentCode(deviceChannel.getChannelId()); String civilCode = null; if (parentCivilCodePo != null) { civilCode = parentCivilCodePo.getCode(); } // 行政区划条目 - Region region = Region.getInstance(deviceChannel.getChannelId(), deviceChannel.getName(), + region = Region.getInstance(deviceChannel.getChannelId(), deviceChannel.getName(), civilCode); - regionMap.put(deviceChannel.getChannelId(), region); + addRegions.add(region); + }else { + // 区域存在记录并检查是否需要更新 + region = regionMap.get(deviceChannel.getChannelId()); + if (region.getCommonRegionName().equals(deviceChannel.getName())) { + // 对于行政区划,父节点是不会变化的,所以只需要判断名称变化,执行更新就可以 + region.setCommonRegionName(deviceChannel.getName()); + updateRegionForInfo.add(region); + } } - if (channelIdType == Gb28181CodeType.BUSINESS_GROUP - && !businessGroupMap.containsKey(deviceChannel.getChannelId())) { - Group group = Group.getInstance(deviceChannel.getChannelId(), deviceChannel.getName(), + regionMap.put(region.getCommonRegionDeviceId(), region); + + }else if (channelIdType == Gb28181CodeType.BUSINESS_GROUP) { + Group group; + if (!businessGroupMap.containsKey(deviceChannel.getChannelId())) { + group = Group.getInstance(deviceChannel.getChannelId(), deviceChannel.getName(), null, deviceChannel.getChannelId()); businessGroupMap.put(deviceChannel.getChannelId(), group); + }else { + // 对于业务分组,因为它本身即使顶级节点,所以不能父节点变化,所以只需要考虑名称变化的情况 + group = businessGroupMap.get(deviceChannel.getChannelId()); + if (!group.getCommonGroupName().equals(deviceChannel.getName())) { + group.setCommonGroupName(deviceChannel.getName()); + } + updateGroupForInfo.add(group); } - if (channelIdType == Gb28181CodeType.VIRTUAL_ORGANIZATION - && !virtuallyGroupMap.containsKey(deviceChannel.getChannelId())) { - Group group = Group.getInstance(deviceChannel.getChannelId(), deviceChannel.getName(), deviceChannel.getParentId(), deviceChannel.getBusinessGroupId()); - virtuallyGroupMap.put(deviceChannel.getChannelId(), group); - } - }else { - if (!StringUtils.isEmpty(deviceChannel.getCivilCode())) { - civilCodeSet.add(deviceChannel.getCivilCode()); - } - addCommonChannels.add(CommonGbChannel.getInstance(null, deviceChannel)); - } + businessGroupMap.put(group.getCommonGroupDeviceId(), group); + }else if (channelIdType == Gb28181CodeType.VIRTUAL_ORGANIZATION) { + Group group; + if (!virtuallyGroupMap.containsKey(deviceChannel.getChannelId())) { + group = Group.getInstance(deviceChannel.getChannelId(), deviceChannel.getName(), deviceChannel.getParentId(), deviceChannel.getBusinessGroupId()); + virtuallyGroupMap.put(deviceChannel.getChannelId(), group); + }else { + // 对于虚拟组织的变化,需要考虑三点, 名称, 顶级父节点(所属业务分组)或者 父节点 + group = virtuallyGroupMap.get(deviceChannel.getChannelId()); + if (!group.getCommonGroupName().equals(device.getName()) + || !group.getCommonGroupParentId().equals(deviceChannel.getParentId()) + || !group.getCommonGroupTopId().equals(deviceChannel.getBusinessGroupId()) + ) { + group.setCommonGroupName(deviceChannel.getName()); + group.setCommonGroupTopId(deviceChannel.getBusinessGroupId()); + group.setCommonGroupParentId(deviceChannel.getParentId()); + updateGroupForInfo.add(group); + } + } + virtuallyGroupMap.put(group.getCommonGroupDeviceId(), group); + } + }else { + if (!StringUtils.isEmpty(deviceChannel.getCivilCode())) { + civilCodeSet.add(deviceChannel.getCivilCode()); + } + addCommonChannels.add(CommonGbChannel.getInstance(null, deviceChannel)); } + channels.add(deviceChannel); + // 统计每个节点的子节点个数 if (!ObjectUtils.isEmpty(deviceChannel.getParentId())) { if (subContMap.get(deviceChannel.getParentId()) == null) { subContMap.put(deviceChannel.getParentId(), 1); @@ -422,16 +469,29 @@ public class DeviceChannelServiceImpl implements IDeviceChannelService { } } } - // 检测行政区划信息是否完整 + // 对于只发送了行政区划编号,没有发送行政区划的情况进行兼容,自动为通道创建一个行政区划。 for (String civilCode : civilCodeSet) { if (!regionMap.containsKey(civilCode)) { logger.warn("[通道信息中缺少地区信息]补充地区信息 civilCode: {}", civilCode ); - Region region = civilCodeFileConf.createRegion(civilCode); - if (region != null) { - regionMap.put(region.getCommonRegionDeviceId(), region); - }else { - logger.warn("[获取地区信息]失败 civilCode: {}", civilCode ); + if (civilCode.length() == 8) { + Region region = civilCodeFileConf.createRegion(civilCode.substring(0, 6)); + if (region != null) { + Region.getInstance(civilCode, region.getCommonRegionName() + "的基层组织", region.getCommonRegionDeviceId()); + regionMap.put(region.getCommonRegionDeviceId(), region); + }else { + logger.warn("[获取地区信息]失败 civilCode: {}", civilCode ); + } + }else if (civilCode.length() <= 6) { + Region region = civilCodeFileConf.createRegion(civilCode); + if (region != null) { + regionMap.put(region.getCommonRegionDeviceId(), region); + }else { + logger.warn("[获取地区信息]失败 civilCode: {}", civilCode ); + } } + + + } } // 对待写入的数据做处理 @@ -590,4 +650,11 @@ public class DeviceChannelServiceImpl implements IDeviceChannelService { return false; } } + + private boolean isRegion(Gb28181CodeType channelIdType) { + return channelIdType == Gb28181CodeType.CIVIL_CODE_PROVINCE + || channelIdType == Gb28181CodeType.CIVIL_CODE_CITY + || channelIdType == Gb28181CodeType.CIVIL_CODE_COUNTY + || channelIdType == Gb28181CodeType.CIVIL_CODE_GRASS_ROOTS; + } } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/GroupMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/GroupMapper.java index 6afb7407..13038b6e 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/dao/GroupMapper.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/GroupMapper.java @@ -5,6 +5,7 @@ import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Map; @Mapper @Repository @@ -141,4 +142,12 @@ public interface GroupMapper { " #{item.commonGroupId}" + "") void removeGroupByList(@Param("groups") List groups); + + @MapKey("commonGroupDeviceId") + @Select("select * from wvp_common_group where common_group_device_id = common_group_top_id") + Map queryTopGroupForMap(); + + @MapKey("commonGroupDeviceId") + @Select("select * from wvp_common_group where common_group_device_id != common_group_top_id") + Map queryNotTopGroupForMap(); } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/RegionMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/RegionMapper.java index 3cf4938b..8df76e38 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/dao/RegionMapper.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/RegionMapper.java @@ -4,6 +4,7 @@ import com.genersoft.iot.vmp.service.bean.Region; import org.apache.ibatis.annotations.*; import java.util.List; +import java.util.Map; @Mapper public interface RegionMapper { @@ -119,4 +120,8 @@ public interface RegionMapper { @Select("select * from wvp_common_region where common_region_device_id = #{regionDeviceId}") Region queryRegionByDeviceId(@Param("regionDeviceId") String regionDeviceId); + + @MapKey("commonRegionDeviceId") + @Select("select * from wvp_common_region") + Map getAllForMap(); }