Browse Source

手持表修改切换租户 redis查询异常问题

master
wangwei_123 1 week ago
parent
commit
4cfc8a1a67
  1. 4
      cc-admin-master/yudao-module-hand/src/main/java/cn/iocoder/yudao/module/hand/controller/admin/HandDetectorController.java
  2. 7
      cc-admin-master/yudao-module-hand/src/main/java/cn/iocoder/yudao/module/hand/service/impl/HandDetectorServiceImpl.java

4
cc-admin-master/yudao-module-hand/src/main/java/cn/iocoder/yudao/module/hand/controller/admin/HandDetectorController.java

@ -1,6 +1,7 @@
package cn.iocoder.yudao.module.hand.controller.admin; package cn.iocoder.yudao.module.hand.controller.admin;
import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.hand.dal.HandDetectorDO; import cn.iocoder.yudao.module.hand.dal.HandDetectorDO;
import cn.iocoder.yudao.module.hand.service.HandDetectorService; import cn.iocoder.yudao.module.hand.service.HandDetectorService;
import cn.iocoder.yudao.module.hand.util.RedisKeyUtil; import cn.iocoder.yudao.module.hand.util.RedisKeyUtil;
@ -139,7 +140,8 @@ public class HandDetectorController {
if (null == loginUser) { if (null == loginUser) {
throw exception(HAND_DETECTOR_REDIS_NOT_EXISTS); throw exception(HAND_DETECTOR_REDIS_NOT_EXISTS);
} }
String tenantDeviceHashKey = RedisKeyUtil.getTenantDeviceHashKey(loginUser.getTenantId());
Long tenantId = TenantContextHolder.getTenantId();
String tenantDeviceHashKey = RedisKeyUtil.getTenantDeviceHashKey(tenantId);
Map<Object, Object> handData = handDetectorService.getHandData(tenantDeviceHashKey); Map<Object, Object> handData = handDetectorService.getHandData(tenantDeviceHashKey);
return CommonResult.success(handData); return CommonResult.success(handData);
} }

7
cc-admin-master/yudao-module-hand/src/main/java/cn/iocoder/yudao/module/hand/service/impl/HandDetectorServiceImpl.java

@ -267,21 +267,17 @@ public class HandDetectorServiceImpl implements HandDetectorService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@TenantIgnore @TenantIgnore
public void dataMigrate(HandDetectorSaveReqVO migrateReqVO) { public void dataMigrate(HandDetectorSaveReqVO migrateReqVO) {
// 1. --- 输入校验 ---
if (CollectionUtils.isEmpty(migrateReqVO.getIdList()) || migrateReqVO.getTenantId() == null) { if (CollectionUtils.isEmpty(migrateReqVO.getIdList()) || migrateReqVO.getTenantId() == null) {
// 如果没有提供设备ID或目标租户ID,则直接返回或抛出异常
return; return;
} }
// 2. --- 数据准备 ---
// 一次性查询出所有待迁移的设备实体 // 一次性查询出所有待迁移的设备实体
List<HandDetectorDO> devicesToMigrate = handDetectorMapper.selectByIds(migrateReqVO.getIdList()); List<HandDetectorDO> devicesToMigrate = handDetectorMapper.selectByIds(migrateReqVO.getIdList());
if (CollectionUtils.isEmpty(devicesToMigrate)) { if (CollectionUtils.isEmpty(devicesToMigrate)) {
// 如果根据ID没有查到任何设备,直接返回
return; return;
} }
// 获取旧的租户ID(假设一批迁移的设备都来自同一个租户)
// 获取旧的租户ID
Integer oldTenantId = devicesToMigrate.get(0).getTenantId(); Integer oldTenantId = devicesToMigrate.get(0).getTenantId();
// 新的租户ID // 新的租户ID
Integer newTenantId = migrateReqVO.getTenantId(); Integer newTenantId = migrateReqVO.getTenantId();
@ -296,7 +292,6 @@ public class HandDetectorServiceImpl implements HandDetectorService {
.map(HandDetectorDO::getSn) .map(HandDetectorDO::getSn)
.toList(); .toList();
// 3. --- 数据库更新 ---
// 遍历设备列表,更新租户ID并清空特定于旧租户的关联数据 // 遍历设备列表,更新租户ID并清空特定于旧租户的关联数据
for (HandDetectorDO device : devicesToMigrate) { for (HandDetectorDO device : devicesToMigrate) {
device.setTenantId(newTenantId); device.setTenantId(newTenantId);

Loading…
Cancel
Save