# WeeklyMonthlyRevenueSegmentServiceImpl.java

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

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.rateshop.entity.weeklymonthlyrevenue.WeeklyMonthlyRevenueSegment;
    import cloud.wconnect.rateshop.mapper.weeklymonthlyrevenue.WeeklyMonthlyRevenueSegmentMapper;
    import cloud.wconnect.rateshop.service.weeklymonthlyrevenue.IWeeklyMonthlyRevenueSegmentService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.*;
    import java.util.function.Function;
    import java.util.stream.Collectors;

    /**
     * <p>
     * Weekly Monthly Revenue Segment 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-05-13
     */
    @Service
    @DS("rate_shop")
    public class WeeklyMonthlyRevenueSegmentServiceImpl extends ServiceImpl<WeeklyMonthlyRevenueSegmentMapper, WeeklyMonthlyRevenueSegment> implements IWeeklyMonthlyRevenueSegmentService {

        /**
         * 获取指定日期范围的 Room Revenue Segment
         *
         * @param propertyId 资产 id
         * @param startDate  开始日期（yyyyMMdd）
         * @param endDate    结束日期（yyyyMMdd）
         * @return List<WeeklyMonthlyRevenueSegment>
         * @author zc
         * @date 2024/5/14
         */
        @Override
        public List<WeeklyMonthlyRevenueSegment> listByDateRange(int propertyId, long startDate, long endDate) {
            return list(new LambdaQueryWrapper<WeeklyMonthlyRevenueSegment>()
                    .between(WeeklyMonthlyRevenueSegment::getTheDate, startDate, endDate)
                    .eq(WeeklyMonthlyRevenueSegment::getPropertyId, propertyId));
        }

        /**
         * 保存 Room Revenue Segment Group Contracted
         *
         * @param groupContractedList Group Contracted List
         * @author zc
         * @date 2024/5/18
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveGroupContractedBatch(List<WeeklyMonthlyRevenueSegment> groupContractedList) {
            if (CollectionUtils.isEmpty(groupContractedList)) {
                return;
            }
            // 按唯一索引字段排序，避免死锁
            groupContractedList.sort(Comparator.comparing(WeeklyMonthlyRevenueSegment::getPropertyId)
                    .thenComparing(WeeklyMonthlyRevenueSegment::getTheDate));

            baseMapper.insertOrUpdateGroupContractedBatchByUnique(groupContractedList);
        }

        /**
         * 保存 Room Revenue Segment Revenue
         *
         * @param revenueSegmentList Revenue Segment List
         * @author zc
         * @date 2024/5/20
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveRevenueBatch(List<WeeklyMonthlyRevenueSegment> revenueSegmentList) {
            if (CollectionUtils.isEmpty(revenueSegmentList)) {
                return;
            }
            // 按唯一索引字段排序，避免死锁
            revenueSegmentList.sort(Comparator.comparing(WeeklyMonthlyRevenueSegment::getPropertyId)
                    .thenComparing(WeeklyMonthlyRevenueSegment::getTheDate));

            baseMapper.insertOrUpdateRevenueBatchByUnique(revenueSegmentList);
        }

        /**
         * 保存 Room Revenue Segment Group Cancelled
         *
         * @param groupCancelledList Group Cancelled List
         * @author zc
         * @date 2024/5/20
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveGroupCancelledBatch(List<WeeklyMonthlyRevenueSegment> groupCancelledList) {
            if (CollectionUtils.isEmpty(groupCancelledList)) {
                return;
            }
            // 按唯一索引字段排序，避免死锁
            groupCancelledList.sort(Comparator.comparing(WeeklyMonthlyRevenueSegment::getPropertyId)
                    .thenComparing(WeeklyMonthlyRevenueSegment::getTheDate));

            baseMapper.insertOrUpdateGroupCancelledBatchByUnique(groupCancelledList);
        }

        /**
         * 批量保存 Room Revenue Segment Forecast Group Contracted
         *
         * @param propertyDTO  资产DTO
         * @param forecastList 未来数据
         * @author lichen
         * @date 2025/11/1
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveBatchForecastGroupContracted(PropertyDTO propertyDTO, List<WeeklyMonthlyRevenueSegment> forecastList) {
            if (CollectionUtils.isEmpty(forecastList)) {
                return;
            }

            List<Long> theDateList = forecastList.stream().map(WeeklyMonthlyRevenueSegment::getTheDate).toList();
            Long startDate = Collections.min(theDateList);
            Long endDate = Collections.max(theDateList);
            //查询指定范围的数据
            Map<Long, WeeklyMonthlyRevenueSegment> theDate2OriginEntity = listByDateRange(propertyDTO.getId(), startDate, endDate)
                    .stream().collect(Collectors.toMap(WeeklyMonthlyRevenueSegment::getTheDate, Function.identity()));

            ArrayList<WeeklyMonthlyRevenueSegment> batchList = Lists.newArrayListWithCapacity(forecastList.size());

            for (WeeklyMonthlyRevenueSegment forecastEntity : forecastList) {
                WeeklyMonthlyRevenueSegment originEntity = theDate2OriginEntity.get(forecastEntity.getTheDate());
                if (Objects.nonNull(originEntity) && Objects.nonNull(originEntity.getGroupContractedVersion())) {
                    //未来数据的版本>=原来数据的版本，则更新
                    if (forecastEntity.getGroupContractedVersion().compareTo(originEntity.getGroupContractedVersion()) >= 0) {
                        batchList.add(forecastEntity);
                    }
                } else {
                    batchList.add(forecastEntity);
                }
            }
            // 批量更新
            saveGroupContractedBatch(batchList);
        }
    }