huang
2025-11-18 1566e4c7604d85737ea67fe6757e71b8185fa48e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.mes.device.mapper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mes.device.entity.DeviceConfig;
import com.mes.device.entity.DeviceGroupRelation;
import com.mes.device.vo.DeviceGroupVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
 
import java.util.List;
 
/**
 * 设备组关系Mapper接口
 * 
 * @author mes
 * @since 2024-10-30
 */
@Mapper
public interface DeviceGroupRelationMapper extends BaseMapper<DeviceGroupRelation> {
 
    /**
     * 批量添加设备到设备组
     * 
     * @param groupId 设备组ID
     * @param deviceIds 设备ID列表
     * @param deviceRole 设备角色
     */
    void batchAddDevicesToGroup(@Param("groupId") Long groupId, 
                               @Param("deviceIds") List<Long> deviceIds, 
                               @Param("deviceRole") String deviceRole);
 
    /**
     * 批量从设备组移除设备
     * 
     * @param groupId 设备组ID
     * @param deviceIds 设备ID列表
     */
    void batchRemoveDevicesFromGroup(@Param("groupId") Long groupId, 
                                    @Param("deviceIds") List<Long> deviceIds);
 
    /**
     * 更新设备在设备组中的角色
     * 
     * @param groupId 设备组ID
     * @param deviceId 设备ID
     * @param deviceRole 设备角色
     */
    void updateDeviceRole(@Param("groupId") Long groupId, 
                         @Param("deviceId") Long deviceId, 
                         @Param("deviceRole") String deviceRole);
 
    /**
     * 获取设备组下的设备列表(联表查询)
     * 
     * @param groupId 设备组ID
     * @return 设备信息列表
     */
    @Select("SELECT d.id, d.device_name as deviceName, d.device_code as deviceCode, " +
            "d.device_type as deviceType, dgr.role, d.status, " +
            "d.last_heartbeat as lastHeartbeat, d.is_online as isOnline " +
            "FROM device_config d " +
            "INNER JOIN device_group_relation dgr ON d.id = dgr.device_id " +
            "WHERE dgr.group_id = #{groupId} AND dgr.is_deleted = 0 AND d.is_deleted = 0")
    List<DeviceGroupVO.DeviceInfo> getGroupDevices(@Param("groupId") Long groupId);
 
    /**
     * 获取设备所属的设备组列表(联表查询)
     * 
     * @param deviceId 设备ID
     * @return 设备组信息列表
     */
    @Select("SELECT dgc.id, dgc.group_code as groupCode, dgc.group_name as groupName, " +
            "dgc.group_type as groupType, dgc.status, dgr.created_time as createTime, "+
            "dgc.project_id as projectId, " +
            "(SELECT COUNT(*) FROM device_group_relation WHERE group_id = dgc.id AND is_deleted = 0) as deviceCount " +
            "FROM device_group_config dgc " +
            "INNER JOIN device_group_relation dgr ON dgc.id = dgr.group_id " +
            "WHERE dgr.device_id = #{deviceId} AND dgr.is_deleted = 0 AND dgc.is_deleted = 0")
    List<DeviceGroupVO.GroupInfo> getDeviceGroups(@Param("deviceId") Long deviceId);
 
    /**
     * 获取按连接顺序排序的设备配置列表
     *
     * @param groupId 设备组ID
     * @return 设备配置集合
     */
    @Select("SELECT d.* " +
            "FROM device_group_relation dgr " +
            "INNER JOIN device_config d ON dgr.device_id = d.id " +
            "WHERE dgr.group_id = #{groupId} " +
            "  AND dgr.is_deleted = 0 " +
            "  AND d.is_deleted = 0 " +
            "ORDER BY IFNULL(dgr.connection_order, 0) ASC, dgr.id ASC")
    List<DeviceConfig> getOrderedDeviceConfigs(@Param("groupId") Long groupId);
}