# StatisticsOverviewScoreImpl.java

    package cloud.wconnect.rateshop.service.reviewanalysis.impl;

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.internationalization.InternationalizationDateTimeDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.cons.reviewanalysis.ReviewAnalysisCons;
    import cloud.wconnect.rateshop.entity.reviewanalysis.Review;
    import cloud.wconnect.rateshop.pojo.dto.reviewanalysis.GroupStatisticsReviewDTO;
    import cloud.wconnect.rateshop.service.reviewanalysis.IStatisticsReview;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import java.time.DayOfWeek;
    import java.time.LocalDate;
    import java.time.temporal.TemporalAdjusters;
    import java.util.*;
    import java.util.stream.Collectors;

    /**
     * 不同时间单位下的平均得分趋势图（统计字段为date）
     *
     * @author lichen
     * @date 2024/12/27
     */
    @Service
    @DS("rate_shop")
    public class StatisticsOverviewScoreImpl implements IStatisticsReview {

        /**
         * 判断是否支持此统计类型
         *
         * @param type 统计类型
         * @return boolean
         * @author lichen
         * @date 2024/12/27
         */
        @Override
        public boolean supports(int type) {
            return ReviewAnalysisCons.OVERVIEW_SCORE == type;
        }

        /**
         * 统计数据
         *
         * @param propertyDTO         资产DTO
         * @param reviews             评论数据集合
         * @param statisticsDimension 统计维度 1-月 2-周 3-日
         * @return java.util.List<cloud.wconnect.rateshop.pojo.dto.reviewanalysis.StatisticsReviewDTO>
         * @author lichen
         * @date 2024/12/27
         */
        @Override
        public List<GroupStatisticsReviewDTO> statistics(PropertyDTO propertyDTO, List<Review> reviews, Integer statisticsDimension) {
            if (CollectionUtils.isEmpty(reviews)) return Collections.emptyList();

            InternationalizationDateTimeDTO dateTime = propertyDTO.getInternationalization().getDateTime();
            //获取周起始日
            DayOfWeek startOfWeek = DateTimeUtils.getDayOfWeek(dateTime.getStartOfWeek());

            //按照date分组
            Map<String, List<Review>> date2Reviews = reviews.stream().collect(Collectors.groupingBy(item -> {
                String reviewDate = item.getReviewDate();
                if (ReviewAnalysisCons.DAY == statisticsDimension) {
                    return reviewDate;
                } else if (ReviewAnalysisCons.MONTH == statisticsDimension) {
                    return reviewDate.substring(0, 6);
                } else {
                    LocalDate localDate = DateTimeUtils.strToLocalDate(reviewDate, BaseConstant.DATE_FORMAT_YYYYMMDD);
                    //获取当前日期所属的周
                    Map<String, Integer> weekNumber = DateTimeUtils.getWeekNumber(localDate, startOfWeek);
                    return weekNumber.get("year").toString() + weekNumber.get("week").toString();
                }
            }));

            ArrayList<GroupStatisticsReviewDTO> resultList = Lists.newArrayListWithCapacity(date2Reviews.size());

            for (Map.Entry<String, List<Review>> entry : date2Reviews.entrySet()) {
                String date = entry.getKey();
                List<Review> list = entry.getValue();

                //统计维度内的开始日期、结束日期
                String startDate = "";
                String endDate = "";
                if (ReviewAnalysisCons.WEEK == statisticsDimension) {
                    //获取周的开始日期和结束日期
                    int year = Integer.parseInt(date.substring(0, 4));
                    int week = Integer.parseInt(date.substring(4));
                    LocalDate startDateOfNWeek = DateTimeUtils.getStartDateOfNWeek(year, week, startOfWeek);
                    startDate = DateTimeUtils.localDateToStr(startDateOfNWeek, BaseConstant.DATE_FORMAT_YYYYMMDD);
                    endDate = DateTimeUtils.localDateToStr(startDateOfNWeek.plusDays(6), BaseConstant.DATE_FORMAT_YYYYMMDD);
                } else if (ReviewAnalysisCons.MONTH == statisticsDimension) {
                    startDate = date + "01";

                    //当月最后一天
                    LocalDate localDate = DateTimeUtils.strToLocalDate(date + "01", BaseConstant.DATE_FORMAT_YYYYMMDD);
                    LocalDate lastDayOfMonth = localDate.with(TemporalAdjusters.lastDayOfMonth());
                    endDate = DateTimeUtils.localDateToStr(lastDayOfMonth, BaseConstant.DATE_FORMAT_YYYYMMDD);
                }

                //计算
                GroupStatisticsReviewDTO groupStatisticsReviewDTO = IStatisticsReview.compute(list);
                groupStatisticsReviewDTO.setDate(date);
                groupStatisticsReviewDTO.setStartDate(startDate);
                groupStatisticsReviewDTO.setEndDate(endDate);
                resultList.add(groupStatisticsReviewDTO);
            }

            //按照日期正序排列
            resultList.sort(Comparator.comparing(GroupStatisticsReviewDTO::getDate));
            return resultList;
        }
    }