zjk
2026-07-31 dc2872d6d346a7de629b3b0686057ab54f216f09
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
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.domain.SysVisitorMachine;
import com.ruoyi.system.dto.SysVisitorMachineAddRequest;
import com.ruoyi.system.dto.SysVisitorMachineDeleteRequest;
import com.ruoyi.system.dto.SysVisitorMachineDetailRequest;
import com.ruoyi.system.dto.SysVisitorMachineEditRequest;
import com.ruoyi.system.dto.SysVisitorMachineLogoutRequest;
import com.ruoyi.system.dto.SysVisitorMachinePageRequest;
import com.ruoyi.system.dto.SysVisitorMachineStatusRequest;
import com.ruoyi.system.mapper.SysVisitAreaMapper;
import com.ruoyi.system.mapper.SysVisitorMachineMapper;
import com.ruoyi.system.service.ISysVisitorMachineService;
import com.ruoyi.system.vo.SysVisitorMachineVO;
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 SysVisitorMachineServiceImpl implements ISysVisitorMachineService
{
    private static final String ONLINE_STATUS_UNACTIVATED = "UNACTIVATED";
 
    @Autowired
    private SysVisitorMachineMapper visitorMachineMapper;
 
    @Autowired
    private SysVisitAreaMapper visitAreaMapper;
 
    @Override
    public PageInfo<SysVisitorMachineVO> pageList(SysVisitorMachinePageRequest request)
    {
        SysVisitorMachinePageRequest query = request == null ? new SysVisitorMachinePageRequest() : request;
        query.setDeviceName(StringUtils.trim(query.getDeviceName()));
        query.setDeviceId(StringUtils.trim(query.getDeviceId()));
        query.setOnlineStatus(StringUtils.trim(query.getOnlineStatus()));
        PageInfo<SysVisitorMachineVO> pageInfo = new PageInfo<SysVisitorMachineVO>(query.getPageNum(), query.getPageSize());
        List<SysVisitorMachineVO> list = visitorMachineMapper.pageList(query, pageInfo);
        pageInfo.setRecords(list);
        return pageInfo;
    }
 
    @Override
    public SysVisitorMachineVO detail(SysVisitorMachineDetailRequest request)
    {
        Long machineId = getRequiredMachineId(request == null ? null : request.getMachineId());
        SysVisitorMachineVO detail = visitorMachineMapper.selectVoById(machineId);
        if (detail == null)
        {
            throw new ServiceException("访客机不存在");
        }
        return detail;
    }
 
    @Override
    public int add(SysVisitorMachineAddRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("访客机内容不能为空");
        }
        SysVisitorMachine machine = buildMachine(request.getAreaId(), request.getDeviceName(), request.getBrand(),
                request.getDeviceId(), request.getRemark(), request.getEnabled(), null);
        machine.setOnlineStatus(ONLINE_STATUS_UNACTIVATED);
        machine.setCreateBy(username);
        return visitorMachineMapper.insertMachine(machine);
    }
 
    @Override
    public int edit(SysVisitorMachineEditRequest request, String username)
    {
        if (request == null)
        {
            throw new ServiceException("访客机内容不能为空");
        }
        Long machineId = getRequiredMachineId(request.getMachineId());
        if (visitorMachineMapper.selectById(machineId) == null)
        {
            throw new ServiceException("访客机不存在");
        }
        SysVisitorMachine machine = buildMachine(request.getAreaId(), request.getDeviceName(), request.getBrand(),
                request.getDeviceId(), request.getRemark(), request.getEnabled(), machineId);
        machine.setMachineId(machineId);
        machine.setUpdateBy(username);
        return visitorMachineMapper.updateMachine(machine);
    }
 
    @Override
    public int changeStatus(SysVisitorMachineStatusRequest request, String username)
    {
        List<Long> machineIds = getRequiredMachineIds(request == null ? null : request.getMachineIds());
        if (request.getEnabled() == null)
        {
            throw new ServiceException("启用状态不能为空");
        }
        return visitorMachineMapper.updateEnabled(machineIds, request.getEnabled(), username);
    }
 
    @Override
    public int logout(SysVisitorMachineLogoutRequest request, String username)
    {
        List<Long> machineIds = getRequiredMachineIds(request == null ? null : request.getMachineIds());
        return visitorMachineMapper.updateOnlineStatus(machineIds, ONLINE_STATUS_UNACTIVATED, username);
    }
 
    @Override
    public int delete(SysVisitorMachineDeleteRequest request, String username)
    {
        List<Long> machineIds = getRequiredMachineIds(request == null ? null : request.getMachineIds());
        return visitorMachineMapper.deleteByIds(machineIds, username);
    }
 
    private SysVisitorMachine buildMachine(Long areaId, String deviceName, String brand, String deviceId, String remark,
                                           Boolean enabled, Long excludeMachineId)
    {
        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 (visitorMachineMapper.checkDeviceIdUnique(requiredDeviceId, excludeMachineId) != null)
        {
            throw new ServiceException("设备ID已存在");
        }
 
        SysVisitorMachine machine = new SysVisitorMachine();
        machine.setAreaId(requiredAreaId);
        machine.setDeviceName(requiredDeviceName);
        machine.setBrand(requiredBrand);
        machine.setDeviceId(requiredDeviceId);
        machine.setRemark(StringUtils.trim(remark));
        machine.setEnabled(enabled == null ? Boolean.TRUE : enabled);
        return machine;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int activateDevice(String deviceId, String fingerprint) {
        // 1. 根据deviceId查询设备
        SysVisitorMachine device = visitorMachineMapper.checkDeviceIdUnique(deviceId, null);
        if (device == null || device.getDelFlag().equals("1")) {
            throw new ServiceException("设备不存在,请先添加设备");
        }
 
        // 2. 检查是否已激活
        if ("ONLINE".equals(device.getOnlineStatus()) || "ACTIVATED".equals(device.getOnlineStatus())) {
            throw new ServiceException("设备已激活,无需重复操作");
        }
 
        // 3. 检查设备是否启用
        if (device.getEnabled() == null || !device.getEnabled()) {
            throw new ServiceException("设备未启用,请先在后台启用设备");
        }
 
        // 4. 更新状态为激活/在线
        SysVisitorMachine update = new SysVisitorMachine();
        update.setMachineId(device.getMachineId());
        update.setOnlineStatus("ONLINE");  // 或 "ACTIVATED",根据你的枚举定义
        update.setUpdateTime(new Date());
        update.setFingerprint(fingerprint);
        return visitorMachineMapper.updateMachine(update);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deactivateDevice(String deviceId) {
        // 1. 根据deviceId查询设备
        SysVisitorMachine device = visitorMachineMapper.checkDeviceIdUnique(deviceId, null);
        if (device == null || device.getDelFlag().equals("1")) {
            throw new ServiceException("设备不存在");
        }
 
        // 2. 检查是否已注销
        if ("OFFLINE".equals(device.getOnlineStatus()) || "UNACTIVATED".equals(device.getOnlineStatus())) {
            throw new ServiceException("设备已注销,无需重复操作");
        }
 
        // 3. 更新状态为离线/未激活
        SysVisitorMachine update = new SysVisitorMachine();
        update.setMachineId(device.getMachineId());
        update.setOnlineStatus("OFFLINE");  // 或 "UNACTIVATED"
        update.setUpdateTime(new Date());
 
        return visitorMachineMapper.updateMachine(update);
    }
 
    private Long getRequiredAreaId(Long areaId)
    {
        if (areaId == null || areaId <= 0)
        {
            throw new ServiceException("所属区域不能为空");
        }
        return areaId;
    }
 
    private Long getRequiredMachineId(Long machineId)
    {
        if (machineId == null || machineId <= 0)
        {
            throw new ServiceException("访客机主键不能为空");
        }
        return machineId;
    }
 
    private List<Long> getRequiredMachineIds(List<Long> machineIds)
    {
        if (CollectionUtils.isEmpty(machineIds))
        {
            throw new ServiceException("访客机主键集合不能为空");
        }
        for (Long machineId : machineIds)
        {
            getRequiredMachineId(machineId);
        }
        return machineIds;
    }
 
    private String getRequiredText(String value, String message)
    {
        String result = StringUtils.trim(value);
        if (StringUtils.isEmpty(result))
        {
            throw new ServiceException(message);
        }
        return result;
    }
}