Просмотр исходного кода

fix: 添加日期校验、修复分页total错误

lwh 3 недель назад
Родитель
Сommit
a1b7f306c4

+ 62 - 0
pig-statistics/pig-statistics-api/src/main/java/com/pig4cloud/pig/statistics/api/dto/user/PageUserAnalyseDTO.java

@@ -2,6 +2,7 @@ package com.pig4cloud.pig.statistics.api.dto.user;
 
 
 import io.swagger.v3.oas.annotations.media.Schema;
+import jakarta.validation.constraints.AssertTrue;
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotNull;
 import lombok.Data;
@@ -9,6 +10,7 @@ import lombok.Data;
 import java.io.Serial;
 import java.io.Serializable;
 import java.time.LocalDate;
+import java.time.temporal.ChronoUnit;
 import java.util.List;
 
 /**
@@ -74,4 +76,64 @@ public class PageUserAnalyseDTO implements Serializable {
 	 */
 	@Schema(description = "当前页",example = "1")
 	private long current = 1;
+
+	/**
+	 * 校验规则1:fromDate不能大于toDate,只能小于等于
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "开始时间不能晚于结束时间")
+	public boolean isFromDateLessThanOrEqualToToDate() {
+		if (fromDate == null || toDate == null) {
+			return true;
+		}
+		return !fromDate.isAfter(toDate);
+	}
+
+	/**
+	 * 校验规则2:如果timeUnit为hour,则fromDate~toDate的天数不能大于一周
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "小时维度查询时间跨度不能超过7天")
+	public boolean isHourTimeUnitWithinOneWeek() {
+		if (fromDate == null || toDate == null || timeUnit == null) {
+			return true;
+		}
+		if ("hour".equals(timeUnit)) {
+			long days = ChronoUnit.DAYS.between(fromDate, toDate);
+			return days < 7;
+		}
+		return true;
+	}
+
+	/**
+	 * 校验规则3:如果timeUnit为week,则跨度不能小于一周
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "周维度查询时间跨度不能小于7天")
+	public boolean isWeekTimeUnitAtLeastOneWeek() {
+		if (fromDate == null || toDate == null || timeUnit == null) {
+			return true;
+		}
+		if ("week".equals(timeUnit)) {
+			long days = ChronoUnit.DAYS.between(fromDate, toDate);
+			return days >= 7;
+		}
+		return true;
+	}
+
+	/**
+	 * 校验规则4:月维度查询时间跨度不能小于30天
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "月维度查询时间跨度不能小于30天")
+	public boolean isMonthTimeUnitAtLeastMonth() {
+		if (fromDate == null || toDate == null || timeUnit == null) {
+			return true;
+		}
+		if ("month".equals(timeUnit)) {
+			long days = ChronoUnit.DAYS.between(fromDate, toDate);
+			return days >= 30;
+		}
+		return true;
+	}
 }

+ 62 - 0
pig-statistics/pig-statistics-api/src/main/java/com/pig4cloud/pig/statistics/api/dto/user/UserAnalyseQueryBaseDTO.java

@@ -2,6 +2,7 @@ package com.pig4cloud.pig.statistics.api.dto.user;
 
 
 import io.swagger.v3.oas.annotations.media.Schema;
+import jakarta.validation.constraints.AssertTrue;
 import jakarta.validation.constraints.NotBlank;
 import jakarta.validation.constraints.NotNull;
 import lombok.Data;
@@ -9,6 +10,7 @@ import lombok.Data;
 import java.io.Serial;
 import java.io.Serializable;
 import java.time.LocalDate;
+import java.time.temporal.ChronoUnit;
 import java.util.List;
 
 /**
@@ -61,4 +63,64 @@ public class UserAnalyseQueryBaseDTO implements Serializable {
 	 */
 	@Schema(description = "版本", example = "[\"1.0.0\"]")
 	private List<String> version;
+
+	/**
+	 * 校验规则1:fromDate不能大于toDate,只能小于等于
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "开始时间不能晚于结束时间")
+	public boolean isFromDateLessThanOrEqualToToDate() {
+		if (fromDate == null || toDate == null) {
+			return true;
+		}
+		return !fromDate.isAfter(toDate);
+	}
+
+	/**
+	 * 校验规则2:如果timeUnit为hour,则fromDate~toDate的天数不能大于一周
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "小时维度查询时间跨度不能超过7天")
+	public boolean isHourTimeUnitWithinOneWeek() {
+		if (fromDate == null || toDate == null || timeUnit == null) {
+			return true;
+		}
+		if ("hour".equals(timeUnit)) {
+			long days = ChronoUnit.DAYS.between(fromDate, toDate);
+			return days < 7;
+		}
+		return true;
+	}
+
+	/**
+	 * 校验规则3:如果timeUnit为week,则跨度不能小于一周
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "周维度查询时间跨度不能小于7天")
+	public boolean isWeekTimeUnitAtLeastOneWeek() {
+		if (fromDate == null || toDate == null || timeUnit == null) {
+			return true;
+		}
+		if ("week".equals(timeUnit)) {
+			long days = ChronoUnit.DAYS.between(fromDate, toDate);
+			return days >= 7;
+		}
+		return true;
+	}
+
+	/**
+	 * 校验规则4:月维度查询时间跨度不能小于30天
+	 */
+	@Schema(hidden = true)
+	@AssertTrue(message = "月维度查询时间跨度不能小于30天")
+	public boolean isMonthTimeUnitAtLeastMonth() {
+		if (fromDate == null || toDate == null || timeUnit == null) {
+			return true;
+		}
+		if ("month".equals(timeUnit)) {
+			long days = ChronoUnit.DAYS.between(fromDate, toDate);
+			return days >= 30;
+		}
+		return true;
+	}
 }

+ 4 - 2
pig-statistics/pig-statistics-biz/src/main/java/com/pig4cloud/pig/statistics/service/impl/UserAnalyseServiceImpl.java

@@ -1969,8 +1969,10 @@ public class UserAnalyseServiceImpl implements UserAnalyseService {
 				return ChronoUnit.WEEKS.between(firstSunday, lastSunday) + 1;
 
 			case "month":
-				// 计算月数
-				return ChronoUnit.MONTHS.between(fromDate, toDate) + 1;
+				// 计算月数:基于月份第一天计算,与generatePageTimeAxis逻辑保持一致
+				LocalDate firstMonDay = fromDate.with(TemporalAdjusters.firstDayOfMonth());
+				LocalDate lastMonDay = toDate.with(TemporalAdjusters.firstDayOfMonth());
+				return ChronoUnit.MONTHS.between(firstMonDay, lastMonDay) + 1;
 
 			default:
 				throw new IllegalArgumentException("不支持的时间单位: " + timeUnit);