|
@@ -1,13 +1,13 @@
|
|
|
package com.hfln.portal.infrastructure.gateway.impl;
|
|
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import com.alibaba.excel.util.StringUtils;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.hfln.portal.common.constant.UserConstants;
|
|
|
import com.hfln.portal.common.dto.data.event.EventListDTO;
|
|
|
import com.hfln.portal.common.dto.data.event.EventsDTO;
|
|
|
-import com.hfln.portal.common.dto.data.user.UserDailyActiveResult;
|
|
|
-import com.hfln.portal.common.dto.data.user.UsersDailyActiveDTO;
|
|
|
+import com.hfln.portal.common.dto.data.user.UserDailyActiveDTO;
|
|
|
import com.hfln.portal.common.request.event.EventListParams;
|
|
|
import com.hfln.portal.common.request.event.WebEventsParams;
|
|
|
import com.hfln.portal.common.request.user.UserDailyActiveParams;
|
|
@@ -17,19 +17,13 @@ import com.hfln.portal.domain.customer.util.CopyUtils;
|
|
|
import com.hfln.portal.domain.gateway.WebStatsGateway;
|
|
|
import com.hfln.portal.infrastructure.po.DailyActiveUsers;
|
|
|
import com.hfln.portal.infrastructure.po.EventList;
|
|
|
-import com.hfln.portal.infrastructure.service.DailyActiveUsersService;
|
|
|
-import com.hfln.portal.infrastructure.service.DevInfoService;
|
|
|
-import com.hfln.portal.infrastructure.service.EventListService;
|
|
|
-import com.hfln.portal.infrastructure.service.EventsService;
|
|
|
+import com.hfln.portal.infrastructure.po.UserInfo;
|
|
|
+import com.hfln.portal.infrastructure.service.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.time.LocalDate;
|
|
|
-import java.time.LocalDateTime;
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Slf4j
|
|
@@ -49,6 +43,10 @@ public class WebStatsGatewayImpl implements WebStatsGateway {
|
|
|
@Autowired
|
|
|
private EventsService eventsService;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public PageRecord<EventListDTO> fallQuery(EventListParams params) {
|
|
|
Long tenantId = (Long) StpUtil.getSession().get(UserConstants.SA_USER_TENANT_ID);
|
|
@@ -91,50 +89,105 @@ public class WebStatsGatewayImpl implements WebStatsGateway {
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public UserDailyActiveResult queryUserDailyActive(UserDailyActiveParams params) {
|
|
|
-
|
|
|
- // 1.处理查询日期,入参为空则默认当天
|
|
|
- LocalDate queryDate = (params.getQueryDate() != null) ? params.getQueryDate() : LocalDate.now();
|
|
|
- LocalDateTime startOfDay = queryDate.atStartOfDay();
|
|
|
- LocalDateTime endOfDay = queryDate.plusDays(1).atStartOfDay().minusNanos(1);
|
|
|
-
|
|
|
- // 2. 查询总访问次数
|
|
|
- LambdaQueryWrapper<DailyActiveUsers> countWrapper = new LambdaQueryWrapper<>();
|
|
|
- countWrapper.eq(DailyActiveUsers::getUserId, params.getUserId())
|
|
|
- .between(DailyActiveUsers::getCreateTime, startOfDay, endOfDay);
|
|
|
- long totalCount = dailyActiveUsersService.count(countWrapper);
|
|
|
-
|
|
|
- if (totalCount == 0) {
|
|
|
- UserDailyActiveResult emptyResult = new UserDailyActiveResult();
|
|
|
- emptyResult.setVisitCount(0);
|
|
|
- emptyResult.setRecords(Collections.emptyList());
|
|
|
- return emptyResult;
|
|
|
- }
|
|
|
+ public PageRecord<UserDailyActiveDTO> queryUserDailyActive(UserDailyActiveParams params) {
|
|
|
+
|
|
|
+ PageRecord<UserDailyActiveDTO> pageRecord = new PageRecord<>();
|
|
|
|
|
|
- // 3. 查询用户单日的日活记录(按时间倒序)
|
|
|
LambdaQueryWrapper<DailyActiveUsers> queryWrapper = new LambdaQueryWrapper<>();
|
|
|
- queryWrapper.eq(DailyActiveUsers::getUserId, params.getUserId())
|
|
|
- .between(DailyActiveUsers::getCreateTime, startOfDay, endOfDay)
|
|
|
- .orderByDesc(DailyActiveUsers::getCreateTime);
|
|
|
-
|
|
|
- List<DailyActiveUsers> dailyActiveUsers = dailyActiveUsersService.list(queryWrapper);
|
|
|
-
|
|
|
- // 4. 转换 DTO
|
|
|
- List<UsersDailyActiveDTO> records = dailyActiveUsers.stream().map(record -> {
|
|
|
- UsersDailyActiveDTO dto = new UsersDailyActiveDTO();
|
|
|
- dto.setDailyActiveId(record.getDailyActiveId());
|
|
|
- dto.setUserId(record.getUserId());
|
|
|
- dto.setVisitTime(record.getCreateTime()); // visitTime = createTime
|
|
|
- return dto;
|
|
|
- }).collect(Collectors.toList());
|
|
|
-
|
|
|
- // 5. 封装结果
|
|
|
- UserDailyActiveResult result = new UserDailyActiveResult();
|
|
|
- result.setVisitCount((int) totalCount);
|
|
|
- result.setRecords(records);
|
|
|
-
|
|
|
- return result;
|
|
|
+ if (Objects.nonNull(params.getCreateTimeStart())) {
|
|
|
+ queryWrapper.ge(DailyActiveUsers::getCreateTime, params.getCreateTimeStart());
|
|
|
+ }
|
|
|
+ if (Objects.nonNull(params.getCreateTimeEnd())) {
|
|
|
+ queryWrapper.lt(DailyActiveUsers::getCreateTime, params.getCreateTimeEnd().plusDays(1));
|
|
|
+ }
|
|
|
+ queryWrapper.orderByDesc(DailyActiveUsers::getCreateTime);
|
|
|
+
|
|
|
+ // ------------------------
|
|
|
+ // 手机号不为空 → 查询指定用户
|
|
|
+ // ------------------------
|
|
|
+ if (StringUtils.isNotBlank(params.getPhone())) {
|
|
|
+ final String phone = params.getPhone();
|
|
|
+ UserInfo userInfo = userService.lambdaQuery()
|
|
|
+ .eq(UserInfo::getPhone, phone)
|
|
|
+ .one();
|
|
|
+
|
|
|
+ if (userInfo == null) {
|
|
|
+ pageRecord.setRows(Collections.emptyList());
|
|
|
+ pageRecord.setTotal(0L);
|
|
|
+ pageRecord.setPageNum(params.getPageNo());
|
|
|
+ pageRecord.setPageSize(params.getPageSize());
|
|
|
+ pageRecord.setTotalPageNum(0);
|
|
|
+ return pageRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ queryWrapper.eq(DailyActiveUsers::getUserId, userInfo.getUserId());
|
|
|
+ List<DailyActiveUsers> userRecords = dailyActiveUsersService.list(queryWrapper);
|
|
|
+
|
|
|
+ UserDailyActiveDTO dto = new UserDailyActiveDTO();
|
|
|
+ dto.setPhone(phone);
|
|
|
+ dto.setVisitCount(userRecords.size());
|
|
|
+
|
|
|
+ pageRecord.setRows(Collections.singletonList(dto));
|
|
|
+ pageRecord.setTotal(userRecords.isEmpty() ? 0L : 1L);
|
|
|
+ pageRecord.setPageNum(params.getPageNo());
|
|
|
+ pageRecord.setPageSize(params.getPageSize());
|
|
|
+ pageRecord.setTotalPageNum(1);
|
|
|
+ return pageRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ // ------------------------
|
|
|
+ // 手机号为空 → 查询所有用户
|
|
|
+ // ------------------------
|
|
|
+ List<DailyActiveUsers> allRecords = dailyActiveUsersService.list(queryWrapper);
|
|
|
+ if (allRecords.isEmpty()) {
|
|
|
+ pageRecord.setRows(Collections.emptyList());
|
|
|
+ pageRecord.setTotal(0L);
|
|
|
+ pageRecord.setPageNum(params.getPageNo());
|
|
|
+ pageRecord.setPageSize(params.getPageSize());
|
|
|
+ pageRecord.setTotalPageNum(0);
|
|
|
+ return pageRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量查询用户信息
|
|
|
+ Set<Long> userIds = allRecords.stream()
|
|
|
+ .map(DailyActiveUsers::getUserId)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ List<UserInfo> userInfos = userService.listByIds(userIds);
|
|
|
+ Map<Long, String> userIdPhoneMap = userInfos.stream()
|
|
|
+ .collect(Collectors.toMap(UserInfo::getUserId, UserInfo::getPhone));
|
|
|
+
|
|
|
+ // 聚合访问次数
|
|
|
+ Map<Long, Long> visitCountMap = allRecords.stream()
|
|
|
+ .collect(Collectors.groupingBy(DailyActiveUsers::getUserId, Collectors.counting()));
|
|
|
+
|
|
|
+ // 转 DTO 列表
|
|
|
+ List<UserDailyActiveDTO> dtoList = visitCountMap.entrySet().stream()
|
|
|
+ .map(entry -> {
|
|
|
+ UserDailyActiveDTO dto = new UserDailyActiveDTO();
|
|
|
+ dto.setPhone(userIdPhoneMap.get(entry.getKey()));
|
|
|
+ dto.setVisitCount(entry.getValue().intValue());
|
|
|
+ return dto;
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ // ------------------------
|
|
|
+ // Java 层分页
|
|
|
+ // ------------------------
|
|
|
+ int pageNo = params.getPageNo();
|
|
|
+ int pageSize = params.getPageSize();
|
|
|
+ int total = dtoList.size();
|
|
|
+ int start = (pageNo - 1) * pageSize;
|
|
|
+ int end = Math.min(start + pageSize, total);
|
|
|
+ List<UserDailyActiveDTO> pageList = start >= total ? Collections.emptyList() : dtoList.subList(start, end);
|
|
|
+
|
|
|
+ pageRecord.setRows(pageList);
|
|
|
+ pageRecord.setTotal((long) total);
|
|
|
+ pageRecord.setPageNum(pageNo);
|
|
|
+ pageRecord.setPageSize(pageSize);
|
|
|
+ pageRecord.setTotalPageNum((total + pageSize - 1) / pageSize);
|
|
|
+
|
|
|
+ return pageRecord;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+
|
|
|
|