feat: 完成多日读取支持

This commit is contained in:
2026-08-16 17:28:53 +08:00
parent f209d93733
commit c35c985eb0
22 changed files with 1263 additions and 246 deletions
@@ -0,0 +1,30 @@
import type { AdvertisingCostItem } from '@/types/advertisingCost';
function itemKey(item: AdvertisingCostItem): string {
return `${item.feeDate}::${item.goodsId}`;
}
/**
* 合并多次单日读取:按 (feeDate, goodsId) upsert。
* 换日再读保留已有日期;同日再读覆盖该日同商品。
*/
export function mergeAdvertisingItems(
previous: AdvertisingCostItem[],
incoming: AdvertisingCostItem[],
): AdvertisingCostItem[] {
const map = new Map<string, AdvertisingCostItem>();
for (const item of previous) {
map.set(itemKey(item), item);
}
for (const item of incoming) {
map.set(itemKey(item), item);
}
return Array.from(map.values()).sort((a, b) => {
const byDate = a.feeDate.localeCompare(b.feeDate);
return byDate !== 0 ? byDate : a.goodsId.localeCompare(b.goodsId);
});
}
export function listFeeDates(items: AdvertisingCostItem[]): string[] {
return [...new Set(items.map((item) => item.feeDate))].sort();
}
+15 -6
View File
@@ -2,21 +2,30 @@ import type { AdvertisingCostBatch, PageContext } from '@/types/advertisingCost'
export type ExtensionMessage =
| { type: 'GET_PAGE_CONTEXT' }
| { type: 'PARSE_ADVERTISING_COST'; payload: { feeDate: string } }
| { type: 'PARSE_ADVERTISING_COST' }
| { type: 'TOGGLE_FLOATING_PANEL' }
| { type: 'SET_FLOATING_PANEL'; payload: { visible: boolean } }
| { type: 'GET_FLOATING_PANEL_STATE' }
| { type: 'PING' };
export type ExtensionResponse =
| { ok: true; data: PageContext }
| { ok: true; data: AdvertisingCostBatch }
| { ok: true; data: { pong: true } }
| { ok: true; data: { visible: boolean } }
| { ok: false; code: string; message: string };
const MESSAGE_TYPES = new Set([
'GET_PAGE_CONTEXT',
'PARSE_ADVERTISING_COST',
'TOGGLE_FLOATING_PANEL',
'SET_FLOATING_PANEL',
'GET_FLOATING_PANEL_STATE',
'PING',
]);
export function isExtensionMessage(value: unknown): value is ExtensionMessage {
if (!value || typeof value !== 'object') return false;
const type = (value as { type?: unknown }).type;
return (
type === 'GET_PAGE_CONTEXT' ||
type === 'PARSE_ADVERTISING_COST' ||
type === 'PING'
);
return typeof type === 'string' && MESSAGE_TYPES.has(type);
}