20260707213510

This commit is contained in:
oneao committed 2026-07-07 21:35:10 +08:00
1 parent f8e16efa83
commit f94b5152ed
10 files changed
+918 -219

No files matched your search

+26
View File
@@ -8,10 +8,14 @@ import {
FinanceType,
} from "@/types/finance";
import { shouldRun, getMissedDates } from "@/helpers/financeAuto";
import AsyncStorage from "@react-native-async-storage/async-storage";
const AUTO_BILL_IDS_KEY = "finance_auto_bill_ids";
interface FinanceState {
categories: FinanceCategory[];
autoRules: FinanceAuto[];
autoBillIds: number[];
setCategories: (categories: FinanceCategory[]) => void;
clearCategories: () => void;
@@ -26,11 +30,15 @@ interface FinanceState {
fetchAutoRules: (familyId: number) => Promise<void>;
saveAutoRule: (rule: Partial<FinanceAuto>, isDelete?: boolean) => Promise<boolean>;
runAutoRules: (familyId: number, userId: number) => Promise<number>;
loadAutoBillIds: () => Promise<void>;
addAutoBillIds: (ids: number[]) => void;
isAutoBill: (id: number) => boolean;
}
export const useFinanceStore = create<FinanceState>((set, get) => ({
categories: [],
autoRules: [],
autoBillIds: [],
setCategories: (categories: FinanceCategory[]) => set({ categories }),
clearCategories: () => set({ categories: [], autoRules: [] }),
@@ -132,6 +140,9 @@ export const useFinanceStore = create<FinanceState>((set, get) => ({
if (inserts.length > 0) {
await saveDataApi([{ table_name: "b_finance", key_field: "id", inserts, updates: [], deletes: [] }]);
// 记录自动生成的账单 ID
const newIds = inserts.map((item) => item.id as number);
get().addAutoBillIds(newIds);
// 更新 last_run 为最近的一天
const lastDate = missedDates[missedDates.length - 1];
await saveDataApi([{
@@ -147,4 +158,19 @@ export const useFinanceStore = create<FinanceState>((set, get) => ({
return count;
},
loadAutoBillIds: async () => {
try {
const raw = await AsyncStorage.getItem(AUTO_BILL_IDS_KEY);
if (raw) set({ autoBillIds: JSON.parse(raw) });
} catch { /* ignore */ }
},
addAutoBillIds: (ids: number[]) => {
const merged = [...new Set([...get().autoBillIds, ...ids])];
set({ autoBillIds: merged });
AsyncStorage.setItem(AUTO_BILL_IDS_KEY, JSON.stringify(merged)).catch(() => {});
},
isAutoBill: (id: number) => get().autoBillIds.includes(id),
}));