|
|
@@ -9,10 +9,19 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Optional;
|
|
|
+import java.util.Set;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
+import com.punchsettle.server.atomic.entity.RewardExchangeHistory;
|
|
|
+import com.punchsettle.server.atomic.entity.SettleUserHistory;
|
|
|
+import com.punchsettle.server.atomic.service.IRewardExchangeHistoryService;
|
|
|
+import com.punchsettle.server.atomic.service.ISettleUserHistoryService;
|
|
|
+import com.punchsettle.server.pojo.reward.RewardExchangeHistoryQuery;
|
|
|
+import com.punchsettle.server.pojo.settle.SettleUserHistoryQuery;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.cache.Cache;
|
|
|
import org.springframework.cache.CacheManager;
|
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
@@ -62,6 +71,7 @@ import com.punchsettle.server.utiis.UserUtils;
|
|
|
* @date 2025/5/3 9:32
|
|
|
* @description 统计数据服务类
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Component
|
|
|
public class StatManagerImpl implements IStatManager {
|
|
|
|
|
|
@@ -100,6 +110,12 @@ public class StatManagerImpl implements IStatManager {
|
|
|
@Autowired
|
|
|
private CacheManager cacheManager;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private ISettleUserHistoryService settleUserHistoryService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IRewardExchangeHistoryService rewardExchangeHistoryService;
|
|
|
+
|
|
|
@Override
|
|
|
@Cacheable(cacheNames = CacheNameConstant.STAT_POINTS_LINE, key = "T(com.punchsettle.server.utiis.UserUtils).getCurrentUserId()")
|
|
|
public LineVO queryStatPointsLine() {
|
|
|
@@ -121,18 +137,15 @@ public class StatManagerImpl implements IStatManager {
|
|
|
|
|
|
List<Integer> settlePointsList = new ArrayList<>(lineCategories.length);
|
|
|
List<Integer> consumePointsList = new ArrayList<>(lineCategories.length);
|
|
|
- List<Integer> totalPointsList = new ArrayList<>(lineCategories.length);
|
|
|
for (String lineCategory : lineCategories) {
|
|
|
StatPoints statPoints = statPointsMap.get(lineCategory);
|
|
|
if (Objects.isNull(statPoints)) {
|
|
|
settlePointsList.add(null);
|
|
|
consumePointsList.add(null);
|
|
|
- totalPointsList.add(null);
|
|
|
continue;
|
|
|
}
|
|
|
settlePointsList.add(statPoints.getSettlePoints());
|
|
|
consumePointsList.add(statPoints.getConsumePoints());
|
|
|
- totalPointsList.add(statPoints.getTotalPoints());
|
|
|
}
|
|
|
|
|
|
LineSeriesVO settlePointsLineSeriesVO = new LineSeriesVO();
|
|
|
@@ -143,13 +156,9 @@ public class StatManagerImpl implements IStatManager {
|
|
|
consumePointsLineSeriesVO.setName("消耗积分");
|
|
|
consumePointsLineSeriesVO.setData(consumePointsList.toArray(Integer[]::new));
|
|
|
|
|
|
- LineSeriesVO totalPointsLineSeriesVO = new LineSeriesVO();
|
|
|
- totalPointsLineSeriesVO.setName("总积分");
|
|
|
- totalPointsLineSeriesVO.setData(totalPointsList.toArray(Integer[]::new));
|
|
|
-
|
|
|
LineVO lineVO = new LineVO();
|
|
|
lineVO.setCategories(lineCategories);
|
|
|
- lineVO.setSeries(Arrays.asList(settlePointsLineSeriesVO, consumePointsLineSeriesVO, totalPointsLineSeriesVO));
|
|
|
+ lineVO.setSeries(Arrays.asList(settlePointsLineSeriesVO, consumePointsLineSeriesVO));
|
|
|
return lineVO;
|
|
|
}
|
|
|
|
|
|
@@ -384,4 +393,70 @@ public class StatManagerImpl implements IStatManager {
|
|
|
|
|
|
cacheManager.getCache(CacheNameConstant.STAT_NEW_USER_LINE).clear();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void statPoints(List<Long> userIds, LocalDate statDate) {
|
|
|
+ // 校验是否重复统计
|
|
|
+ StatPointsQuery statPointsQuery = new StatPointsQuery();
|
|
|
+ statPointsQuery.setUserIds(userIds);
|
|
|
+ statPointsQuery.setStatTime(statDate.toString());
|
|
|
+ List<StatPoints> oldStatPoints = statPointsService.queryByCondition(statPointsQuery);
|
|
|
+ // 已统计用户列表
|
|
|
+ Set<Long> statUserIds = oldStatPoints.stream().map(StatPoints::getUserId).collect(Collectors.toSet());
|
|
|
+ // 过滤已统计数据
|
|
|
+ userIds = userIds.stream().filter(userId -> !statUserIds.contains(userId)).toList();
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(userIds)) {
|
|
|
+ log.info("========== 积分数据统计定时任务,没有需要统计的用户 结束执行 ==========");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 兑换开始时间
|
|
|
+ String exchangeTimeFrom = String.format("%s 00:00:00.000", statDate);
|
|
|
+ // 兑换结束时间
|
|
|
+ String exchangeTimeTo = String.format("%s 23:59:59.999", statDate);
|
|
|
+
|
|
|
+ // 查询结算数据
|
|
|
+ SettleUserHistoryQuery settleUserHistoryQuery = new SettleUserHistoryQuery();
|
|
|
+ settleUserHistoryQuery.setSettleDate(statDate.toString());
|
|
|
+ settleUserHistoryQuery.setUserIds(userIds);
|
|
|
+ settleUserHistoryQuery.setSettleResults(Arrays.asList(SettleResultEnum.SETTLE, SettleResultEnum.NO_SETTLED));
|
|
|
+ List<SettleUserHistory> settleUserHistories = settleUserHistoryService.querySettleUserHistory(settleUserHistoryQuery);
|
|
|
+ // 用户ID-结算数据 关联
|
|
|
+ Map<Long, SettleUserHistory> settleUserHistoryMap = settleUserHistories.stream().collect(Collectors.toMap(SettleUserHistory::getUserId, Function.identity(), (key1, key2) -> key1));
|
|
|
+
|
|
|
+ // 查询消费记录
|
|
|
+ RewardExchangeHistoryQuery rewardExchangeHistoryQuery = new RewardExchangeHistoryQuery();
|
|
|
+ rewardExchangeHistoryQuery.setUserIds(userIds);
|
|
|
+ rewardExchangeHistoryQuery.setExchangeTimeFrom(exchangeTimeFrom);
|
|
|
+ rewardExchangeHistoryQuery.setExchangeTimeTo(exchangeTimeTo);
|
|
|
+ List<RewardExchangeHistory> rewardExchangeHistories = rewardExchangeHistoryService.queryByCondition(rewardExchangeHistoryQuery);
|
|
|
+ // 用户ID - 兑换列表
|
|
|
+ Map<Long, List<RewardExchangeHistory>> rewardExchangeHistoryMap = rewardExchangeHistories.stream().collect(Collectors.groupingBy(RewardExchangeHistory::getUserId));
|
|
|
+
|
|
|
+ List<StatPoints> addStatPointsList = userIds.stream().map(userId -> {
|
|
|
+ // 结算数据
|
|
|
+ SettleUserHistory settleUserHistory = settleUserHistoryMap.get(userId);
|
|
|
+ // 兑换列表
|
|
|
+ List<RewardExchangeHistory> rewardExchangeHistoryList = rewardExchangeHistoryMap.get(userId);
|
|
|
+ // 消耗总积分
|
|
|
+ int consumePoints = rewardExchangeHistoryList.stream()
|
|
|
+ .mapToInt(rewardExchangeHistory -> Optional.ofNullable(rewardExchangeHistory.getExchangeTotalPoints()).orElse(0))
|
|
|
+ .sum();
|
|
|
+
|
|
|
+ StatPoints addStatPoints = new StatPoints();
|
|
|
+ addStatPoints.setUserId(userId);
|
|
|
+ addStatPoints.setStatTime(statDate.toString());
|
|
|
+ addStatPoints.setSettlePoints(Objects.isNull(settleUserHistory) ? 0 : Optional.ofNullable(settleUserHistory.getSettlePoints()).orElse(0));
|
|
|
+ addStatPoints.setConsumePoints(consumePoints);
|
|
|
+ return addStatPoints;
|
|
|
+ }).toList();
|
|
|
+
|
|
|
+ statPointsService.batchAdd(addStatPointsList);
|
|
|
+
|
|
|
+ // 清除缓存
|
|
|
+ Cache cache = cacheManager.getCache(CacheNameConstant.STAT_POINTS_LINE);
|
|
|
+ userIds.forEach(userId -> cache.evict(userId));
|
|
|
+ }
|
|
|
}
|