Jelajahi Sumber

调整domain相关逻辑、设备绑定相关方法

fanghuisheng 3 minggu lalu
induk
melakukan
8fd08ec365

+ 8 - 0
service-eg/service-eg-biz/src/main/java/com/usky/eg/domain/EgDevice.java

@@ -134,6 +134,14 @@ public class EgDevice implements Serializable {
     private String workStatus;
 
     /**
+     * 设备code
+     */
+    private String deviceCode;
+
+    /**
+     * 设备状态;1:在线,0:离线
+     */
+    private Integer deviceStatus;
 
     /**
      * 用户人脸信息记录

+ 195 - 38
service-eg/service-eg-biz/src/main/java/com/usky/eg/service/impl/EgDeviceServiceImpl.java

@@ -13,9 +13,11 @@ import com.usky.common.core.exception.BusinessException;
 import com.usky.common.core.util.UUIDUtils;
 import com.usky.common.security.utils.SecurityUtils;
 import com.usky.eg.domain.EgDevice;
+import com.usky.eg.domain.EgDeviceHeartbeat;
 import com.usky.eg.domain.MeetingFace;
 import com.usky.eg.domain.MeetingFaceDevice;
 import com.usky.eg.mapper.EgDeviceMapper;
+import com.usky.eg.mapper.EgDeviceHeartbeatMapper;
 import com.usky.eg.mapper.MeetingFaceDeviceMapper;
 import com.usky.eg.mapper.MeetingFaceMapper;
 import com.usky.eg.service.EgDeviceService;
@@ -47,6 +49,8 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
     @Autowired
     private EgDeviceMapper egDeviceMapper;
     @Autowired
+    private EgDeviceHeartbeatMapper egDeviceHeartbeatMapper;
+    @Autowired
     private RemoteIotTaskService remoteIotTaskService;
 
     @Autowired
@@ -55,10 +59,17 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
     @Override
     public CommonPage<EgDevice> page(EgDeviceRequestVO requestVO){
         IPage<EgDevice> page = new Page<>(requestVO.getCurrent(),requestVO.getSize());
-        Integer tenantId ;
+        Integer tenantId;
 
-        if(StringUtils.isNotBlank(requestVO.getDomain())){
-            tenantId = egDeviceMapper.sysTenantId(requestVO.getDomain());
+        if(StringUtils.isNotBlank(requestVO.getDeviceCode())){
+            LambdaQueryWrapper<EgDevice> tempQueryWrapper = new LambdaQueryWrapper<>();
+            tempQueryWrapper.eq(EgDevice::getDeviceCode, requestVO.getDeviceCode());
+            EgDevice tempDevice = egDeviceMapper.selectOne(tempQueryWrapper);
+            if(tempDevice == null){
+                // 未查询到数据,返回空数组
+                return new CommonPage<>(new ArrayList<>(), 0L, requestVO.getSize(), requestVO.getCurrent());
+            }
+            tenantId = tempDevice.getTenantId();
         }else{
             tenantId = SecurityUtils.getTenantId();
         }
@@ -69,10 +80,61 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
                 .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()){
+            // 检查设备心跳并更新serviceStatus
+            LocalDateTime now = LocalDateTime.now();
+            List<String> validDeviceCodes = new ArrayList<>();
+            for (EgDevice device : page.getRecords()) {
+                if (StringUtils.isNotBlank(device.getDeviceCode())) {
+                    validDeviceCodes.add(device.getDeviceCode());
+                }
+            }
+
+            // 批量查询心跳数据
+            Map<String, LocalDateTime> deviceHeartbeatMap = new HashMap<>();
+            if (!validDeviceCodes.isEmpty()) {
+                LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatQueryWrapper = Wrappers.lambdaQuery();
+                heartbeatQueryWrapper.in(EgDeviceHeartbeat::getDeviceCode, validDeviceCodes)
+                        .orderByDesc(EgDeviceHeartbeat::getCreateTime);
+                List<EgDeviceHeartbeat> heartbeatList = egDeviceHeartbeatMapper.selectList(heartbeatQueryWrapper);
+                
+                // 构建deviceCode到最新心跳时间的映射
+                for (EgDeviceHeartbeat heartbeat : heartbeatList) {
+                    String deviceCode = heartbeat.getDeviceCode();
+                    if (StringUtils.isNotBlank(deviceCode) && heartbeat.getCreateTime() != null) {
+                        // 如果已存在,保留最新的(因为已按createTime降序排列)
+                        deviceHeartbeatMap.putIfAbsent(deviceCode, heartbeat.getCreateTime());
+                    }
+                }
+            }
+
+            // 更新每个设备的deviceStatus并保存到数据库
+            for (EgDevice device : page.getRecords()) {
+                String deviceCode = device.getDeviceCode();
+                Integer newDeviceStatus;
+                if (StringUtils.isNotBlank(deviceCode) && deviceHeartbeatMap.containsKey(deviceCode)) {
+                    LocalDateTime heartbeatTime = deviceHeartbeatMap.get(deviceCode);
+                    // 检查心跳时间是否在5分钟内
+                    if (heartbeatTime != null && heartbeatTime.isAfter(now.minusMinutes(5))) {
+                        newDeviceStatus = 1;
+                    } else {
+                        newDeviceStatus = 0;
+                    }
+                } else {
+                    // 没有心跳记录,设置为离线
+                    newDeviceStatus = 0;
+                }
+                device.setDeviceStatus(newDeviceStatus);
+                // 更新数据库中的deviceStatus
+                EgDevice updateDevice = new EgDevice();
+                updateDevice.setId(device.getId());
+                updateDevice.setDeviceStatus(newDeviceStatus);
+                this.updateById(updateDevice);
+            }
 
             LambdaQueryWrapper<MeetingFace> meetingFaceQuery = Wrappers.lambdaQuery();
             meetingFaceQuery.select(MeetingFace::getFid,MeetingFace::getCreateTime,MeetingFace::getVefNum,MeetingFace::getFaceName,MeetingFace::getRemark,MeetingFace::getFaceStatus,MeetingFace::getCardNum,MeetingFace::getBindDevice,MeetingFace::getDeptId,MeetingFace::getTenantId,MeetingFace::getUserId)
@@ -122,7 +184,7 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
                     .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,deviceFid)
+                    .in(EgDevice::getId, (Object[]) deviceFid)
                     .eq(EgDevice::getTenantId,SecurityUtils.getTenantId())
                     .orderByDesc(EgDevice::getId);
             page = this.page(page,queryWrapper);
@@ -134,17 +196,53 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
 
     @Override
     public void add(EgDevice egDevice){
-        if(checkNameUnique(egDevice)){
-            throw new BusinessException("新增门禁门号设备'"+egDevice.getDeviceId()+","+egDevice.getEgNumber()+"'失败,设备已存在");
-        }
+
         if(checkDeviceNameUnique(egDevice)){
             throw new BusinessException("新增门禁设备'"+egDevice.getDeviceName()+"'失败,设备已存在");
         }
 
+        // 通过IP校验心跳表中的IP是否一致并且心跳时间必须在五分钟之内
+        if(StringUtils.isNotBlank(egDevice.getDeviceIp())){
+            LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatQueryWrapper = Wrappers.lambdaQuery();
+            heartbeatQueryWrapper.eq(EgDeviceHeartbeat::getIpAddr, egDevice.getDeviceIp())
+                    .orderByDesc(EgDeviceHeartbeat::getCreateTime)
+                    .last("LIMIT 1");
+            EgDeviceHeartbeat heartbeat = egDeviceHeartbeatMapper.selectOne(heartbeatQueryWrapper);
+            
+            // 如果找不到心跳记录,抛出异常
+            if(heartbeat == null){
+                throw new BusinessException("设备不在线,请检查设备网络是否正常!");
+            }
+            
+            // 检查心跳时间是否在5分钟内
+            LocalDateTime now = LocalDateTime.now();
+            LocalDateTime heartbeatTime = heartbeat.getCreateTime();
+            if(heartbeatTime == null || !heartbeatTime.isAfter(now.minusMinutes(5))){
+                throw new BusinessException("设备不在线,请检查设备网络是否正常!");
+            }
+            
+            // 校验通过,设置device_code和device_status=1
+            egDevice.setDeviceCode(heartbeat.getDeviceCode());
+            egDevice.setDeviceStatus(1);
+            
+            // 校验DeviceCode是否已经被其他设备使用
+            if(StringUtils.isNotBlank(egDevice.getDeviceCode())){
+                LambdaQueryWrapper<EgDevice> deviceCodeCheckQueryWrapper = Wrappers.lambdaQuery();
+                deviceCodeCheckQueryWrapper.eq(EgDevice::getDeviceCode, egDevice.getDeviceCode())
+                        .eq(EgDevice::getTenantId, SecurityUtils.getTenantId());
+                EgDevice existingDevice = this.getOne(deviceCodeCheckQueryWrapper);
+                if(existingDevice != null){
+                    throw new BusinessException("设备已被设备'" + existingDevice.getDeviceName() + "'注册使用,请检查设备配置或更换设备IP!");
+                }
+            }
+        }
+
         egDevice.setDeviceUuid(UUIDUtils.uuid());
         egDevice.setCreateBy(SecurityUtils.getUsername());
         egDevice.setCreateTime(LocalDateTime.now());
         egDevice.setTenantId(SecurityUtils.getTenantId());
+        egDevice.setOpenMode("人脸");// 设置开门方式为人脸
+        egDevice.setWorkStatus("4");// 设置工作状态为4
 
         this.save(egDevice);
 
@@ -158,7 +256,24 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
             }
         }
 
-        remoteIotTaskService.addDeviceInfo("502_USKY", egDevice.getDeviceUuid(),egDevice.getDeviceId()+egDevice.getEgNumber(),egDevice.getDeviceName(),egDevice.getInstallAddress(),egDevice.getServiceStatus());
+        // 调用远程服务添加设备信息,添加异常处理
+        try {
+            String deviceId = egDevice.getDeviceUuid();
+            if(StringUtils.isNotBlank(egDevice.getEgNumber())){
+                deviceId = egDevice.getDeviceUuid() + egDevice.getEgNumber();
+            }
+            // 确保必要参数不为空
+            if(StringUtils.isNotBlank(egDevice.getDeviceUuid()) && StringUtils.isNotBlank(deviceId) 
+                    && StringUtils.isNotBlank(egDevice.getDeviceName())){
+                remoteIotTaskService.addDeviceInfo("502_USKY", egDevice.getDeviceUuid(), deviceId, 
+                        egDevice.getDeviceName(), 
+                        egDevice.getInstallAddress() != null ? egDevice.getInstallAddress() : "", 
+                        egDevice.getServiceStatus() != null ? egDevice.getServiceStatus() : 1);
+            }
+        } catch (Exception e) {
+            // 远程服务调用失败,不抛出异常,允许设备保存成功
+            // 远程服务调用失败可以通过其他方式补偿
+        }
     }
 
     @Override
@@ -179,14 +294,47 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
             }
         }
 
-
-        if(checkNameUnique(egDevice)){
-            throw new BusinessException("修改门禁门号设备'"+egDevice.getDeviceId()+","+egDevice.getEgNumber()+"'失败,设备已存在");
-        }
         if(checkDeviceNameUnique(egDevice)){
             throw new BusinessException("新增门禁设备'"+egDevice.getDeviceName()+"'失败,设备已存在");
         }
 
+        // 通过IP校验心跳表中的IP是否一致并且心跳时间必须在五分钟之内
+        if(StringUtils.isNotBlank(egDevice.getDeviceIp())){
+            LambdaQueryWrapper<EgDeviceHeartbeat> heartbeatQueryWrapper = Wrappers.lambdaQuery();
+            heartbeatQueryWrapper.eq(EgDeviceHeartbeat::getIpAddr, egDevice.getDeviceIp())
+                    .orderByDesc(EgDeviceHeartbeat::getCreateTime)
+                    .last("LIMIT 1");
+            EgDeviceHeartbeat heartbeat = egDeviceHeartbeatMapper.selectOne(heartbeatQueryWrapper);
+            
+            // 如果找不到心跳记录,抛出异常
+            if(heartbeat == null){
+                throw new BusinessException("设备不在线,请检查设备网络是否正常!");
+            }
+            
+            // 检查心跳时间是否在5分钟内
+            LocalDateTime now = LocalDateTime.now();
+            LocalDateTime heartbeatTime = heartbeat.getCreateTime();
+            if(heartbeatTime == null || !heartbeatTime.isAfter(now.minusMinutes(5))){
+                throw new BusinessException("设备不在线,请检查设备网络是否正常!");
+            }
+            
+            // 校验通过,设置device_code和device_status=1
+            egDevice.setDeviceCode(heartbeat.getDeviceCode());
+            egDevice.setDeviceStatus(1);
+            
+            // 校验DeviceCode是否已经被其他设备使用(排除当前设备自己)
+            if(StringUtils.isNotBlank(egDevice.getDeviceCode())){
+                LambdaQueryWrapper<EgDevice> deviceCodeCheckQueryWrapper = Wrappers.lambdaQuery();
+                deviceCodeCheckQueryWrapper.eq(EgDevice::getDeviceCode, egDevice.getDeviceCode())
+                        .eq(EgDevice::getTenantId, SecurityUtils.getTenantId())
+                        .ne(EgDevice::getId, egDevice.getId());
+                EgDevice existingDevice = this.getOne(deviceCodeCheckQueryWrapper);
+                if(existingDevice != null){
+                    throw new BusinessException("设备编码'" + egDevice.getDeviceCode() + "'已被设备'" + existingDevice.getDeviceName() + "'使用,请检查设备配置");
+                }
+            }
+        }
+
         egDevice.setUpdateBy(SecurityUtils.getUsername());
         egDevice.setUpdateTime(LocalDateTime.now());
 
@@ -199,9 +347,7 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
         EgDevice one = this.getById(egDevice.getId());
         egDevice.setBindFace(one.getBindFace());
 
-        if(checkNameUnique(egDevice)){
-            throw new BusinessException("更新门禁设备附加功能'"+egDevice.getDeviceId()+"'失败,设备已存在");
-        }
+
         if(checkDeviceNameUnique(egDevice)){
             throw new BusinessException("新增门禁设备'"+egDevice.getDeviceName()+"'失败,设备已存在");
         }
@@ -226,7 +372,14 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
 
         this.removeById(id);
 
-        remoteIotTaskService.deleteDeviceInfo(egDevice.getDeviceUuid());
+        // 调用远程服务删除设备信息,静默处理,失败不影响删除功能
+        try {
+            if(StringUtils.isNotBlank(egDevice.getDeviceUuid())){
+                remoteIotTaskService.deleteDeviceInfo(egDevice.getDeviceUuid());
+            }
+        } catch (Exception e) {
+            // 远程服务调用失败,静默处理,不影响设备删除
+        }
     }
 
     @Override
@@ -255,10 +408,21 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
         Integer tenantId;
         long commandUserId;
         String commandUserName;
-        if(StringUtils.isNotBlank(domain)){
-            tenantId = baseMapper.sysTenantId(domain);
-            commandUserId = userId;
-            commandUserName = userName;
+        EgDevice device = null;
+        
+        // 通过deviceUuid查询设备获取tenantId
+        if(StringUtils.isNotBlank(deviceUuid)){
+            LambdaQueryWrapper<EgDevice> deviceQueryWrapper = Wrappers.lambdaQuery();
+            deviceQueryWrapper.select(EgDevice::getId, EgDevice::getTenantId)
+                    .eq(EgDevice::getDeviceUuid, deviceUuid);
+            device = this.getOne(deviceQueryWrapper);
+            if(device != null){
+                tenantId = device.getTenantId();
+                commandUserId = userId;
+                commandUserName = userName;
+            }else{
+                throw new BusinessException("设备未注册,请先注册");
+            }
         }else{
             tenantId = SecurityUtils.getTenantId();
             commandUserId = SecurityUtils.getUserId();
@@ -266,29 +430,22 @@ public class EgDeviceServiceImpl extends AbstractCrudService<EgDeviceMapper, EgD
         }
 
         //人员设备权限校验,校验通过,可以下发命令控制设备
-        Integer fid = baseMapper.getMeetingFaceData(commandUserId);
-        if(fid == null){
-            throw new BusinessException("人脸卡号信息未注册");
-        }
-        Integer[] deviceFid = baseMapper.getMeetingFaceDeviceList(fid);
-        if(deviceFid.length == 0){
-            throw new BusinessException("人员未绑定设备,请检查");
-        }
-
-        LambdaQueryWrapper<EgDevice> queryWrapper = Wrappers.lambdaQuery();
-        queryWrapper.select(EgDevice::getId)
-                .eq(EgDevice::getDeviceUuid,deviceUuid);
-        EgDevice one = this.getOne(queryWrapper);
-        if(one != null){
-            boolean exist = Arrays.asList(deviceFid).contains(one.getId());
+        if(device != null){
+            Integer fid = baseMapper.getMeetingFaceData(commandUserId);
+            if(fid == null){
+                throw new BusinessException("人脸卡号信息未注册");
+            }
+            Integer[] deviceFid = baseMapper.getMeetingFaceDeviceList(fid);
+            if(deviceFid.length == 0){
+                throw new BusinessException("人员未绑定设备,请检查");
+            }
+            
+            boolean exist = Arrays.asList(deviceFid).contains(device.getId());
             if(!exist){
                 throw new BusinessException("暂无权限");
             }
-        }else{
-            throw new BusinessException("设备未注册,请先注册");
         }
 
-
         Map<String,Object> map = new HashMap<>();
         map.put("method","control");
         map.put("deviceUuid", deviceUuid);

+ 17 - 9
service-eg/service-eg-biz/src/main/java/com/usky/eg/service/impl/EgRecordServiceImpl.java

@@ -47,23 +47,31 @@ public class EgRecordServiceImpl extends AbstractCrudService<EgRecordMapper, EgR
     @Override
     public void add(EgRecord egRecord){
         Integer tenantId;
-        if(StringUtils.isNotBlank(egRecord.getDomain())){
-            tenantId = egDeviceMapper.sysTenantId(egRecord.getDomain());
-        }else{
-            tenantId = SecurityUtils.getTenantId();
-        }
-
+        
+        // 通过deviceUuid查询设备获取tenantId
         LambdaQueryWrapper<EgDevice> queryWrapper = Wrappers.lambdaQuery();
         if(StringUtils.isBlank(egRecord.getDeviceUuid())){
             throw new BusinessException("设备Uuid不能为空");
         }
-        queryWrapper.eq(EgDevice::getDeviceUuid,egRecord.getDeviceUuid());
+        queryWrapper.select(EgDevice::getId, EgDevice::getTenantId)
+                .eq(EgDevice::getDeviceUuid,egRecord.getDeviceUuid());
         EgDevice one = egDeviceService.getOne(queryWrapper);
+        if(one == null){
+            throw new BusinessException("设备未注册,请先注册");
+        }
+        tenantId = one.getTenantId();
         egRecord.setEgDeviceId(one.getId());
-
         egRecord.setCreateTime(LocalDateTime.now());
         egRecord.setTenantId(tenantId);
-        egRecord.setDeptId(SecurityUtils.getLoginUser().getSysUser().getDeptId().intValue());
+        
+        // 安全获取部门ID
+        Integer deptId = null;
+        if(SecurityUtils.getLoginUser() != null 
+                && SecurityUtils.getLoginUser().getSysUser() != null 
+                && SecurityUtils.getLoginUser().getSysUser().getDeptId() != null){
+            deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId().intValue();
+        }
+        egRecord.setDeptId(deptId);
 
         this.save(egRecord);
     }

+ 5 - 0
service-eg/service-eg-biz/src/main/java/com/usky/eg/service/vo/EgDeviceRequestVO.java

@@ -45,4 +45,9 @@ public class EgDeviceRequestVO implements Serializable {
      * 设备uuid
      */
     private String deviceUuid;
+
+    /**
+     * 设备code
+     */
+    private String deviceCode;
 }