liujie
2026-08-11 323764c813a09cd56cdab780155ef659dbe0b364
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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.utils.AreaIdUtils;
import com.ruoyi.system.utils.DoorDeviceCredentialCrypto;
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;
import java.nio.charset.Charset;
 
/**
 * 门禁设备服务实现
 *
 * @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.setAreaId(StringUtils.trim(query.getAreaId()));
        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,
                AreaIdUtils.parseLocalAreaId(query.getAreaId()), 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);
        fillConnectionSettings(doorDevice, request.getDevicePort(), request.getLoginUsername(),
                request.getLoginPassword(), request.getDoorNo(), request.getTimeSectionNo(), request.getCharset(),
                null, true);
        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());
        SysDoorDevice existing = doorDeviceMapper.selectById(doorId);
        if (existing == null)
        {
            throw new ServiceException("门禁设备不存在");
        }
        SysDoorDevice doorDevice = buildDoorDevice(request.getAreaId(), request.getDeviceName(), request.getBrand(),
                request.getDeviceId(), request.getRemark(), request.getEnabled(), doorId);
        fillConnectionSettings(doorDevice, request.getDevicePort(), request.getLoginUsername(),
                request.getLoginPassword(), request.getDoorNo(), request.getTimeSectionNo(), request.getCharset(),
                existing.getLoginPasswordCipher(), StringUtils.isEmpty(existing.getLoginPasswordCipher()));
        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 void fillConnectionSettings(SysDoorDevice doorDevice, Integer devicePort, String loginUsername,
                                        String loginPassword, Integer doorNo, Integer timeSectionNo, String charset,
                                        String existingPasswordCipher, boolean requirePassword)
    {
        int resolvedPort = devicePort == null ? 37777 : devicePort;
        if (resolvedPort <= 0 || resolvedPort > 65535)
        {
            throw new ServiceException("设备端口必须在1到65535之间");
        }
        int resolvedDoorNo = doorNo == null ? 0 : doorNo;
        if (resolvedDoorNo < 0)
        {
            throw new ServiceException("门通道序号不能小于0");
        }
        int resolvedTimeSectionNo = timeSectionNo == null ? 255 : timeSectionNo;
        if (resolvedTimeSectionNo < 0 || resolvedTimeSectionNo > 255)
        {
            throw new ServiceException("时间模板序号必须在0到255之间");
        }
        String resolvedCharset = StringUtils.isEmpty(StringUtils.trim(charset)) ? "GBK" : StringUtils.trim(charset);
        try
        {
            Charset.forName(resolvedCharset);
        }
        catch (Exception e)
        {
            throw new ServiceException("设备姓名编码无效");
        }
 
        String password = StringUtils.trim(loginPassword);
        if (requirePassword && StringUtils.isEmpty(password))
        {
            throw new ServiceException("设备登录密码不能为空");
        }
        doorDevice.setDevicePort(resolvedPort);
        doorDevice.setLoginUsername(StringUtils.isEmpty(StringUtils.trim(loginUsername))
                ? "admin" : StringUtils.trim(loginUsername));
        doorDevice.setLoginPasswordCipher(StringUtils.isEmpty(password)
                ? existingPasswordCipher : DoorDeviceCredentialCrypto.encrypt(password));
        doorDevice.setDoorNo(resolvedDoorNo);
        doorDevice.setTimeSectionNo(resolvedTimeSectionNo);
        doorDevice.setCharset(resolvedCharset);
    }
 
    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;
    }
}