# RevenueHistoryAndForecastServiceImpl.java

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

    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.internationalization.InternationalizationNumberDTO;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.InternationalizationUtils;
    import cloud.wconnect.rateshop.entity.revenue.RevenueHistoryAndForecast;
    import cloud.wconnect.rateshop.mapper.revenue.RevenueHistoryAndForecastMapper;
    import cloud.wconnect.rateshop.pojo.bo.revenue.HistoryAndForecastBO;
    import cloud.wconnect.rateshop.service.revenue.IRevenueHistoryAndForecastService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.stereotype.Service;

    import java.math.RoundingMode;
    import java.util.List;
    import java.util.Objects;
    import java.util.stream.Collectors;

    /**
     * <p>
     * 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2025-08-05
     */
    @Service
    @DS("rate_shop")
    @Slf4j
    public class RevenueHistoryAndForecastServiceImpl extends ServiceImpl<RevenueHistoryAndForecastMapper, RevenueHistoryAndForecast> implements IRevenueHistoryAndForecastService {

        /**
         * 保存History and Forecast from thirty days ago to yesterday数据
         *
         * @param propertyDTO           资产DTO
         * @param historyAndForecastBOS 报告数据
         * @param userId                操作人
         * @return void
         * @author lichen
         * @date 2025/8/5
         */
        @Override
        public void saveHistoryAndForecastData(PropertyDTO propertyDTO, List<HistoryAndForecastBO> historyAndForecastBOS, int userId) {
            Integer propertyId = propertyDTO.getId();
            long now = DateTimeUtils.nowTimestamp();
            InternationalizationNumberDTO number = propertyDTO.getInternationalization().getNumber();
            int dataPrecision = BaseConstant.DATA_PRECISION;
            RoundingMode roundingMode = InternationalizationUtils.getRoundingMode(number.getCalculationRule());

            List<RevenueHistoryAndForecast> transientGroupLyFutures = historyAndForecastBOS.stream().map(item ->
                    new RevenueHistoryAndForecast().setPropertyId(propertyId)
                            .setTargetDate(Integer.parseInt(StringUtils.replace(item.getTargetDate(), "-", "")))
                            .setOccPercent(Objects.isNull(item.getOccPercent()) ? null : item.getOccPercent().setScale(dataPrecision, roundingMode))
                            .setTransientRoomNum(item.getTransientRoomNum())
                            .setGroupRoomNum(item.getGroupRoomNum())
                            .setAdr(Objects.isNull(item.getAdr()) ? null : item.getAdr().setScale(dataPrecision, roundingMode))
                            .setUpdateBy(userId).setUpdateTime(now)
            ).collect(Collectors.toList());

            if (CollectionUtils.isNotEmpty(transientGroupLyFutures)) {
                baseMapper.saveOrUpdateBatch(transientGroupLyFutures);
            }
        }

        /**
         * 查询指定日期范围的History And Forecast数据
         *
         * @param propertyId 资产id
         * @param startDate  开始日期（yyyyMMdd）
         * @param endDate    结束日期（yyyyMMdd）
         * @return void
         * @author lichen
         * @date 2025/8/5
         */
        @Override
        public List<RevenueHistoryAndForecast> listByDate(Integer propertyId, String startDate, String endDate) {
            return list(new LambdaQueryWrapper<RevenueHistoryAndForecast>().eq(RevenueHistoryAndForecast::getPropertyId, propertyId)
                    .between(RevenueHistoryAndForecast::getTargetDate, startDate, endDate));
        }
    }