|
@ -1,6 +1,13 @@ |
|
|
import { Component, EventEmitter, OnInit, Output } from "@angular/core"; |
|
|
import { Component, EventEmitter, OnInit, Output } from "@angular/core"; |
|
|
import { FormArray, FormBuilder, FormGroup } from "@angular/forms"; |
|
|
import { FormArray, FormBuilder, FormGroup } from "@angular/forms"; |
|
|
|
|
|
import { Router } from "@angular/router"; |
|
|
|
|
|
import { OrgDTO } from "@cdk/dtos"; |
|
|
|
|
|
import { ApiService } from "@cdk/services"; |
|
|
|
|
|
import { OptionItemInterface } from "@cdk/types"; |
|
|
|
|
|
import { Utils } from "@cdk/utils"; |
|
|
import { FormValidators } from "@cdk/validators"; |
|
|
import { FormValidators } from "@cdk/validators"; |
|
|
|
|
|
import { NzMessageService } from "ng-zorro-antd/message"; |
|
|
|
|
|
import { Subject, debounceTime, distinctUntilChanged, filter, finalize, switchMap, takeUntil } from "rxjs"; |
|
|
|
|
|
|
|
|
@Component({ |
|
|
@Component({ |
|
|
selector: "app-ingredient-form-basic", |
|
|
selector: "app-ingredient-form-basic", |
|
@ -8,36 +15,138 @@ import { FormValidators } from "@cdk/validators"; |
|
|
styleUrls: ["./ingredient-form-basic.component.less"], |
|
|
styleUrls: ["./ingredient-form-basic.component.less"], |
|
|
}) |
|
|
}) |
|
|
export class IngredientFormBasicComponent { |
|
|
export class IngredientFormBasicComponent { |
|
|
constructor(private fb: FormBuilder) {} |
|
|
constructor( |
|
|
|
|
|
private fb: FormBuilder, |
|
|
|
|
|
private msg: NzMessageService, |
|
|
|
|
|
private api: ApiService, |
|
|
|
|
|
private router: Router |
|
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
@Output() onSave = new EventEmitter(); |
|
|
@Output() onSave = new EventEmitter(); |
|
|
|
|
|
|
|
|
|
|
|
private standardSearch$ = new Subject<string>(); |
|
|
|
|
|
|
|
|
|
|
|
private destroy$ = new Subject<void>(); |
|
|
|
|
|
|
|
|
formGroup!: FormGroup; |
|
|
formGroup!: FormGroup; |
|
|
|
|
|
|
|
|
ages = [ |
|
|
meals = this.api.globalEnum.mealType; |
|
|
{ value: "1", label: "6-8岁(男)", checked: false }, |
|
|
|
|
|
{ value: "2", label: "6-8岁(女)", checked: false }, |
|
|
standardOfOption: Array<OptionItemInterface> = []; |
|
|
{ value: "3", label: "9-11岁(男)", checked: false }, |
|
|
|
|
|
{ value: "4", label: "9-11岁(女)", checked: false }, |
|
|
searchedStandard: Array<OptionItemInterface> = []; |
|
|
{ value: "5", label: "12-14岁(男)", checked: false }, |
|
|
|
|
|
{ value: "6", label: "12-14岁(女)", checked: false }, |
|
|
currentOrgs: OptionItemInterface[] = []; |
|
|
]; |
|
|
|
|
|
|
|
|
currentPeoples: OptionItemInterface[] = []; |
|
|
|
|
|
|
|
|
|
|
|
submitLoading = false; |
|
|
|
|
|
|
|
|
|
|
|
nzFilterOption = (): boolean => true; |
|
|
|
|
|
|
|
|
ngOnInit(): void { |
|
|
ngOnInit(): void { |
|
|
this.formGroup = this.fb.group({ |
|
|
this.formGroup = this.fb.group({ |
|
|
id: this.fb.control("", [FormValidators.required()]), |
|
|
id: this.fb.control("", []), |
|
|
unit: this.fb.control("", [FormValidators.required()]), |
|
|
|
|
|
day: this.fb.control("1", [FormValidators.required()]), |
|
|
|
|
|
name: this.fb.control("", [FormValidators.required()]), |
|
|
name: this.fb.control("", [FormValidators.required()]), |
|
|
food: this.fb.array([], [FormValidators.required()]), |
|
|
nutrient: this.fb.control("", [FormValidators.required()]), |
|
|
tag: this.fb.control([], []), |
|
|
day: this.fb.control("1", [FormValidators.required()]), |
|
|
month: this.fb.control([], []), |
|
|
vendors: this.fb.control([], [FormValidators.required()]), |
|
|
|
|
|
month: this.fb.control([], [FormValidators.required()]), |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.standardSearch$ |
|
|
|
|
|
.pipe( |
|
|
|
|
|
filter((f) => !!f), |
|
|
|
|
|
debounceTime(500), |
|
|
|
|
|
distinctUntilChanged(), |
|
|
|
|
|
takeUntil(this.destroy$), |
|
|
|
|
|
switchMap((term: string) => this.api.getStandard({ name: term })) |
|
|
|
|
|
) |
|
|
|
|
|
.subscribe((data) => { |
|
|
|
|
|
let listOfOption: Array<OptionItemInterface> = []; |
|
|
|
|
|
if (data.body) { |
|
|
|
|
|
data.body.forEach((item) => { |
|
|
|
|
|
listOfOption.push({ |
|
|
|
|
|
label: item.name, |
|
|
|
|
|
value: item.id, |
|
|
|
|
|
...item, |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
this.searchedStandard = this.searchedStandard.concat(listOfOption); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
this.standardOfOption = listOfOption; |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ngOnDestroy(): void { |
|
|
|
|
|
this.destroy$.next(); |
|
|
|
|
|
this.destroy$.complete(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
ageChange() {} |
|
|
ageChange() {} |
|
|
|
|
|
|
|
|
|
|
|
searchStandard(k: string) { |
|
|
|
|
|
this.standardSearch$.next(k); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onStandardChange(v: any) { |
|
|
|
|
|
const currentStandard = this.searchedStandard.find((f) => f.value === v); |
|
|
|
|
|
if (currentStandard) { |
|
|
|
|
|
this.api.getOrgList({ vendors: currentStandard["vendors"] }).subscribe((res) => { |
|
|
|
|
|
this.currentOrgs = res.body.map((i) => ({ |
|
|
|
|
|
...i, |
|
|
|
|
|
value: String(i.id), |
|
|
|
|
|
label: i.name, |
|
|
|
|
|
})); |
|
|
|
|
|
}); |
|
|
|
|
|
this.currentPeoples = Object.entries(currentStandard["ingredient"] ?? {}).map(([k, v]) => { |
|
|
|
|
|
return { |
|
|
|
|
|
label: k, |
|
|
|
|
|
value: k, |
|
|
|
|
|
...(v as any), |
|
|
|
|
|
}; |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
onSubmit() { |
|
|
onSubmit() { |
|
|
|
|
|
if (Utils.validateFormGroup(this.formGroup)) { |
|
|
|
|
|
if (!this.meals.some((s) => s["checked"])) { |
|
|
|
|
|
this.msg.error("请选择餐次"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
if (!this.currentPeoples.some((s) => s["checked"])) { |
|
|
|
|
|
this.msg.error("请选择人群显示"); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
this.submitLoading = true; |
|
|
|
|
|
this.api |
|
|
|
|
|
.saveMenu({ |
|
|
|
|
|
...this.formGroup.value, |
|
|
|
|
|
crows: this.currentPeoples |
|
|
|
|
|
.reduce((a, c) => { |
|
|
|
|
|
return c["checked"] ? a.concat(c.value) : a; |
|
|
|
|
|
}, [] as string[]) |
|
|
|
|
|
.join(","), |
|
|
|
|
|
month: this.formGroup.value.month.join(","), |
|
|
|
|
|
vendors: this.formGroup.value.vendors.join(","), |
|
|
|
|
|
meals: this.meals |
|
|
|
|
|
.reduce((a, c) => { |
|
|
|
|
|
return c["checked"] ? a.concat(c.value) : a; |
|
|
|
|
|
}, [] as string[]) |
|
|
|
|
|
.join(","), |
|
|
|
|
|
}) |
|
|
|
|
|
.pipe( |
|
|
|
|
|
finalize(() => { |
|
|
|
|
|
this.submitLoading = false; |
|
|
|
|
|
}) |
|
|
|
|
|
) |
|
|
|
|
|
.subscribe((res) => { |
|
|
|
|
|
this.msg.success(res.desc); |
|
|
this.onSave.emit(); |
|
|
this.onSave.emit(); |
|
|
|
|
|
this.router.navigate(["/ingredient/item/list"]); |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|