|
|
|
|
import { Component, Input, OnChanges, SimpleChanges, TemplateRef } from "@angular/core";
|
|
|
|
|
// import { MealDishInterface } from "../ingredient-meals/ingredient-meals.component";
|
|
|
|
|
import { NzModalService } from "ng-zorro-antd/modal";
|
|
|
|
|
import { Augmented, OptionItemInterface } from "@cdk/types";
|
|
|
|
|
import { NzMessageService } from "ng-zorro-antd/message";
|
|
|
|
|
import { forkJoin } from "rxjs";
|
|
|
|
|
import { ApiService } from "@cdk/services";
|
|
|
|
|
|
|
|
|
|
// export interface MenuObjectInterface {
|
|
|
|
|
// [Day: number]: {
|
|
|
|
|
// [MealIndex: number]: MealDishInterface[];
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
export type DishInterface = Augmented<{
|
|
|
|
|
dishId: number;
|
|
|
|
|
day: number;
|
|
|
|
|
meal: string;
|
|
|
|
|
mark: string;
|
|
|
|
|
items: Array<
|
|
|
|
|
Augmented<{
|
|
|
|
|
key: string;
|
|
|
|
|
isMain: boolean;
|
|
|
|
|
value: Record<string, number>;
|
|
|
|
|
}>
|
|
|
|
|
>;
|
|
|
|
|
}>;
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: "app-ingredient-dish",
|
|
|
|
|
templateUrl: "./ingredient-dish.component.html",
|
|
|
|
|
styleUrls: ["./ingredient-dish.component.less"],
|
|
|
|
|
})
|
|
|
|
|
export class IngredientDishComponent implements OnChanges {
|
|
|
|
|
constructor(private modal: NzModalService, private msg: NzMessageService, private api: ApiService) {}
|
|
|
|
|
|
|
|
|
|
@Input() menuBaisc: any | null;
|
|
|
|
|
|
|
|
|
|
@Input() menuDishFormServer: any | null;
|
|
|
|
|
|
|
|
|
|
expanded = new Set<number>();
|
|
|
|
|
|
|
|
|
|
days: number[] = [];
|
|
|
|
|
|
|
|
|
|
selectDay: OptionItemInterface[] = [];
|
|
|
|
|
|
|
|
|
|
mealCurrentIndex: Record<number, number> = {};
|
|
|
|
|
|
|
|
|
|
// d = {
|
|
|
|
|
// 1:{
|
|
|
|
|
// zaocan:[
|
|
|
|
|
// {dishId:1...}
|
|
|
|
|
// ]
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// menuObject!: MenuObjectInterface;
|
|
|
|
|
|
|
|
|
|
mealDishList: DishInterface[] = [];
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
if (changes["menuBaisc"]?.currentValue) {
|
|
|
|
|
this.initMenuBasic();
|
|
|
|
|
}
|
|
|
|
|
if (changes["menuDishFormServer"]?.currentValue) {
|
|
|
|
|
this.initMenuDish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initMenuDish() {
|
|
|
|
|
const foodIds = new Set<number>();
|
|
|
|
|
this.menuDishFormServer.forEach((i: any) => {
|
|
|
|
|
i.ingredient.map((food: any) => {
|
|
|
|
|
foodIds.add(food.key);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
forkJoin([this.api.getFoodList({ keys: Array.from(foodIds) })]).subscribe(([res]) => {
|
|
|
|
|
this.mealDishList = this.menuDishFormServer.map((i: any) => {
|
|
|
|
|
return {
|
|
|
|
|
...i,
|
|
|
|
|
dishName: i.name,
|
|
|
|
|
mealIndex: this.menuBaisc.meals.findIndex((m: string) => m === i.meal),
|
|
|
|
|
items: i.ingredient.map((food: any) => {
|
|
|
|
|
const fd = res.body.find((f) => f.key === food.key);
|
|
|
|
|
food.foodName = fd.name;
|
|
|
|
|
return food;
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initMenuBasic() {
|
|
|
|
|
if (this.menuBaisc) {
|
|
|
|
|
const meals = this.menuBaisc.meals as string[];
|
|
|
|
|
const day = this.menuBaisc.day as number;
|
|
|
|
|
this.days = Array.from({ length: day }, (_, i) => {
|
|
|
|
|
this.selectDay.push({
|
|
|
|
|
label: `第${i + 1}天`,
|
|
|
|
|
value: String(i + 1),
|
|
|
|
|
});
|
|
|
|
|
this.mealCurrentIndex[i] = 0;
|
|
|
|
|
// if (!this.menuObject) {
|
|
|
|
|
// this.menuObject = {};
|
|
|
|
|
// }
|
|
|
|
|
// this.menuObject[i + 1] = meals.reduce((a, c, idx) => {
|
|
|
|
|
// return {
|
|
|
|
|
// ...a,
|
|
|
|
|
// [idx]: [],
|
|
|
|
|
// };
|
|
|
|
|
// }, {} as Record<number, MealDishInterface[]>);
|
|
|
|
|
return i;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expandChange(i: number) {
|
|
|
|
|
if (this.expanded.has(i)) {
|
|
|
|
|
this.expanded.delete(i);
|
|
|
|
|
} else {
|
|
|
|
|
this.expanded.add(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSaveDish(v: DishInterface[], day: number, mealIndex: number) {
|
|
|
|
|
this.mealDishList = JSON.parse(JSON.stringify(v));
|
|
|
|
|
// this.menuObject[day][mealIndex] = JSON.parse(JSON.stringify(v));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reuse(day: number, nzContent: TemplateRef<{}>) {
|
|
|
|
|
const thisDayDishs = this.mealDishList.filter((f) => f.day === day);
|
|
|
|
|
console.log("dayDishs", day, this.mealDishList, thisDayDishs);
|
|
|
|
|
this.modal.create({
|
|
|
|
|
nzTitle: "请选择应用到的日期",
|
|
|
|
|
nzContent,
|
|
|
|
|
nzOnOk: () => {
|
|
|
|
|
this.selectDay.forEach((i) => {
|
|
|
|
|
if (i["checked"]) {
|
|
|
|
|
thisDayDishs.forEach((reused) => {
|
|
|
|
|
this.mealDishList.push(JSON.parse(JSON.stringify({ ...reused, day: Number(i.value) })));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
i["checked"] = false;
|
|
|
|
|
});
|
|
|
|
|
this.mealDishList = JSON.parse(JSON.stringify(this.mealDishList));
|
|
|
|
|
this.msg.success("操作成功");
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|