# GroupPickupDataSourceServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.rateshop.entity.rateshop.GroupPickupDataSource;
    import cloud.wconnect.rateshop.mapper.rateshop.GroupPickupDataSourceMapper;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.GroupPickupLatestUpdateInfoDTO;
    import cloud.wconnect.rateshop.service.rateshop.IGroupPickupDataSourceService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.util.Comparator;
    import java.util.List;

    /**
     * <p>
     *  服务实现类
     * </p>
     *
     * @author yyt
     * @since 2025-06-24
     */
    @Service
    @DS("rate_shop")
    public class GroupPickupDataSourceServiceImpl extends ServiceImpl<GroupPickupDataSourceMapper, GroupPickupDataSource> implements IGroupPickupDataSourceService {

        @Autowired
        private BeanMapper beanMapper;

        /**
         * 获取 group pickup 最新更新信息
         *
         * @param propertyId 资产 id
         * @param targetDate 指定日期（yyyyMMdd）
         * @return group pickup 最新更新信息
         * @author yyt
         * @date 2025/6/24
         **/
        @Override
        public List<GroupPickupLatestUpdateInfoDTO> listLatestUpdateInfo(Integer propertyId, Integer targetDate) {
            return beanMapper.mapList(list(new QueryWrapper<GroupPickupDataSource>().lambda()
                    .eq(GroupPickupDataSource::getPropertyId, propertyId)
                    .eq(GroupPickupDataSource::getTargetDate, targetDate)), GroupPickupLatestUpdateInfoDTO.class);
        }

        /**
         * 保存 group pickup 最新更新时间
         *
         * @param propertyId        资产 id
         * @param dataSource        1、pms，2、商业平台
         * @param targetDates       指定日期
         * @param bookingStatusList booking status
         * @author yyt
         * @date 2025/6/24
         **/
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveBatch(Integer propertyId, Integer dataSource, List<Integer> targetDates, List<Integer> bookingStatusList) {
            if (CollectionUtils.isEmpty(targetDates)) return;

            long now = DateTimeUtils.nowTimestamp();

            List<GroupPickupDataSource> dataSources = Lists.newArrayListWithExpectedSize(targetDates.size() * bookingStatusList.size());
            for (Integer targetDate : targetDates) {
                dataSources.addAll(bookingStatusList.stream()
                        .map(item -> new GroupPickupDataSource()
                                .setPropertyId(propertyId)
                                .setTargetDate(targetDate)
                                .setBookingStatus(item)
                                .setDataSource(dataSource)
                                .setCreatedTime(now))
                        .toList());
            }

            // 先根据唯一索引条件排序
            dataSources.sort(Comparator.comparing(GroupPickupDataSource::getTargetDate).thenComparing(GroupPickupDataSource::getBookingStatus));

            baseMapper.saveOrUpdateBatch(dataSources);
        }

    }