# HotelKpiWconnectOvertimeServiceImpl.java

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

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiWconnectOvertime;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiWconnectOvertimeMapper;
    import cloud.wconnect.workperformance.pojo.bo.modelevent.overtime.MorOvertimeEventBO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWconnectOvertimeService;
    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 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 OT Report Overtime 数据 服务实现类
     * </p>
     *
     * @author zc
     * @since 2024-07-20
     */
    @Service
    @DS("work_performance")
    public class HotelKpiWconnectOvertimeServiceImpl extends ServiceImpl<HotelKpiWconnectOvertimeMapper, HotelKpiWconnectOvertime> implements IHotelKpiWconnectOvertimeService {

        @Resource
        private BeanMapper beanMapper;

        /**
         * 批量保存数据
         *
         * @param overtimeEventBOList 数据集合
         * @author zc
         * @date 2024/7/22
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void save(List<MorOvertimeEventBO> overtimeEventBOList) {
            List<HotelKpiWconnectOvertime> overtimeList = Lists.newArrayList();
            for (MorOvertimeEventBO overtimeEventBO : overtimeEventBOList) {
                // 删除指定时间范围内的旧数据
                remove(new LambdaQueryWrapper<HotelKpiWconnectOvertime>()
                        .eq(HotelKpiWconnectOvertime::getPropertyId, overtimeEventBO.getPropertyId())
                        .between(HotelKpiWconnectOvertime::getTheDate,
                                overtimeEventBO.getStartDate(), overtimeEventBO.getEndDate()));
                // 汇总保存数据
                overtimeList.addAll(beanMapper.mapList(overtimeEventBO.getList(), HotelKpiWconnectOvertime.class,
                        entity -> entity.setPropertyId(overtimeEventBO.getPropertyId()))
                );
            }
            if (CollectionUtils.isEmpty(overtimeList)) {
                return;
            }

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

            // 保存新数据
            baseMapper.insertOrUpdateBatchByUnique(overtimeList);
        }

        /**
         * 获取指定日期范围的数据
         *
         * @param propertyId 资产 id
         * @param startMonth 开始日期（yyyyMM）
         * @param endMonth   结束日期（yyyyMM）
         * @return HotelKpiWconnectOvertime 集合
         * @author zc
         * @date 2025/3/24
         */
        @Override
        public List<HotelKpiWconnectOvertime> listByDateRange(int propertyId, long startMonth, long endMonth) {
            return list(new LambdaQueryWrapper<HotelKpiWconnectOvertime>()
                    .between(HotelKpiWconnectOvertime::getTheDate, startMonth, endMonth)
                    .eq(HotelKpiWconnectOvertime::getPropertyId, propertyId));
        }

    }