# OperationController.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.PropertyOperation;
    import cloud.wconnect.rateshop.pojo.query.rateshop.OperationQuery;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.OperationVO;
    import cloud.wconnect.rateshop.service.rateshop.IPropertyOperationService;
    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;

    /**
     * 资产 operation 数据的控制层
     *
     * @author lichen
     * @date 2023/6/16
     */
    @RestController
    @RequestMapping("/rate-shop/operation")
    public class OperationController {

        @Autowired
        private SessionContext sessionContext;
        @Autowired
        private IPropertyOperationService projectOperationService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private I18nMessageUtils i18nMessageUtils;
        @Autowired
        private PropertyI18nRedisService propertyI18nRedisService;

        /**
         * 查询运营数据
         *
         * @param property       资产标识
         * @param operationQuery operation数据查询对象
         * @return OperationVO
         * @author zc
         * @date 2024/3/23
         */
        @ApiId(342)
        @GetMapping
        public List<OperationVO> list(String property, @Valid OperationQuery operationQuery) {
            int propertyId = sessionContext.getPropertyId(property);
            PropertyDTO propertyDTO = propertyI18nRedisService.get(propertyId);
            // 把yyyy-MM-dd的日期转化成时间戳
            long fromTimestamp = DateTimeUtils.dateStrToEpochSecond(operationQuery.getFromDate(),
                    BaseConstant.DATE_FORMAT_YYYY_MM_DD, propertyDTO.getTimezone());
            long toTimestamp = DateTimeUtils.dateStrToEpochSecond(operationQuery.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.numberOfDaysApart(fromTimestamp, toTimestamp, propertyDTO.getTimezone());
            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<PropertyOperation> propertyOperationList = projectOperationService.listByDateRange(propertyDTO.getId(), fromTimestamp, toTimestamp);
            if (CollectionUtils.isEmpty(propertyOperationList)) return Collections.emptyList();

            // List<PropertyOperation> -> List<OperationVO>
            return beanMapper.mapList(propertyOperationList, OperationVO.class,
                    (item, vo) -> vo.setDate(DateTimeUtils.timestampToStr(item.getTargetDate(),
                            BaseConstant.DATE_FORMAT_YYYY_MM_DD, propertyDTO.getTimezone())));
        }

    }