Commit e78f51f0ee5c4cccde7f83e65d3ffd7039406582

Authored by 黄 x
1 parent cf57e0c1

feat: 提交首页Controller

  1 +package org.thingsboard.server.controller.yunteng;
  2 +
  3 +import org.springframework.web.bind.annotation.RequestMapping;
  4 +import org.thingsboard.server.controller.BaseController;
  5 +
  6 +@RequestMapping("api/yt/homepage")
  7 +public class HomePageController extends BaseController {
  8 +
  9 +}
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +
  6 +@Data
  7 +public class HomeAlarmInfoDTO {
  8 + @ApiModelProperty("告警总数")
  9 + private Integer alarmSumCount;
  10 + @ApiModelProperty("今日新增告警")
  11 + private Integer alarmTodayAdd;
  12 +}
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +
  6 +@Data
  7 +public class HomeDeviceInfoDTO {
  8 + @ApiModelProperty("设备总数")
  9 + private Integer sumCount;
  10 +
  11 + @ApiModelProperty("在线设备数量")
  12 + private Integer onLine;
  13 +
  14 + @ApiModelProperty("离线设备数量")
  15 + private Integer offLine;
  16 +
  17 + @ApiModelProperty("未激活设备数量")
  18 + private Integer inActive;
  19 +
  20 + @ApiModelProperty("今日新增设备数量")
  21 + private Integer todayAdd;
  22 +
  23 + public HomeDeviceInfoDTO(Integer defaultValue) {
  24 + this.sumCount = defaultValue;
  25 + this.onLine = defaultValue;
  26 + this.offLine = defaultValue;
  27 + this.inActive = defaultValue;
  28 + this.todayAdd = defaultValue;
  29 + }
  30 +
  31 + public HomeDeviceInfoDTO() {}
  32 +}
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModelProperty;
  4 +import lombok.Data;
  5 +
  6 +@Data
  7 +public class HomeMessageInfoDTO {
  8 + @ApiModelProperty("消息总数")
  9 + private Integer messageSumCount;
  10 + @ApiModelProperty("今日新增消息")
  11 + private Integer messageTodayAdd;
  12 +}
... ...
  1 +package org.thingsboard.server.common.data.yunteng.dto;
  2 +
  3 +import io.swagger.annotations.ApiModel;
  4 +import io.swagger.annotations.ApiModelProperty;
  5 +import lombok.Data;
  6 +import lombok.EqualsAndHashCode;
  7 +@Data
  8 +@EqualsAndHashCode(callSuper = true)
  9 +@ApiModel("首页左边顶部信息")
  10 +public class HomePageLeftTopDTO extends BaseDTO {
  11 + @ApiModelProperty("顶部设备统计")
  12 + private HomeDeviceInfoDTO deviceInfo;
  13 +
  14 + @ApiModelProperty("顶部告警统计")
  15 + private HomeAlarmInfoDTO alarmInfo;
  16 +
  17 + @ApiModelProperty("顶部消息统计")
  18 + private HomeMessageInfoDTO messageInfo;
  19 +}
