import { Component } from "@angular/core"; import { FormBuilder, FormGroup } from "@angular/forms"; import { ActivatedRoute, Router } from "@angular/router"; import { ApiService } from "@cdk/services"; import { AnyObject } from "@cdk/types"; import { FormValidators } from "@cdk/validators"; import { NzMessageService } from "ng-zorro-antd/message"; export type StandardItemInterface = { type: string; value: number; }; export type StandardPeopleInterface = { ul?: number; hasUl: boolean; max: number; min: number; nutrition: string; }; @Component({ selector: "app-standard-setting", templateUrl: "./standard-setting.component.html", styleUrls: ["./standard-setting.component.less"], }) export class StandardSettingComponent { constructor( private fb: FormBuilder, private router: Router, private route: ActivatedRoute, private api: ApiService, private msg: NzMessageService ) { const id = this.route.snapshot.paramMap.get("id"); if (id !== "create") { const data = this.router.getCurrentNavigation()?.extras; if (data) { this.state = data.state; } else { this.router.navigate(["/standard/list"]); } } } public globalEnum = this.api.globalEnum; state: any; calcType = "种"; uploadLoading = false; foodCategoryDay: StandardItemInterface[] = []; foodCategoryWeek: StandardItemInterface[] = []; ingredient: { name: string; nutritions: StandardPeopleInterface[] }[] = []; ngOnInit(): void { this.foodCategoryDay = this.parseFoodCategory(this.state?.foodCategoryDay); this.foodCategoryWeek = this.parseFoodCategory(this.state?.foodCategoryWeek); this.ingredient = this.parseIngredient(this.state?.ingredient); } parseIngredient(data: any): { name: string; nutritions: StandardPeopleInterface[] }[] { if (!data) { return []; } return Object.entries(data).map(([k, v]) => { return { name: k, nutritions: !v ? [] : Object.entries(v).map(([kn, vn]) => { return { ul: vn?.ul ?? void 0, hasUl: !!vn?.ul, max: vn?.max ?? 0, min: vn?.min ?? 0, nutrition: kn, }; }), }; }); } parseFoodCategory(data: any): StandardItemInterface[] { if (!data) { return []; } return Object.entries(data).map(([k, v]) => { return { type: k, value: v as number, }; }); } onSubmit() { if (this.foodCategoryDay.some((s) => !s.type || !s.value)) { this.msg.error("请设置正确的食物种类及数量标准(日)"); return; } if (this.foodCategoryWeek.some((s) => !s.type || !s.value)) { this.msg.error("请设置正确的食物种类及数量标准(周)"); return; } if (this.ingredient.some((s) => !s.name || !s.nutritions.some((sn) => sn.nutrition))) { this.msg.error("请设置正确的营养标准人群"); return; } const foodCategoryDay = this.foodCategoryDay.reduce((a, c) => { return { ...a, [c.type]: c.value, }; }, {} as AnyObject); const foodCategoryWeek = this.foodCategoryWeek.reduce((a, c) => { return { ...a, [c.type]: c.value, }; }, {} as AnyObject); const ingredient = this.ingredient.reduce((a, c) => { return { ...a, [c.name]: c.nutritions.reduce((an, cn) => { const ul = { ul: cn.hasUl ? cn.ul : void 0 }; return { ...an, [cn.nutrition]: { min: cn.min, max: cn.max, ...ul, }, }; }, {} as AnyObject), }; }, {} as AnyObject); this.api.saveStandard({ ...this.state, foodCategoryDay, foodCategoryWeek, ingredient }, true).subscribe((res) => { this.msg.success(res.desc); this.router.navigate(["/standard/list"]); }); } addFoodType(type: string) { const item = type === "day" ? this.foodCategoryDay : this.foodCategoryWeek; const withoutSelectType = this.globalEnum.category.find((f) => !item.some((s) => s.type === f.key)); item.push({ type: withoutSelectType?.key ?? "", value: 1 }); } removeFoodType(type: string, foodType: string) { if (type === "day") { this.foodCategoryDay = this.foodCategoryDay.filter((f) => f.type !== foodType); } else { this.foodCategoryWeek = this.foodCategoryWeek.filter((f) => f.type !== foodType); } } addPeopleGroup() { this.ingredient.push({ name: "", nutritions: [], }); } removePeopleGroup(idx: number) { this.ingredient = this.ingredient.filter((_, i) => i !== idx); } addNutrition(idx: number) { const current = this.ingredient[idx]; const withoutSelectNutritions = this.globalEnum.nutrient.find( (f) => !current.nutritions.some((s) => s.nutrition === f.key) ); current.nutritions.push({ nutrition: withoutSelectNutritions?.key ?? "", min: 0, max: 0, hasUl: false, }); } removeNutrition(idx: number, nutrition: string) { this.ingredient[idx].nutritions = this.ingredient[idx].nutritions.filter((f) => f.nutrition !== nutrition); } ulChange(idx: number, nutrition: string, checked: boolean) { this.ingredient[idx].nutritions = this.ingredient[idx].nutritions.map((i) => { return i.nutrition === nutrition ? { ...i, ul: checked ? 1 : void 0 } : i; }); } }