# HandleHistoryAndForecastReportCommonServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.entity.revenue.RevenueDailySummary;
    import cloud.wconnect.rateshop.entity.revenue.RevenueImportLog;
    import cloud.wconnect.rateshop.pojo.bo.revenue.DailySummaryBO;
    import cloud.wconnect.rateshop.pojo.dto.revenue.HistoryAndForecastDailySummaryDTO;
    import cloud.wconnect.rateshop.service.revenue.IHandleHistoryAndForecastReportCommonService;
    import cloud.wconnect.rateshop.service.revenue.IRevenueImportLogService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import lombok.RequiredArgsConstructor;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;

    import java.time.LocalDate;
    import java.time.ZonedDateTime;
    import java.util.Collections;
    import java.util.List;

    /**
     *
     * @author: yyt
     * 2026-03-03
     */
    @RequiredArgsConstructor
    @Service
    @DS("rate_shop")
    public class HandleHistoryAndForecastReportCommonServiceImpl implements IHandleHistoryAndForecastReportCommonService {

        private final IRevenueImportLogService revenueImportLogService;
        private final BeanMapper beanMapper;

        /**
         * 过滤和转化数据
         *
         * @param propertyDTO                        资产DTO
         * @param uid                                用户ID
         * @param logId                              日志id
         * @param historyAndForecastDailySummaryDTOS 数据
         * @return java.util.List<cloud.wconnect.rateshop.pojo.bo.revenue.DailySummaryBO>
         * @author lichen
         * @date 2025/11/19
         */
        @Override
        public List<DailySummaryBO> filterAndTransform(PropertyDTO propertyDTO, Integer uid, Long logId, List<HistoryAndForecastDailySummaryDTO> historyAndForecastDailySummaryDTOS) {
            if (CollectionUtils.isEmpty(historyAndForecastDailySummaryDTOS)) {
                return Collections.emptyList();
            }

            String timezone = propertyDTO.getTimezone();
            ZonedDateTime zonedDateTime = DateTimeUtils.dateNow(timezone);
            //当天
            long currentDate = zonedDateTime.toEpochSecond();
            //前一天
            long yesterdayLong = zonedDateTime.minusDays(1).toEpochSecond();
            //转化为BO
            List<DailySummaryBO> dailySummaryBOS = beanMapper.mapList(historyAndForecastDailySummaryDTOS, DailySummaryBO.class);

            List<DailySummaryBO> filterList;
            /*
             * 1. uid==0表示自动导入
             *    (1)5:00 AM以后，保存昨天（当前日期-1）的Final时刻、当天数据
             *    (2)5:00 AM以前，保存当天数据
             * 2. uid > 0表示手动导入，保存businessDate-1天的Final时刻
             *    手动导入时，已限制只能导入businessDate-1天的数据，所以只需要把hour设置为final即可
             */
            if (uid == 0) {
                //当前时刻
                int hour = DateTimeUtils.dateTimeNow(timezone).getHour();
                if (hour < 5) {
                    //5:00 AM以前，保存当天数据
                    filterList = dailySummaryBOS.stream().filter(item -> item.getTheDate() == currentDate).toList();
                } else {
                    //5:00 AM以后，保存昨天（当前日期-1）的Final时刻、当天数据
                    filterList = dailySummaryBOS.stream()
                            .filter(item -> item.getTheDate() == yesterdayLong || item.getTheDate() == currentDate)
                            .peek(bo -> {
                                if (bo.getTheDate() == yesterdayLong) {
                                    //把前一天的数据设置成Final
                                    bo.setTheHour(RevenueDailySummary.FINAL_HOUR);
                                }
                            }).toList();
                }
            } else {
                //获取businessDate - 1天
                RevenueImportLog importLog = revenueImportLogService.getById(logId);
                Long businessDate = importLog.getTheDate();
                LocalDate beforeDateOfBusinessDate = DateTimeUtils.longToLocalDate(businessDate, timezone).minusDays(1);
                long beforeDateLong = DateTimeUtils.localDateToLong(beforeDateOfBusinessDate, timezone);

                filterList = dailySummaryBOS.stream()
                        .filter(item -> item.getTheDate() == beforeDateLong)
                        .peek(bo -> bo.setTheHour(RevenueDailySummary.FINAL_HOUR)).toList();
            }
            return filterList;
        }

    }