# HotelKpiWconnectSalesSourceServiceImpl.java

    package cloud.wconnect.workperformance.service.hotelkpi.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiWconnectSalesSource;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiWconnectSalesSourceMapper;
    import cloud.wconnect.workperformance.pojo.bo.modelevent.salessource.MorSalesSourceEventBO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWconnectSalesSourceService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import jakarta.annotation.Resource;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * <p>
     * Hotel KPI wconnect Sales source Total Revenue 数据 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-08-11
     */
    @Service
    @DS("work_performance")
    public class HotelKpiWconnectSalesSourceServiceImpl extends ServiceImpl<HotelKpiWconnectSalesSourceMapper, HotelKpiWconnectSalesSource> implements IHotelKpiWconnectSalesSourceService {

        @Resource
        private BeanMapper beanMapper;

        /**
         * 保存事件数据
         *
         * @param boList 事件数据
         * @author zc
         * @date 2024/8/11
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(List<MorSalesSourceEventBO> boList) {
            if (CollectionUtils.isEmpty(boList)) {
                return;
            }
            List<HotelKpiWconnectSalesSource> salesSourceList = beanMapper.mapList(boList,
                    HotelKpiWconnectSalesSource.class);

            // 按唯一索引字段排序，避免死锁
            salesSourceList.sort(Comparator.comparing(HotelKpiWconnectSalesSource::getPropertyId)
                    .thenComparing(HotelKpiWconnectSalesSource::getDate));

            baseMapper.insertOrUpdateBatchByUnique(salesSourceList);
        }

        /**
         * 根据指定日期范围获取数据
         *
         * @param propertyId 资产 id
         * @param startDate  开始日期（yyyyMMdd）
         * @param endDate    结束日期（yyyyMMdd）
         * @return HotelKpiWconnectSalesSource 集合
         * @author zc
         * @date 2024/8/11
         */
        @Override
        public List<HotelKpiWconnectSalesSource> listByDateRange(int propertyId, long startDate, long endDate) {
            return list(new LambdaQueryWrapper<HotelKpiWconnectSalesSource>()
                    .between(HotelKpiWconnectSalesSource::getDate, startDate, endDate)
                    .eq(HotelKpiWconnectSalesSource::getPropertyId, propertyId));
        }

    }