You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
3.9 KiB
157 lines
3.9 KiB
import { Component } from "@angular/core";
|
|
import { ActivatedRoute, Router } from "@angular/router";
|
|
import { ApiService } from "@cdk/services";
|
|
import { NzMessageService } from "ng-zorro-antd/message";
|
|
import { DishInterface } from "../ingredient-dish/ingredient-dish.component";
|
|
import { forkJoin, map } from "rxjs";
|
|
import { IngredientAnalysisComponent } from "../ingredient-analysis/ingredient-analysis.component";
|
|
import { NzDrawerService } from "ng-zorro-antd/drawer";
|
|
import { weekdayMap } from "../ingredient-form-basic/ingredient-form-basic.component";
|
|
|
|
@Component({
|
|
selector: "app-ingredient-preview",
|
|
templateUrl: "./ingredient-preview.component.html",
|
|
styleUrls: ["./ingredient-preview.component.less"],
|
|
})
|
|
export class IngredientPreviewComponent {
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private api: ApiService,
|
|
private msg: NzMessageService,
|
|
private drawer: NzDrawerService
|
|
) {}
|
|
|
|
basic: any | null;
|
|
|
|
dishs: DishInterface[] = [];
|
|
|
|
totalObj: Record<string, string> | null = null;
|
|
|
|
monthText = {
|
|
1: "一月",
|
|
2: "二月",
|
|
3: "三月",
|
|
4: "四月",
|
|
5: "五月",
|
|
6: "六月",
|
|
7: "七月",
|
|
8: "八月",
|
|
9: "九月",
|
|
10: "十月",
|
|
11: "十一月",
|
|
12: "十二月",
|
|
} as any;
|
|
|
|
weekdayMap = weekdayMap;
|
|
|
|
days: number[] = [];
|
|
|
|
ngOnInit(): void {
|
|
const id = this.route.snapshot.queryParamMap.get("id");
|
|
const snapshot = this.route.snapshot.queryParamMap.get("snapshot");
|
|
const storage = snapshot ? sessionStorage.getItem(snapshot) : null;
|
|
|
|
if (id) {
|
|
forkJoin([this.api.getMenuDist(id), this.api.getMenuItem(id)])
|
|
.pipe(
|
|
map(([d, b]) => {
|
|
return [d.body, b.body];
|
|
})
|
|
)
|
|
.subscribe(([dishs, basic]) => {
|
|
// this.days = Array.from({ length: basic.day }, (_, i) => i + 1);
|
|
this.days = basic.day;
|
|
|
|
this.getStandardName(basic.nutrient);
|
|
this.basic = basic;
|
|
|
|
this.initMenuDish(dishs);
|
|
});
|
|
|
|
return;
|
|
}
|
|
if (storage) {
|
|
try {
|
|
const { basic, dishs } = JSON.parse(storage);
|
|
// this.days = Array.from({ length: basic.day }, (_, i) => i + 1);
|
|
this.days = basic.day;
|
|
this.getStandardName(basic.nutrient);
|
|
this.basic = basic;
|
|
this.dishs = dishs;
|
|
} catch (error) {
|
|
this.msg.error("解析食谱数据出错了,请重试!");
|
|
this.router.navigate(["/ingredient/item/list"]);
|
|
}
|
|
return;
|
|
}
|
|
this.msg.error("没有找到食谱数据");
|
|
this.router.navigate(["/ingredient/item/list"]);
|
|
}
|
|
|
|
initMenuDish(menuDishFormServer: any[]) {
|
|
const foodIds = new Set<number>();
|
|
menuDishFormServer.forEach((i: any) => {
|
|
i.ingredient.map((food: any) => {
|
|
// 收集 食材 key 获取 食材名称 & 把 value 对象转换成 groupValues 数组
|
|
foodIds.add(food.key);
|
|
food["groupValues"] = Object.entries(food.value).map(([peopleName, value]) => {
|
|
return {
|
|
peopleName,
|
|
value,
|
|
};
|
|
});
|
|
});
|
|
});
|
|
forkJoin([this.api.getFoodList({ keys: Array.from(foodIds) })]).subscribe(([res]) => {
|
|
this.dishs = menuDishFormServer.map((i: any) => {
|
|
return {
|
|
...i,
|
|
dishName: i.name,
|
|
mealIndex: this.basic.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;
|
|
}),
|
|
};
|
|
});
|
|
});
|
|
}
|
|
|
|
getStandardName(id: string) {
|
|
this.api.getStandard({ id }).subscribe((res) => {
|
|
this.basic["standardName"] = res.body.name;
|
|
});
|
|
}
|
|
|
|
calcTotal(day: number, meal: string, people: string) {
|
|
let total = 0;
|
|
this.dishs.forEach((d) => {
|
|
if (d.day === day && d.meal === meal) {
|
|
d.items.forEach((food) => {
|
|
total += food.value[people];
|
|
});
|
|
}
|
|
});
|
|
return total.toFixed(2);
|
|
}
|
|
|
|
analysis(day: number) {
|
|
this.drawer.create({
|
|
nzWidth: 620,
|
|
nzWrapClassName: "analysis-drawer",
|
|
nzContent: IngredientAnalysisComponent,
|
|
nzContentParams: {
|
|
menu: {
|
|
...this.basic,
|
|
days: this.days,
|
|
},
|
|
current: {
|
|
day,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|