# RateShopWidgetController.java

    package cloud.wconnect.rateshop.controller.rateshop.widget;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.SessionContext;
    import cloud.wconnect.commonlib.cons.BaseConstant;
    import cloud.wconnect.commonlib.cons.BaseErrorCodeEnum;
    import cloud.wconnect.commonlib.cons.CommonErrorI18nConstant;
    import cloud.wconnect.commonlib.exception.BadRequestException;
    import cloud.wconnect.commonlib.exception.ForbiddenException;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.redis.PropertyI18nRedisService;
    import cloud.wconnect.commonlib.util.DateTimeUtils;
    import cloud.wconnect.commonlib.util.I18nMessageUtils;
    import cloud.wconnect.rateshop.pojo.query.rateshop.widget.RateShopAdrWidgetQuery;
    import cloud.wconnect.rateshop.pojo.query.rateshop.widget.RateShopOccWidgetQuery;
    import cloud.wconnect.rateshop.pojo.query.rateshop.widget.RateShopRevParWidgetQuery;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.widget.RateShopWidgetAdrVO;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.widget.RateShopWidgetOccVO;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.widget.RateShopWidgetRevParVO;
    import cloud.wconnect.rateshop.service.rateshop.IRateShopWidgetService;
    import jakarta.annotation.Resource;
    import jakarta.validation.Valid;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    import java.text.MessageFormat;
    import java.time.LocalDate;
    import java.time.temporal.ChronoUnit;
    import java.util.List;

    /**
     * Controller: Widget
     *
     * @author zc
     * @date 2023/03/25
     */
    @RestController
    @RequestMapping("/rate-shop/widget")
    public class RateShopWidgetController {

        @Resource
        private IRateShopWidgetService widgetService;
        @Resource
        private SessionContext sessionContext;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 获取各资产 Occ.% 数据
         *
         * @param query 查询条件
         * @return RateShopWidgetOccVO 集合
         * @author zc
         * @date 2024/3/25
         */
        @ApiId(1117)
        @GetMapping("/occ")
        public List<RateShopWidgetOccVO> list(@Valid RateShopOccWidgetQuery query) {
            // 校验日期范围
            dateParamCheck(query.getStartDate(), query.getEndDate());

            // 校验是否有权限处理资产数据
            List<Integer> propertyIds = query.getPropertyIds();
            if (!sessionContext.getPropertyIds().containsAll(propertyIds)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        "Cannot be operated others property data; propertyIds: " + propertyIds);
            }

            List<PropertyDTO> propertyDTOList = propertyI18nRedisService.list(propertyIds);

            return widgetService.listOcc(propertyDTOList, query);
        }

        /**
         * 获取各资产 adr 数据
         *
         * @param query 查询条件
         * @return RateShopWidgetAdrVO 集合
         * @author zc
         * @date 2024/4/8
         */
        @ApiId(1130)
        @GetMapping("/adr")
        public List<RateShopWidgetAdrVO> list(@Valid RateShopAdrWidgetQuery query) {
            // 校验日期范围
            dateParamCheck(query.getStartDate(), query.getEndDate());

            // 校验是否有权限处理资产数据
            List<Integer> propertyIds = query.getPropertyIds();
            if (!sessionContext.getPropertyIds().containsAll(propertyIds)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        "Cannot be operated others property data; propertyIds: " + propertyIds);
            }

            List<PropertyDTO> propertyDTOList = propertyI18nRedisService.list(propertyIds);

            return widgetService.listAdr(propertyDTOList, query);
        }

        /**
         * 获取各资产 revPAR 数据
         *
         * @param query 查询条件
         * @return RateShopWidgetRevParVO 集合
         * @author zc
         * @date 2024/4/8
         */
        @ApiId(1131)
        @GetMapping("/revPar")
        public List<RateShopWidgetRevParVO> list(@Valid RateShopRevParWidgetQuery query) {
            // 校验日期范围
            dateParamCheck(query.getStartDate(), query.getEndDate());

            // 校验是否有权限处理资产数据
            List<Integer> propertyIds = query.getPropertyIds();
            if (!sessionContext.getPropertyIds().containsAll(propertyIds)) {
                throw new ForbiddenException(BaseErrorCodeEnum.DATA_FORBIDDEN.getI18nCode(),
                        BaseErrorCodeEnum.DATA_FORBIDDEN.getCode(),
                        "Cannot be operated others property data; propertyIds: " + propertyIds);
            }

            List<PropertyDTO> propertyDTOList = propertyI18nRedisService.list(propertyIds);

            return widgetService.listRevPar(propertyDTOList, query);
        }

        /**
         * 校验日期查询条件
         *
         * @param fromDate 开始时间
         * @param toDate   结束时间
         * @author zc
         * @date 2024/3/25
         */
        private void dateParamCheck(long fromDate, long toDate) {
            if (fromDate > toDate) {
                throw new BadRequestException(CommonErrorI18nConstant.START_DATE_CANNOT_GREATER_THAN_END_DATE,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The fromDate cannot be greater than toDate; fromDate:{0}, toDate:{1}",
                                fromDate, toDate));
            }
            LocalDate fromLocalDate = DateTimeUtils.strToLocalDate(Long.valueOf(fromDate).toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
            LocalDate toLocalDate = DateTimeUtils.strToLocalDate(Long.valueOf(toDate).toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
            if (ChronoUnit.DAYS.between(fromLocalDate, toLocalDate) > 90) {
                throw new BadRequestException(CommonErrorI18nConstant.SELECT_DATE_LIMIT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate(90),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(),
                        MessageFormat.format("The query date range cannot exceed 90 days; fromDate:{0}, toDate:{1}",
                                fromDate, toDate));
            }
        }
    }