配餐项目前端文件
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.

225 lines
5.1 KiB

2 years ago
import { Component, OnInit, TemplateRef, ViewChild } from "@angular/core";
2 years ago
import { FormControl, FormGroup } from "@angular/forms";
import { NzDrawerRef, NzDrawerService } from "ng-zorro-antd/drawer";
2 years ago
import { AnyObject, OrgDTO, TableListOption } from "@cdk/public-api";
import { DishFormComponent } from "@client/app/components";
2 years ago
import { ApiService } from "@cdk/services";
2 years ago
import {
Subject,
debounceTime,
distinctUntilChanged,
filter,
finalize,
lastValueFrom,
switchMap,
takeUntil,
tap,
} from "rxjs";
import { NzModalService } from "ng-zorro-antd/modal";
import { NzMessageService } from "ng-zorro-antd/message";
import { ResponseType } from "@cdk/types";
import { PrintComponent } from "@cdk/shared/components";
2 years ago
@Component({
selector: "app-dish",
templateUrl: "./dish.component.html",
styleUrls: ["./dish.component.less"],
})
2 years ago
export class DishComponent {
constructor(
private drawer: NzDrawerService,
private api: ApiService,
private modal: NzModalService,
private msg: NzMessageService
) {}
@ViewChild("formFooterTpl") formFooterTpl!: TemplateRef<{}>;
2 years ago
2 years ago
@ViewChild("print") printRef!: PrintComponent;
2 years ago
2 years ago
private drawerRef?: NzDrawerRef;
private destroy$ = new Subject<void>();
private orgSearch$ = new Subject<string>();
public globalEnum = this.api.globalEnum;
public printData: any | null;
2 years ago
public tableList = new TableListOption(this.fetchData.bind(this), {
selectable: true,
2 years ago
frontPagination: false,
2 years ago
});
public queryForm = new FormGroup({
2 years ago
keyword: new FormControl(""),
mark: new FormControl(""),
vendors: new FormControl(""),
2 years ago
});
public selectedIds: string[] = [];
2 years ago
tableOrg: { [k: number]: OrgDTO } = {};
tableFoods: { [k: string]: any } = {};
listOfOption: Array<{ value: number; text: string }> = [];
nzFilterOption = (): boolean => true;
submitLoading = false;
2 years ago
ngOnInit(): void {
this.initTableList();
2 years ago
2 years ago
this.tableList.getState$.pipe(takeUntil(this.destroy$)).subscribe((res) => {
this.selectedIds = res.selectedKeys as Array<string>;
});
}
2 years ago
searchOrg = (k: string) => {
this.orgSearch$.next(k);
};
2 years ago
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
initTableList() {
this.tableList.scroll = { x: null };
this.tableList = this.tableList.setColumns([
2 years ago
{ key: "icon", title: "菜品图片", width: "66px" },
2 years ago
{ key: "name", title: "菜品名称" },
2 years ago
{ key: "marks", title: "菜品标签" },
{ key: "poly", title: "烹饪方式" },
2 years ago
{ key: "ingredient", title: "食材及含量", width: "30%" },
2 years ago
]);
this.tableList = this.tableList.setOptions([
{
title: "打印营养标签",
premissions: [],
onClick: this.printTag.bind(this),
2 years ago
},
{
title: "编辑",
premissions: [],
onClick: this.showFoodForm.bind(this),
},
{
title: "删除",
premissions: [],
onClick: this.deleteItem.bind(this),
},
]);
}
2 years ago
fetchData(query: AnyObject, pager: AnyObject) {
return this.api.getDishPage(pager, query).pipe(
tap((res) => {
this.getTableColumData(res);
})
);
}
getTableColumData(res: ResponseType) {
if (Array.isArray(res.body.content)) {
const vendors = res.body.content.map((i: any) => i.vender);
const foodKeys = new Set(
res.body.content.reduce((a: string[], c: any) => {
return a.concat(c.ingredient.map((i: any) => i.key));
}, [] as string[])
);
if (foodKeys.size > 0) {
this.api.getFoodList({ keys: Array.from(foodKeys) }).subscribe((foods) => {
if (Array.isArray(foods.body)) {
this.tableFoods = foods.body.reduce((a, c) => {
return {
...a,
[c.key]: c,
};
}, {} as AnyObject);
}
});
}
}
}
printTag(v?: any) {
2 years ago
this.msg.loading("数据请求中,请不要刷新页面", {
nzDuration: 0,
});
const ids = v ? [v.id] : this.selectedIds;
2 years ago
this.api
.getDishLabel(ids)
2 years ago
.pipe(
finalize(() => {
setTimeout(() => {
this.msg.remove();
}, 1000);
})
)
.subscribe((res) => {
this.printData = res.body;
2 years ago
this.printRef.print();
});
2 years ago
}
showFoodForm(data?: any) {
this.drawerRef = this.drawer.create({
nzTitle: data ? "编辑菜品" : "新增菜品",
nzWidth: 700,
nzContent: DishFormComponent,
nzContentParams: {
data,
orgs: Object.values(this.tableOrg),
foods: Object.values(this.tableFoods),
},
nzFooter: this.formFooterTpl,
});
}
cancelForm() {
this.drawerRef?.close();
}
onSubmit() {
if (this.drawerRef) {
const com = this.drawerRef.getContentComponent() as DishFormComponent;
const val = com.getValues();
if (val) {
this.submitLoading = true;
this.api
.saveDish(val)
.pipe(
finalize(() => {
this.submitLoading = false;
})
)
.subscribe((res) => {
this.msg.success(res.desc);
this.tableList.run();
this.cancelForm();
});
}
}
2 years ago
}
deleteItem(v?: any) {
const ids = v ? [v.id] : this.selectedIds;
this.modal.confirm({
nzTitle: "警告",
2 years ago
nzContent: `是否要删除${ids.length}个菜品?`,
2 years ago
nzOkDanger: true,
2 years ago
nzOnOk: async () => {
const res = await lastValueFrom(this.api.deleteDish(ids));
this.msg.success(res.desc);
this.tableList.run();
},
2 years ago
});
}
}