|
@@ -38,12 +38,13 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
|
|
|
@Autowired
|
|
|
private DevInfoService devInfoService;
|
|
|
-
|
|
|
+
|
|
|
@Autowired
|
|
|
private GroupDevMapMapper groupDevMapMapper;
|
|
|
|
|
|
/**
|
|
|
* 添加群组
|
|
|
+ *
|
|
|
* @param (groupName)群组名称 (userId)用户id
|
|
|
* @return
|
|
|
*/
|
|
@@ -54,10 +55,10 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
DevGroup devGroup = new DevGroup();
|
|
|
devGroup.setGroupName(request.getGroupName());
|
|
|
devGroup.setUserId(request.getUserId());
|
|
|
- if (Objects.isNull(request.getUserId())){
|
|
|
+ if (Objects.isNull(request.getUserId())) {
|
|
|
throw new BizException(ErrorEnum.USER_IS_NOT_EXIST.getErrorCode(), ErrorEnum.USER_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
- devGroup.setGroupUuid(UUID.randomUUID().toString());
|
|
|
+ devGroup.setGroupUuid(UUID.randomUUID().toString().replace("-", ""));
|
|
|
devGroup.setIsDeleted(0);
|
|
|
//2.把群组名称和UUID存储数据库
|
|
|
// TODO mqtt消息 主题 /mps/add_group
|
|
@@ -66,29 +67,65 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
* 删除群组
|
|
|
- * @param (groupId) 群组id
|
|
|
+ *
|
|
|
+ * @param (groupId) 群组id
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public Boolean deleteGroup(Long devGroupId) {
|
|
|
+ public Boolean deleteGroup(Long groupId) {
|
|
|
+ //TODO MQTT消息 主题 /mps/delete_group
|
|
|
//1.查询群组信息
|
|
|
- DevGroup devGroup = devGroupService.getById(devGroupId);
|
|
|
- if (Objects.isNull(devGroup)){
|
|
|
+ DevGroup devGroup = devGroupService.getById(groupId);
|
|
|
+ if (Objects.isNull(devGroup)) {
|
|
|
throw new BizException(ErrorEnum.GROUP_IS_NOT_EXIST.getErrorCode(), ErrorEnum.GROUP_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
- //2.删除群组信息
|
|
|
- devGroup.setIsDeleted(1);
|
|
|
|
|
|
- //3.TODO MQTT消息 主题 /mps/delete_group
|
|
|
- return devGroupService.updateById(devGroup);
|
|
|
+ try {
|
|
|
+ //2.删除群组信息
|
|
|
+ devGroup.setIsDeleted(1);
|
|
|
+ boolean updateGroupResult = devGroupService.updateById(devGroup);
|
|
|
+ if (!updateGroupResult) {
|
|
|
+ log.error("删除群组失败, groupID: {}", groupId);
|
|
|
+ throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "删除群组失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //3.删除群组设备映射关系
|
|
|
+ LambdaUpdateWrapper<GroupDevMap> devMapUpdateWrapper = Wrappers.<GroupDevMap>lambdaUpdate()
|
|
|
+ .eq(GroupDevMap::getGroupId, groupId)
|
|
|
+ .eq(GroupDevMap::getIsDeleted, 0) //只更新未删除记录
|
|
|
+ .set(GroupDevMap::getIsDeleted, 1);
|
|
|
+ int devMapUpdateCount = groupDevMapMapper.update(null, devMapUpdateWrapper);
|
|
|
+ if (devMapUpdateCount < 0) { //MyBatis-Plus在更新失败时返回-1
|
|
|
+ log.error("映射关系表删除失败, groupId: {}", groupId);
|
|
|
+ throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "删除群组设备映射关系失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //4.删除群组分享记录
|
|
|
+ LambdaUpdateWrapper<GroupShare> shareUpdateWrapper = Wrappers.<GroupShare>lambdaUpdate()
|
|
|
+ .eq(GroupShare::getGroupUuid, devGroup.getGroupUuid())
|
|
|
+ .eq(GroupShare::getIsDeleted, 0)
|
|
|
+ .set(GroupShare::getIsDeleted, 1);
|
|
|
+ boolean shareUpdateResult = groupShareService.update(shareUpdateWrapper);
|
|
|
+ if (!shareUpdateResult) {
|
|
|
+ log.error("删除群组分享记录失败, groupUuid: {}", devGroup.getGroupUuid());
|
|
|
+ throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "删除群组分享记录失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (BizException e) {
|
|
|
+ throw e; //业务异常直接抛出
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("删除群组过程中发生异常, groupId: {}, error: {}", groupId, e.getMessage(), e);
|
|
|
+ throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "删除群组失败");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新群组
|
|
|
- * @param (群组Uuid)groupUuid (群组名称)groupName (长度)length (宽度)width (北向夹角)northAngle
|
|
|
+ *
|
|
|
+ * @param (群组Uuid)groupUuid (群组名称)groupName (长度)length (宽度)width (北向夹角)northAngle
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
@@ -114,16 +151,16 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
updateWrapper.set(DevGroup::getLength, params.getLength());
|
|
|
hasUpdate = true;
|
|
|
}
|
|
|
- if (Objects.nonNull(params.getWidth())){
|
|
|
+ if (Objects.nonNull(params.getWidth())) {
|
|
|
updateWrapper.set(DevGroup::getWidth, params.getWidth());
|
|
|
hasUpdate = true;
|
|
|
}
|
|
|
- if (Objects.nonNull(params.getNorthAngle())){
|
|
|
+ if (Objects.nonNull(params.getNorthAngle())) {
|
|
|
updateWrapper.set(DevGroup::getNorthAngle, params.getNorthAngle());
|
|
|
hasUpdate = true;
|
|
|
}
|
|
|
// 如果所有字段均为空,无需更新
|
|
|
- if (!hasUpdate){
|
|
|
+ if (!hasUpdate) {
|
|
|
return false;
|
|
|
}
|
|
|
return devGroupService.update(updateWrapper);
|
|
@@ -132,16 +169,17 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
|
|
|
/**
|
|
|
* 获取群组列表
|
|
|
+ *
|
|
|
* @param (用户id)userId
|
|
|
* @return
|
|
|
*/
|
|
|
@Override
|
|
|
- public List<GroupDTO> queryGroupList(GroupListParams request) {
|
|
|
+ public List<GroupDTO> queryGroupList(Long userId) {
|
|
|
// 1. 查询被分享群组
|
|
|
- List<String> groupUUids = groupShareService.queryGroupUUidByUserId(request.getUserId());
|
|
|
+ List<String> groupUUids = groupShareService.queryGroupUUidByUserId(userId);
|
|
|
|
|
|
// 2. 获取用户自己创建的群组和被分享的群组
|
|
|
- List<DevGroup> devGroups = devGroupService.queryGroupByUserId(request.getUserId(), groupUUids);
|
|
|
+ List<DevGroup> devGroups = devGroupService.queryGroupByUserId(userId, groupUUids);
|
|
|
|
|
|
// 3. 将群组信息转化为DTO
|
|
|
List<GroupDTO> groupDTOs = new ArrayList<>();
|
|
@@ -184,6 +222,7 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
|
|
|
/**
|
|
|
* 获取群组中设备列表
|
|
|
+ *
|
|
|
* @param (群组id)groupId
|
|
|
* @return
|
|
|
*/
|
|
@@ -201,7 +240,7 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
|
|
|
// 2. 查询设备并转换为DTO
|
|
|
List<DevInfo> deviceList = queryDevicesByGroupUuid(devGroup.getGroupUuid());
|
|
|
-
|
|
|
+
|
|
|
// 3. 转换为DTO列表
|
|
|
List<DeviceDTO> deviceDTOs = new ArrayList<>();
|
|
|
for (DevInfo devInfo : deviceList) {
|
|
@@ -209,12 +248,13 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
BeanUtils.copyProperties(devInfo, dto);
|
|
|
deviceDTOs.add(dto);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return deviceDTOs;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 根据群组UUID查询设备列表(私有方法 不对外接口)
|
|
|
+ *
|
|
|
* @param (groupUuid) 群组UUID
|
|
|
* @return 设备列表
|
|
|
*/
|
|
@@ -222,7 +262,7 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
if (Objects.isNull(groupUuid)) {
|
|
|
return Collections.emptyList();
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 1. 查询群组设备映射表获取设备ID列表
|
|
|
LambdaQueryWrapper<GroupDevMap> mapWrapper = new LambdaQueryWrapper<>();
|
|
|
mapWrapper.eq(GroupDevMap::getGroupUuid, groupUuid);
|
|
@@ -246,6 +286,7 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
|
|
|
/**
|
|
|
* 添加设备到群组
|
|
|
+ *
|
|
|
* @param (设备ID)dev_id (群组ID) group_id
|
|
|
* @return
|
|
|
*/
|
|
@@ -258,32 +299,32 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
if (Objects.isNull(devInfo)) {
|
|
|
throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
DevGroup devGroup = devGroupService.getById(params.getGroupId());
|
|
|
if (Objects.isNull(devGroup)) {
|
|
|
throw new BizException(ErrorEnum.GROUP_IS_NOT_EXIST.getErrorCode(), ErrorEnum.GROUP_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 检查设备是否已经添加到群组中
|
|
|
LambdaQueryWrapper<GroupDevMap> queryWrapper = Wrappers.<GroupDevMap>lambdaQuery()
|
|
|
.eq(GroupDevMap::getDevId, devInfo.getClientId())
|
|
|
.eq(GroupDevMap::getGroupUuid, devGroup.getGroupUuid());
|
|
|
-
|
|
|
+
|
|
|
GroupDevMap groupDevMap = groupDevMapMapper.selectOne(queryWrapper);
|
|
|
-
|
|
|
+
|
|
|
// 3. 如果已存在记录
|
|
|
if (Objects.nonNull(groupDevMap)) {
|
|
|
// 检查是否已添加(is_delete为0)
|
|
|
if (groupDevMap.getIsDeleted() == 0) {
|
|
|
throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "请勿重复添加设备");
|
|
|
- }
|
|
|
+ }
|
|
|
// 如果是逻辑删除状态,恢复关系
|
|
|
else {
|
|
|
groupDevMap.setIsDeleted(0);
|
|
|
return groupDevMapMapper.updateById(groupDevMap) > 0;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 4. 不存在记录,创建新的映射关系
|
|
|
GroupDevMap newMap = new GroupDevMap();
|
|
|
newMap.setGroupId(params.getGroupId());
|
|
@@ -291,13 +332,14 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
newMap.setDevId(params.getDevId());
|
|
|
newMap.setClientId(devInfo.getClientId());
|
|
|
newMap.setIsDeleted(0);
|
|
|
-
|
|
|
+
|
|
|
return groupDevMapMapper.insert(newMap) > 0;
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 从群组中删除设备
|
|
|
+ *
|
|
|
* @param (设备ID)dev_id (群组ID) group_id
|
|
|
* @return
|
|
|
*/
|
|
@@ -308,23 +350,26 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
.eq(GroupDevMap::getDevId, request.getDevId())
|
|
|
.eq(GroupDevMap::getGroupId, request.getGroupId())
|
|
|
.eq(GroupDevMap::getIsDeleted, 0);
|
|
|
-
|
|
|
+
|
|
|
GroupDevMap groupDevMap = groupDevMapMapper.selectOne(queryWrapper);
|
|
|
-
|
|
|
+
|
|
|
// 2. 如果关联关系不存在,返回错误
|
|
|
if (Objects.isNull(groupDevMap)) {
|
|
|
- throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "设备不在此群组中");
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST_GROUP.getErrorCode(), "设备未加入此群组");
|
|
|
}
|
|
|
-
|
|
|
- // 3. 将关联关系标记为逻辑删除
|
|
|
- groupDevMap.setIsDeleted(1);
|
|
|
-
|
|
|
+
|
|
|
+ // 3. 创建更新条件
|
|
|
+ LambdaUpdateWrapper<GroupDevMap> updateWrapper = Wrappers.<GroupDevMap>lambdaUpdate()
|
|
|
+ .eq(GroupDevMap::getDevMapId, groupDevMap.getDevMapId())
|
|
|
+ .set(GroupDevMap::getIsDeleted, 1);
|
|
|
+
|
|
|
// 4. 更新数据库并返回结果
|
|
|
- return groupDevMapMapper.updateById(groupDevMap) > 0;
|
|
|
+ return groupDevMapMapper.update(null, updateWrapper) > 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 分享群组
|
|
|
+ *
|
|
|
* @param (群组uuid) groupUuid (分享人userid)sharer (分享人手机号)sharerPhone
|
|
|
* @return 分享结果
|
|
|
*/
|
|
@@ -337,21 +382,24 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
if (Objects.isNull(devGroup)) {
|
|
|
throw new BizException(ErrorEnum.GROUP_IS_NOT_EXIST.getErrorCode(), ErrorEnum.GROUP_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 创建分享记录
|
|
|
GroupShare groupShare = new GroupShare();
|
|
|
// 设置群组UUID
|
|
|
groupShare.setGroupUuid(request.getGroupUuid().toString());
|
|
|
// 生成分享唯一标识
|
|
|
- String shareUuid = UUID.randomUUID().toString();
|
|
|
+ String shareUuid = UUID.randomUUID().toString().replace("-", "");
|
|
|
groupShare.setUuid(shareUuid);
|
|
|
// 设置分享人信息
|
|
|
groupShare.setSharerUserId(request.getSharerUserId());
|
|
|
groupShare.setSharerPhone(request.getSharerPhone());
|
|
|
+ // 设置被分享人信息的默认值
|
|
|
+ groupShare.setSharedUserId(0L); // 设置默认值为0
|
|
|
+ groupShare.setSharedPhone(""); // 设置默认值为空字符串
|
|
|
// 初始化其他字段
|
|
|
groupShare.setState(0); // 初始状态为待确认
|
|
|
groupShare.setIsDeleted(0); // 未删除
|
|
|
-
|
|
|
+
|
|
|
// 3. 保存分享记录
|
|
|
boolean saveResult = groupShareService.save(groupShare);
|
|
|
return saveResult;
|
|
@@ -359,7 +407,8 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
|
|
|
/**
|
|
|
* 分享群组确认
|
|
|
- * @param (被分享人userid)shared (被分享人shared_phone)sharedPhone
|
|
|
+ *
|
|
|
+ * @param (被分享人userid)sharedUserId (被分享人shared_phone)sharedPhone (状态)state
|
|
|
* @return 确认结果
|
|
|
*/
|
|
|
@Override
|
|
@@ -368,50 +417,51 @@ public class GroupGatewayImpl implements GroupGateway {
|
|
|
LambdaQueryWrapper<GroupShare> queryWrapper = Wrappers.<GroupShare>lambdaQuery()
|
|
|
.eq(GroupShare::getUuid, request.getUuid())
|
|
|
.eq(GroupShare::getIsDeleted, 0);
|
|
|
-
|
|
|
+
|
|
|
GroupShare groupShare = groupShareService.getOne(queryWrapper);
|
|
|
-
|
|
|
+
|
|
|
// 如果记录不存在,返回异常
|
|
|
if (Objects.isNull(groupShare)) {
|
|
|
throw new BizException(ErrorEnum.SHARE_IS_EXPIRED.getErrorCode(), ErrorEnum.SHARE_IS_EXPIRED.getErrorMessage());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 检查分享状态
|
|
|
if (groupShare.getState() != 0) {
|
|
|
throw new BizException(ErrorEnum.SHARE_IS_EXPIRED.getErrorCode(), ErrorEnum.SHARE_IS_EXPIRED.getErrorMessage());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 3. 更新分享记录
|
|
|
groupShare.setState(3); // 更新状态为已确认
|
|
|
groupShare.setSharedUserId(request.getSharedUserId()); // 设置被分享人ID
|
|
|
groupShare.setSharedPhone(request.getSharedPhone()); // 设置被分享人手机号
|
|
|
-
|
|
|
+
|
|
|
// 4. 保存更新
|
|
|
return groupShareService.updateById(groupShare);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 群组分享处理
|
|
|
+ *
|
|
|
* @param request 群组分享处理参数
|
|
|
* @return 处理结果
|
|
|
*/
|
|
|
@Override
|
|
|
- public Boolean groupShareHandle(GroupShareHandleParams request){
|
|
|
+ public Boolean groupShareHandle(GroupShareHandleParams request) {
|
|
|
// 1. 根据uuid查询分享记录
|
|
|
LambdaQueryWrapper<GroupShare> queryWrapper = Wrappers.<GroupShare>lambdaQuery()
|
|
|
.eq(GroupShare::getUuid, request.getUuid())
|
|
|
.eq(GroupShare::getIsDeleted, 0);
|
|
|
-
|
|
|
+
|
|
|
GroupShare groupShare = groupShareService.getOne(queryWrapper);
|
|
|
-
|
|
|
+
|
|
|
// 如果记录不存在,返回异常
|
|
|
if (Objects.isNull(groupShare)) {
|
|
|
throw new BizException(ErrorEnum.SHARE_GROUP_IS_NOT_EXIST.getErrorCode(), ErrorEnum.SHARE_GROUP_IS_NOT_EXIST.getErrorMessage());
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 2. 更新分享状态
|
|
|
groupShare.setState(request.getState());
|
|
|
-
|
|
|
+
|
|
|
// 3. 保存更新
|
|
|
return groupShareService.updateById(groupShare);
|
|
|
}
|