# HotelKpiWconnectMonthlyReportServiceImpl.java

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

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.workperformance.entity.hotelkpi.HotelKpiWconnectMonthlyReport;
    import cloud.wconnect.workperformance.mapper.hotelkpi.HotelKpiWconnectMonthlyReportMapper;
    import cloud.wconnect.workperformance.pojo.bo.modelevent.monthlyreport.MonthlyReportEventDataBO;
    import cloud.wconnect.workperformance.pojo.bo.modelevent.monthlyreport.MonthlyReportEventValueBO;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiWconnectMonthlyReportService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

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

    /**
     * <p>
     * Hotel KPI - wconnect Monthly Report 服务实现类
     * </p>
     *
     * @author zc
     * @since 2025-09-08
     */
    @Service
    @DS("work_performance")
    public class HotelKpiWconnectMonthlyReportServiceImpl extends ServiceImpl<HotelKpiWconnectMonthlyReportMapper, HotelKpiWconnectMonthlyReport> implements IHotelKpiWconnectMonthlyReportService {

        /**
         * 构建实体列表
         *
         * @param propertyDTO 资产
         * @param type        数据类型
         * @param boList      数据
         * @return 实体列表
         * @author zc
         * @date 2026/2/14
         */
        @Override
        public List<HotelKpiWconnectMonthlyReport> buildEntityList(PropertyDTO propertyDTO, int type,
                                                                   List<MonthlyReportEventDataBO> boList) {
            List<HotelKpiWconnectMonthlyReport> list = new ArrayList<>();
            for (MonthlyReportEventDataBO bo : boList) {
                for (MonthlyReportEventValueBO valueBO : bo.getData()) {
                    HotelKpiWconnectMonthlyReport entity = new HotelKpiWconnectMonthlyReport();
                    entity.setPropertyId(propertyDTO.getId());
                    entity.setMonth(bo.getYearMonth());
                    entity.setType(type);
                    entity.setName(valueBO.getCategoryName());
                    entity.setActual(valueBO.getActual());
                    entity.setBudget(valueBO.getTarget());
                    entity.setLastYear(valueBO.getLy());
                    list.add(entity);
                }
            }
            return list;
        }

        /**
         * 在锁内执行事务
         *
         * @param monthlyReportList 数据
         * @author zc
         * @date 2026/2/14
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void saveData(List<HotelKpiWconnectMonthlyReport> monthlyReportList) {
            monthlyReportList.sort(Comparator.comparing(HotelKpiWconnectMonthlyReport::getPropertyId)
                    .thenComparing(HotelKpiWconnectMonthlyReport::getMonth)
                    .thenComparing(HotelKpiWconnectMonthlyReport::getType)
                    .thenComparing(HotelKpiWconnectMonthlyReport::getName));

            baseMapper.insertOrUpdateBatchByUnique(monthlyReportList);
        }

        /**
         * 根据时间范围获取数据
         *
         * @param propertyId 资产ID
         * @param type       类型
         * @param startDate  开始时间(yyyyMM)
         * @param endDate    结束时间(yyyyMM)
         * @return 数据
         * @author zc
         * @date 2025/9/8
         */
        @Override
        public List<HotelKpiWconnectMonthlyReport> listByDateRange(int propertyId, int type, long startDate, long endDate) {
            return list(new LambdaQueryWrapper<HotelKpiWconnectMonthlyReport>()
                    .eq(HotelKpiWconnectMonthlyReport::getPropertyId, propertyId)
                    .eq(HotelKpiWconnectMonthlyReport::getType, type)
                    .between(HotelKpiWconnectMonthlyReport::getMonth, startDate, endDate));
        }
    }