|
@@ -0,0 +1,288 @@
|
|
|
+package com.hfln.portal.infrastructure.gateway.impl;
|
|
|
+
|
|
|
+import cn.hfln.framework.extension.BizException;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.hfln.portal.common.dto.data.device.DeviceDTO;
|
|
|
+import com.hfln.portal.common.dto.data.event.EventListDTO;
|
|
|
+import com.hfln.portal.common.dto.data.home.HomeInfoDTO;
|
|
|
+import com.hfln.portal.common.request.device.DeviceBandingParams;
|
|
|
+import com.hfln.portal.common.request.device.DeviceListParams;
|
|
|
+import com.hfln.portal.common.request.device.DeviceLocationParams;
|
|
|
+import com.hfln.portal.common.request.event.EventListParams;
|
|
|
+import com.hfln.portal.common.vo.PageRecord;
|
|
|
+import com.hfln.portal.domain.exception.ErrorEnum;
|
|
|
+import com.hfln.portal.domain.gateway.DeviceGateway;
|
|
|
+import com.hfln.portal.infrastructure.po.DevGroup;
|
|
|
+import com.hfln.portal.infrastructure.po.DevInfo;
|
|
|
+import com.hfln.portal.infrastructure.po.DevShare;
|
|
|
+import com.hfln.portal.infrastructure.po.EventList;
|
|
|
+import com.hfln.portal.infrastructure.service.*;
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class DeviceGatewayImpl implements DeviceGateway {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevShareService devShareService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevInfoService devInfoService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private GroupShareService groupShareService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevGroupService devGroupService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private EventService eventService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public HomeInfoDTO queryHomeInfo(Long userId) {
|
|
|
+ HomeInfoDTO homeInfoDTO = new HomeInfoDTO();
|
|
|
+ //todo 添加轮播图相关信息
|
|
|
+ homeInfoDTO.setCarouselImage(null);
|
|
|
+ /*
|
|
|
+ 查询设备详情
|
|
|
+ */
|
|
|
+ // 查询当前OpenId被分享的设备
|
|
|
+ Optional<List<String>> sharedDevIds = devShareService.queryDeviceIdByUserId(userId);
|
|
|
+
|
|
|
+ List<DeviceDTO> deviceDTOs = new ArrayList<>();
|
|
|
+ List<DevInfo> devInfos = devInfoService.queryDeviceList(userId, null, null, sharedDevIds.orElse(Collections.emptyList()));
|
|
|
+
|
|
|
+ for (DevInfo devInfo : devInfos) {
|
|
|
+ DeviceDTO dto = new DeviceDTO();
|
|
|
+ BeanUtils.copyProperties(devInfo, dto);
|
|
|
+ deviceDTOs.add(dto);
|
|
|
+ }
|
|
|
+ homeInfoDTO.setDeviceList(deviceDTOs);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 查询群组数量
|
|
|
+ */
|
|
|
+ // 查询被分享群组
|
|
|
+ List<String> groupUUid = groupShareService.queryGroupUUidByUserId(userId);
|
|
|
+ List<DevGroup> devGroups = devGroupService.queryGroupByUserId(userId, groupUUid);
|
|
|
+ homeInfoDTO.setGroupNum(devGroups.size());
|
|
|
+
|
|
|
+ /*
|
|
|
+ * 查询被分享数量
|
|
|
+ */
|
|
|
+ homeInfoDTO.setShareNum(devShareService.querySharedNumByUserId(userId).size());
|
|
|
+
|
|
|
+ return homeInfoDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<DeviceDTO> queryDeviceList(DeviceListParams request) {
|
|
|
+
|
|
|
+ Optional<List<String>> sharedDevIds = devShareService.queryDeviceIdByUserId(request.getUserId());
|
|
|
+
|
|
|
+ List<DeviceDTO> deviceDTOs = new ArrayList<>();
|
|
|
+ List<DevInfo> devInfos = devInfoService.queryDeviceList(request.getUserId(), request.getKeyWord(),
|
|
|
+ request.getStatus(), sharedDevIds.orElse(Collections.emptyList()));
|
|
|
+
|
|
|
+ for (DevInfo devInfo : devInfos) {
|
|
|
+ DeviceDTO dto = new DeviceDTO();
|
|
|
+ BeanUtils.copyProperties(devInfo, dto);
|
|
|
+ deviceDTOs.add(dto);
|
|
|
+ }
|
|
|
+ return deviceDTOs;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Boolean deviceUnBind(Long userId, String deviceId) {
|
|
|
+ List<DevInfo> devInfos = devInfoService.queryDevices(userId, deviceId);
|
|
|
+ boolean updateFlag;
|
|
|
+ // 绑定人解绑
|
|
|
+ if (CollectionUtils.isNotEmpty(devInfos) && !devInfos.isEmpty()) {
|
|
|
+ //更新设备表
|
|
|
+ boolean flag = devInfoService.update(null,
|
|
|
+ Wrappers.<DevInfo>lambdaUpdate()
|
|
|
+ .set(DevInfo::getUserId, null)
|
|
|
+ .eq(DevInfo::getDevId, deviceId));
|
|
|
+ //更新设备分享表
|
|
|
+ boolean flagV2 = devShareService.update(null,
|
|
|
+ Wrappers.<DevShare>lambdaUpdate()
|
|
|
+ .set(DevShare::getState, 0)
|
|
|
+ .set(DevShare::getIsDeleted, 1)
|
|
|
+ .eq(DevShare::getDevId, deviceId));
|
|
|
+ updateFlag = flag && flagV2;
|
|
|
+ } else {
|
|
|
+ // 被分享者解绑
|
|
|
+ updateFlag = devShareService.update(null,
|
|
|
+ Wrappers.<DevShare>lambdaUpdate()
|
|
|
+ .set(DevShare::getState, 0)
|
|
|
+ .set(DevShare::getIsDeleted, 1)
|
|
|
+ .eq(DevShare::getDevId, deviceId)
|
|
|
+ .eq(DevShare::getSharedUserId, userId));
|
|
|
+ }
|
|
|
+
|
|
|
+ return updateFlag;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean deviceBind(DeviceBandingParams request) {
|
|
|
+ // 1. 检查设备是否存在
|
|
|
+ DevInfo devInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .eq(DevInfo::getDevId, request.getDevId())
|
|
|
+ );
|
|
|
+ if (Objects.isNull(devInfo)) {
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(devInfo.getUserId())) {
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_BINDING.getErrorCode(), ErrorEnum.DEVICE_IS_BINDING.getErrorMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 更新设备信息
|
|
|
+ return devInfoService.updateDevice(devInfo.getDevInfoId(), request);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DeviceDTO queryDeviceById(String deviceId) {
|
|
|
+ DevInfo devInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .eq(DevInfo::getDevId, deviceId));
|
|
|
+ if (Objects.isNull(devInfo)) {
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+ DeviceDTO dto = new DeviceDTO();
|
|
|
+ BeanUtils.copyProperties(devInfo, dto);
|
|
|
+ return dto;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageRecord<EventListDTO> queryEventByPage(EventListParams request) {
|
|
|
+ Integer pageNo = Optional.ofNullable(request.getPageNo()).orElse(1);
|
|
|
+ Integer pageSize = Optional.ofNullable(request.getPageNo()).orElse(10);
|
|
|
+
|
|
|
+ // 执行分页查询
|
|
|
+ Page<EventList> eventPage = eventService.queryEventListByDevId(request.getDeviceId(),
|
|
|
+ request.getStartTime(), request.getEndTime(), pageNo, pageSize);
|
|
|
+ // 换为目标VO
|
|
|
+ List<EventListDTO> targets = convertToTargetVO(eventPage.getRecords());
|
|
|
+ return getEventListDTOPageRecord(eventPage, targets);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 填充PageRecord
|
|
|
+ private PageRecord<EventListDTO> getEventListDTOPageRecord(Page<EventList> eventPage, List<EventListDTO> targets) {
|
|
|
+ int totalPages = (int) Math.ceil((double) eventPage.getTotal() / eventPage.getSize());
|
|
|
+ PageRecord<EventListDTO> pageRecord = new PageRecord<>();
|
|
|
+ pageRecord.setRows(targets);
|
|
|
+ pageRecord.setTotal(eventPage.getTotal());
|
|
|
+ pageRecord.setPageNum((int) eventPage.getCurrent());
|
|
|
+ pageRecord.setPageSize((int) eventPage.getSize());
|
|
|
+ pageRecord.setOutTotalPageNum(eventPage.getCurrent() > totalPages);
|
|
|
+ pageRecord.setTotalPageNum(totalPages);
|
|
|
+ return pageRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 实体转换方法
|
|
|
+ private List<EventListDTO> convertToTargetVO(List<EventList> records) {
|
|
|
+ return records.stream().map(event -> {
|
|
|
+ EventListDTO vo = new EventListDTO();
|
|
|
+ // 使用BeanUtils简化属性拷贝
|
|
|
+ BeanUtils.copyProperties(event, vo);
|
|
|
+ return vo;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean handleEvent(Long eventId) {
|
|
|
+ LambdaUpdateWrapper<EventList> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(EventList::getEventListId, eventId)
|
|
|
+ .set(EventList::getIsHandle, 1);
|
|
|
+ return this.eventService.update(updateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public Boolean updateDevice(DeviceBandingParams request) {
|
|
|
+ // 1. 安全获取长度和宽度,提供默认值
|
|
|
+ BigDecimal length = Optional.ofNullable(request.getLength()).orElse(BigDecimal.ZERO);
|
|
|
+ BigDecimal width = Optional.ofNullable(request.getWidth()).orElse(BigDecimal.ZERO);
|
|
|
+
|
|
|
+ // 2. 计算半长半宽
|
|
|
+ BigDecimal halfLength = length.divide(BigDecimal.valueOf(2), 10, RoundingMode.HALF_UP);
|
|
|
+ BigDecimal halfWidth = width.divide(BigDecimal.valueOf(2), 10, RoundingMode.HALF_UP);
|
|
|
+
|
|
|
+ // 3. 设置默认值
|
|
|
+ setIfNull(request::getXStart, () -> halfLength, request::setXStart);
|
|
|
+ setIfNull(request::getXEnd, () -> halfLength, request::setXEnd);
|
|
|
+ setIfNull(request::getYStart, () -> halfWidth, request::setYStart);
|
|
|
+ setIfNull(request::getYEnd, () -> halfWidth, request::setYEnd);
|
|
|
+
|
|
|
+
|
|
|
+ Boolean flag = devInfoService.updateDevice(request.getDevId(), request);
|
|
|
+ DevInfo devInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .eq(DevInfo::getDevId, request.getDevId()));
|
|
|
+
|
|
|
+ //检查parts.length >= 2确保数组有足够元素,提供默认值"2.0"
|
|
|
+ String result = Optional.ofNullable(devInfo.getSoftware())
|
|
|
+ .filter(s -> s.contains("."))
|
|
|
+ .map(s -> s.split("\\."))
|
|
|
+ .filter(parts -> parts.length >= 2)
|
|
|
+ .map(parts -> parts[0] + "." + parts[1])
|
|
|
+ .orElse("2.0");
|
|
|
+
|
|
|
+ //mqttHandler.handleDeviceUpdateMessage(result, request);
|
|
|
+ return flag;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 辅助方法:如果值为null则设置默认值
|
|
|
+ private <T> void setIfNull(java.util.function.Supplier<T> getter,
|
|
|
+ java.util.function.Supplier<T> defaultValueSupplier,
|
|
|
+ java.util.function.Consumer<T> setter) {
|
|
|
+ if (getter.get() == null) {
|
|
|
+ setter.accept(defaultValueSupplier.get());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean updateDeviceLocation(DeviceLocationParams params) {
|
|
|
+
|
|
|
+ DevInfo devInfo = devInfoService.getOne(
|
|
|
+ Wrappers.<DevInfo>lambdaQuery()
|
|
|
+ .eq(DevInfo::getDevId, params.getDeviceId()));
|
|
|
+ if (Objects.isNull(devInfo)) {
|
|
|
+ throw new BizException(ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorCode(), ErrorEnum.DEVICE_IS_NOT_EXIST.getErrorMessage());
|
|
|
+ }
|
|
|
+ LambdaUpdateWrapper<DevInfo> updateWrapper = Wrappers.lambdaUpdate();
|
|
|
+ // 仅当字段非空时才更新
|
|
|
+ if (Objects.nonNull(params.getXValue())) {
|
|
|
+ updateWrapper.set(DevInfo::getX, params.getXValue());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(params.getYValue())) {
|
|
|
+ updateWrapper.set(DevInfo::getY, params.getYValue());
|
|
|
+ }
|
|
|
+ // 如果所有字段均为空,无需更新
|
|
|
+ if (params.getXValue() == null && params.getYValue() == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return devInfoService.update(updateWrapper);
|
|
|
+ }
|
|
|
+}
|