|
@@ -1,11 +1,12 @@
|
|
|
import type { FeedingPlanProduct } from '@/model/feeding-plan'
|
|
import type { FeedingPlanProduct } from '@/model/feeding-plan'
|
|
|
import type { Product } from '@/model/product'
|
|
import type { Product } from '@/model/product'
|
|
|
-import { createFeedingPlan } from '@/api/feeding-plan'
|
|
|
|
|
|
|
+import { createFeedingPlan, updateFeedingPlan } from '@/api/feeding-plan'
|
|
|
import { createPet } from '@/api/pet'
|
|
import { createPet } from '@/api/pet'
|
|
|
import {
|
|
import {
|
|
|
type CreateFeedingPlanRequest,
|
|
type CreateFeedingPlanRequest,
|
|
|
type CreatePetRequest,
|
|
type CreatePetRequest,
|
|
|
FeedingGoal,
|
|
FeedingGoal,
|
|
|
|
|
+ type FeedingPlan,
|
|
|
Gender,
|
|
Gender,
|
|
|
type Pet,
|
|
type Pet,
|
|
|
PetBodyType,
|
|
PetBodyType,
|
|
@@ -220,6 +221,8 @@ export const useFeedingPlanStore = defineStore('feeding-plan', () => {
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const isEdit = ref(false)
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 修改指定 productId 的 percentage
|
|
* 修改指定 productId 的 percentage
|
|
|
* @param productId 产品的id
|
|
* @param productId 产品的id
|
|
@@ -245,6 +248,7 @@ export const useFeedingPlanStore = defineStore('feeding-plan', () => {
|
|
|
arrangeDailyConsumeWeight()
|
|
arrangeDailyConsumeWeight()
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+ const savedFeedingPlan = ref<FeedingPlan | null>(null)
|
|
|
|
|
|
|
|
const confirm = async () => {
|
|
const confirm = async () => {
|
|
|
if (savedPet.value === null) {
|
|
if (savedPet.value === null) {
|
|
@@ -256,9 +260,54 @@ export const useFeedingPlanStore = defineStore('feeding-plan', () => {
|
|
|
return {
|
|
return {
|
|
|
id: item.product.id,
|
|
id: item.product.id,
|
|
|
dailyUsageWeight: item.dailyUsageWeight,
|
|
dailyUsageWeight: item.dailyUsageWeight,
|
|
|
|
|
+ percentage: item.percentage,
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
|
- await createFeedingPlan(feedingPlan.value)
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if ((isEdit.value && savedFeedingPlan.value)) {
|
|
|
|
|
+ await updateFeedingPlan(savedFeedingPlan.value.id, feedingPlan.value)
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ await createFeedingPlan(feedingPlan.value)
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const initPet = (payload: Pet) => {
|
|
|
|
|
+ // @ts-expect-error @ts-ignore
|
|
|
|
|
+ pet.value = {
|
|
|
|
|
+ birthday: payload.birthday,
|
|
|
|
|
+ bodyType: payload.bodyType,
|
|
|
|
|
+ gender: payload.gender,
|
|
|
|
|
+ isActive: payload.isActive,
|
|
|
|
|
+ isLactation: payload.isLactation,
|
|
|
|
|
+ isPregnant: payload.isPregnant,
|
|
|
|
|
+ isSterilization: payload.isSterilization,
|
|
|
|
|
+ name: payload.name,
|
|
|
|
|
+ photo: payload.photo,
|
|
|
|
|
+ type: payload.type,
|
|
|
|
|
+ weight: payload.weight,
|
|
|
|
|
+ }
|
|
|
|
|
+ savedPet.value = payload
|
|
|
|
|
+ feedingPlan.value.targetWeight = calculateIdealWeight()
|
|
|
|
|
+ rer.value = calculateRER(pet.value.weight)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const initFeedingPlan = (payload: FeedingPlan) => {
|
|
|
|
|
+ // @ts-expect-error @ts-ignore
|
|
|
|
|
+ feedingPlan.value = {
|
|
|
|
|
+ feedingGoal: payload.feedingGoal,
|
|
|
|
|
+ targetWeight: payload.targetWeight,
|
|
|
|
|
+ petId: savedPet.value?.id || '',
|
|
|
|
|
+ products: payload.feedingPlanProducts.map((item) => {
|
|
|
|
|
+ return {
|
|
|
|
|
+ id: item.product.id,
|
|
|
|
|
+ dailyUsageWeight: item.dailyUsageWeight,
|
|
|
|
|
+ }
|
|
|
|
|
+ }),
|
|
|
|
|
+ }
|
|
|
|
|
+ savedFeedingPlan.value = payload
|
|
|
|
|
+ selectedProducts.value = payload.feedingPlanProducts
|
|
|
|
|
+ dailyCalories.value = Math.floor(rer.value * derRate.value)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
return {
|
|
@@ -275,5 +324,7 @@ export const useFeedingPlanStore = defineStore('feeding-plan', () => {
|
|
|
changePercentage,
|
|
changePercentage,
|
|
|
dailyCalories,
|
|
dailyCalories,
|
|
|
confirm,
|
|
confirm,
|
|
|
|
|
+ initPet,
|
|
|
|
|
+ initFeedingPlan,
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|