mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
fix: 修复月度目标筛选及重置问题
- DateTimeConfig: 新增LocalDate反序列化器,支持yyyy-MM等多种格式 - monthly-target: 修复搜索表单重置后筛选条件不重置的问题
This commit is contained in:
+69
-2
@@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
@@ -53,7 +54,9 @@ public class DateTimeConfig {
|
||||
// 点格式:yyyy.MM.dd HH:mm:ss
|
||||
DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"),
|
||||
// 点格式:yyyy.MM.dd
|
||||
DateTimeFormatter.ofPattern("yyyy.MM.dd")
|
||||
DateTimeFormatter.ofPattern("yyyy.MM.dd"),
|
||||
// 年月格式:yyyy-MM
|
||||
DateTimeFormatter.ofPattern("yyyy-MM")
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -74,6 +77,64 @@ public class DateTimeConfig {
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ")
|
||||
);
|
||||
|
||||
/**
|
||||
* 自定义LocalDate反序列化器
|
||||
*/
|
||||
public static class FlexibleLocalDateDeserializer extends JsonDeserializer<LocalDate> {
|
||||
@Override
|
||||
public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||
String dateString = p.getText().trim();
|
||||
|
||||
if (dateString == null || dateString.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 年月格式特殊处理(yyyy-MM 长度为7),返回该月第一天
|
||||
if (dateString.length() == 7) {
|
||||
int year = Integer.parseInt(dateString.substring(0, 4));
|
||||
int month = Integer.parseInt(dateString.substring(5, 7));
|
||||
return LocalDate.of(year, month, 1);
|
||||
}
|
||||
|
||||
for (DateTimeFormatter formatter : DATE_TIME_FORMATTERS) {
|
||||
try {
|
||||
return LocalDate.parse(dateString, formatter);
|
||||
} catch (DateTimeParseException e) {
|
||||
// 继续尝试下一个格式
|
||||
}
|
||||
}
|
||||
|
||||
// 如果所有格式都失败,尝试从 ISO ZonedDateTime 解析再转 LocalDate
|
||||
for (DateTimeFormatter formatter : ZONED_DATE_TIME_FORMATTERS) {
|
||||
try {
|
||||
ZonedDateTime zdt = ZonedDateTime.parse(dateString, formatter);
|
||||
return zdt.toLocalDate();
|
||||
} catch (DateTimeParseException e) {
|
||||
// 继续尝试下一个格式
|
||||
}
|
||||
}
|
||||
|
||||
throw new IOException("无法解析日期格式: " + dateString);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义LocalDate序列化器
|
||||
* 统一输出格式为 yyyy-MM-dd
|
||||
*/
|
||||
public static class StandardLocalDateSerializer extends JsonSerializer<LocalDate> {
|
||||
private static final DateTimeFormatter OUTPUT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
|
||||
@Override
|
||||
public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
|
||||
if (value == null) {
|
||||
gen.writeNull();
|
||||
} else {
|
||||
gen.writeString(value.format(OUTPUT_FORMATTER));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义LocalDateTime反序列化器
|
||||
*/
|
||||
@@ -148,7 +209,13 @@ public class DateTimeConfig {
|
||||
|
||||
// 注册自定义模块
|
||||
mapper.registerModule(dateTimeModule);
|
||||
|
||||
|
||||
// 单独为 LocalDate 注册反序列化器和序列化器(覆盖 JavaTimeModule 的默认实现)
|
||||
SimpleModule localDateModule = new SimpleModule();
|
||||
localDateModule.addDeserializer(LocalDate.class, new FlexibleLocalDateDeserializer());
|
||||
localDateModule.addSerializer(LocalDate.class, new StandardLocalDateSerializer());
|
||||
mapper.registerModule(localDateModule);
|
||||
|
||||
return mapper;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user