Browse Source

设备组设备重复添加问题

master
review512jwy@163.com 6 days ago
parent
commit
f304c41a78
  1. 3
      dongjian-dashboard-back-dao/src/main/java/com/dongjian/dashboard/back/dao/ex/DeviceGroupRelationMapperExt.java
  2. 12
      dongjian-dashboard-back-dao/src/main/resources/mappers/ex/DeviceGroupRelationMapperExt.xml
  3. 2
      dongjian-dashboard-back-model/src/main/java/com/dongjian/dashboard/back/dto/devicegroup/BindDeviceForGroupParams.java
  4. 20
      dongjian-dashboard-back-service/src/main/java/com/dongjian/dashboard/back/service/impl/DeviceGroupServiceImpl.java

3
dongjian-dashboard-back-dao/src/main/java/com/dongjian/dashboard/back/dao/ex/DeviceGroupRelationMapperExt.java

@ -3,7 +3,10 @@ package com.dongjian.dashboard.back.dao.ex;
import com.dongjian.dashboard.back.dao.auto.DeviceGroupRelationMapper;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeviceGroupRelationMapperExt extends DeviceGroupRelationMapper {
List<Integer> selectDeviceInfoIdsByGroupId(Long groupId);
}

12
dongjian-dashboard-back-dao/src/main/resources/mappers/ex/DeviceGroupRelationMapperExt.xml

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dongjian.dashboard.back.dao.ex.DeviceGroupRelationMapperExt">
<select id="selectDeviceInfoIdsByGroupId" resultType="int">
SELECT device_info_id
FROM dashboard_device_group_relation
WHERE device_group_id = #{deviceGroupId}
</select>
</mapper>

2
dongjian-dashboard-back-model/src/main/java/com/dongjian/dashboard/back/dto/devicegroup/BindDeviceForGroupParams.java

@ -7,7 +7,7 @@ import lombok.Data;
@Data
public class BindDeviceForGroupParams {
@Schema(description = "组ID",example = "27,587")
@Schema(description = "组ID",example = "587")
private Long deviceGroupId;
@Schema(description = "设备主键ID,多个使用半角字符逗号连接,注意不是设备ID")

20
dongjian-dashboard-back-service/src/main/java/com/dongjian/dashboard/back/service/impl/DeviceGroupServiceImpl.java

@ -358,9 +358,23 @@ public class DeviceGroupServiceImpl implements DeviceGroupService {
List<Long> deviceInfoIdList = CommonUtil.commaStr2LongList(bindDeviceForGroupParams.getDeviceInfoIds());
// 4. 插入新绑定关系
if (CollectionUtils.isNotEmpty(deviceInfoIdList)) {
for (Long deviceInfoId : deviceInfoIdList){
if (CollectionUtils.isEmpty(deviceInfoIdList)) {
return SimpleDataResponse.success();
}
Long groupId = bindDeviceForGroupParams.getDeviceGroupId();
List<Integer> existedIds = deviceGroupRelationMapperExt.selectDeviceInfoIdsByGroupId(groupId);
Set<Integer> existedSet = new HashSet<>(existedIds);
// 过滤掉已绑定的
List<Long> filteredList = deviceInfoIdList.stream()
.filter(id -> !existedSet.contains(id.intValue()))
.collect(Collectors.toList());
// 插入新绑定关系
if (CollectionUtils.isNotEmpty(filteredList)) {
for (Long deviceInfoId : filteredList){
DeviceGroupRelation deviceGroupRelation = new DeviceGroupRelation();
deviceGroupRelation.setDeviceGroupId(bindDeviceForGroupParams.getDeviceGroupId());
deviceGroupRelation.setDeviceInfoId(deviceInfoId.intValue());

Loading…
Cancel
Save