liujie
2026-07-24 7a201eec2a4284743386cc0c2d03eefa9e7cd1d7
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.ruoyi.system.service.impl;
 
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDoorDevice;
import com.ruoyi.system.domain.SysVisitArea;
import com.ruoyi.system.dto.SysDoorDeviceAddRequest;
import com.ruoyi.system.dto.SysDoorDeviceDeleteRequest;
import com.ruoyi.system.dto.SysDoorDeviceDetailRequest;
import com.ruoyi.system.dto.SysDoorDeviceEditRequest;
import com.ruoyi.system.dto.SysDoorDeviceLogoutRequest;
import com.ruoyi.system.dto.SysDoorDevicePageRequest;
import com.ruoyi.system.dto.SysDoorDeviceStatusRequest;
import com.ruoyi.system.mapper.SysDoorDeviceMapper;
import com.ruoyi.system.mapper.SysVisitAreaMapper;
import com.ruoyi.system.service.ISysDoorDeviceService;
import com.ruoyi.system.vo.SysDoorDeviceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.Date;
import java.util.List;
 
/**
 * 门禁设备服务实现
 *
 * @author ruoyi
 */
@Service
public class SysDoorDeviceServiceImpl implements ISysDoorDeviceService
{
    private static final String ONLINE_STATUS_UNACTIVATED = "UNACTIVATED";
 
    @Autowired
    private SysDoorDeviceMapper doorDeviceMapper;
 
    @Autowired
    private SysVisitAreaMapper visitAreaMapper;
 
    @Override
    public PageInfo<SysDoorDeviceVO> pageList(SysDoorDevicePageRequest request)
    {
        SysDoorDevicePageRequest query = request == null ? new SysDoorDevicePageRequest() : request;
        query.setDeviceName(StringUtils.trim(query.getDeviceName()));
        query.setDeviceId(StringUtils.trim(query.getDeviceId()));
        query.setOnlineStatus(StringUtils.trim(query.getOnlineStatus()));
        PageInfo<SysDoorDeviceVO> pageInfo = new PageInfo<SysDoorDeviceVO>(query.getPageNum(), query.getPageSize());
        List<SysDoorDeviceVO> list = doorDeviceMapper.pageList(query, pageInfo);
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public SysDoorDeviceVO detail(SysDoorDeviceDetailRequest request)
    {
        Long doorId = getRequiredDoorId(request == null ? null : request.getDoorId());
        SysDoorDeviceVO detail = doorDeviceMapper.selectVoById(doorId);
        if (detail == null)
        {
            throw new ServiceException("门禁设备不存在");
        }
        return detail;
    }
 
    @Override
    public int add(SysDoorDeviceAddRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("门禁设备内容不能为空");
        }
        SysDoorDevice doorDevice = buildDoorDevice(request.getAreaId(), request.getDeviceName(), request.getBrand(),
                request.getDeviceId(), request.getRemark(), request.getEnabled(), null);
        doorDevice.setOnlineStatus(ONLINE_STATUS_UNACTIVATED);
        doorDevice.setCreateBy(username);
        return doorDeviceMapper.insertDoorDevice(doorDevice);
    }
 
    @Override
    public int edit(SysDoorDeviceEditRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("门禁设备内容不能为空");
        }
        Long doorId = getRequiredDoorId(request.getDoorId());
        if (doorDeviceMapper.selectById(doorId) == null)
        {
            throw new ServiceException("门禁设备不存在");
        }
        SysDoorDevice doorDevice = buildDoorDevice(request.getAreaId(), request.getDeviceName(), request.getBrand(),
                request.getDeviceId(), request.getRemark(), request.getEnabled(), doorId);
        doorDevice.setDoorId(doorId);
        doorDevice.setUpdateBy(username);
        return doorDeviceMapper.updateDoorDevice(doorDevice);
    }
 
    @Override
    public int changeStatus(SysDoorDeviceStatusRequest request, String username)
    {
        List<Long> doorIds = getRequiredDoorIds(request == null ? null : request.getDoorIds());
        if (request.getEnabled() == null)
        {
            throw new ServiceException("启用状态不能为空");
        }
        return doorDeviceMapper.updateEnabled(doorIds, request.getEnabled(), username);
    }
 
    @Override
    public int logout(SysDoorDeviceLogoutRequest request, String username)
    {
        List<Long> doorIds = getRequiredDoorIds(request == null ? null : request.getDoorIds());
        return doorDeviceMapper.updateOnlineStatus(doorIds, ONLINE_STATUS_UNACTIVATED, username);
    }
 
    @Override
    public int delete(SysDoorDeviceDeleteRequest request, String username)
    {
        List<Long> doorIds = getRequiredDoorIds(request == null ? null : request.getDoorIds());
        return doorDeviceMapper.deleteByIds(doorIds, username);
    }
 
    private SysDoorDevice buildDoorDevice(Long areaId, String deviceName, String brand, String deviceId, String remark,
                                          Boolean enabled, Long excludeDoorId)
    {
        Long requiredAreaId = getRequiredAreaId(areaId);
        SysVisitArea area = visitAreaMapper.selectById(requiredAreaId);
        if (area == null)
        {
            throw new ServiceException("所属区域不存在");
        }
        String requiredDeviceName = getRequiredText(deviceName, "设备名称不能为空");
        String requiredBrand = getRequiredText(brand, "品牌不能为空");
        String requiredDeviceId = getRequiredText(deviceId, "设备ID不能为空");
        if (doorDeviceMapper.checkDeviceIdUnique(requiredDeviceId, excludeDoorId) != null)
        {
            throw new ServiceException("设备ID已存在");
        }
 
        SysDoorDevice doorDevice = new SysDoorDevice();
        doorDevice.setAreaId(requiredAreaId);
        doorDevice.setDeviceName(requiredDeviceName);
        doorDevice.setBrand(requiredBrand);
        doorDevice.setDeviceId(requiredDeviceId);
        doorDevice.setRemark(StringUtils.trim(remark));
        doorDevice.setEnabled(enabled == null ? Boolean.TRUE : enabled);
        return doorDevice;
    }
 
    private Long getRequiredAreaId(Long areaId)
    {
        if (areaId == null || areaId <= 0)
        {
            throw new ServiceException("所属区域不能为空");
        }
        return areaId;
    }
 
    private Long getRequiredDoorId(Long doorId)
    {
        if (doorId == null || doorId <= 0)
        {
            throw new ServiceException("门禁设备主键不能为空");
        }
        return doorId;
    }
 
    private List<Long> getRequiredDoorIds(List<Long> doorIds)
    {
        if (CollectionUtils.isEmpty(doorIds))
        {
            throw new ServiceException("门禁设备主键集合不能为空");
        }
        for (Long doorId : doorIds)
        {
            getRequiredDoorId(doorId);
        }
        return doorIds;
    }
 
    private String getRequiredText(String value, String message)
    {
        String result = StringUtils.trim(value);
        if (StringUtils.isEmpty(result))
        {
            throw new ServiceException(message);
        }
        return result;
    }
}