|
|
@ -1,37 +1,37 @@ |
|
|
|
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from "@angular/core"; |
|
|
|
import { FormArray, FormBuilder, FormGroup } from "@angular/forms"; |
|
|
|
import { ActivatedRoute, Router } from "@angular/router"; |
|
|
|
import { OrgDTO } from "@cdk/dtos"; |
|
|
|
import { ApiService } from "@cdk/services"; |
|
|
|
import { AnyObject, OptionItemInterface } from "@cdk/types"; |
|
|
|
import { Utils } from "@cdk/utils"; |
|
|
|
import { FormValidators } from "@cdk/validators"; |
|
|
|
import { NzMessageService } from "ng-zorro-antd/message"; |
|
|
|
import { Subject, debounceTime, distinctUntilChanged, filter, finalize, switchMap, takeUntil } from "rxjs"; |
|
|
|
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core' |
|
|
|
import { FormArray, FormBuilder, FormGroup } from '@angular/forms' |
|
|
|
import { ActivatedRoute, Router } from '@angular/router' |
|
|
|
import { OrgDTO } from '@cdk/dtos' |
|
|
|
import { ApiService } from '@cdk/services' |
|
|
|
import { AnyObject, OptionItemInterface } from '@cdk/types' |
|
|
|
import { Utils } from '@cdk/utils' |
|
|
|
import { FormValidators } from '@cdk/validators' |
|
|
|
import { NzMessageService } from 'ng-zorro-antd/message' |
|
|
|
import { Subject, debounceTime, distinctUntilChanged, filter, finalize, switchMap, takeUntil } from 'rxjs' |
|
|
|
|
|
|
|
export const weekday = [ |
|
|
|
{ label: "周一", value: 1, checked: false }, |
|
|
|
{ label: "周二", value: 2, checked: false }, |
|
|
|
{ label: "周三", value: 3, checked: false }, |
|
|
|
{ label: "周四", value: 4, checked: false }, |
|
|
|
{ label: "周五", value: 5, checked: false }, |
|
|
|
{ label: "周六", value: 6 }, |
|
|
|
{ label: "周日", value: 7 }, |
|
|
|
]; |
|
|
|
{ label: '周一', value: 1, checked: false }, |
|
|
|
{ label: '周二', value: 2, checked: false }, |
|
|
|
{ label: '周三', value: 3, checked: false }, |
|
|
|
{ label: '周四', value: 4, checked: false }, |
|
|
|
{ label: '周五', value: 5, checked: false }, |
|
|
|
{ label: '周六', value: 6 }, |
|
|
|
{ label: '周日', value: 7 }, |
|
|
|
] |
|
|
|
export const weekdayMap: Record<number, string> = { |
|
|
|
1: "周一", |
|
|
|
2: "周二", |
|
|
|
3: "周三", |
|
|
|
4: "周四", |
|
|
|
5: "周五", |
|
|
|
6: "周六", |
|
|
|
7: "周日", |
|
|
|
}; |
|
|
|
1: '周一', |
|
|
|
2: '周二', |
|
|
|
3: '周三', |
|
|
|
4: '周四', |
|
|
|
5: '周五', |
|
|
|
6: '周六', |
|
|
|
7: '周日', |
|
|
|
} |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: "app-ingredient-form-basic", |
|
|
|
templateUrl: "./ingredient-form-basic.component.html", |
|
|
|
styleUrls: ["./ingredient-form-basic.component.less"], |
|
|
|
selector: 'app-ingredient-form-basic', |
|
|
|
templateUrl: './ingredient-form-basic.component.html', |
|
|
|
styleUrls: ['./ingredient-form-basic.component.less'], |
|
|
|
}) |
|
|
|
export class IngredientFormBasicComponent implements OnChanges { |
|
|
|
constructor( |
|
|
@ -39,46 +39,48 @@ export class IngredientFormBasicComponent implements OnChanges { |
|
|
|
private msg: NzMessageService, |
|
|
|
private api: ApiService, |
|
|
|
private router: Router, |
|
|
|
private route: ActivatedRoute |
|
|
|
private route: ActivatedRoute, |
|
|
|
) {} |
|
|
|
|
|
|
|
@Input() menu: any | null; |
|
|
|
@Input() menu: any | null |
|
|
|
|
|
|
|
@Input() client: boolean = false |
|
|
|
|
|
|
|
@Input() client: boolean = false; |
|
|
|
@Output() onSave = new EventEmitter() |
|
|
|
|
|
|
|
@Output() onSave = new EventEmitter(); |
|
|
|
private standardSearch$ = new Subject<{ id?: string; name?: string }>() |
|
|
|
|
|
|
|
private standardSearch$ = new Subject<{ id?: string; name?: string }>(); |
|
|
|
private destroy$ = new Subject<void>() |
|
|
|
|
|
|
|
private destroy$ = new Subject<void>(); |
|
|
|
formGroup!: FormGroup |
|
|
|
|
|
|
|
formGroup!: FormGroup; |
|
|
|
weekday = weekday |
|
|
|
|
|
|
|
weekday = weekday; |
|
|
|
meals = this.api.globalEnum.mealType |
|
|
|
|
|
|
|
meals = this.api.globalEnum.mealType; |
|
|
|
standardOfOption: Array<OptionItemInterface> = [] |
|
|
|
|
|
|
|
standardOfOption: Array<OptionItemInterface> = []; |
|
|
|
searchedStandard: Array<OptionItemInterface> = [] |
|
|
|
|
|
|
|
searchedStandard: Array<OptionItemInterface> = []; |
|
|
|
allOrgs: OptionItemInterface[] = [] |
|
|
|
|
|
|
|
currentOrgs: OptionItemInterface[] = []; |
|
|
|
currentOrgs: OptionItemInterface[] = [] |
|
|
|
|
|
|
|
currentPeoples: OptionItemInterface[] = []; |
|
|
|
currentPeoples: OptionItemInterface[] = [] |
|
|
|
|
|
|
|
submitLoading = false; |
|
|
|
submitLoading = false |
|
|
|
|
|
|
|
nzFilterOption = (): boolean => true; |
|
|
|
nzFilterOption = (): boolean => true |
|
|
|
|
|
|
|
ngOnInit(): void { |
|
|
|
this.formGroup = this.fb.group({ |
|
|
|
id: this.fb.control("", []), |
|
|
|
name: this.fb.control("", [FormValidators.required()]), |
|
|
|
nutrient: this.fb.control("", [FormValidators.required()]), |
|
|
|
id: this.fb.control('', []), |
|
|
|
name: this.fb.control('', [FormValidators.required()]), |
|
|
|
nutrient: this.fb.control('', [FormValidators.required()]), |
|
|
|
// day: this.fb.control([], [FormValidators.required()]),
|
|
|
|
vendors: this.fb.control([], this.client ? [] : [FormValidators.required()]), |
|
|
|
month: this.fb.control([], [FormValidators.required()]), |
|
|
|
}); |
|
|
|
}) |
|
|
|
// this.standardSearch$
|
|
|
|
// .pipe(
|
|
|
|
// filter((f) => !!f),
|
|
|
@ -109,124 +111,126 @@ export class IngredientFormBasicComponent implements OnChanges { |
|
|
|
// this.standardOfOption = listOfOption;
|
|
|
|
// });
|
|
|
|
this.api.getStandard().subscribe((res) => { |
|
|
|
this.standardOfOption = res; |
|
|
|
this.standardOfOption = res |
|
|
|
if (this.menu) { |
|
|
|
this.onStandardChange(String(this.menu.nutrient)); |
|
|
|
this.setValues(); |
|
|
|
this.onStandardChange(String(this.menu.nutrient)) |
|
|
|
this.setValues() |
|
|
|
} |
|
|
|
}); |
|
|
|
}) |
|
|
|
|
|
|
|
this.api.getOrgList().subscribe((res) => { |
|
|
|
this.allOrgs = res.map((i) => ({ |
|
|
|
...i, |
|
|
|
value: String(i.id), |
|
|
|
label: i.name, |
|
|
|
})) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {} |
|
|
|
|
|
|
|
setValues() { |
|
|
|
let weekday = []; |
|
|
|
let weekday = [] |
|
|
|
// console.log("this.menu", this.menu);
|
|
|
|
if (this.menu) { |
|
|
|
this.standardSearch$.next({ id: this.menu.nutrient }); |
|
|
|
this.meals = this.meals.map((i) => (this.menu.meals.includes(i.value) ? { ...i, checked: true } : i)); |
|
|
|
weekday = this.weekday.map((i) => (this.menu.day.includes(i.value) ? { ...i, checked: true } : i)); |
|
|
|
this.standardSearch$.next({ id: this.menu.nutrient }) |
|
|
|
this.meals = this.meals.map((i) => (this.menu.meals.includes(i.value) ? { ...i, checked: true } : i)) |
|
|
|
weekday = this.weekday.map((i) => (this.menu.day.includes(i.value) ? { ...i, checked: true } : i)) |
|
|
|
this.formGroup.patchValue({ |
|
|
|
...this.menu, |
|
|
|
nutrient: String(this.menu.nutrient), |
|
|
|
vendors: [String(this.menu.vender)], |
|
|
|
}); |
|
|
|
}) |
|
|
|
} else { |
|
|
|
weekday = this.weekday.map((i) => ({ ...i, checked: i.value <= 5 })); |
|
|
|
weekday = this.weekday.map((i) => ({ ...i, checked: i.value <= 5 })) |
|
|
|
} |
|
|
|
this.weekday = weekday; |
|
|
|
this.weekday = weekday |
|
|
|
} |
|
|
|
|
|
|
|
ngOnDestroy(): void { |
|
|
|
this.destroy$.next(); |
|
|
|
this.destroy$.complete(); |
|
|
|
this.destroy$.next() |
|
|
|
this.destroy$.complete() |
|
|
|
} |
|
|
|
|
|
|
|
ageChange() {} |
|
|
|
|
|
|
|
searchStandard(k: string) { |
|
|
|
if (k) { |
|
|
|
this.standardSearch$.next({ name: k }); |
|
|
|
this.standardSearch$.next({ name: k }) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
onStandardChange(v: any) { |
|
|
|
const currentStandard = this.standardOfOption.find((f) => f.value === v); |
|
|
|
onStandardChange(v: string) { |
|
|
|
const currentStandard = this.standardOfOption.find((f) => f.value === v) |
|
|
|
this.formGroup.patchValue({ |
|
|
|
vendors: [], |
|
|
|
}); |
|
|
|
}) |
|
|
|
if (currentStandard) { |
|
|
|
this.currentPeoples = Object.entries(currentStandard["ingredient"] ?? {}).map(([k, v]) => { |
|
|
|
this.currentPeoples = Object.entries(currentStandard['ingredient'] ?? {}).map(([k, v]) => { |
|
|
|
return { |
|
|
|
label: k, |
|
|
|
value: k, |
|
|
|
checked: this.menu?.crows?.includes(k), |
|
|
|
...(v as any), |
|
|
|
}; |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
if (this.client) { |
|
|
|
return; |
|
|
|
return |
|
|
|
} |
|
|
|
this.api.getOrgList({ vendors: currentStandard["vendors"] }).subscribe((res) => { |
|
|
|
this.currentOrgs = res.map((i) => ({ |
|
|
|
...i, |
|
|
|
value: String(i.id), |
|
|
|
label: i.name, |
|
|
|
})); |
|
|
|
}); |
|
|
|
this.currentOrgs = this.allOrgs.filter((f) => currentStandard['vendors'].includes(f['id'])) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
checkboxToValue(data: any[]) { |
|
|
|
return data |
|
|
|
.reduce((a, c) => { |
|
|
|
return c["checked"] ? a.concat(c.value) : a; |
|
|
|
return c['checked'] ? a.concat(c.value) : a |
|
|
|
}, [] as string[]) |
|
|
|
.join(","); |
|
|
|
.join(',') |
|
|
|
} |
|
|
|
|
|
|
|
onSubmit() { |
|
|
|
if (Utils.validateFormGroup(this.formGroup)) { |
|
|
|
if (!this.meals.some((s) => s["checked"])) { |
|
|
|
this.msg.error("请选择餐次"); |
|
|
|
return; |
|
|
|
if (!this.meals.some((s) => s['checked'])) { |
|
|
|
this.msg.error('请选择餐次') |
|
|
|
return |
|
|
|
} |
|
|
|
if (!this.weekday.some((s) => s["checked"])) { |
|
|
|
this.msg.error("请选择天数"); |
|
|
|
return; |
|
|
|
if (!this.weekday.some((s) => s['checked'])) { |
|
|
|
this.msg.error('请选择天数') |
|
|
|
return |
|
|
|
} |
|
|
|
if (!this.currentPeoples.some((s) => s["checked"])) { |
|
|
|
this.msg.error("请选择人群显示"); |
|
|
|
return; |
|
|
|
if (!this.currentPeoples.some((s) => s['checked'])) { |
|
|
|
this.msg.error('请选择人群显示') |
|
|
|
return |
|
|
|
} |
|
|
|
this.submitLoading = true; |
|
|
|
this.submitLoading = true |
|
|
|
|
|
|
|
this.api |
|
|
|
.saveMenu({ |
|
|
|
...this.formGroup.value, |
|
|
|
crows: this.checkboxToValue(this.currentPeoples), |
|
|
|
month: this.formGroup.value.month.join(","), |
|
|
|
vendors: this.formGroup.value.vendors.join(","), |
|
|
|
month: this.formGroup.value.month.join(','), |
|
|
|
vendors: this.formGroup.value.vendors.join(','), |
|
|
|
meals: this.checkboxToValue(this.meals), |
|
|
|
day: this.checkboxToValue(this.weekday), |
|
|
|
}) |
|
|
|
.pipe( |
|
|
|
finalize(() => { |
|
|
|
this.submitLoading = false; |
|
|
|
}) |
|
|
|
this.submitLoading = false |
|
|
|
}), |
|
|
|
) |
|
|
|
.subscribe((res) => { |
|
|
|
this.msg.success(res.desc); |
|
|
|
this.msg.success(res.desc) |
|
|
|
|
|
|
|
let id = this.menu?.id; |
|
|
|
let id = this.menu?.id |
|
|
|
|
|
|
|
if (Array.isArray(res.body)) { |
|
|
|
id = res.body.join(","); |
|
|
|
this.router.navigate(["/ingredient/item/form/", id]); |
|
|
|
id = res.body.join(',') |
|
|
|
this.router.navigate(['/ingredient/item/form/', id]) |
|
|
|
} |
|
|
|
|
|
|
|
this.onSave.emit(id); |
|
|
|
}); |
|
|
|
this.onSave.emit(id) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|