|
|
@@ -6,10 +6,16 @@ import com.aijiuyi.admin.common.util.LogUtil;
|
|
|
import com.aijiuyi.admin.controller.dto.DeviceQueryDTO;
|
|
|
import com.aijiuyi.admin.controller.dto.DeviceSaveDTO;
|
|
|
import com.aijiuyi.admin.entity.Device;
|
|
|
+import com.aijiuyi.admin.entity.DeviceGroup;
|
|
|
+import com.aijiuyi.admin.entity.DeviceGroupMember;
|
|
|
import com.aijiuyi.admin.entity.DeviceUserVO;
|
|
|
import com.aijiuyi.admin.entity.UserDevice;
|
|
|
+import com.aijiuyi.admin.entity.UserProfile;
|
|
|
import com.aijiuyi.admin.mapper.DeviceMapper;
|
|
|
+import com.aijiuyi.admin.mapper.DeviceGroupMapper;
|
|
|
+import com.aijiuyi.admin.mapper.DeviceGroupMemberMapper;
|
|
|
import com.aijiuyi.admin.mapper.UserDeviceMapper;
|
|
|
+import com.aijiuyi.admin.mapper.UserProfileMapper;
|
|
|
import com.aijiuyi.admin.service.DeviceService;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
@@ -34,11 +40,24 @@ import java.util.stream.Collectors;
|
|
|
@Service
|
|
|
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
|
|
|
|
|
|
+ private static final int DEVICE_TYPE_FAMILY = 1;
|
|
|
+ private static final int DEVICE_TYPE_PUBLIC = 2;
|
|
|
+ private static final int STATUS_ENABLED = 1;
|
|
|
+
|
|
|
@Autowired
|
|
|
private UserDeviceMapper userDeviceMapper;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DeviceGroupMapper deviceGroupMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DeviceGroupMemberMapper deviceGroupMemberMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserProfileMapper userProfileMapper;
|
|
|
+
|
|
|
/**
|
|
|
- * 分页查询设备列表(联表聚合绑定用户信息)
|
|
|
+ * 分页查询设备列表(聚合家庭群组成员或公共设备绑定用户)
|
|
|
*
|
|
|
* @param queryDTO 查询条件
|
|
|
* @return 分页结果
|
|
|
@@ -72,12 +91,12 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
Device device = buildDeviceFromDto(dto);
|
|
|
save(device);
|
|
|
// 绑定初始用户
|
|
|
- bindUsers(device.getDeviceCode(), device.getDeviceName(), device.getDeviceModel(), dto.getUserIds());
|
|
|
+ bindUsers(device, dto.getUserIds());
|
|
|
LogUtil.info(DeviceServiceImpl.class, "新增设备[{}],编号[{}]", dto.getDeviceName(), dto.getDeviceCode());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 修改设备信息(可同步更新绑定用户)
|
|
|
+ * 修改设备信息(可同步更新关联用户/成员)
|
|
|
*
|
|
|
* @param dto 设备信息
|
|
|
*/
|
|
|
@@ -88,6 +107,11 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
if (exist == null) {
|
|
|
throw new BusinessException(ResultCode.DEVICE_NOT_FOUND);
|
|
|
}
|
|
|
+ Integer targetDeviceType = resolveDeviceType(dto.getDeviceType());
|
|
|
+ Integer currentDeviceType = resolveDeviceType(exist);
|
|
|
+ if (!targetDeviceType.equals(currentDeviceType) && hasActiveDeviceUsers(exist.getDeviceCode())) {
|
|
|
+ throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "已有绑定用户或群组成员的设备不允许直接修改设备类型");
|
|
|
+ }
|
|
|
// 若修改了序列号,校验唯一性
|
|
|
if (StringUtils.hasText(dto.getSerialNo()) && !dto.getSerialNo().equals(exist.getSerialNo())) {
|
|
|
long serialCount = lambdaQuery()
|
|
|
@@ -103,9 +127,11 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
// 设备编号不允许修改,清除该字段防止误更新
|
|
|
update.setDeviceCode(null);
|
|
|
updateById(update);
|
|
|
- // 若传入了 userIds(包括空列表),则同步更新绑定用户
|
|
|
+ update.setDeviceCode(exist.getDeviceCode());
|
|
|
+ syncUserDeviceInfo(update);
|
|
|
+ // 若传入了 userIds(包括空列表),则同步更新关联用户/成员
|
|
|
if (dto.getUserIds() != null) {
|
|
|
- reconcileUserBindings(exist.getDeviceCode(), exist.getDeviceName(), exist.getDeviceModel(), dto.getUserIds());
|
|
|
+ reconcileDeviceUsers(update, dto.getUserIds());
|
|
|
}
|
|
|
LogUtil.info(DeviceServiceImpl.class, "修改设备[{}]", dto.getId());
|
|
|
}
|
|
|
@@ -139,7 +165,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
if (CollectionUtils.isEmpty(ids)) {
|
|
|
throw new BusinessException(ResultCode.PARAM_ERROR);
|
|
|
}
|
|
|
- // 先查出设备编号再删除,用于解绑用户
|
|
|
+ // 先查出设备编号再删除,用于移除/解绑关联用户
|
|
|
List<Device> devices = listByIds(ids);
|
|
|
removeByIds(ids);
|
|
|
for (Device device : devices) {
|
|
|
@@ -171,10 +197,10 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 查询设备绑定的用户列表
|
|
|
+ * 查询设备关联用户/成员列表
|
|
|
*
|
|
|
* @param deviceId 设备ID
|
|
|
- * @return 绑定用户列表
|
|
|
+ * @return 关联用户/成员列表
|
|
|
*/
|
|
|
@Override
|
|
|
public List<DeviceUserVO> getDeviceUsers(Long deviceId) {
|
|
|
@@ -186,7 +212,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 为设备添加绑定用户
|
|
|
+ * 为设备添加成员/绑定用户
|
|
|
*
|
|
|
* @param deviceId 设备ID
|
|
|
* @param profileId 用户档案ID
|
|
|
@@ -197,27 +223,12 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
if (device == null) {
|
|
|
throw new BusinessException(ResultCode.DEVICE_NOT_FOUND);
|
|
|
}
|
|
|
- // 检查用户是否已绑定该设备
|
|
|
- long count = userDeviceMapper.selectCount(
|
|
|
- new LambdaQueryWrapper<UserDevice>()
|
|
|
- .eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
- .eq(UserDevice::getUserId, profileId)
|
|
|
- .eq(UserDevice::getStatus, 1)
|
|
|
- );
|
|
|
- if (count > 0) {
|
|
|
- throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND);
|
|
|
+ if (isFamilyDevice(device)) {
|
|
|
+ addFamilyDeviceUser(device, profileId);
|
|
|
+ } else {
|
|
|
+ addPublicDeviceUser(device, profileId);
|
|
|
}
|
|
|
- UserDevice ud = new UserDevice();
|
|
|
- ud.setUserId(profileId);
|
|
|
- ud.setDeviceCode(device.getDeviceCode());
|
|
|
- ud.setDeviceName(device.getDeviceName());
|
|
|
- ud.setDeviceModel(device.getDeviceModel());
|
|
|
- ud.setStatus(1);
|
|
|
- ud.setIsPrimary(0);
|
|
|
- ud.setOnlineStatus(0);
|
|
|
- ud.setBindTime(LocalDateTime.now());
|
|
|
- userDeviceMapper.insert(ud);
|
|
|
- LogUtil.info(DeviceServiceImpl.class, "设备[{}]添加绑定用户[{}]", device.getDeviceCode(), profileId);
|
|
|
+ LogUtil.info(DeviceServiceImpl.class, "设备[{}]添加关联用户[{}]", device.getDeviceCode(), profileId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -232,18 +243,16 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
if (device == null) {
|
|
|
throw new BusinessException(ResultCode.DEVICE_NOT_FOUND);
|
|
|
}
|
|
|
- LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
- wrapper.eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
- .eq(UserDevice::getUserId, profileId)
|
|
|
- .eq(UserDevice::getStatus, 1)
|
|
|
- .set(UserDevice::getStatus, 0)
|
|
|
- .set(UserDevice::getIsPrimary, 0);
|
|
|
- userDeviceMapper.update(null, wrapper);
|
|
|
- LogUtil.info(DeviceServiceImpl.class, "设备[{}]解绑用户[{}]", device.getDeviceCode(), profileId);
|
|
|
+ if (isFamilyDevice(device)) {
|
|
|
+ removeFamilyDeviceUser(device, profileId);
|
|
|
+ } else {
|
|
|
+ removePublicDeviceUser(device, profileId);
|
|
|
+ }
|
|
|
+ LogUtil.info(DeviceServiceImpl.class, "设备[{}]移除关联用户[{}]", device.getDeviceCode(), profileId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设为主用户(同一设备下只能有一个主用户)
|
|
|
+ * 设为主用户(仅公共设备兼容旧绑定关系,家庭设备无主用户概念)
|
|
|
*
|
|
|
* @param deviceId 设备ID
|
|
|
* @param profileId 用户档案ID
|
|
|
@@ -255,27 +264,30 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
if (device == null) {
|
|
|
throw new BusinessException(ResultCode.DEVICE_NOT_FOUND);
|
|
|
}
|
|
|
+ if (isFamilyDevice(device)) {
|
|
|
+ throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "家庭设备没有主用户概念");
|
|
|
+ }
|
|
|
// 先清除该设备下所有用户的主用户标记
|
|
|
LambdaUpdateWrapper<UserDevice> clearWrapper = new LambdaUpdateWrapper<>();
|
|
|
clearWrapper.eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
- .eq(UserDevice::getStatus, 1)
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
.set(UserDevice::getIsPrimary, 0);
|
|
|
userDeviceMapper.update(null, clearWrapper);
|
|
|
// 再将目标用户设为主用户
|
|
|
LambdaUpdateWrapper<UserDevice> setWrapper = new LambdaUpdateWrapper<>();
|
|
|
setWrapper.eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
.eq(UserDevice::getUserId, profileId)
|
|
|
- .eq(UserDevice::getStatus, 1)
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
.set(UserDevice::getIsPrimary, 1);
|
|
|
userDeviceMapper.update(null, setWrapper);
|
|
|
LogUtil.info(DeviceServiceImpl.class, "设备[{}]设置主用户[{}]", device.getDeviceCode(), profileId);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 批量解绑设备用户(通过绑定记录ID批量解绑)
|
|
|
+ * 批量移除设备成员/解绑设备用户(通过关联记录ID批量处理)
|
|
|
*
|
|
|
* @param deviceId 设备ID(校验用)
|
|
|
- * @param bindIds 绑定记录ID列表(user_device.id)
|
|
|
+ * @param bindIds 关联记录ID列表(家庭设备为 device_group_member.id,公共设备为 user_device.id)
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
@@ -283,12 +295,21 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
if (CollectionUtils.isEmpty(bindIds)) {
|
|
|
return;
|
|
|
}
|
|
|
- LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
- wrapper.in(UserDevice::getId, bindIds)
|
|
|
- .set(UserDevice::getStatus, 0)
|
|
|
- .set(UserDevice::getIsPrimary, 0);
|
|
|
- userDeviceMapper.update(null, wrapper);
|
|
|
- LogUtil.info(DeviceServiceImpl.class, "设备[{}]批量解绑用户,共[{}]条", deviceId, bindIds.size());
|
|
|
+ Device device = getById(deviceId);
|
|
|
+ if (device == null) {
|
|
|
+ throw new BusinessException(ResultCode.DEVICE_NOT_FOUND);
|
|
|
+ }
|
|
|
+ if (isFamilyDevice(device)) {
|
|
|
+ batchRemoveFamilyDeviceUsers(device, bindIds);
|
|
|
+ } else {
|
|
|
+ LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.in(UserDevice::getId, bindIds)
|
|
|
+ .eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
+ .set(UserDevice::getStatus, 0)
|
|
|
+ .set(UserDevice::getIsPrimary, 0);
|
|
|
+ userDeviceMapper.update(null, wrapper);
|
|
|
+ }
|
|
|
+ LogUtil.info(DeviceServiceImpl.class, "设备[{}]批量移除关联用户,共[{}]条", deviceId, bindIds.size());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -316,55 +337,62 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
|
|
|
private Integer resolveDeviceType(Integer deviceType) {
|
|
|
if (deviceType == null) {
|
|
|
- return 1;
|
|
|
+ return DEVICE_TYPE_FAMILY;
|
|
|
}
|
|
|
- if (!Integer.valueOf(1).equals(deviceType) && !Integer.valueOf(2).equals(deviceType)) {
|
|
|
+ if (!Integer.valueOf(DEVICE_TYPE_FAMILY).equals(deviceType) && !Integer.valueOf(DEVICE_TYPE_PUBLIC).equals(deviceType)) {
|
|
|
throw new BusinessException(ResultCode.PARAM_ERROR.getCode(), "设备类型不合法");
|
|
|
}
|
|
|
return deviceType;
|
|
|
}
|
|
|
|
|
|
+ private Integer resolveDeviceType(Device device) {
|
|
|
+ return resolveDeviceType(device.getDeviceType());
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean isFamilyDevice(Device device) {
|
|
|
+ return Integer.valueOf(DEVICE_TYPE_FAMILY).equals(resolveDeviceType(device));
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * 为设备批量绑定用户(新增时使用)
|
|
|
+ * 为设备批量关联用户(新增时使用)
|
|
|
*
|
|
|
- * @param deviceCode 设备编号
|
|
|
- * @param deviceName 设备名称
|
|
|
- * @param deviceModel 设备型号
|
|
|
- * @param userIds 用户ID列表
|
|
|
+ * @param device 设备信息
|
|
|
+ * @param userIds 用户档案ID列表
|
|
|
*/
|
|
|
- private void bindUsers(String deviceCode, String deviceName, String deviceModel, List<Long> userIds) {
|
|
|
+ private void bindUsers(Device device, List<Long> userIds) {
|
|
|
if (CollectionUtils.isEmpty(userIds)) {
|
|
|
return;
|
|
|
}
|
|
|
for (Long profileId : userIds) {
|
|
|
- UserDevice ud = new UserDevice();
|
|
|
- ud.setUserId(profileId);
|
|
|
- ud.setDeviceCode(deviceCode);
|
|
|
- ud.setDeviceName(deviceName);
|
|
|
- ud.setDeviceModel(deviceModel);
|
|
|
- ud.setStatus(1);
|
|
|
- ud.setIsPrimary(0);
|
|
|
- ud.setOnlineStatus(0);
|
|
|
- ud.setBindTime(LocalDateTime.now());
|
|
|
- userDeviceMapper.insert(ud);
|
|
|
+ if (isFamilyDevice(device)) {
|
|
|
+ addFamilyDeviceUser(device, profileId);
|
|
|
+ } else {
|
|
|
+ addPublicDeviceUser(device, profileId);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 同步更新设备绑定用户(编辑时使用)
|
|
|
- * 对比当前绑定用户和新列表,新增差集,解绑多余项
|
|
|
+ * 同步更新设备关联用户(编辑时使用)
|
|
|
+ * 家庭设备同步群组成员,公共设备同步 user_device 绑定关系。
|
|
|
*
|
|
|
- * @param deviceCode 设备编号
|
|
|
- * @param deviceName 设备名称
|
|
|
- * @param deviceModel 设备型号
|
|
|
- * @param newUserIds 新的用户ID列表
|
|
|
+ * @param device 设备信息
|
|
|
+ * @param newUserIds 新的用户档案ID列表
|
|
|
*/
|
|
|
- private void reconcileUserBindings(String deviceCode, String deviceName, String deviceModel, List<Long> newUserIds) {
|
|
|
+ private void reconcileDeviceUsers(Device device, List<Long> newUserIds) {
|
|
|
+ if (isFamilyDevice(device)) {
|
|
|
+ reconcileFamilyDeviceUsers(device, newUserIds);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ reconcilePublicDeviceUsers(device, newUserIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reconcilePublicDeviceUsers(Device device, List<Long> newUserIds) {
|
|
|
// 查询当前已绑定的用户
|
|
|
List<UserDevice> currentBindings = userDeviceMapper.selectList(
|
|
|
new LambdaQueryWrapper<UserDevice>()
|
|
|
- .eq(UserDevice::getDeviceCode, deviceCode)
|
|
|
- .eq(UserDevice::getStatus, 1)
|
|
|
+ .eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
);
|
|
|
Set<Long> currentIds = currentBindings.stream()
|
|
|
.map(UserDevice::getUserId)
|
|
|
@@ -383,20 +411,263 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
// 新增不在当前绑定中的用户
|
|
|
for (Long profileId : newUserIds) {
|
|
|
if (!currentIds.contains(profileId)) {
|
|
|
- UserDevice ud = new UserDevice();
|
|
|
- ud.setUserId(profileId);
|
|
|
- ud.setDeviceCode(deviceCode);
|
|
|
- ud.setDeviceName(deviceName);
|
|
|
- ud.setDeviceModel(deviceModel);
|
|
|
- ud.setStatus(1);
|
|
|
- ud.setIsPrimary(0);
|
|
|
- ud.setOnlineStatus(0);
|
|
|
- ud.setBindTime(LocalDateTime.now());
|
|
|
- userDeviceMapper.insert(ud);
|
|
|
+ addPublicDeviceUser(device, profileId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void reconcileFamilyDeviceUsers(Device device, List<Long> newUserIds) {
|
|
|
+ DeviceGroup group = ensureDeviceGroup(device.getDeviceCode());
|
|
|
+ List<DeviceGroupMember> currentMembers = deviceGroupMemberMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DeviceGroupMember>()
|
|
|
+ .eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ );
|
|
|
+ Set<Long> currentIds = currentMembers.stream()
|
|
|
+ .map(DeviceGroupMember::getProfileId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ Set<Long> newIds = new HashSet<>(newUserIds);
|
|
|
+
|
|
|
+ for (DeviceGroupMember member : currentMembers) {
|
|
|
+ if (!newIds.contains(member.getProfileId())) {
|
|
|
+ removeFamilyDeviceUser(device, member.getProfileId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (Long profileId : newUserIds) {
|
|
|
+ if (!currentIds.contains(profileId)) {
|
|
|
+ addFamilyDeviceUser(device, profileId);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void addFamilyDeviceUser(Device device, Long profileId) {
|
|
|
+ UserProfile profile = requireProfile(profileId);
|
|
|
+ DeviceGroup group = ensureDeviceGroup(device.getDeviceCode());
|
|
|
+ DeviceGroupMember current = deviceGroupMemberMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<DeviceGroupMember>()
|
|
|
+ .eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .eq(DeviceGroupMember::getProfileId, profileId)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+ if (current != null && Integer.valueOf(STATUS_ENABLED).equals(current.getStatus())) {
|
|
|
+ throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND);
|
|
|
+ }
|
|
|
+ checkFamilyMemberUnique(group.getId(), profile, profileId);
|
|
|
+ if (current == null) {
|
|
|
+ DeviceGroupMember member = new DeviceGroupMember();
|
|
|
+ member.setGroupId(group.getId());
|
|
|
+ member.setProfileId(profileId);
|
|
|
+ member.setAppUserId(profile.getUserId());
|
|
|
+ member.setStatus(STATUS_ENABLED);
|
|
|
+ member.setJoinTime(LocalDateTime.now());
|
|
|
+ deviceGroupMemberMapper.insert(member);
|
|
|
+ } else {
|
|
|
+ LambdaUpdateWrapper<DeviceGroupMember> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(DeviceGroupMember::getId, current.getId())
|
|
|
+ .set(DeviceGroupMember::getAppUserId, profile.getUserId())
|
|
|
+ .set(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ .set(DeviceGroupMember::getJoinTime, LocalDateTime.now());
|
|
|
+ deviceGroupMemberMapper.update(null, wrapper);
|
|
|
+ }
|
|
|
+ ensureUserDevice(device, profileId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void removeFamilyDeviceUser(Device device, Long profileId) {
|
|
|
+ DeviceGroup group = findDeviceGroup(device.getDeviceCode());
|
|
|
+ if (group != null) {
|
|
|
+ LambdaUpdateWrapper<DeviceGroupMember> memberWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ memberWrapper.eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .eq(DeviceGroupMember::getProfileId, profileId)
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ .set(DeviceGroupMember::getStatus, 0);
|
|
|
+ deviceGroupMemberMapper.update(null, memberWrapper);
|
|
|
+ }
|
|
|
+ removePublicDeviceUser(device, profileId);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void batchRemoveFamilyDeviceUsers(Device device, List<Long> memberIds) {
|
|
|
+ DeviceGroup group = findDeviceGroup(device.getDeviceCode());
|
|
|
+ if (group == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<DeviceGroupMember> members = deviceGroupMemberMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DeviceGroupMember>()
|
|
|
+ .in(DeviceGroupMember::getId, memberIds)
|
|
|
+ .eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ );
|
|
|
+ LambdaUpdateWrapper<DeviceGroupMember> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.in(DeviceGroupMember::getId, memberIds)
|
|
|
+ .eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .set(DeviceGroupMember::getStatus, 0);
|
|
|
+ deviceGroupMemberMapper.update(null, wrapper);
|
|
|
+ for (DeviceGroupMember member : members) {
|
|
|
+ removePublicDeviceUser(device, member.getProfileId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addPublicDeviceUser(Device device, Long profileId) {
|
|
|
+ requireProfile(profileId);
|
|
|
+ UserDevice current = userDeviceMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<UserDevice>()
|
|
|
+ .eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
+ .eq(UserDevice::getUserId, profileId)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+ if (current != null && Integer.valueOf(STATUS_ENABLED).equals(current.getStatus())) {
|
|
|
+ throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND);
|
|
|
+ }
|
|
|
+ if (current == null) {
|
|
|
+ UserDevice ud = new UserDevice();
|
|
|
+ ud.setUserId(profileId);
|
|
|
+ ud.setDeviceCode(device.getDeviceCode());
|
|
|
+ ud.setDeviceName(device.getDeviceName());
|
|
|
+ ud.setDeviceModel(device.getDeviceModel());
|
|
|
+ ud.setStatus(STATUS_ENABLED);
|
|
|
+ ud.setIsPrimary(0);
|
|
|
+ ud.setOnlineStatus(0);
|
|
|
+ ud.setBindTime(LocalDateTime.now());
|
|
|
+ userDeviceMapper.insert(ud);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(UserDevice::getId, current.getId())
|
|
|
+ .set(UserDevice::getDeviceName, device.getDeviceName())
|
|
|
+ .set(UserDevice::getDeviceModel, device.getDeviceModel())
|
|
|
+ .set(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
+ .set(UserDevice::getIsPrimary, 0)
|
|
|
+ .set(UserDevice::getBindTime, LocalDateTime.now());
|
|
|
+ userDeviceMapper.update(null, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ensureUserDevice(Device device, Long profileId) {
|
|
|
+ UserDevice current = userDeviceMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<UserDevice>()
|
|
|
+ .eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
+ .eq(UserDevice::getUserId, profileId)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+ if (current == null) {
|
|
|
+ UserDevice ud = new UserDevice();
|
|
|
+ ud.setUserId(profileId);
|
|
|
+ ud.setDeviceCode(device.getDeviceCode());
|
|
|
+ ud.setDeviceName(device.getDeviceName());
|
|
|
+ ud.setDeviceModel(device.getDeviceModel());
|
|
|
+ ud.setStatus(STATUS_ENABLED);
|
|
|
+ ud.setIsPrimary(0);
|
|
|
+ ud.setOnlineStatus(0);
|
|
|
+ ud.setBindTime(LocalDateTime.now());
|
|
|
+ userDeviceMapper.insert(ud);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(UserDevice::getId, current.getId())
|
|
|
+ .set(UserDevice::getDeviceName, device.getDeviceName())
|
|
|
+ .set(UserDevice::getDeviceModel, device.getDeviceModel())
|
|
|
+ .set(UserDevice::getStatus, STATUS_ENABLED);
|
|
|
+ userDeviceMapper.update(null, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void removePublicDeviceUser(Device device, Long profileId) {
|
|
|
+ LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
+ .eq(UserDevice::getUserId, profileId)
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
+ .set(UserDevice::getStatus, 0)
|
|
|
+ .set(UserDevice::getIsPrimary, 0);
|
|
|
+ userDeviceMapper.update(null, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void syncUserDeviceInfo(Device device) {
|
|
|
+ LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
+ wrapper.eq(UserDevice::getDeviceCode, device.getDeviceCode())
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
+ .set(UserDevice::getDeviceName, device.getDeviceName())
|
|
|
+ .set(UserDevice::getDeviceModel, device.getDeviceModel());
|
|
|
+ userDeviceMapper.update(null, wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ private UserProfile requireProfile(Long profileId) {
|
|
|
+ UserProfile profile = userProfileMapper.selectById(profileId);
|
|
|
+ if (profile == null) {
|
|
|
+ throw new BusinessException(ResultCode.PROFILE_NOT_FOUND);
|
|
|
+ }
|
|
|
+ return profile;
|
|
|
+ }
|
|
|
+
|
|
|
+ private DeviceGroup ensureDeviceGroup(String deviceCode) {
|
|
|
+ DeviceGroup group = findDeviceGroup(deviceCode);
|
|
|
+ if (group != null) {
|
|
|
+ return group;
|
|
|
+ }
|
|
|
+ group = new DeviceGroup();
|
|
|
+ group.setDeviceCode(deviceCode);
|
|
|
+ group.setStatus(STATUS_ENABLED);
|
|
|
+ deviceGroupMapper.insert(group);
|
|
|
+ return group;
|
|
|
+ }
|
|
|
+
|
|
|
+ private DeviceGroup findDeviceGroup(String deviceCode) {
|
|
|
+ return deviceGroupMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<DeviceGroup>()
|
|
|
+ .eq(DeviceGroup::getDeviceCode, deviceCode)
|
|
|
+ .eq(DeviceGroup::getStatus, STATUS_ENABLED)
|
|
|
+ .last("LIMIT 1")
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private void checkFamilyMemberUnique(Long groupId, UserProfile profile, Long excludeProfileId) {
|
|
|
+ if (StringUtils.hasText(profile.getPhone())) {
|
|
|
+ List<DeviceGroupMember> members = deviceGroupMemberMapper.selectList(
|
|
|
+ new LambdaQueryWrapper<DeviceGroupMember>()
|
|
|
+ .eq(DeviceGroupMember::getGroupId, groupId)
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ );
|
|
|
+ for (DeviceGroupMember member : members) {
|
|
|
+ if (member.getProfileId().equals(excludeProfileId)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ UserProfile memberProfile = userProfileMapper.selectById(member.getProfileId());
|
|
|
+ if (memberProfile != null && profile.getPhone().equals(memberProfile.getPhone())) {
|
|
|
+ throw new BusinessException(ResultCode.PHONE_PROFILE_EXISTS.getCode(), "同一家庭群组内手机号不能重复");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (profile.getUserId() != null) {
|
|
|
+ long count = deviceGroupMemberMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<DeviceGroupMember>()
|
|
|
+ .eq(DeviceGroupMember::getGroupId, groupId)
|
|
|
+ .eq(DeviceGroupMember::getAppUserId, profile.getUserId())
|
|
|
+ .ne(DeviceGroupMember::getProfileId, excludeProfileId)
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ );
|
|
|
+ if (count > 0) {
|
|
|
+ throw new BusinessException(ResultCode.DEVICE_USER_ALREADY_BOUND.getCode(), "该账号已在家庭群组中");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private boolean hasActiveDeviceUsers(String deviceCode) {
|
|
|
+ long publicCount = userDeviceMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<UserDevice>()
|
|
|
+ .eq(UserDevice::getDeviceCode, deviceCode)
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
+ );
|
|
|
+ if (publicCount > 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ DeviceGroup group = findDeviceGroup(deviceCode);
|
|
|
+ if (group == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ long memberCount = deviceGroupMemberMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<DeviceGroupMember>()
|
|
|
+ .eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ );
|
|
|
+ return memberCount > 0;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 解绑指定设备的所有用户
|
|
|
*
|
|
|
@@ -405,9 +676,18 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> impleme
|
|
|
private void unbindAllUsers(String deviceCode) {
|
|
|
LambdaUpdateWrapper<UserDevice> wrapper = new LambdaUpdateWrapper<>();
|
|
|
wrapper.eq(UserDevice::getDeviceCode, deviceCode)
|
|
|
- .eq(UserDevice::getStatus, 1)
|
|
|
+ .eq(UserDevice::getStatus, STATUS_ENABLED)
|
|
|
.set(UserDevice::getStatus, 0)
|
|
|
.set(UserDevice::getIsPrimary, 0);
|
|
|
userDeviceMapper.update(null, wrapper);
|
|
|
+
|
|
|
+ DeviceGroup group = findDeviceGroup(deviceCode);
|
|
|
+ if (group != null) {
|
|
|
+ LambdaUpdateWrapper<DeviceGroupMember> memberWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ memberWrapper.eq(DeviceGroupMember::getGroupId, group.getId())
|
|
|
+ .eq(DeviceGroupMember::getStatus, STATUS_ENABLED)
|
|
|
+ .set(DeviceGroupMember::getStatus, 0);
|
|
|
+ deviceGroupMemberMapper.update(null, memberWrapper);
|
|
|
+ }
|
|
|
}
|
|
|
}
|