|
|
@@ -0,0 +1,191 @@
|
|
|
+package com.punchsettle.server.service.manager.impl;
|
|
|
+
|
|
|
+import com.punchsettle.server.atomic.entity.PiTaskHistory;
|
|
|
+import com.punchsettle.server.atomic.entity.StatNewUser;
|
|
|
+import com.punchsettle.server.atomic.entity.StatPoints;
|
|
|
+import com.punchsettle.server.atomic.service.IPiTaskHistoryService;
|
|
|
+import com.punchsettle.server.atomic.service.IStatNewUserService;
|
|
|
+import com.punchsettle.server.atomic.service.IStatPointsService;
|
|
|
+import com.punchsettle.server.constant.PunchInResultEnum;
|
|
|
+import com.punchsettle.server.core.config.BizProperties;
|
|
|
+import com.punchsettle.server.pojo.punchIn.PiTaskHistoryQuery;
|
|
|
+import com.punchsettle.server.pojo.stat.StatNewUserQuery;
|
|
|
+import com.punchsettle.server.pojo.stat.StatPointsQuery;
|
|
|
+import com.punchsettle.server.pojo.ucharts.LineSeriesVO;
|
|
|
+import com.punchsettle.server.pojo.ucharts.LineVO;
|
|
|
+import com.punchsettle.server.service.manager.IStatManager;
|
|
|
+import com.punchsettle.server.utiis.DateUtils;
|
|
|
+import com.punchsettle.server.utiis.UserUtils;
|
|
|
+import lombok.Data;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author myou
|
|
|
+ * @version 1.0.0
|
|
|
+ * @date 2025/5/3 9:32
|
|
|
+ * @description 统计数据服务类
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class StatManagerImpl implements IStatManager {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IStatPointsService statPointsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IStatNewUserService statNewUserService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IPiTaskHistoryService piTaskHistoryService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private BizProperties bizProperties;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LineVO queryStatPointsLine() {
|
|
|
+ LocalDate endDate = LocalDate.now();
|
|
|
+ LocalDate startDate = endDate.minusDays(bizProperties.getLineItemCount());
|
|
|
+
|
|
|
+ // 获取折线图时间范围
|
|
|
+ List<LocalDate> dateRange = DateUtils.getDateRange(startDate, endDate);
|
|
|
+ String[] lineCategories = dateRange.stream().map(LocalDate::toString).toArray(String[]::new);
|
|
|
+
|
|
|
+ StatPointsQuery statPointsQuery = new StatPointsQuery();
|
|
|
+ statPointsQuery.setUserId(UserUtils.getCurrentUserId());
|
|
|
+ statPointsQuery.setStatStartTime(startDate.toString());
|
|
|
+ statPointsQuery.setStatEndTime(endDate.toString());
|
|
|
+ List<StatPoints> statPointsList = statPointsService.queryByCondition(statPointsQuery);
|
|
|
+ Map<String, StatPoints> statPointsMap = statPointsList.stream().collect(Collectors.toMap(StatPoints::getStatTime, Function.identity(), (v1, v2) -> v1));
|
|
|
+
|
|
|
+ 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();
|
|
|
+ settlePointsLineSeriesVO.setName("结算积分");
|
|
|
+ settlePointsLineSeriesVO.setData(settlePointsList.toArray(Integer[]::new));
|
|
|
+
|
|
|
+ LineSeriesVO consumePointsLineSeriesVO = new LineSeriesVO();
|
|
|
+ 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));
|
|
|
+ return lineVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LineVO queryStatNewUserLine() {
|
|
|
+ LocalDate endDate = LocalDate.now();
|
|
|
+ LocalDate startDate = endDate.minusDays(bizProperties.getLineItemCount());
|
|
|
+
|
|
|
+ // 获取折线图时间范围
|
|
|
+ List<LocalDate> dateRange = DateUtils.getDateRange(startDate, endDate);
|
|
|
+ String[] lineCategories = dateRange.stream().map(LocalDate::toString).toArray(String[]::new);
|
|
|
+
|
|
|
+ StatNewUserQuery statNewUserQuery = new StatNewUserQuery();
|
|
|
+ statNewUserQuery.setStatStartTime(startDate.toString());
|
|
|
+ statNewUserQuery.setStatEndTime(endDate.toString());
|
|
|
+ List<StatNewUser> statNewUserList = statNewUserService.queryByCondition(statNewUserQuery);
|
|
|
+ Map<String, Integer> statNewUserMap = statNewUserList.stream().collect(Collectors.toMap(StatNewUser::getStatTime, StatNewUser::getNewUserCount, (v1, v2) -> v1));
|
|
|
+
|
|
|
+ List<Integer> newUserCountList = new ArrayList<>(lineCategories.length);
|
|
|
+ for (String lineCategory : lineCategories) {
|
|
|
+ Integer newUserCount = Optional.ofNullable(statNewUserMap.get(lineCategory)).orElse(0);
|
|
|
+ newUserCountList.add(newUserCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ LineSeriesVO newUserLineSeriesVO = new LineSeriesVO();
|
|
|
+ newUserLineSeriesVO.setName("新增用户数");
|
|
|
+ newUserLineSeriesVO.setData(newUserCountList.toArray(Integer[]::new));
|
|
|
+
|
|
|
+ LineVO lineVO = new LineVO();
|
|
|
+ lineVO.setCategories(lineCategories);
|
|
|
+ lineVO.setSeries(Arrays.asList(newUserLineSeriesVO));
|
|
|
+ return lineVO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public LineVO queryStatTaskLine() {
|
|
|
+ LocalDate endDate = LocalDate.now();
|
|
|
+ LocalDate startDate = endDate.minusDays(bizProperties.getLineItemCount());
|
|
|
+
|
|
|
+ // 获取折线图时间范围
|
|
|
+ List<LocalDate> dateRange = DateUtils.getDateRange(startDate, endDate);
|
|
|
+ String[] lineCategories = dateRange.stream().map(LocalDate::toString).toArray(String[]::new);
|
|
|
+
|
|
|
+ PiTaskHistoryQuery piTaskHistoryQuery = new PiTaskHistoryQuery();
|
|
|
+ piTaskHistoryQuery.setPunchInDateFrom(startDate.toString());
|
|
|
+ piTaskHistoryQuery.setPunchInDateTo(endDate.toString());
|
|
|
+ List<PiTaskHistory> piTaskHistoryList = piTaskHistoryService.queryByCondition(piTaskHistoryQuery);
|
|
|
+ Map<String, List<PiTaskHistory>> piTaskHistoryMap = piTaskHistoryList.stream().collect(Collectors.groupingBy(PiTaskHistory::getPunchInDate));
|
|
|
+
|
|
|
+ // 总任务数
|
|
|
+ List<Integer> totalTaskCountList = new ArrayList<>(lineCategories.length);
|
|
|
+ // 打卡任务数
|
|
|
+ List<Integer> punchInTaskCountList = new ArrayList<>(lineCategories.length);
|
|
|
+ // 完成打卡任务数
|
|
|
+ List<Integer> doneTaskCountList = new ArrayList<>(lineCategories.length);
|
|
|
+ for (String lineCategory : lineCategories) {
|
|
|
+ List<PiTaskHistory> piTaskHistories = piTaskHistoryMap.get(lineCategory);
|
|
|
+ if (CollectionUtils.isEmpty(piTaskHistories)) {
|
|
|
+ totalTaskCountList.add(0);
|
|
|
+ punchInTaskCountList.add(0);
|
|
|
+ doneTaskCountList.add(0);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 总任数
|
|
|
+ totalTaskCountList.add(piTaskHistories.size());
|
|
|
+ // TODO 这里要单独弄一个统计表,不然无法计算
|
|
|
+ punchInTaskCountList.add(piTaskHistories.size());
|
|
|
+ // 任务完成数
|
|
|
+ doneTaskCountList.add(piTaskHistories.stream().filter(piTaskHistory -> piTaskHistory.getPunchInResult().equals(PunchInResultEnum.DONE)).collect(Collectors.toList()).size());
|
|
|
+ }
|
|
|
+
|
|
|
+ LineSeriesVO totalTaskCountLineSeriesVO = new LineSeriesVO();
|
|
|
+ totalTaskCountLineSeriesVO.setName("总任务数");
|
|
|
+ totalTaskCountLineSeriesVO.setData(totalTaskCountList.toArray(Integer[]::new));
|
|
|
+
|
|
|
+ LineSeriesVO punchInTaskCountLineSeriesVO = new LineSeriesVO();
|
|
|
+ punchInTaskCountLineSeriesVO.setName("打卡数量");
|
|
|
+ punchInTaskCountLineSeriesVO.setData(punchInTaskCountList.toArray(Integer[]::new));
|
|
|
+
|
|
|
+ LineSeriesVO doneTaskCountLineSeriesVO = new LineSeriesVO();
|
|
|
+ doneTaskCountLineSeriesVO.setName("完成数量");
|
|
|
+ doneTaskCountLineSeriesVO.setData(doneTaskCountList.toArray(Integer[]::new));
|
|
|
+
|
|
|
+ LineVO lineVO = new LineVO();
|
|
|
+ lineVO.setCategories(lineCategories);
|
|
|
+ lineVO.setSeries(Arrays.asList(totalTaskCountLineSeriesVO, punchInTaskCountLineSeriesVO, doneTaskCountLineSeriesVO));
|
|
|
+ return lineVO;
|
|
|
+ }
|
|
|
+}
|