... ...
  1 +package org.thingsboard.server.dao.yunteng.impl;
  2 +
  3 +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4 +import lombok.RequiredArgsConstructor;
  5 +import org.springframework.stereotype.Service;
  6 +import org.thingsboard.server.common.data.alarm.Alarm;
  7 +import org.thingsboard.server.common.data.alarm.AlarmInfo;
  8 +import org.thingsboard.server.common.data.yunteng.constant.FastIotConstants;
  9 +import org.thingsboard.server.common.data.yunteng.dto.HomeDeviceInfoDTO;
  10 +import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;
  11 +import org.thingsboard.server.common.data.yunteng.utils.ReflectUtils;
  12 +import org.thingsboard.server.dao.yunteng.entities.YtDevice;
  13 +import org.thingsboard.server.dao.yunteng.mapper.DeviceMapper;
  14 +import org.thingsboard.server.dao.yunteng.service.HomePageService;
  15 +
  16 +import java.time.LocalDate;
  17 +import java.time.LocalDateTime;
  18 +import java.time.LocalTime;
  19 +import java.time.ZoneOffset;
  20 +import java.util.List;
  21 +
  22 +@Service
  23 +@RequiredArgsConstructor
  24 +public class HomePageServiceImpl implements HomePageService {
  25 +
  26 + private final DeviceMapper deviceMapper;
  27 +
  28 + @Override
  29 + public HomePageLeftTopDTO getHomePageLeftTopInfo(
  30 + boolean isPtSysAdmin,
  31 + boolean isPtAdmin,
  32 + boolean isTenantAdmin,
  33 + String tenantId,
  34 + String currentUserId) {
  35 + // 如果当前用户是超级管理员或平台管理员,查询所有的设备、告警、消息量
  36 + // 如果当前用户是租户管理员,查询租户下的所有设备、告警、消息量
  37 + // 如果当前用户是客户,查询客户可以查看的设备、告警、消息量
  38 + HomePageLeftTopDTO homePageLeftTopDTO = new HomePageLeftTopDTO();
  39 + HomeDeviceInfoDTO homeDeviceInfo;
  40 + List<YtDevice> deviceList;
  41 + if (isPtSysAdmin || isPtAdmin) {
  42 + deviceList = deviceMapper.selectList(new LambdaQueryWrapper<>());
  43 + } else if (isTenantAdmin) {
  44 + deviceList =
  45 + deviceMapper.selectList(
  46 + new LambdaQueryWrapper<YtDevice>().eq(YtDevice::getTenantId, tenantId));
  47 + } else {
  48 + deviceList =
  49 + ReflectUtils.sourceToTarget(
  50 + deviceMapper.getDeviceByCustomerUserId(currentUserId), YtDevice.class);
  51 + }
  52 + homeDeviceInfo = new HomeDeviceInfoDTO(Integer.valueOf(FastIotConstants.MagicNumber.ZERO));
  53 + if (null != deviceList) {
  54 + homeDeviceInfo = setDeviceInfoData(deviceList, homeDeviceInfo);
  55 + }
  56 + homePageLeftTopDTO.setDeviceInfo(homeDeviceInfo);
  57 + return homePageLeftTopDTO;
  58 + }
  59 +
  60 + /**
  61 + * 设置设备相关的数据
  62 + *
  63 + * @param deviceList 拥有的设备
  64 + * @param homeDeviceInfoDTO 首页信息
  65 + * @return 设置后的首页信息
  66 + */
  67 + private HomeDeviceInfoDTO setDeviceInfoData(
  68 + List<YtDevice> deviceList, HomeDeviceInfoDTO homeDeviceInfoDTO) {
  69 + homeDeviceInfoDTO.setSumCount(deviceList.size());
  70 + deviceList.forEach(
  71 + ytDevice -> {
  72 + int basicNum = FastIotConstants.MagicNumber.ONE;
  73 + ZoneOffset zoneOffset = ZoneOffset.of("+8");
  74 + LocalDateTime todayZeroTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
  75 + if (ytDevice.getCreateTime().atZone(zoneOffset).toInstant().toEpochMilli()
  76 + >= todayZeroTime.toInstant(zoneOffset).toEpochMilli()) {
  77 + homeDeviceInfoDTO.setTodayAdd(homeDeviceInfoDTO.getTodayAdd() + basicNum);
  78 + }
  79 + switch (ytDevice.getDeviceState()) {
  80 + case OFFLINE:
  81 + homeDeviceInfoDTO.setOffLine(homeDeviceInfoDTO.getOffLine() + basicNum);
  82 + break;
  83 + case ONLINE:
  84 + homeDeviceInfoDTO.setOnLine(homeDeviceInfoDTO.getOnLine() + basicNum);
  85 + break;
  86 + case INACTIVE:
  87 + homeDeviceInfoDTO.setInActive(homeDeviceInfoDTO.getInActive() + basicNum);
  88 + break;
  89 + default:
  90 + break;
  91 + }
  92 + });
  93 + return homeDeviceInfoDTO;
  94 + }
  95 +
  96 + /**
  97 + * 设置告警相关的数据
  98 + * @param alarmInfoList 告警数据
  99 + * @param homeDeviceInfoDTO 首页数据
  100 + * @return 设置后的首页数据
  101 + */
  102 + private HomeDeviceInfoDTO setAlarmInfoData(List<AlarmInfo> alarmInfoList,HomeDeviceInfoDTO homeDeviceInfoDTO){
  103 + return homeDeviceInfoDTO;
  104 + }
  105 +
  106 + private HomeDeviceInfoDTO setMessageInfoData(HomeDeviceInfoDTO homeDeviceInfoDTO){
  107 + return homeDeviceInfoDTO;
  108 + }
  109 +}
... ...
  1 +package org.thingsboard.server.dao.yunteng.service;
  2 +
  3 +import org.thingsboard.server.common.data.yunteng.dto.HomePageLeftTopDTO;
  4 +
  5 +public interface HomePageService {
  6 + /**
  7 + * 通过租户ID获取首页左边顶部信息
  8 + *
  9 + * @param isPtSysAdmin 是否超级管理员
  10 + * @param isPtAdmin 是否平台管理员
  11 + * @param isTenantAdmin 是否租户管理员
  12 + * @param tenantId 租户ID
  13 + * @param currentUserId 当前用户ID
  14 + * @return 首页左边顶部信息
  15 + */
  16 + HomePageLeftTopDTO getHomePageLeftTopInfo(
  17 + boolean isPtSysAdmin,
  18 + boolean isPtAdmin,
  19 + boolean isTenantAdmin,
  20 + String tenantId,
  21 + String currentUserId);
  22 +}
... ...