|
@@ -1,12 +1,156 @@
|
|
|
package com.hfln.portal.infrastructure.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.hfln.portal.common.dto.data.event.EventsDTO;
|
|
|
+import com.hfln.portal.common.request.event.WapEventsParams;
|
|
|
+import com.hfln.portal.common.request.event.WebEventsParams;
|
|
|
+import com.hfln.portal.common.vo.PageRecord;
|
|
|
+import com.hfln.portal.domain.customer.AdminUserType;
|
|
|
+import com.hfln.portal.domain.customer.util.CopyUtils;
|
|
|
import com.hfln.portal.infrastructure.mapper.EventsMapper;
|
|
|
+import com.hfln.portal.infrastructure.po.DevInfo;
|
|
|
import com.hfln.portal.infrastructure.po.Events;
|
|
|
+import com.hfln.portal.infrastructure.service.DevInfoService;
|
|
|
import com.hfln.portal.infrastructure.service.EventsService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
@Service
|
|
|
public class EventsServiceImpl extends ServiceImpl<EventsMapper, Events> implements EventsService {
|
|
|
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DevInfoService devInfoService;
|
|
|
+ /**
|
|
|
+ * 小程序查询设备事件列表
|
|
|
+ * @param params
|
|
|
+ * @param clientIds
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public Page<Events> QueryEvents(WapEventsParams params, List<String> clientIds) {
|
|
|
+ Page<Events> page = new Page<>(params.getPageNo(), params.getPageSize());
|
|
|
+
|
|
|
+ // 1. 构建查询条件
|
|
|
+ LambdaQueryWrapper<Events> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ if (clientIds != null && !clientIds.isEmpty()){
|
|
|
+ queryWrapper.in(Events::getClientId, clientIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 添加日期条件
|
|
|
+ if (Objects.nonNull(params.getCreateTimeStart())) {
|
|
|
+ queryWrapper.ge(Events::getCreateTime, params.getCreateTimeStart());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(params.getCreateTimeEnd())) {
|
|
|
+ queryWrapper.lt(Events::getCreateTime, params.getCreateTimeEnd().plusDays(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3.添加设备ID条件
|
|
|
+ if (params.getClientId() != null) {
|
|
|
+ queryWrapper.eq(Events::getClientId, params.getClientId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4.添加事件类型条件
|
|
|
+ if (params.getEventType() != null) {
|
|
|
+ queryWrapper.eq(Events::getEventType, params.getEventType());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 6. 设置排序
|
|
|
+ queryWrapper.orderByDesc(Events::getCreateTime);
|
|
|
+ return this.baseMapper.selectPage(page, queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * web端查询设备事件列表
|
|
|
+ * @param params
|
|
|
+ * @param userType
|
|
|
+ * @param tenantId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public PageRecord<EventsDTO> queryEvents(WebEventsParams params, String userType, Long tenantId) {
|
|
|
+
|
|
|
+ Page<Events> page = new Page<>(params.getPageNo(), params.getPageSize());
|
|
|
+ LambdaQueryWrapper<Events> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+
|
|
|
+ // 1. 时间过滤
|
|
|
+ Optional.ofNullable(params.getCreateTimeStart())
|
|
|
+ .ifPresent(start -> queryWrapper.ge(Events::getCreateTime, start));
|
|
|
+ Optional.ofNullable(params.getCreateTimeEnd())
|
|
|
+ .ifPresent(end -> queryWrapper.lt(Events::getCreateTime, end.plusDays(1)));
|
|
|
+
|
|
|
+ // 2. 事件类型过滤
|
|
|
+ Optional.ofNullable(params.getEventType())
|
|
|
+ .ifPresent(type -> queryWrapper.eq(Events::getEventType, type));
|
|
|
+
|
|
|
+ // 3. 用户类型处理
|
|
|
+ if (AdminUserType.getBgManagerTypes().contains(userType)) {
|
|
|
+ // 后台管理员可选按tenantId过滤
|
|
|
+ if (params.getTenantId() != null) {
|
|
|
+ queryWrapper.eq(Events::getTenantId, params.getTenantId());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 租户管理员/普通用户:只能查看自己租户
|
|
|
+ queryWrapper.eq(Events::getTenantId, tenantId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. 排序
|
|
|
+ queryWrapper.orderByDesc(Events::getCreateTime);
|
|
|
+
|
|
|
+ // 5. 分页查询
|
|
|
+ page = this.baseMapper.selectPage(page, queryWrapper);
|
|
|
+
|
|
|
+ // 6. 转换为 DTO
|
|
|
+ List<EventsDTO> dtoList = CopyUtils.copyList(page.getRecords(), EventsDTO.class);
|
|
|
+
|
|
|
+ // 7. 填充 devName
|
|
|
+ fillDevNames(dtoList);
|
|
|
+
|
|
|
+ // 8. 封装返回
|
|
|
+ return CopyUtils.copyPage(page, dtoList);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计用户设备当天的异常事件数量(按 clientId)
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public long countUserDevicesEvents(List<String> clientIds) {
|
|
|
+ if (clientIds == null || clientIds.isEmpty()) {
|
|
|
+ return 0L;
|
|
|
+ }
|
|
|
+
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
+ LocalDateTime startOfDay = today.atStartOfDay();
|
|
|
+ LocalDateTime endOfDay = today.plusDays(1).atStartOfDay();
|
|
|
+
|
|
|
+ LambdaQueryWrapper<Events> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
+ queryWrapper.in(Events::getClientId, clientIds)
|
|
|
+ .ge(Events::getCreateTime, startOfDay)
|
|
|
+ .lt(Events::getCreateTime, endOfDay);
|
|
|
+
|
|
|
+ return this.baseMapper.selectCount(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 辅助方法:填充 devName
|
|
|
+ public void fillDevNames(List<EventsDTO> dtoList) {
|
|
|
+ Set<String> allClientIds = dtoList.stream()
|
|
|
+ .map(EventsDTO::getClientId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ if (!allClientIds.isEmpty()) {
|
|
|
+ Map<String, String> devIdNameMap = devInfoService.queryByIds(allClientIds)
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.toMap(DevInfo::getClientId, DevInfo::getDevName));
|
|
|
+
|
|
|
+ dtoList.forEach(dto -> dto.setDevName(devIdNameMap.get(dto.getClientId())));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|