mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 07:09:20 +08:00
fix: 修复月度目标筛选及重置问题
- DateTimeConfig: 新增LocalDate反序列化器,支持yyyy-MM等多种格式 - monthly-target: 修复搜索表单重置后筛选条件不重置的问题
This commit is contained in:
+68
-1
@@ -14,6 +14,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.context.annotation.Primary;
|
import org.springframework.context.annotation.Primary;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@@ -53,7 +54,9 @@ public class DateTimeConfig {
|
|||||||
// 点格式:yyyy.MM.dd HH:mm:ss
|
// 点格式:yyyy.MM.dd HH:mm:ss
|
||||||
DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"),
|
DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss"),
|
||||||
// 点格式:yyyy.MM.dd
|
// 点格式: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")
|
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反序列化器
|
* 自定义LocalDateTime反序列化器
|
||||||
*/
|
*/
|
||||||
@@ -149,6 +210,12 @@ public class DateTimeConfig {
|
|||||||
// 注册自定义模块
|
// 注册自定义模块
|
||||||
mapper.registerModule(dateTimeModule);
|
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;
|
return mapper;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type {
|
|||||||
import type { ShopVO } from '#/api/ecommerce/shop/types';
|
import type { ShopVO } from '#/api/ecommerce/shop/types';
|
||||||
import type { PageQueryDTO } from '#/types/common';
|
import type { PageQueryDTO } from '#/types/common';
|
||||||
|
|
||||||
import { computed, onMounted, ref } from 'vue';
|
import { computed, ref } from 'vue';
|
||||||
|
|
||||||
import { Page } from '@vben/common-ui';
|
import { Page } from '@vben/common-ui';
|
||||||
import { createIconifyIcon } from '@vben/icons';
|
import { createIconifyIcon } from '@vben/icons';
|
||||||
@@ -40,8 +40,10 @@ const searchForm = ref({
|
|||||||
endTargetMonth: undefined as string | undefined,
|
endTargetMonth: undefined as string | undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
// 店铺选项
|
// 店铺选项(懒加载)
|
||||||
const shopOptions = ref<{ label: string; value: number }[]>([]);
|
const shopOptions = ref<{ label: string; value: number }[]>([]);
|
||||||
|
const shopLoading = ref(false);
|
||||||
|
const shopFetched = ref(false);
|
||||||
|
|
||||||
// VbenVxeGrid 表格配置
|
// VbenVxeGrid 表格配置
|
||||||
const gridOptions: VxeGridProps<any> = {
|
const gridOptions: VxeGridProps<any> = {
|
||||||
@@ -171,7 +173,14 @@ const formOptions = computed(() => ({
|
|||||||
placeholder: '请选择店铺',
|
placeholder: '请选择店铺',
|
||||||
options: shopOptions,
|
options: shopOptions,
|
||||||
allowClear: true,
|
allowClear: true,
|
||||||
onChange: () => gridReloadRef.value?.(),
|
showSearch: true,
|
||||||
|
filterOption: false,
|
||||||
|
loading: shopLoading,
|
||||||
|
onFocus: fetchShopOptions,
|
||||||
|
onChange: (val: any) => {
|
||||||
|
searchForm.value.shopId = val;
|
||||||
|
gridReloadRef.value?.();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fieldName: 'shopId',
|
fieldName: 'shopId',
|
||||||
label: '店铺',
|
label: '店铺',
|
||||||
@@ -182,7 +191,10 @@ const formOptions = computed(() => ({
|
|||||||
placeholder: '开始月份',
|
placeholder: '开始月份',
|
||||||
picker: 'month',
|
picker: 'month',
|
||||||
style: { width: '100%' },
|
style: { width: '100%' },
|
||||||
onChange: () => gridReloadRef.value?.(),
|
onChange: (val: any) => {
|
||||||
|
searchForm.value.startTargetMonth = val ? val.format('YYYY-MM') : undefined;
|
||||||
|
gridReloadRef.value?.();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fieldName: 'startTargetMonth',
|
fieldName: 'startTargetMonth',
|
||||||
label: '目标月份',
|
label: '目标月份',
|
||||||
@@ -193,7 +205,10 @@ const formOptions = computed(() => ({
|
|||||||
placeholder: '结束月份',
|
placeholder: '结束月份',
|
||||||
picker: 'month',
|
picker: 'month',
|
||||||
style: { width: '100%' },
|
style: { width: '100%' },
|
||||||
onChange: () => gridReloadRef.value?.(),
|
onChange: (val: any) => {
|
||||||
|
searchForm.value.endTargetMonth = val ? val.format('YYYY-MM') : undefined;
|
||||||
|
gridReloadRef.value?.();
|
||||||
|
},
|
||||||
},
|
},
|
||||||
fieldName: 'endTargetMonth',
|
fieldName: 'endTargetMonth',
|
||||||
label: '至',
|
label: '至',
|
||||||
@@ -211,12 +226,23 @@ function refreshTable() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 重置搜索表单
|
||||||
|
function handleReset() {
|
||||||
|
searchForm.value = {
|
||||||
|
shopId: undefined,
|
||||||
|
startTargetMonth: undefined,
|
||||||
|
endTargetMonth: undefined,
|
||||||
|
};
|
||||||
|
gridReloadRef.value?.();
|
||||||
|
}
|
||||||
|
|
||||||
// 创建 VbenVxeGrid 实例
|
// 创建 VbenVxeGrid 实例
|
||||||
const [Grid, gridApi] = useVbenVxeGrid({
|
const [Grid, gridApi] = useVbenVxeGrid({
|
||||||
gridOptions,
|
gridOptions,
|
||||||
formOptions: formOptions.value,
|
formOptions: formOptions.value,
|
||||||
showSearchForm: true,
|
showSearchForm: true,
|
||||||
tableTitle: '店铺月销量目标管理',
|
tableTitle: '店铺月销量目标管理',
|
||||||
|
handleReset,
|
||||||
});
|
});
|
||||||
gridReloadRef.value = () => gridApi.reload();
|
gridReloadRef.value = () => gridApi.reload();
|
||||||
|
|
||||||
@@ -253,8 +279,10 @@ const formatAmount = (amount: number) => {
|
|||||||
return `¥${amount.toFixed(2)}`;
|
return `¥${amount.toFixed(2)}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 获取店铺选项
|
// 获取店铺选项(首次触发时加载)
|
||||||
const fetchShopOptions = async () => {
|
async function fetchShopOptions() {
|
||||||
|
if (shopFetched.value || shopLoading.value) return;
|
||||||
|
shopLoading.value = true;
|
||||||
try {
|
try {
|
||||||
const response = await getShopPageApi({
|
const response = await getShopPageApi({
|
||||||
page: 1,
|
page: 1,
|
||||||
@@ -265,15 +293,15 @@ const fetchShopOptions = async () => {
|
|||||||
label: shop.name,
|
label: shop.name,
|
||||||
value: shop.id,
|
value: shop.id,
|
||||||
}));
|
}));
|
||||||
|
shopFetched.value = true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取店铺列表失败:', error);
|
console.error('获取店铺列表失败:', error);
|
||||||
|
} finally {
|
||||||
|
shopLoading.value = false;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// 初始化
|
// 初始化
|
||||||
onMounted(() => {
|
|
||||||
fetchShopOptions();
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
|||||||
Reference in New Issue
Block a user