Commit 159e29c8 by guoxuejian Committed by gdj

Refactor workspaceId handling in DeviceServiceImpl and UserOrgServiceImpl to…

Refactor workspaceId handling in DeviceServiceImpl and UserOrgServiceImpl to improve null checks and reduce redundancy
parent 3c187c77
......@@ -1556,18 +1556,25 @@ public class DeviceServiceImpl extends ServiceImpl<IDeviceMapper, DeviceEntity>
throw new RuntimeException("device does not exist!");
}
String workspaceId = StringUtils.hasText(device.getWorkspaceId())
? device.getWorkspaceId()
: (StringUtils.hasText(dbDevice.getWorkspaceId()) ? dbDevice.getWorkspaceId() : getWorkspaceId());
if (!StringUtils.hasText(workspaceId)) {
throw new RuntimeException("workspaceId is null");
}
// 查询是否已经分享
LambdaQueryWrapper<DeviceOrgEntity> deviceOrgQueryWrapper = new LambdaQueryWrapper<>();
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getDeviceSn, device.getDeviceSn());
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getOrgId, device.getOrgId());
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getWorkspaceId, StringUtils.hasText(device.getWorkspaceId()) ? device.getWorkspaceId() : getWorkspaceId());
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getWorkspaceId, workspaceId);
List<DeviceOrgEntity> deviceOrgEntities = deviceOrgService.list(deviceOrgQueryWrapper);
if (!CollectionUtils.isEmpty(deviceOrgEntities)) {
throw new RuntimeException("the device has been shared with the Org");
}
DeviceOrgEntity deviceOrgEntity = new DeviceOrgEntity();
deviceOrgEntity.setWorkspaceId(StringUtils.hasText(device.getWorkspaceId()) ? device.getWorkspaceId() : getWorkspaceId());
deviceOrgEntity.setWorkspaceId(workspaceId);
deviceOrgEntity.setOrgId(device.getOrgId());
deviceOrgEntity.setDeviceSn(device.getDeviceSn());
deviceOrgEntity.setDeviceId(dbDevice.getId());
......@@ -1600,11 +1607,18 @@ public class DeviceServiceImpl extends ServiceImpl<IDeviceMapper, DeviceEntity>
throw new RuntimeException("device does not exist!");
}
String workspaceId = StringUtils.hasText(device.getWorkspaceId())
? device.getWorkspaceId()
: (StringUtils.hasText(dbDevice.getWorkspaceId()) ? dbDevice.getWorkspaceId() : getWorkspaceId());
if (!StringUtils.hasText(workspaceId)) {
throw new RuntimeException("workspaceId is null");
}
// 删除已经分享的
LambdaQueryWrapper<DeviceOrgEntity> deviceOrgQueryWrapper = new LambdaQueryWrapper<>();
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getDeviceSn, device.getDeviceSn());
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getOrgId, device.getOrgId());
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getWorkspaceId, StringUtils.hasText(device.getWorkspaceId()) ? device.getWorkspaceId() : getWorkspaceId());
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getWorkspaceId, workspaceId);
deviceOrgQueryWrapper.eq(DeviceOrgEntity::getIsShared, 1);
boolean deviceOrgSaveRes = deviceOrgService.remove(deviceOrgQueryWrapper);
......
......@@ -6,10 +6,12 @@ import com.dji.sample.common.util.SecurityUtils; // 修正导入包
import com.dji.sample.manage.dao.IUserOrgMapper;
import com.dji.sample.manage.model.dto.UserOrgDTO;
import com.dji.sample.manage.model.entity.UserOrgEntity;
import com.dji.sample.manage.service.IOrgService;
import com.dji.sample.manage.service.IUserOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
......@@ -24,6 +26,9 @@ public class UserOrgServiceImpl extends ServiceImpl<IUserOrgMapper, UserOrgEntit
@Autowired
private IUserOrgMapper userOrgMapper;
@Autowired
private IOrgService orgService;
@Override
public Optional<UserOrgEntity> getUserOrg(String userId, String orgId) {
LambdaQueryWrapper<UserOrgEntity> queryWrapper = new LambdaQueryWrapper<>();
......@@ -89,9 +94,17 @@ public class UserOrgServiceImpl extends ServiceImpl<IUserOrgMapper, UserOrgEntit
}
// 创建新关联
String workspaceId = orgService.getWorkspaceIdByOrgId(orgId);
if (!StringUtils.hasText(workspaceId)) {
workspaceId = getWorkspaceId();
}
if (!StringUtils.hasText(workspaceId)) {
throw new RuntimeException("workspaceId is null");
}
UserOrgEntity userOrg = new UserOrgEntity();
userOrg.setUserId(userId);
userOrg.setWorkspaceId(getWorkspaceId());
userOrg.setWorkspaceId(workspaceId);
userOrg.setOrgId(orgId);
userOrg.setRoleType(roleType);
userOrg.setStatus(1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment