# RateShopIwcController.java

    package cloud.wconnect.rateshop.controller.rateshop;

    import cloud.wconnect.commonlib.annotation.ApiId;
    import cloud.wconnect.commonlib.component.BeanMapper;
    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.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.entity.rateshop.PropertyHotel;
    import cloud.wconnect.rateshop.entity.rateshop.Setting;
    import cloud.wconnect.rateshop.entity.rateshop.TrendSourceSetting;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.iwc.IwcDTO;
    import cloud.wconnect.rateshop.pojo.query.rateshop.IwcQuery;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.IwcVO;
    import cloud.wconnect.rateshop.service.rateshop.*;
    import jakarta.validation.Valid;
    import org.apache.commons.collections.CollectionUtils;
    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.util.Collections;
    import java.util.List;

    /**
     * iwc Controller
     *
     * @author lichen
     * @date 2025/4/5
     */
    @RestController
    @RequestMapping("/rate-shop/iwc")
    public class RateShopIwcController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IPropertyHotelService propertyHotelService;
        @Autowired
        private ISettingService settingService;
        @Autowired
        private ITrendSourceSettingService trendSourceSettingService;
        @Autowired
        private IIwcService iwcService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询iwc数据
         *
         * @param property 资产标识
         * @param iwcQuery 查询对象
         * @return java.util.List<cloud.wconnect.rateshop.pojo.vo.rateshop.IwcVO>
         * @author lichen
         * @date 2025/4/5
         */
        @ApiId(1372)
        @GetMapping
        public List<IwcVO> listIwc(String property, @Valid IwcQuery iwcQuery) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            //把yyyy-MM-dd的日期转化成时间戳
            long fromTimestamp = DateTimeUtils.dateStrToEpochSecond(iwcQuery.getFromDate(), BaseConstant.DATE_FORMAT_YYYY_MM_DD, propertyDTO.getTimezone());
            long toTimestamp = DateTimeUtils.dateStrToEpochSecond(iwcQuery.getToDate(), BaseConstant.DATE_FORMAT_YYYY_MM_DD, propertyDTO.getTimezone());
            //开始时间不能大于结束时间
            if (fromTimestamp > toTimestamp) {
                throw new BadRequestException(CommonErrorI18nConstant.START_DATE_CANNOT_GREATER_THAN_END_DATE,
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "The toDate must be greater than or equal to the fromDate");
            }
            //判断时间间隔必须小于70天
            long diffDay = DateTimeUtils.dateDiffOfDays(fromTimestamp, toTimestamp, propertyDTO.getTimezone());
            //此处限制最大区间70天
            if (diffDay > 70) {
                throw new BadRequestException(CommonErrorI18nConstant.SELECT_DATE_LIMIT,
                        i18nMessageUtils.getErrorParamMapNotNeedTranslate(70),
                        BaseErrorCodeEnum.DATA_VALIDATION_ERROR.getCode(), "The interval between fromDate and toDate must be less than or equal to 70 days");
            }

            //查询资产的自身酒店
            PropertyHotel propertySelfHotel = propertyHotelService.getPropertySelfHotel(propertyDTO.getId());
            //查询资产的rate shop配置
            Setting setting = settingService.getByPropertyId(propertyDTO.getId());

            //查询资产rate shop配置的需求趋势地址
            List<TrendSourceSetting> trendSourceSettings = trendSourceSettingService.listByPropertyId(propertyDTO.getId());
            if(CollectionUtils.isEmpty(trendSourceSettings)) return Collections.emptyList();

            //目前资产只支持配置一个
            Integer locationId = trendSourceSettings.get(0).getLocationId();
            List<IwcDTO> iwcDTOList = iwcService.list(propertyDTO, propertySelfHotel.getHotelId(), locationId, setting, iwcQuery);
            return beanMapper.mapList(iwcDTOList, IwcVO.class);
        }

    }