# HotelKpiWconnectMonthlyRevenueServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiWconnectMonthlyRevenue;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiWconnectMonthlyRevenueMapper;
    import cloud.wconnect.workperformance.pojo.bo.modelevent.monthlyrevenue.MonthlyRevenueEventBO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWconnectMonthlyRevenueService;
    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.collections4.CollectionUtils;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * <p>
     * Hotel KPI wconnect Monthly Revenue 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-10-15
     */
    @Service
    @DS("work_performance")
    public class HotelKpiWconnectMonthlyRevenueServiceImpl extends ServiceImpl<HotelKpiWconnectMonthlyRevenueMapper, HotelKpiWconnectMonthlyRevenue> implements IHotelKpiWconnectMonthlyRevenueService {

        @Resource
        private BeanMapper beanMapper;

        /**
         * 保存数据
         *
         * @param eventBO 事件体
         * @author zc
         * @date 2025/10/15
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(MonthlyRevenueEventBO eventBO) {
            if (CollectionUtils.isEmpty(eventBO.getData())) {
                return;
            }
            long nowTimestamp = DateTimeUtils.nowTimestamp();
            List<HotelKpiWconnectMonthlyRevenue> monthlyRevenueList = beanMapper.mapList(eventBO.getData(),
                    HotelKpiWconnectMonthlyRevenue.class, (bo, entity) -> {
                        entity.setPropertyId(eventBO.getPropertyId());
                        entity.setUpdatedTime(nowTimestamp);
                    });

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

            baseMapper.insertOrUpdateBatchByUnique(monthlyRevenueList);
        }

        /**
         * 根据日期范围查询数据
         *
         * @param propertyId 资产 id
         * @param startDate  开始日期（yyyyMMdd）
         * @param endDate    结束日期（yyyyMMdd）
         * @return HotelKpiWconnectMonthlyRevenue 集合
         * @author zc
         * @date 2025/10/15
         */
        @Override
        public List<HotelKpiWconnectMonthlyRevenue> listByDateRange(int propertyId, long startDate, long endDate) {
            return list(new LambdaQueryWrapper<HotelKpiWconnectMonthlyRevenue>()
                    .eq(HotelKpiWconnectMonthlyRevenue::getPropertyId, propertyId)
                    .between(HotelKpiWconnectMonthlyRevenue::getDate, startDate, endDate));
        }
    }