20260706213434
This commit is contained in:
1 parent
4d1e6c8b8f
commit
f8e16efa83
23 files changed
+1232
-765
No files matched your search
@@ -10,6 +10,7 @@ import {
|
||||
MealPlanItem,
|
||||
MealPlanLog,
|
||||
MealPlanLogType,
|
||||
MealPlanRating,
|
||||
MealPlanStatus,
|
||||
MealSaveChanges,
|
||||
MealSlot,
|
||||
@@ -28,6 +29,7 @@ const TABLES = {
|
||||
plan: "b_meal_plan",
|
||||
planItem: "b_meal_plan_item",
|
||||
planLog: "b_meal_plan_log",
|
||||
planRating: "b_meal_plan_rating",
|
||||
} as const;
|
||||
|
||||
interface MealState {
|
||||
@@ -41,6 +43,7 @@ interface MealState {
|
||||
plans: MealPlan[];
|
||||
planItems: MealPlanItem[];
|
||||
planLogs: MealPlanLog[];
|
||||
planRatings: MealPlanRating[];
|
||||
|
||||
clear: () => void;
|
||||
|
||||
@@ -107,6 +110,10 @@ interface MealState {
|
||||
completePlan: (params: { planId: number; familyId: number; userId: number }) => Promise<boolean>;
|
||||
addDishToPlan: (params: { familyId: number; planId: number; dishId: number; dishName: string; userId: number; sortNumber: number }) => Promise<{ planItemId?: number; logId?: number }>;
|
||||
removeDishFromPlan: (params: { familyId: number; planId: number; planItemId: number; dishName: string; userId: number }) => Promise<boolean>;
|
||||
|
||||
// 评分
|
||||
fetchPlanRatings: (familyId: number, planId: number) => Promise<void>;
|
||||
ratePlan: (params: { familyId: number; planId: number; rating: number; content?: string; userId: number }) => Promise<boolean>;
|
||||
}
|
||||
|
||||
function normalizeChanges<T>(changes: MealSaveChanges<T>) {
|
||||
@@ -129,6 +136,7 @@ export const useMealStore = create<MealState>((set, get) => ({
|
||||
plans: [],
|
||||
planItems: [],
|
||||
planLogs: [],
|
||||
planRatings: [],
|
||||
|
||||
clear: () =>
|
||||
set({
|
||||
@@ -632,4 +640,53 @@ export const useMealStore = create<MealState>((set, get) => ({
|
||||
const res = await saveDataApi(ops);
|
||||
return res.isSuccess;
|
||||
},
|
||||
|
||||
// ======== 评分 ========
|
||||
|
||||
fetchPlanRatings: async (familyId, planId) => {
|
||||
if (!familyId || !planId) return;
|
||||
const res = await loadDataApi({
|
||||
view_name: TABLES.planRating,
|
||||
search_condition: "family_id = ? and plan_id = ?",
|
||||
search_columns: [],
|
||||
order_by: "create_time asc, id asc",
|
||||
args: [familyId, planId],
|
||||
});
|
||||
if (res.isSuccess) {
|
||||
set({ planRatings: (res.data ?? []) as MealPlanRating[] });
|
||||
}
|
||||
},
|
||||
|
||||
ratePlan: async ({ familyId, planId, rating, content, userId }) => {
|
||||
if (!familyId || !planId || !userId) return false;
|
||||
const idRes = await getUniqueIdApi(1);
|
||||
const id = idRes.data?.[0] ?? 0;
|
||||
if (!id) return false;
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const inserts = [{
|
||||
id,
|
||||
family_id: familyId,
|
||||
plan_id: planId,
|
||||
rater_id: userId,
|
||||
rating,
|
||||
content: content ?? "",
|
||||
create_time: now,
|
||||
create_by: userId,
|
||||
}];
|
||||
|
||||
const res = await saveDataApi([{
|
||||
table_name: TABLES.planRating,
|
||||
key_field: "id",
|
||||
inserts,
|
||||
updates: [],
|
||||
deletes: [],
|
||||
}]);
|
||||
|
||||
if (res.isSuccess) {
|
||||
// 乐观更新本地 state
|
||||
set((s) => ({ planRatings: [...s.planRatings, inserts[0] as any] }));
|
||||
}
|
||||
return res.isSuccess;
|
||||
},
|
||||
}));
|
||||
Reference in new issue
Block a user