修改设备表 索引定义 与 添加设备 逻辑

一个下级平台 有多个设备, 每个设备为一个通道
This commit is contained in:
shikong 2023-09-11 10:17:58 +08:00
parent 84e0addf53
commit ffac3458fd
2 changed files with 24 additions and 17 deletions

View File

@ -4,9 +4,8 @@
<mapper namespace="cn.skcks.docking.gb28181.wvp.orm.mybatis.operation.WvpProxyOperateTableMapper"> <mapper namespace="cn.skcks.docking.gb28181.wvp.orm.mybatis.operation.WvpProxyOperateTableMapper">
<update id="createDeviceTable"> <update id="createDeviceTable">
CREATE TABLE IF NOT EXISTS `wvp_proxy_device` CREATE TABLE `wvp_proxy_device` (
( `id` bigint(20) NOT NULL AUTO_INCREMENT,
`id` bigint NOT NULL AUTO_INCREMENT,
`device_code` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, `device_code` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`gb_device_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, `gb_device_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
`gb_device_channel_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL, `gb_device_channel_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
@ -14,10 +13,8 @@
`address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`), PRIMARY KEY (`id`),
UNIQUE KEY `gb_device_id` (`gb_device_id`), UNIQUE KEY `gb_device_id` (`gb_device_id`),
UNIQUE KEY `device_code` (`device_code`), UNIQUE KEY `device_code_2` (`device_code`,`gb_device_id`,`gb_device_channel_id`),
UNIQUE KEY `gb_device_id_2` (`gb_device_id`, `gb_device_channel_id`) KEY `device_code` (`device_code`) USING BTREE
) ENGINE = InnoDB ) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
DEFAULT CHARSET = utf8mb4
COLLATE = utf8mb4_unicode_ci;
</update> </update>
</mapper> </mapper>

View File

@ -40,6 +40,12 @@ public class DeviceService {
s.where(WvpProxyDeviceDynamicSqlSupport.gbDeviceId,isEqualTo(gbDeviceId))); s.where(WvpProxyDeviceDynamicSqlSupport.gbDeviceId,isEqualTo(gbDeviceId)));
} }
public Optional<WvpProxyDevice> getDeviceByGbDeviceIdAndChannel(String gbDeviceId,String channel){
return deviceMapper.selectOne(s->
s.where(WvpProxyDeviceDynamicSqlSupport.gbDeviceId,isEqualTo(gbDeviceId))
.and(WvpProxyDeviceDynamicSqlSupport.gbDeviceChannelId,isEqualTo(channel)));
}
/** /**
* 添加设备 * 添加设备
* @param device 设备 * @param device 设备
@ -60,11 +66,15 @@ public class DeviceService {
} }
String gbDeviceId = device.getGbDeviceId(); String gbDeviceId = device.getGbDeviceId();
String channel = device.getGbDeviceChannelId();
if(StringUtils.isBlank(gbDeviceId)){ if(StringUtils.isBlank(gbDeviceId)){
throw new JsonException("国标编码不能为空"); throw new JsonException("国标编码不能为空");
} }
if(getDeviceByGbDeviceId(gbDeviceId).isPresent()){ if(StringUtils.isBlank(channel)){
throw new JsonException(MessageFormat.format("国标编码 {0} 已存在" ,gbDeviceId)); throw new JsonException("国标通道不能为空");
}
if(getDeviceByGbDeviceIdAndChannel(gbDeviceId,channel).isPresent()){
throw new JsonException(MessageFormat.format("国标编码 {0}, 通道 {1} 已存在" ,gbDeviceId, channel));
} }
return deviceMapper.insert(device) > 0; return deviceMapper.insert(device) > 0;