# TrendController.java

    package cloud.wconnect.rateshop.controller.rateshop;

    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.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.Trend;
    import cloud.wconnect.rateshop.entity.rateshop.TrendSourceSetting;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.TrendVO;
    import cloud.wconnect.rateshop.pojo.query.rateshop.TrendQuery;
    import cloud.wconnect.rateshop.service.rateshop.ITrendService;
    import cloud.wconnect.rateshop.service.rateshop.ITrendSourceSettingService;
    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 jakarta.validation.Valid;

    import java.math.BigDecimal;
    import java.time.LocalDate;
    import java.util.Collections;
    import java.util.List;
    import java.util.stream.Collectors;

    /**
     * 市场趋势控制层
     *
     * @author lichen
     * @date 2023/6/16
     */
    @RestController
    @RequestMapping("/rate-shop/market-demand")
    public class TrendController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private ITrendService trendService;
        @Autowired
        private ITrendSourceSettingService trendSourceSettingService;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;

        /**
         * 查询市场需求趋势信息
         *
         * @param property   资产标识
         * @param trendQuery 需求趋势查询对象
         * @return java.util.List<cloud.wconnect.rateshop.pojo.vo.rateshop.TrendVO>
         * @author lichen
         * @date 2023/6/16
         */
        @ApiId(343)
        @GetMapping
        public List<TrendVO> list(String property, @Valid TrendQuery trendQuery) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            //把yyyy-MM-dd的日期转化成时间戳
            long fromTimestamp = DateTimeUtils.dateStrToEpochSecond(trendQuery.getFromDate(), BaseConstant.DATE_FORMAT_YYYY_MM_DD, propertyDTO.getTimezone());
            long toTimestamp = DateTimeUtils.dateStrToEpochSecond(trendQuery.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");
            }

            //查询资产配置的市场趋势（目前资产只支持配置一个需求趋势源）
            List<TrendSourceSetting> trendSourceSettings = trendSourceSettingService.listByPropertyId(propertyDTO.getId());
            if (CollectionUtils.isEmpty(trendSourceSettings)) return Collections.emptyList();

            //查询市场需求趋势数据
            Long startDate = Long.valueOf(trendQuery.getFromDate().replace("-", ""));
            Long endDate = Long.valueOf(trendQuery.getToDate().replace("-", ""));
            List<Trend> trendList = trendService.listByLocationIdAndDataRange(trendSourceSettings.get(0).getLocationId(), startDate, endDate);
            if (CollectionUtils.isEmpty(trendList)) return Collections.emptyList();

            //List<Trend> -> List<TrendVO>
            return trendList.stream().map(item -> {
                TrendVO trendVO = new TrendVO();

                //把yyyyMMdd的checkInDate转成LocalDate
                LocalDate checkInDateLocalDate = DateTimeUtils.strToLocalDate(item.getCheckInDate().toString(), BaseConstant.DATE_FORMAT_YYYYMMDD);
                //把localDate转成yyyy-MM-dd格式的字符串
                String date = DateTimeUtils.localDateToStr(checkInDateLocalDate, BaseConstant.DATE_FORMAT_YYYY_MM_DD);
                trendVO.setDate(date);

                //转化成需求百分比
                trendVO.setDemand(item.getRate().multiply(new BigDecimal(100)));

                trendVO.setUpdateTime(item.getQueryDate());
                return trendVO;
            }).collect(Collectors.toList());
        }

    }