# HotelKpiController.java

    package cloud.wconnect.workperformance.controller.hotelkpi.old;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.pojo.dto.ListPagedDTO;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.pojo.dto.jsonview.Views;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.YearMonthWeekUtils;
    import cloud.wconnect.workperformance.cons.hotelkpi.old.HotelKpiDateTypeCons;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiCommentDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiMetricsItemDTO;
    import cloud.wconnect.workperformance.pojo.dto.hotelkpi.old.kpi.HotelKpiWarningCommentDTO;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.old.HotelKpiCommentQuery;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.old.HotelKpiMetricsItemQuery;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.old.HotelKpiQuery;
    import cloud.wconnect.workperformance.pojo.query.hotelkpi.old.HotelKpiWarningCommentQuery;
    import cloud.wconnect.workperformance.service.hotelkpi.old.*;
    import com.fasterxml.jackson.annotation.JsonView;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.springframework.web.bind.annotation.*;

    import java.util.List;
    import java.util.Objects;

    /**
     * <p>
     * Hotel KPI 前端控制器
     * </p>
     *
     * @author zc
     * @since 2025-03-29
     */
    @RestController("hotelKpiController_old")
    @RequestMapping("/hotel-kpi/history")
    public class HotelKpiController {

        @Resource
        private IHotelKpiMetricsConfigService metricsConfigService;
        @Resource
        private IHotelKpiWeeklyService weeklyService;
        @Resource
        private IHotelKpiMonthlyService monthlyService;
        @Resource
        private IHotelKpiQuarterlyService quarterlyService;
        @Resource
        private IHotelKpiYearlyService yearlyService;
        @Resource
        private IHotelKpiCommentService commentService;
        @Resource
        private IHotelKpiWarningCommentService warningCommentService;
        @Resource
        private PropertyI18nRedisService propertyI18nRedisService;
        @Resource
        private SessionContext sessionContext;

        /**
         * 获取 Hotel KPI 数据
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return Hotel KPI 数据
         * @author zc
         * @date 2025/3/29
         */
        @ApiId(1352)
        @GetMapping
        public List<HotelKpiDTO> list(@RequestParam String property, @Valid HotelKpiQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);

            if (query.getDateType() == HotelKpiDateTypeCons.WEEKLY) {
                return listForWeekly(propertyDTO, query);
            } else if (query.getDateType() == HotelKpiDateTypeCons.MONTHLY) {
                return listForMonthly(propertyDTO, query);
            } else if (query.getDateType() == HotelKpiDateTypeCons.QUARTERLY) {
                return listForQuarterly(propertyDTO, query);
            } else {
                return listForYearly(propertyDTO, query);
            }
        }

        /**
         * 获取 Weekly Hotel KPI 数据
         *
         * @param propertyDTO 资产
         * @param query       查询条件
         * @return HotelKpiDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        private List<HotelKpiDTO> listForWeekly(PropertyDTO propertyDTO, HotelKpiQuery query) {
            // 查询日期格式转换
            Long startDateLong = YearMonthWeekUtils.convertWeekToYYYYMMDD(query.getStartDate());
            Long endDateLong = YearMonthWeekUtils.convertWeekToYYYYMMDD(query.getEndDate());
            if (Objects.isNull(startDateLong) || Objects.isNull(endDateLong)) {
                throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The query date is wrong format");
            }
            query.setStartDateLong(startDateLong);
            query.setEndDateLong(endDateLong);

            return weeklyService.listHotelKpi(propertyDTO, query);
        }

        /**
         * 获取 Monthly Hotel KPI 数据
         *
         * @param propertyDTO 资产
         * @param query       查询条件
         * @return HotelKpiDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        private List<HotelKpiDTO> listForMonthly(PropertyDTO propertyDTO, HotelKpiQuery query) {
            // 查询日期格式转换
            Long startDateLong = YearMonthWeekUtils.convertMonthToYYYYMM(query.getStartDate());
            Long endDateLong = YearMonthWeekUtils.convertMonthToYYYYMM(query.getEndDate());
            if (Objects.isNull(startDateLong) || Objects.isNull(endDateLong)) {
                throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The query date is wrong format");
            }
            query.setStartDateLong(startDateLong);
            query.setEndDateLong(endDateLong);

            return monthlyService.listHotelKpi(propertyDTO, query);
        }

        /**
         * 获取 Quarterly Hotel KPI 数据
         *
         * @param propertyDTO 资产
         * @param query       查询条件
         * @return HotelKpiDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        private List<HotelKpiDTO> listForQuarterly(PropertyDTO propertyDTO, HotelKpiQuery query) {
            // 查询日期格式转换
            Long startDateLong = YearMonthWeekUtils.convertQuarterToYYYYMM(query.getStartDate());
            Long endDateLong = YearMonthWeekUtils.convertQuarterToYYYYMM(query.getEndDate());
            if (Objects.isNull(startDateLong) || Objects.isNull(endDateLong)) {
                throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The query date is wrong format");
            }
            query.setStartDateLong(startDateLong);
            query.setEndDateLong(endDateLong);

            return quarterlyService.listHotelKpi(propertyDTO, query);
        }

        /**
         * 获取 Yearly Hotel KPI 数据
         *
         * @param propertyDTO 资产
         * @param query       查询条件
         * @return HotelKpiDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        private List<HotelKpiDTO> listForYearly(PropertyDTO propertyDTO, HotelKpiQuery query) {
            // 查询日期格式转换
            Long startDateLong = YearMonthWeekUtils.convertYearToYYYY(query.getStartDate());
            Long endDateLong = YearMonthWeekUtils.convertYearToYYYY(query.getEndDate());
            if (Objects.isNull(startDateLong) || Objects.isNull(endDateLong)) {
                throw new BadRequestException(BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getI18nCode(),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        "The query date is wrong format");
            }
            query.setStartDateLong(startDateLong);
            query.setEndDateLong(endDateLong);

            return yearlyService.listHotelKpi(propertyDTO, query);
        }

        /**
         * 获取酒店KPI评论
         *
         * @param property 资产标识
         * @param date     日期(yyyyMMdd)
         * @param query    查询条件
         * @return HotelKpiCommentDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        @ApiId(1353)
        @GetMapping("/{date:\\d+}/comment")
        public ListPagedDTO<HotelKpiCommentDTO> listComment(@RequestParam String property, @PathVariable long date,
                                                            @Valid HotelKpiCommentQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            return commentService.list(propertyDTO, date, query);
        }

        /**
         * 获取指标项
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return HotelKpiMetricsItemDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        @ApiId(1368)
        @GetMapping("/metrics")
        public List<HotelKpiMetricsItemDTO> listMetricsItem(@RequestParam String property, @Valid HotelKpiMetricsItemQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            return metricsConfigService.listMetricsItem(propertyId, query);
        }

        /**
         * 获取酒店 KPI 警示评论
         *
         * @param property 资产标识
         * @param query    查询条件
         * @return HotelKpiWarningCommentDTO 集合
         * @author zc
         * @date 2025/3/29
         */
        @ApiId(1354)
        @GetMapping("/warning-comment")
        @JsonView({Views.ListView.class})
        public ListPagedDTO<HotelKpiWarningCommentDTO> listWarningComment(@RequestParam String property, @Valid HotelKpiWarningCommentQuery query) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            return warningCommentService.list(propertyDTO, query);
        }

    }