|
|
@@ -0,0 +1,418 @@
|
|
|
+package com.hfln.portal.infrastructure.gateway.impl;
|
|
|
+
|
|
|
+import cn.hfln.framework.extension.BizException;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.hfln.portal.common.dto.data.device.DeviceDTO;
|
|
|
+import com.hfln.portal.common.dto.data.group.GroupDTO;
|
|
|
+import com.hfln.portal.common.request.group.*;
|
|
|
+import com.hfln.portal.domain.exception.ErrorEnum;
|
|
|
+import com.hfln.portal.domain.gateway.GroupGateway;
|
|
|
+import com.hfln.portal.infrastructure.po.DevGroup;
|
|
|
+import com.hfln.portal.infrastructure.po.DevInfo;
|
|
|
+import com.hfln.portal.infrastructure.po.GroupDevMap;
|
|
|
+import com.hfln.portal.infrastructure.po.GroupShare;
|
|
|
+import com.hfln.portal.infrastructure.mapper.GroupDevMapMapper;
|
|
|
+import com.hfln.portal.infrastructure.service.DevGroupService;
|
|
|
+import com.hfln.portal.infrastructure.service.DevInfoService;
|
|
|
+import com.hfln.portal.infrastructure.service.GroupShareService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class GroupGatewayImpl implements GroupGateway {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevGroupService devGroupService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GroupShareService groupShareService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevInfoService devInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GroupDevMapMapper groupDevMapMapper;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加群组
|
|
|
+ * @param (groupName)群组名称 (userId)用户id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean addGroup(GroupAddParams request) {
|
|
|
+ //1.设置群组属性
|
|
|
+ DevGroup devGroup = new DevGroup();
|
|
|
+ devGroup.setGroupName(request.getGroupName());
|
|
|
+ devGroup.setUserId(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.setIsDeleted(0);
|
|
|
+ //2.把群组名称和UUID存储数据库
|
|
|
+ // TODO mqtt消息 主题 /mps/add_group
|
|
|
+
|
|
|
+ return devGroupService.save(devGroup);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除群组
|
|
|
+ * @param (groupId) 群组id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteGroup(Long devGroupId) {
|
|
|
+ //1.查询群组信息
|
|
|
+ DevGroup devGroup = devGroupService.getById(devGroupId);
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新群组
|
|
|
+ * @param (群组Uuid)groupUuid (群组名称)groupName (长度)length (宽度)width (北向夹角)northAngle
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean updateGroup(GroupUpdateParams params) {
|
|
|
+ //1.检查群组是否存
|
|
|
+ DevGroup devGroup = devGroupService.getOne(
|
|
|
+ Wrappers.<DevGroup>lambdaQuery()
|
|
|
+ .eq(DevGroup::getGroupUuid, params.getGroupUuid()));
|
|
|
+ if (Objects.isNull(devGroup)) {
|
|
|
+ throw new BizException(ErrorEnum.GROUP_IS_NOT_EXIST.getErrorCode(), ErrorEnum.GROUP_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+ //2.创建更新包装器
|
|
|
+ LambdaUpdateWrapper<DevGroup> updateWrapper = Wrappers.lambdaUpdate();
|
|
|
+ //设置更新条件
|
|
|
+ updateWrapper.eq(DevGroup::getGroupUuid, params.getGroupUuid());
|
|
|
+ // 仅当字段非空时才更新
|
|
|
+ boolean hasUpdate = false;
|
|
|
+ if (Objects.nonNull(params.getGroupName())) {
|
|
|
+ updateWrapper.set(DevGroup::getGroupName, params.getGroupName());
|
|
|
+ hasUpdate = true;
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(params.getLength())) {
|
|
|
+ updateWrapper.set(DevGroup::getLength, params.getLength());
|
|
|
+ hasUpdate = true;
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(params.getWidth())){
|
|
|
+ updateWrapper.set(DevGroup::getWidth, params.getWidth());
|
|
|
+ hasUpdate = true;
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(params.getNorthAngle())){
|
|
|
+ updateWrapper.set(DevGroup::getNorthAngle, params.getNorthAngle());
|
|
|
+ hasUpdate = true;
|
|
|
+ }
|
|
|
+ // 如果所有字段均为空,无需更新
|
|
|
+ if (!hasUpdate){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return devGroupService.update(updateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取群组列表
|
|
|
+ * @param (用户id)userId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<GroupDTO> queryGroupList(GroupListParams request) {
|
|
|
+ // 1. 查询被分享群组
|
|
|
+ List<String> groupUUids = groupShareService.queryGroupUUidByUserId(request.getUserId());
|
|
|
+
|
|
|
+ // 2. 获取用户自己创建的群组和被分享的群组
|
|
|
+ List<DevGroup> devGroups = devGroupService.queryGroupByUserId(request.getUserId(), groupUUids);
|
|
|
+
|
|
|
+ // 3. 将群组信息转化为DTO
|
|
|
+ List<GroupDTO> groupDTOs = new ArrayList<>();
|
|
|
+ for (DevGroup devGroup : devGroups) {
|
|
|
+ GroupDTO dto = new GroupDTO();
|
|
|
+ BeanUtils.copyProperties(devGroup, dto);
|
|
|
+
|
|
|
+ // 4. 查询该群组下的设备列表
|
|
|
+ List<DevInfo> groupDevices = queryDevicesByGroupUuid(devGroup.getGroupUuid());
|
|
|
+
|
|
|
+ // 5. 统计设备信息
|
|
|
+ int totalDevice = groupDevices.size();
|
|
|
+ int onlineDevice = 0;
|
|
|
+ int warningDevice = 0;
|
|
|
+
|
|
|
+ // 6. 统计在线和告警设备数量
|
|
|
+ for (DevInfo device : groupDevices) {
|
|
|
+ // 使用online字段判断在线状态(根据DevInfo类定义)
|
|
|
+ if (Objects.nonNull(device.getOnline()) && device.getOnline() == DevInfo.Constants.OnlineStatus.ONLINE) {
|
|
|
+ onlineDevice++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用devWarn字段判断告警状态(根据DevInfo类定义)
|
|
|
+ if (Objects.nonNull(device.getDevWarn()) && device.getDevWarn() == DevInfo.Constants.DevWarnStatus.WARNING) {
|
|
|
+ warningDevice++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. 设置统计值
|
|
|
+ dto.setTotalDevice(totalDevice);
|
|
|
+ dto.setOnlineDevice(onlineDevice);
|
|
|
+ dto.setWarningDevice(warningDevice);
|
|
|
+
|
|
|
+ groupDTOs.add(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ return groupDTOs;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取群组中设备列表
|
|
|
+ * @param (群组id)groupId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<DeviceDTO> queryGroupDeviceInfoById(Long groupId) {
|
|
|
+ if (Objects.isNull(groupId)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 查询群组信息
|
|
|
+ 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. 查询设备并转换为DTO
|
|
|
+ List<DevInfo> deviceList = queryDevicesByGroupUuid(devGroup.getGroupUuid());
|
|
|
+
|
|
|
+ // 3. 转换为DTO列表
|
|
|
+ List<DeviceDTO> deviceDTOs = new ArrayList<>();
|
|
|
+ for (DevInfo devInfo : deviceList) {
|
|
|
+ DeviceDTO dto = new DeviceDTO();
|
|
|
+ BeanUtils.copyProperties(devInfo, dto);
|
|
|
+ deviceDTOs.add(dto);
|
|
|
+ }
|
|
|
+
|
|
|
+ return deviceDTOs;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据群组UUID查询设备列表(私有方法 不对外接口)
|
|
|
+ * @param (groupUuid) 群组UUID
|
|
|
+ * @return 设备列表
|
|
|
+ */
|
|
|
+ private List<DevInfo> queryDevicesByGroupUuid(String groupUuid) {
|
|
|
+ if (Objects.isNull(groupUuid)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 查询群组设备映射表获取设备ID列表
|
|
|
+ LambdaQueryWrapper<GroupDevMap> mapWrapper = new LambdaQueryWrapper<>();
|
|
|
+ mapWrapper.eq(GroupDevMap::getGroupUuid, groupUuid);
|
|
|
+ List<GroupDevMap> devMaps = groupDevMapMapper.selectList(mapWrapper);
|
|
|
+
|
|
|
+ // 如果没有关联设备,返回空列表
|
|
|
+ if (CollectionUtils.isEmpty(devMaps)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 提取设备ID列表
|
|
|
+ List<String> devIds = devMaps.stream()
|
|
|
+ .map(GroupDevMap::getDevId)
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ // 3. 查询所有设备详情
|
|
|
+ return devInfoService.list(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .in(DevInfo::getDevId, devIds));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 添加设备到群组
|
|
|
+ * @param (设备ID)dev_id (群组ID) group_id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean addDeviceToGroup(AddDeviceToGroupParams params) {
|
|
|
+ // 1. 查询设备和群组是否存在
|
|
|
+ DevInfo devInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .eq(DevInfo::getDevId, params.getDevId()));
|
|
|
+ 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.setDevGroupId(params.getGroupId());
|
|
|
+ newMap.setGroupUuid(devGroup.getGroupUuid());
|
|
|
+ newMap.setDevInfoId(params.getDevId());
|
|
|
+ newMap.setDevId(devInfo.getClientId());
|
|
|
+ newMap.setIsDeleted(0);
|
|
|
+
|
|
|
+ return groupDevMapMapper.insert(newMap) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从群组中删除设备
|
|
|
+ * @param (设备ID)dev_id (群组ID) group_id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean deleteDeviceFromGroup(DeleteDeviceFromGroupParams request) {
|
|
|
+ // 1. 直接查询设备与群组的关联关系
|
|
|
+ LambdaQueryWrapper<GroupDevMap> queryWrapper = Wrappers.<GroupDevMap>lambdaQuery()
|
|
|
+ .eq(GroupDevMap::getDevId, request.getDevId())
|
|
|
+ .eq(GroupDevMap::getDevGroupId, request.getGroupId())
|
|
|
+ .eq(GroupDevMap::getIsDeleted, 0);
|
|
|
+
|
|
|
+ GroupDevMap groupDevMap = groupDevMapMapper.selectOne(queryWrapper);
|
|
|
+
|
|
|
+ // 2. 如果关联关系不存在,返回错误
|
|
|
+ if (Objects.isNull(groupDevMap)) {
|
|
|
+ throw new BizException(ErrorEnum.ERROR_BIZ_FAIL.getErrorCode(), "设备不在此群组中");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 将关联关系标记为逻辑删除
|
|
|
+ groupDevMap.setIsDeleted(1);
|
|
|
+
|
|
|
+ // 4. 更新数据库并返回结果
|
|
|
+ return groupDevMapMapper.updateById(groupDevMap) > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分享群组
|
|
|
+ * @param (群组uuid) groupUuid (分享人userid)sharer (分享人手机号)sharerPhone
|
|
|
+ * @return 分享结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean shareGroup(ShareGroupParams request) {
|
|
|
+ // 1. 验证群组是否存在
|
|
|
+ DevGroup devGroup = devGroupService.getOne(
|
|
|
+ Wrappers.<DevGroup>lambdaQuery()
|
|
|
+ .eq(DevGroup::getGroupUuid, request.getGroupUuid()));
|
|
|
+ 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();
|
|
|
+ groupShare.setUuid(shareUuid);
|
|
|
+ // 设置分享人信息
|
|
|
+ groupShare.setSharer(request.getSharer());
|
|
|
+ groupShare.setSharerPhone(request.getSharerPhone());
|
|
|
+ // 初始化其他字段
|
|
|
+ groupShare.setState(0); // 初始状态为待确认
|
|
|
+ groupShare.setIsDeleted(0); // 未删除
|
|
|
+
|
|
|
+ // 3. 保存分享记录
|
|
|
+ boolean saveResult = groupShareService.save(groupShare);
|
|
|
+ return saveResult;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分享群组确认
|
|
|
+ * @param (被分享人userid)shared (被分享人shared_phone)sharedPhone
|
|
|
+ * @return 确认结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Boolean groupShareConfirm(GroupShareConfirmParams 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_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.setShared(request.getShared()); // 设置被分享人ID
|
|
|
+ groupShare.setSharedPhone(request.getSharedPhone()); // 设置被分享人手机号
|
|
|
+
|
|
|
+ // 4. 保存更新
|
|
|
+ return groupShareService.updateById(groupShare);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 群组分享处理
|
|
|
+ * @param request 群组分享处理参数
|
|
|
+ * @return 处理结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+}
|