package com.zc.business.request; import lombok.Data; import org.springframework.format.annotation.DateTimeFormat; import javax.validation.constraints.NotNull; import java.util.Date; @Data public class DcMessageAccessCountRequest { // 年、月、日、小时,用于定义时间周期类型 public static final String YEAR = "year"; public static final String MONTH = "month"; public static final String DAY = "day"; public static final String HOUR = "hour"; @NotNull(message = "开始时间不能为空") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private String startTime; // 查询结束时间 @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @NotNull(message = "结束时间不能为空") private String endTime; // 时间周期类型(年、月、日、小时) private String periodType; private String productId; /** * 根据给定的周期类型字符串,将其转换为简写的字符串表示。 * @return 对应的简写字符串,比如"1y"、"1m"等。 * @throws IllegalArgumentException 如果传入的periodType不是预期的值或为null。 */ public String convertPeriodType() throws IllegalArgumentException { // 检查输入参数是否为null或空字符串 if (periodType == null || periodType.isEmpty()) { throw new IllegalArgumentException("periodType不能为null或空"); } // 使用switch语句处理不同的周期类型 switch (periodType) { case YEAR: return "1y"; case MONTH: return "1M"; case DAY: return "1d"; case HOUR: return "1h"; default: // 对于未预期的输入值,抛出异常 throw new IllegalArgumentException("不受支持的 periodType: " + periodType); } } }