# HotelKpiCommonSummaryServiceImpl.java

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

    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.workperformance.cons.hotelkpi.HotelKpiDateTypeCons;
    import cloud.wconnect.workperformance.cons.hotelkpi.HotelKpiMetricsEnum;
    import cloud.wconnect.workperformance.service.hotelkpi.IHotelKpiCommonSummaryService;
    import cloud.wconnect.workperformance.service.hotelkpi.impl.metrics.IHotelKpiMetricsService;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import jakarta.annotation.Resource;
    import org.springframework.context.ApplicationContext;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;

    import java.time.LocalDate;
    import java.util.Collection;
    import java.util.List;

    /**
     * @author zc
     * @date 2025/08/20
     */
    @Service
    @DS("work_performance")
    public class HotelKpiCommonSummaryServiceImpl implements IHotelKpiCommonSummaryService {

        @Resource
        private ApplicationContext applicationContext;

        /**
         * 初始化指标数据
         *
         * @param propertyDTO  资产
         * @param nowLocalDate 当前日期
         * @param metricsType  指标类型
         * @author zc
         * @date 2025/9/12
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void initMetricsData(PropertyDTO propertyDTO, LocalDate nowLocalDate, int metricsType) {
            // 初始化当前指标
            initData(propertyDTO, nowLocalDate, metricsType);

            // 初始化关联的指标
            for (int dateType : HotelKpiDateTypeCons.DATE_TYPES) {
                List<Integer> relationalMetricsTypes = HotelKpiMetricsEnum.getRelationalMetricsTypes(metricsType, dateType);
                for (Integer type : relationalMetricsTypes) {
                    initData(propertyDTO, nowLocalDate, type);
                }
            }
        }

        /**
         * 初始化数据
         *
         * @param propertyDTO  资产
         * @param nowLocalDate 当前日期
         * @param metricsType  指标类型
         * @author zc
         * @date 2025/9/12
         */
        private void initData(PropertyDTO propertyDTO, LocalDate nowLocalDate, int metricsType) {
            Collection<IHotelKpiMetricsService> parserList = applicationContext.getBeansOfType(IHotelKpiMetricsService.class).values();
            for (IHotelKpiMetricsService parser : parserList) {
                if (parser.supports(metricsType)) {
                    parser.initData(propertyDTO, nowLocalDate);
                    return;
                }
            }
        }

        /**
         * 触发设置指标数据
         *
         * @param propertyDTO 资产
         * @param triggerType 触发类型，不同类型的日期格式不同
         * @param startDate   开始日期(yyyyMMdd,yyyyMM,yyyy)
         * @param endDate     结束日期(yyyyMMdd,yyyyMM,yyyy)
         * @param metricsType 指标类型
         * @author zc
         * @date 2025/9/12
         */
        @Override
        @Transactional(rollbackFor = Exception.class)
        public void triggerSetMetricsData(PropertyDTO propertyDTO, int triggerType, long startDate, long endDate, int metricsType) {
            // 触发当前指标
            triggerSetData(propertyDTO, triggerType, startDate, endDate, metricsType);

            // 触发关联的指标
            for (int dateType : HotelKpiDateTypeCons.DATE_TYPES) {
                List<Integer> relationalMetricsTypes = HotelKpiMetricsEnum.getRelationalMetricsTypes(metricsType, dateType);
                for (Integer type : relationalMetricsTypes) {
                    triggerSetData(propertyDTO, triggerType, startDate, endDate, type);
                }
            }
        }

        /**
         * 触发设置数据
         *
         * @param propertyDTO 资产
         * @param triggerType 触发类型，不同类型的日期格式不同
         * @param startDate   开始日期(yyyyMMdd,yyyyMM,yyyy)
         * @param endDate     结束日期(yyyyMMdd,yyyyMM,yyyy)
         * @param metricsType 指标类型
         * @author zc
         * @date 2025/9/12
         */
        private void triggerSetData(PropertyDTO propertyDTO, int triggerType, long startDate, long endDate, int metricsType) {
            Collection<IHotelKpiMetricsService> parserList = applicationContext.getBeansOfType(IHotelKpiMetricsService.class).values();
            for (IHotelKpiMetricsService parser : parserList) {
                if (parser.supports(metricsType)) {
                    parser.triggerSetData(propertyDTO, triggerType, startDate, endDate);
                    return;
                }
            }
        }

        /**
         * 设置当前指标和关联的其他指标的数值
         *
         * @param propertyDTO 资产
         * @param dateType    日期类型
         * @param startDate   开始日期
         * @param endDate     结束日期
         * @param metricsType 指标类型
         * @author zc
         * @date 2025/9/12
         */
        @Transactional(rollbackFor = Exception.class)
        public void setMetricsData(PropertyDTO propertyDTO, int dateType, LocalDate startDate, LocalDate endDate, int metricsType) {
            // 设置当前指标
            setData(propertyDTO, dateType, startDate, endDate, metricsType);

            // 设置关联的指标
            List<Integer> relationalMetricsTypes = HotelKpiMetricsEnum.getRelationalMetricsTypes(metricsType, dateType);
            for (Integer type : relationalMetricsTypes) {
                setData(propertyDTO, dateType, startDate, endDate, type);
            }
        }

        /**
         * 设置指标数据
         *
         * @param propertyDTO 资产
         * @param dateType    日期类型
         * @param startDate   开始日期
         * @param endDate     结束日期
         * @param metricsType 指标类型
         * @author zc
         * @date 2025/9/12
         */
        private void setData(PropertyDTO propertyDTO, int dateType, LocalDate startDate, LocalDate endDate, int metricsType) {
            Collection<IHotelKpiMetricsService> parserList = applicationContext.getBeansOfType(IHotelKpiMetricsService.class).values();
            for (IHotelKpiMetricsService parser : parserList) {
                if (parser.supports(metricsType)) {
                    parser.setData(propertyDTO, dateType, startDate, endDate);
                    return;
                }
            }
        }
    }