|
|
@@ -2,7 +2,6 @@ package com.usky.eg.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|
|
@@ -16,11 +15,13 @@ import com.usky.eg.domain.EgDevice;
|
|
|
import com.usky.eg.domain.EgDeviceHeartbeat;
|
|
|
import com.usky.eg.domain.EgDevicePersonBind;
|
|
|
import com.usky.eg.domain.EgRecord;
|
|
|
+import com.usky.eg.domain.SpaceDeviceAccess;
|
|
|
import com.usky.eg.domain.SysPerson;
|
|
|
import com.usky.eg.mapper.EgDeviceMapper;
|
|
|
import com.usky.eg.mapper.EgDeviceHeartbeatMapper;
|
|
|
import com.usky.eg.mapper.EgDevicePersonBindMapper;
|
|
|
import com.usky.eg.mapper.EgRecordMapper;
|
|
|
+import com.usky.eg.mapper.SpaceDeviceAccessMapper;
|
|
|
import com.usky.eg.mapper.SysPersonMapper;
|
|
|
import com.usky.eg.service.EgDeviceService;
|
|
|
import com.usky.common.mybatis.core.AbstractCrudService;
|
|
|
@@ -37,12 +38,7 @@ import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
|
|
/**
|
|
|
- * <p>
|
|
|
- * 门禁_设备信息表 服务实现类
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @author zyj
|
|
|
- * @since 2024-11-27
|
|
|
+ * 空间设备(门禁)服务实现
|
|
|
*/
|
|
|
@Service
|
|
|
public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgDevice> implements EgDeviceService {
|
|
|
@@ -60,151 +56,66 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
private EgRecordMapper egRecordMapper;
|
|
|
@Autowired
|
|
|
private SysPersonMapper sysPersonMapper;
|
|
|
+ @Autowired
|
|
|
+ private SpaceDeviceAccessMapper spaceDeviceAccessMapper;
|
|
|
|
|
|
@Override
|
|
|
- public CommonPage<EgDevice> page(EgDeviceRequestVO requestVO){
|
|
|
- IPage<EgDevice> page = new Page<>(requestVO.getCurrent(),requestVO.getSize());
|
|
|
+ public CommonPage<EgDevice> page(EgDeviceRequestVO requestVO) {
|
|
|
Integer tenantId;
|
|
|
-
|
|
|
- if(StringUtils.isNotBlank(requestVO.getDeviceCode())){
|
|
|
+ if (StringUtils.isNotBlank(requestVO.getDeviceCode())) {
|
|
|
LambdaQueryWrapper<EgDevice> tempQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
tempQueryWrapper.eq(EgDevice::getDeviceCode, requestVO.getDeviceCode());
|
|
|
EgDevice tempDevice = egDeviceMapper.selectOne(tempQueryWrapper);
|
|
|
- if(tempDevice == null){
|
|
|
- // 未查询到数据,返回空数组
|
|
|
+ if (tempDevice == null) {
|
|
|
return new CommonPage<>(new ArrayList<>(), 0L, requestVO.getSize(), requestVO.getCurrent());
|
|
|
}
|
|
|
tenantId = tempDevice.getTenantId();
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
tenantId = SecurityUtils.getTenantId();
|
|
|
}
|
|
|
+ applyDefaultDeviceType(requestVO);
|
|
|
|
|
|
- LambdaQueryWrapper<EgDevice> queryWrapper = Wrappers.lambdaQuery();
|
|
|
- queryWrapper.like(StringUtils.isNotBlank(requestVO.getDeviceName()),EgDevice::getDeviceName,requestVO.getDeviceName())
|
|
|
- .like(StringUtils.isNotBlank(requestVO.getInstallAddress()),EgDevice::getInstallAddress,requestVO.getInstallAddress())
|
|
|
- .eq(null != requestVO.getServiceStatus(),EgDevice::getServiceStatus,requestVO.getServiceStatus())
|
|
|
- .eq(null != requestVO.getId(),EgDevice::getId,requestVO.getId())
|
|
|
- .eq(null != requestVO.getDeviceUuid(),EgDevice::getDeviceUuid, requestVO.getDeviceUuid())
|
|
|
- .eq(StringUtils.isNotBlank(requestVO.getDeviceCode()),EgDevice::getDeviceCode, requestVO.getDeviceCode())
|
|
|
- .eq(EgDevice::getTenantId,tenantId)
|
|
|
- .orderByDesc(EgDevice::getId);
|
|
|
- page = this.page(page,queryWrapper);
|
|
|
-
|
|
|
- if(!page.getRecords().isEmpty()){
|
|
|
- // 检查设备心跳
|
|
|
- List<String> validDeviceCodes = new ArrayList<>();
|
|
|
- Map<String, EgDevice> deviceCodeMap = new HashMap<>();
|
|
|
- for (EgDevice device : page.getRecords()) {
|
|
|
- String deviceCode = device.getDeviceCode();
|
|
|
- if (StringUtils.isNotBlank(deviceCode)) {
|
|
|
- validDeviceCodes.add(deviceCode);
|
|
|
- deviceCodeMap.put(deviceCode, device);
|
|
|
- } else {
|
|
|
- // deviceCode为空,直接设置为离线
|
|
|
- device.setDeviceStatus(0);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 批量查询心跳数据
|
|
|
- if (!validDeviceCodes.isEmpty()) {
|
|
|
- LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatQueryWrapper = Wrappers.lambdaQuery();
|
|
|
- heartbeatQueryWrapper.in(EgDeviceHeartbeat::getDeviceCode, validDeviceCodes)
|
|
|
- .orderByDesc(EgDeviceHeartbeat::getCreateTime);
|
|
|
- List<EgDeviceHeartbeat> heartbeatList = egDeviceHeartbeatMapper.selectList(heartbeatQueryWrapper);
|
|
|
-
|
|
|
- // 构建deviceCode到最新心跳记录的映射,并同时设置deviceStatus
|
|
|
- Map<String, EgDeviceHeartbeat> deviceHeartbeatMap = new HashMap<>();
|
|
|
- for (EgDeviceHeartbeat heartbeat : heartbeatList) {
|
|
|
- String deviceCode = heartbeat.getDeviceCode();
|
|
|
- if (StringUtils.isNotBlank(deviceCode)) {
|
|
|
- // 如果已存在,保留最新的(因为已按createTime降序排列)
|
|
|
- if (deviceHeartbeatMap.putIfAbsent(deviceCode, heartbeat) == null) {
|
|
|
- // 首次遇到该deviceCode,从心跳表直接获取deviceStatus并设置到EgDevice
|
|
|
- EgDevice device = deviceCodeMap.get(deviceCode);
|
|
|
- if (device != null && heartbeat.getDeviceStatus() != null) {
|
|
|
- device.setDeviceStatus(heartbeat.getDeviceStatus());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // 如果未找到对应的设备心跳记录,则设置DeviceStatus为0(离线)
|
|
|
- for (String deviceCode : validDeviceCodes) {
|
|
|
- if (!deviceHeartbeatMap.containsKey(deviceCode)) {
|
|
|
- EgDevice device = deviceCodeMap.get(deviceCode);
|
|
|
- if (device != null) {
|
|
|
- device.setDeviceStatus(0);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- // 如果没有有效的deviceCode,将所有设备的deviceStatus设置为0
|
|
|
- for (EgDevice device : page.getRecords()) {
|
|
|
- device.setDeviceStatus(0);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // 查询并填充绑定人员信息(来自 eg_device_person_bind,person_id 逗号分隔后写入 bindPerson 字段)
|
|
|
- if (!page.getRecords().isEmpty()) {
|
|
|
- fillBindPerson(page.getRecords());
|
|
|
- }
|
|
|
-
|
|
|
- return new CommonPage<>(page.getRecords(),page.getTotal(),requestVO.getSize(),requestVO.getCurrent());
|
|
|
+ Page<EgDevice> page = new Page<>(requestVO.getCurrent(), requestVO.getSize());
|
|
|
+ IPage<EgDevice> result = egDeviceMapper.selectPageJoin(page, requestVO, tenantId, null);
|
|
|
+ fillOnlineStatus(result.getRecords());
|
|
|
+ fillBindPerson(result.getRecords());
|
|
|
+ return new CommonPage<>(result.getRecords(), result.getTotal(), requestVO.getSize(), requestVO.getCurrent());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public CommonPage<EgDevice> wePage(EgDeviceRequestVO requestVO){
|
|
|
+ public CommonPage<EgDevice> wePage(EgDeviceRequestVO requestVO) {
|
|
|
long userId = SecurityUtils.getUserId();
|
|
|
- // 通过 sys_user_person -> sys_person -> eg_device_person_bind -> eg_device 查询当前用户绑定的设备ID列表
|
|
|
- List<Integer> deviceIds = egDevicePersonBindMapper.selectDeviceIdsByUserId(userId);
|
|
|
- if (CollectionUtils.isEmpty(deviceIds)) {
|
|
|
- // 未绑定任何设备,返回空分页
|
|
|
+ List<String> deviceUuids = egDevicePersonBindMapper.selectDeviceUuidsByUserId(userId);
|
|
|
+ if (CollectionUtils.isEmpty(deviceUuids)) {
|
|
|
return new CommonPage<>(new ArrayList<>(), 0L, requestVO.getSize(), requestVO.getCurrent());
|
|
|
}
|
|
|
+ applyDefaultDeviceType(requestVO);
|
|
|
|
|
|
- IPage<EgDevice> page = new Page<>(requestVO.getCurrent(),requestVO.getSize());
|
|
|
- LambdaQueryWrapper<EgDevice> queryWrapper = Wrappers.lambdaQuery();
|
|
|
- queryWrapper.like(StringUtils.isNotBlank(requestVO.getDeviceName()),EgDevice::getDeviceName,requestVO.getDeviceName())
|
|
|
- .like(StringUtils.isNotBlank(requestVO.getInstallAddress()),EgDevice::getInstallAddress,requestVO.getInstallAddress())
|
|
|
- .eq(null != requestVO.getServiceStatus(),EgDevice::getServiceStatus,requestVO.getServiceStatus())
|
|
|
- .eq(null != requestVO.getId(),EgDevice::getId,requestVO.getId())
|
|
|
- .in(EgDevice::getId, deviceIds)
|
|
|
- .eq(EgDevice::getTenantId,SecurityUtils.getTenantId())
|
|
|
- .orderByDesc(EgDevice::getId);
|
|
|
- page = this.page(page,queryWrapper);
|
|
|
-
|
|
|
- // 查询并填充绑定人员信息
|
|
|
- if (!page.getRecords().isEmpty()) {
|
|
|
- fillBindPerson(page.getRecords());
|
|
|
- }
|
|
|
-
|
|
|
- return new CommonPage<>(page.getRecords(),page.getTotal(),requestVO.getSize(),requestVO.getCurrent());
|
|
|
+ Page<EgDevice> page = new Page<>(requestVO.getCurrent(), requestVO.getSize());
|
|
|
+ IPage<EgDevice> result = egDeviceMapper.selectPageJoin(page, requestVO, SecurityUtils.getTenantId(), deviceUuids);
|
|
|
+ fillBindPerson(result.getRecords());
|
|
|
+ return new CommonPage<>(result.getRecords(), result.getTotal(), requestVO.getSize(), requestVO.getCurrent());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void add(EgDevice egDevice) {
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
-
|
|
|
- // 1. 校验设备名称唯一性
|
|
|
+
|
|
|
if (checkDeviceNameUnique(egDevice)) {
|
|
|
throw new BusinessException("新增门禁设备'" + egDevice.getDeviceName() + "'失败,设备已存在");
|
|
|
}
|
|
|
|
|
|
- // 2. 校验设备IP是否已被绑定
|
|
|
if (StringUtils.isNotBlank(egDevice.getDeviceIp())) {
|
|
|
LambdaQueryWrapper<EgDevice> ipCheckWrapper = Wrappers.lambdaQuery();
|
|
|
ipCheckWrapper.eq(EgDevice::getDeviceIp, egDevice.getDeviceIp())
|
|
|
.eq(EgDevice::getTenantId, SecurityUtils.getTenantId());
|
|
|
EgDevice existingDeviceByIp = this.getOne(ipCheckWrapper);
|
|
|
if (existingDeviceByIp != null) {
|
|
|
- throw new BusinessException("设备IP'" + egDevice.getDeviceIp() + "'已被设备'"
|
|
|
+ throw new BusinessException("设备IP'" + egDevice.getDeviceIp() + "'已被设备'"
|
|
|
+ existingDeviceByIp.getDeviceName() + "'绑定,请更换IP或检查设备配置!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 3. 若未传deviceCode但传了deviceIp,尝试从心跳表中获取deviceCode
|
|
|
if (StringUtils.isBlank(egDevice.getDeviceCode()) && StringUtils.isNotBlank(egDevice.getDeviceIp())) {
|
|
|
LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatCheckWrapper = Wrappers.lambdaQuery();
|
|
|
heartbeatCheckWrapper.eq(EgDeviceHeartbeat::getIpAddr, egDevice.getDeviceIp())
|
|
|
@@ -215,46 +126,44 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 4. 校验设备编码是否已被绑定
|
|
|
if (StringUtils.isNotBlank(egDevice.getDeviceCode())) {
|
|
|
LambdaQueryWrapper<EgDevice> codeCheckWrapper = Wrappers.lambdaQuery();
|
|
|
codeCheckWrapper.eq(EgDevice::getDeviceCode, egDevice.getDeviceCode())
|
|
|
.eq(EgDevice::getTenantId, SecurityUtils.getTenantId());
|
|
|
EgDevice existingDeviceByCode = this.getOne(codeCheckWrapper);
|
|
|
if (existingDeviceByCode != null) {
|
|
|
- throw new BusinessException("设备编码'" + egDevice.getDeviceCode() + "'已被设备'"
|
|
|
+ throw new BusinessException("设备编码'" + egDevice.getDeviceCode() + "'已被设备'"
|
|
|
+ existingDeviceByCode.getDeviceName() + "'绑定,请更换设备编码或检查设备配置!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 5. 设置设备基本信息
|
|
|
egDevice.setDeviceUuid(UUIDUtils.uuid());
|
|
|
+ egDevice.setDeviceType(EgDevice.DEVICE_TYPE_ACCESS);
|
|
|
egDevice.setCreateBy(SecurityUtils.getUsername());
|
|
|
egDevice.setCreateTime(now);
|
|
|
egDevice.setTenantId(SecurityUtils.getTenantId());
|
|
|
- egDevice.setOpenMode("人脸");
|
|
|
- egDevice.setWorkStatus("4");
|
|
|
+ if (StringUtils.isBlank(egDevice.getOpenMode())) {
|
|
|
+ egDevice.setOpenMode("人脸");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(egDevice.getWorkStatus())) {
|
|
|
+ egDevice.setWorkStatus("4");
|
|
|
+ }
|
|
|
|
|
|
- // 6. 保存设备
|
|
|
this.save(egDevice);
|
|
|
+ saveAccess(egDevice, now);
|
|
|
|
|
|
- // 7. 异步调用远程服务添加设备信息,静默处理异常,不影响设备保存成功
|
|
|
String deviceUuid = egDevice.getDeviceUuid();
|
|
|
String deviceName = egDevice.getDeviceName();
|
|
|
if (StringUtils.isNotBlank(deviceUuid) && StringUtils.isNotBlank(deviceName)) {
|
|
|
- String installAddress = StringUtils.isNotBlank(egDevice.getInstallAddress())
|
|
|
- ? egDevice.getInstallAddress()
|
|
|
- : "";
|
|
|
- Integer serviceStatus = egDevice.getServiceStatus() != null
|
|
|
- ? egDevice.getServiceStatus()
|
|
|
- : 1;
|
|
|
-
|
|
|
+ String installAddress = StringUtils.isNotBlank(egDevice.getInstallAddress())
|
|
|
+ ? egDevice.getInstallAddress() : "";
|
|
|
+ Integer serviceStatus = egDevice.getServiceStatus() != null ? egDevice.getServiceStatus() : 1;
|
|
|
CompletableFuture.runAsync(() -> {
|
|
|
try {
|
|
|
- remoteIotTaskService.addDeviceInfo("502_USKY", deviceUuid, deviceUuid,
|
|
|
+ remoteIotTaskService.addDeviceInfo("502_USKY", deviceUuid, deviceUuid,
|
|
|
deviceName, installAddress, serviceStatus);
|
|
|
} catch (Exception e) {
|
|
|
- // 远程服务调用失败,静默处理,不影响设备保存成功
|
|
|
+ // 远程服务调用失败,静默处理
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
@@ -262,24 +171,25 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
|
|
|
@Override
|
|
|
public void update(EgDevice egDevice) {
|
|
|
- if(checkDeviceNameUnique(egDevice)){
|
|
|
- throw new BusinessException("修改门禁设备'"+egDevice.getDeviceName()+"'失败,设备名称已存在");
|
|
|
+ if (StringUtils.isBlank(egDevice.getDeviceUuid())) {
|
|
|
+ throw new BusinessException("设备uuid不能为空");
|
|
|
+ }
|
|
|
+ if (checkDeviceNameUnique(egDevice)) {
|
|
|
+ throw new BusinessException("修改门禁设备'" + egDevice.getDeviceName() + "'失败,设备名称已存在");
|
|
|
}
|
|
|
|
|
|
- // 校验设备IP是否已被其他设备绑定(排除当前设备)
|
|
|
if (StringUtils.isNotBlank(egDevice.getDeviceIp())) {
|
|
|
LambdaQueryWrapper<EgDevice> ipCheckWrapper = Wrappers.lambdaQuery();
|
|
|
ipCheckWrapper.eq(EgDevice::getDeviceIp, egDevice.getDeviceIp())
|
|
|
.eq(EgDevice::getTenantId, SecurityUtils.getTenantId())
|
|
|
- .ne(EgDevice::getId, egDevice.getId());
|
|
|
+ .ne(EgDevice::getDeviceUuid, egDevice.getDeviceUuid());
|
|
|
EgDevice existingDeviceByIp = this.getOne(ipCheckWrapper);
|
|
|
if (existingDeviceByIp != null) {
|
|
|
- throw new BusinessException("设备IP'" + egDevice.getDeviceIp() + "'已被设备'"
|
|
|
+ throw new BusinessException("设备IP'" + egDevice.getDeviceIp() + "'已被设备'"
|
|
|
+ existingDeviceByIp.getDeviceName() + "'绑定,请更换IP或检查设备配置!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 若传了deviceIp,尝试从心跳表中获取deviceCode
|
|
|
if (StringUtils.isNotBlank(egDevice.getDeviceIp())) {
|
|
|
LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatCheckWrapper = Wrappers.lambdaQuery();
|
|
|
heartbeatCheckWrapper.eq(EgDeviceHeartbeat::getIpAddr, egDevice.getDeviceIp())
|
|
|
@@ -290,102 +200,96 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 校验设备编码是否已被其他设备绑定(排除当前设备)
|
|
|
if (StringUtils.isNotBlank(egDevice.getDeviceCode())) {
|
|
|
LambdaQueryWrapper<EgDevice> codeCheckWrapper = Wrappers.lambdaQuery();
|
|
|
codeCheckWrapper.eq(EgDevice::getDeviceCode, egDevice.getDeviceCode())
|
|
|
.eq(EgDevice::getTenantId, SecurityUtils.getTenantId())
|
|
|
- .ne(EgDevice::getId, egDevice.getId());
|
|
|
+ .ne(EgDevice::getDeviceUuid, egDevice.getDeviceUuid());
|
|
|
EgDevice existingDeviceByCode = this.getOne(codeCheckWrapper);
|
|
|
if (existingDeviceByCode != null) {
|
|
|
- throw new BusinessException("设备编码'" + egDevice.getDeviceCode() + "'已被设备'"
|
|
|
+ throw new BusinessException("设备编码'" + egDevice.getDeviceCode() + "'已被设备'"
|
|
|
+ existingDeviceByCode.getDeviceName() + "'绑定,请更换设备编码或检查设备配置!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
egDevice.setUpdateBy(SecurityUtils.getUsername());
|
|
|
- egDevice.setUpdateTime(LocalDateTime.now());
|
|
|
-
|
|
|
+ egDevice.setUpdateTime(now);
|
|
|
this.updateById(egDevice);
|
|
|
+ saveAccess(egDevice, now);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void attachUpdate(EgDevice egDevice){
|
|
|
- if(checkDeviceNameUnique(egDevice)){
|
|
|
- throw new BusinessException("新增门禁设备'"+egDevice.getDeviceName()+"'失败,设备已存在");
|
|
|
+ public void attachUpdate(EgDevice egDevice) {
|
|
|
+ if (StringUtils.isBlank(egDevice.getDeviceUuid())) {
|
|
|
+ throw new BusinessException("设备uuid不能为空");
|
|
|
}
|
|
|
-
|
|
|
+ if (checkDeviceNameUnique(egDevice)) {
|
|
|
+ throw new BusinessException("新增门禁设备'" + egDevice.getDeviceName() + "'失败,设备已存在");
|
|
|
+ }
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
egDevice.setUpdateBy(SecurityUtils.getUsername());
|
|
|
- egDevice.setUpdateTime(LocalDateTime.now());
|
|
|
-
|
|
|
+ egDevice.setUpdateTime(now);
|
|
|
this.updateById(egDevice);
|
|
|
+ saveAccess(egDevice, now);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void remove(Integer id){
|
|
|
- EgDevice egDevice = this.getById(id);
|
|
|
+ public void remove(String deviceUuid) {
|
|
|
+ if (StringUtils.isBlank(deviceUuid)) {
|
|
|
+ throw new BusinessException("设备uuid不能为空");
|
|
|
+ }
|
|
|
+ EgDevice egDevice = this.getById(deviceUuid);
|
|
|
Optional.ofNullable(egDevice).orElseThrow(() -> new BusinessException("门禁设备信息不存在"));
|
|
|
|
|
|
- // 校验 eg_device_person_bind 是否存在绑定人员
|
|
|
- Integer personBindCount = egDevicePersonBindMapper.countByDeviceId(id);
|
|
|
+ Integer personBindCount = egDevicePersonBindMapper.countByDeviceUuid(deviceUuid);
|
|
|
if (personBindCount != null && personBindCount > 0) {
|
|
|
throw new BusinessException("已绑定人员不能删除");
|
|
|
}
|
|
|
|
|
|
- this.removeById(id);
|
|
|
+ spaceDeviceAccessMapper.deleteById(deviceUuid);
|
|
|
+ this.removeById(deviceUuid);
|
|
|
|
|
|
- // 异步调用远程服务删除设备信息,静默处理异常,不影响设备删除
|
|
|
- String deviceUuid = egDevice.getDeviceUuid();
|
|
|
- if (StringUtils.isNotBlank(deviceUuid)) {
|
|
|
- CompletableFuture.runAsync(() -> {
|
|
|
- try {
|
|
|
- remoteIotTaskService.deleteDeviceInfo(deviceUuid);
|
|
|
- } catch (Exception e) {
|
|
|
- // 远程服务调用失败,静默处理,不影响设备删除
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ try {
|
|
|
+ remoteIotTaskService.deleteDeviceInfo(deviceUuid);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // 远程服务调用失败,静默处理
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 根据设备ID集合查询 eg_device_person_bind,将 person_id 按逗号拼接后写入设备的 bindPerson 字段
|
|
|
- */
|
|
|
private void fillBindPerson(List<EgDevice> devices) {
|
|
|
if (CollectionUtils.isEmpty(devices)) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- List<Integer> deviceIds = new ArrayList<>();
|
|
|
+ List<String> deviceUuids = new ArrayList<>();
|
|
|
for (EgDevice device : devices) {
|
|
|
- if (device != null && device.getId() != null) {
|
|
|
- deviceIds.add(device.getId());
|
|
|
+ if (device != null && StringUtils.isNotBlank(device.getDeviceUuid())) {
|
|
|
+ deviceUuids.add(device.getDeviceUuid());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- if (deviceIds.isEmpty()) {
|
|
|
+ if (deviceUuids.isEmpty()) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- List<EgDevicePersonBind> bindList = egDevicePersonBindMapper.selectByDeviceIds(deviceIds);
|
|
|
+ List<EgDevicePersonBind> bindList = egDevicePersonBindMapper.selectByDeviceUuids(deviceUuids);
|
|
|
if (CollectionUtils.isEmpty(bindList)) {
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- Map<Integer, List<String>> devicePersonMap = new HashMap<>();
|
|
|
+ Map<String, List<String>> devicePersonMap = new HashMap<>();
|
|
|
for (EgDevicePersonBind bind : bindList) {
|
|
|
- if (bind.getDeviceId() == null || bind.getPersonId() == null) {
|
|
|
+ if (StringUtils.isBlank(bind.getDeviceUuid()) || bind.getPersonId() == null) {
|
|
|
continue;
|
|
|
}
|
|
|
devicePersonMap
|
|
|
- .computeIfAbsent(bind.getDeviceId(), k -> new ArrayList<>())
|
|
|
+ .computeIfAbsent(bind.getDeviceUuid(), k -> new ArrayList<>())
|
|
|
.add(String.valueOf(bind.getPersonId()));
|
|
|
}
|
|
|
-
|
|
|
for (EgDevice device : devices) {
|
|
|
- if (device == null || device.getId() == null) {
|
|
|
+ if (device == null || StringUtils.isBlank(device.getDeviceUuid())) {
|
|
|
continue;
|
|
|
}
|
|
|
- List<String> personIds = devicePersonMap.get(device.getId());
|
|
|
+ List<String> personIds = devicePersonMap.get(device.getDeviceUuid());
|
|
|
if (personIds != null && !personIds.isEmpty()) {
|
|
|
device.setBindPerson(String.join(",", personIds));
|
|
|
} else {
|
|
|
@@ -395,30 +299,19 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public boolean checkNameUnique(EgDevice egDevice){
|
|
|
- Integer id = null == egDevice.getId() ? -1 : egDevice.getId();
|
|
|
- LambdaQueryWrapper<EgDevice> queryWrapper = Wrappers.lambdaQuery();
|
|
|
- queryWrapper.eq(EgDevice::getDeviceId,egDevice.getDeviceId())
|
|
|
- .eq(EgDevice::getTenantId, SecurityUtils.getTenantId());
|
|
|
- EgDevice one = this.getOne(queryWrapper);
|
|
|
- return null != one && !Objects.equals(one.getId(),id);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public boolean checkDeviceNameUnique(EgDevice egDevice){
|
|
|
- Integer id = null == egDevice.getId() ? -1 : egDevice.getId();
|
|
|
+ public boolean checkDeviceNameUnique(EgDevice egDevice) {
|
|
|
+ String uuid = egDevice.getDeviceUuid() == null ? "" : egDevice.getDeviceUuid();
|
|
|
LambdaQueryWrapper<EgDevice> queryWrapper = Wrappers.lambdaQuery();
|
|
|
- queryWrapper.eq(EgDevice::getDeviceName,egDevice.getDeviceName())
|
|
|
+ queryWrapper.eq(EgDevice::getDeviceName, egDevice.getDeviceName())
|
|
|
.eq(EgDevice::getTenantId, SecurityUtils.getTenantId());
|
|
|
EgDevice one = this.getOne(queryWrapper);
|
|
|
- return null != one && !Objects.equals(one.getId(),id);
|
|
|
+ return one != null && !Objects.equals(one.getDeviceUuid(), uuid);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Map<String,Object> control(String productCode, String deviceUuid, String commandCode, String commandValue, Long userId, String userName, Integer categoryType, String gatewayUuid, Boolean skipCheck, Integer passType){
|
|
|
- // 1. 查询设备
|
|
|
+ public Map<String, Object> control(String productCode, String deviceUuid, String commandCode, String commandValue, Long userId, String userName, Integer categoryType, String gatewayUuid, Boolean skipCheck, Integer passType) {
|
|
|
EgDevice device = this.getOne(Wrappers.<EgDevice>lambdaQuery()
|
|
|
- .select(EgDevice::getId, EgDevice::getTenantId)
|
|
|
+ .select(EgDevice::getDeviceUuid, EgDevice::getTenantId)
|
|
|
.eq(EgDevice::getDeviceUuid, deviceUuid));
|
|
|
if (device == null) {
|
|
|
throw new BusinessException("设备未注册,请先注册");
|
|
|
@@ -428,27 +321,24 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
long commandUserId = userId != null ? userId : SecurityUtils.getUserId();
|
|
|
String commandUserName = userName != null ? userName : SecurityUtils.getUsername();
|
|
|
|
|
|
- // 2. 权限校验
|
|
|
if (!Boolean.TRUE.equals(skipCheck)) {
|
|
|
- Integer bindCount = egDevicePersonBindMapper.countByUserIdAndDeviceId(commandUserId, device.getId());
|
|
|
+ Integer bindCount = egDevicePersonBindMapper.countByUserIdAndDeviceUuid(commandUserId, deviceUuid);
|
|
|
if (bindCount == null || bindCount == 0) {
|
|
|
- saveRecord(device.getId(), tenantId, commandUserName, passType, "失败:暂无权限");
|
|
|
+ saveRecord(deviceUuid, tenantId, commandUserName, passType, "失败:暂无权限");
|
|
|
throw new BusinessException("暂无权限");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 3. 构建下发命令参数
|
|
|
- Map<String,Object> params = new HashMap<>();
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
params.put("commandCode", commandCode);
|
|
|
params.put("commandValue", commandValue);
|
|
|
- Map<String,Object> map = new HashMap<>();
|
|
|
+ Map<String, Object> map = new HashMap<>();
|
|
|
map.put("method", "control");
|
|
|
map.put("deviceUuid", deviceUuid);
|
|
|
map.put("params", params);
|
|
|
|
|
|
- // 4. 下发命令并记录结果
|
|
|
String targetUuid = categoryType == 3 ? gatewayUuid : deviceUuid;
|
|
|
- Map<String,Object> result = null;
|
|
|
+ Map<String, Object> result = null;
|
|
|
String passResult = "下发命令失败";
|
|
|
try {
|
|
|
result = remoteTransferService.deviceControl(productCode, targetUuid, JSON.toJSONString(map), tenantId, commandUserId, commandUserName);
|
|
|
@@ -459,17 +349,14 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
passResult = result.get("message") != null ? result.get("message").toString() : "下发命令成功";
|
|
|
return result;
|
|
|
} finally {
|
|
|
- saveRecord(device.getId(), tenantId, commandUserName, passType, passResult);
|
|
|
+ saveRecord(deviceUuid, tenantId, commandUserName, passType, passResult);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 插入通行记录,失败不影响主流程
|
|
|
- */
|
|
|
- private void saveRecord(Integer deviceId, Integer tenantId, String userName, Integer passType, String passResult) {
|
|
|
+ private void saveRecord(String deviceUuid, Integer tenantId, String userName, Integer passType, String passResult) {
|
|
|
try {
|
|
|
EgRecord record = new EgRecord();
|
|
|
- record.setEgDeviceId(deviceId);
|
|
|
+ record.setDeviceUuid(deviceUuid);
|
|
|
record.setTenantId(tenantId);
|
|
|
record.setPassTime(LocalDateTime.now());
|
|
|
record.setCreateTime(LocalDateTime.now());
|
|
|
@@ -477,7 +364,8 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
record.setUserName(userName);
|
|
|
record.setPassType(passType);
|
|
|
egRecordMapper.insert(record);
|
|
|
- } catch (Exception ignored) {}
|
|
|
+ } catch (Exception ignored) {
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -486,7 +374,7 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
Integer sz = queryVO.getSize();
|
|
|
IPage<SysPerson> page = new Page<>(cur, sz);
|
|
|
|
|
|
- List<Integer> deviceIds = new ArrayList<>();
|
|
|
+ List<String> deviceUuids = new ArrayList<>();
|
|
|
if (StringUtils.isNotBlank(queryVO.getDeviceCode())) {
|
|
|
EgDevice tempDevice = egDeviceMapper.selectOne(Wrappers.<EgDevice>lambdaQuery()
|
|
|
.eq(EgDevice::getDeviceCode, queryVO.getDeviceCode()));
|
|
|
@@ -496,27 +384,28 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
EgDevice device = egDeviceMapper.selectOne(Wrappers.<EgDevice>lambdaQuery()
|
|
|
.eq(EgDevice::getDeviceCode, queryVO.getDeviceCode())
|
|
|
.eq(EgDevice::getTenantId, tempDevice.getTenantId()));
|
|
|
- if (device == null || device.getId() == null) {
|
|
|
+ if (device == null || StringUtils.isBlank(device.getDeviceUuid())) {
|
|
|
return new CommonPage<>(new ArrayList<>(), 0L, sz, cur);
|
|
|
}
|
|
|
- deviceIds.add(device.getId());
|
|
|
+ deviceUuids.add(device.getDeviceUuid());
|
|
|
} else {
|
|
|
List<EgDevice> devices = egDeviceMapper.selectList(Wrappers.<EgDevice>lambdaQuery()
|
|
|
- .eq(EgDevice::getTenantId, SecurityUtils.getTenantId()));
|
|
|
+ .eq(EgDevice::getTenantId, SecurityUtils.getTenantId())
|
|
|
+ .eq(EgDevice::getDeviceType, EgDevice.DEVICE_TYPE_ACCESS));
|
|
|
if (!CollectionUtils.isEmpty(devices)) {
|
|
|
for (EgDevice d : devices) {
|
|
|
- if (d != null && d.getId() != null) {
|
|
|
- deviceIds.add(d.getId());
|
|
|
+ if (d != null && StringUtils.isNotBlank(d.getDeviceUuid())) {
|
|
|
+ deviceUuids.add(d.getDeviceUuid());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (deviceIds.isEmpty()) {
|
|
|
+ if (deviceUuids.isEmpty()) {
|
|
|
return new CommonPage<>(new ArrayList<>(), 0L, sz, cur);
|
|
|
}
|
|
|
|
|
|
- List<EgDevicePersonBind> bindList = egDevicePersonBindMapper.selectByDeviceIds(deviceIds);
|
|
|
+ List<EgDevicePersonBind> bindList = egDevicePersonBindMapper.selectByDeviceUuids(deviceUuids);
|
|
|
LinkedHashSet<Integer> personIdSet = new LinkedHashSet<>();
|
|
|
if (!CollectionUtils.isEmpty(bindList)) {
|
|
|
for (EgDevicePersonBind b : bindList) {
|
|
|
@@ -592,19 +481,14 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void bindPerson(Integer deviceId, String personIds, Integer isLoginNotify) {
|
|
|
- if (deviceId == null) {
|
|
|
- throw new BusinessException("设备ID不能为空");
|
|
|
+ public void bindPerson(String deviceUuid, String personIds, Integer isLoginNotify) {
|
|
|
+ if (StringUtils.isBlank(deviceUuid)) {
|
|
|
+ throw new BusinessException("设备uuid不能为空");
|
|
|
}
|
|
|
-
|
|
|
- // 先校验设备是否存在
|
|
|
- EgDevice device = this.getById(deviceId);
|
|
|
+ EgDevice device = this.getById(deviceUuid);
|
|
|
Optional.ofNullable(device).orElseThrow(() -> new BusinessException("门禁设备信息不存在"));
|
|
|
|
|
|
- // 先清空原有绑定关系
|
|
|
- egDevicePersonBindMapper.deleteByDeviceId(deviceId);
|
|
|
-
|
|
|
- // 为空则视为解绑所有人员
|
|
|
+ egDevicePersonBindMapper.deleteByDeviceUuid(deviceUuid);
|
|
|
if (!StringUtils.isNotBlank(personIds)) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -616,10 +500,82 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
|
|
|
continue;
|
|
|
}
|
|
|
EgDevicePersonBind bind = new EgDevicePersonBind();
|
|
|
- bind.setDeviceId(deviceId);
|
|
|
+ bind.setDeviceUuid(deviceUuid);
|
|
|
bind.setPersonId(Integer.parseInt(personIdStr.trim()));
|
|
|
bind.setIsLoginNotify(loginNotify);
|
|
|
egDevicePersonBindMapper.insert(bind);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ private void applyDefaultDeviceType(EgDeviceRequestVO requestVO) {
|
|
|
+ if (requestVO.getDeviceType() == null) {
|
|
|
+ requestVO.setDeviceType(EgDevice.DEVICE_TYPE_ACCESS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveAccess(EgDevice egDevice, LocalDateTime now) {
|
|
|
+ SpaceDeviceAccess access = spaceDeviceAccessMapper.selectById(egDevice.getDeviceUuid());
|
|
|
+ if (access == null) {
|
|
|
+ access = new SpaceDeviceAccess();
|
|
|
+ access.setDeviceUuid(egDevice.getDeviceUuid());
|
|
|
+ access.setCreateTime(now);
|
|
|
+ access.setServiceStatus(egDevice.getServiceStatus());
|
|
|
+ access.setOpenMode(egDevice.getOpenMode());
|
|
|
+ access.setPassword(egDevice.getPassword());
|
|
|
+ access.setWorkStatus(egDevice.getWorkStatus());
|
|
|
+ spaceDeviceAccessMapper.insert(access);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ access.setServiceStatus(egDevice.getServiceStatus());
|
|
|
+ access.setOpenMode(egDevice.getOpenMode());
|
|
|
+ access.setPassword(egDevice.getPassword());
|
|
|
+ access.setWorkStatus(egDevice.getWorkStatus());
|
|
|
+ access.setUpdateTime(now);
|
|
|
+ spaceDeviceAccessMapper.updateById(access);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillOnlineStatus(List<EgDevice> records) {
|
|
|
+ if (CollectionUtils.isEmpty(records)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<String> validDeviceCodes = new ArrayList<>();
|
|
|
+ Map<String, EgDevice> deviceCodeMap = new HashMap<>();
|
|
|
+ for (EgDevice device : records) {
|
|
|
+ String deviceCode = device.getDeviceCode();
|
|
|
+ if (StringUtils.isNotBlank(deviceCode)) {
|
|
|
+ validDeviceCodes.add(deviceCode);
|
|
|
+ deviceCodeMap.put(deviceCode, device);
|
|
|
+ } else {
|
|
|
+ device.setDeviceStatus(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (validDeviceCodes.isEmpty()) {
|
|
|
+ for (EgDevice device : records) {
|
|
|
+ device.setDeviceStatus(0);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatQueryWrapper = Wrappers.lambdaQuery();
|
|
|
+ heartbeatQueryWrapper.in(EgDeviceHeartbeat::getDeviceCode, validDeviceCodes)
|
|
|
+ .orderByDesc(EgDeviceHeartbeat::getCreateTime);
|
|
|
+ List<EgDeviceHeartbeat> heartbeatList = egDeviceHeartbeatMapper.selectList(heartbeatQueryWrapper);
|
|
|
+ Map<String, EgDeviceHeartbeat> deviceHeartbeatMap = new HashMap<>();
|
|
|
+ for (EgDeviceHeartbeat heartbeat : heartbeatList) {
|
|
|
+ String deviceCode = heartbeat.getDeviceCode();
|
|
|
+ if (StringUtils.isNotBlank(deviceCode) && deviceHeartbeatMap.putIfAbsent(deviceCode, heartbeat) == null) {
|
|
|
+ EgDevice device = deviceCodeMap.get(deviceCode);
|
|
|
+ if (device != null && heartbeat.getDeviceStatus() != null) {
|
|
|
+ device.setDeviceStatus(heartbeat.getDeviceStatus());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (String deviceCode : validDeviceCodes) {
|
|
|
+ if (!deviceHeartbeatMap.containsKey(deviceCode)) {
|
|
|
+ EgDevice device = deviceCodeMap.get(deviceCode);
|
|
|
+ if (device != null) {
|
|
|
+ device.setDeviceStatus(0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|