import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core' import { SharedModule } from 'app/shared/shared.module' import { ApiService } from 'app/services' import { FormBuilder, FormGroup } from '@angular/forms' import { AnyObject, TableOption } from 'app/shared/components/server-paginated-table' import { NzSafeAny } from 'ng-zorro-antd/core/types' import { NzModalService } from 'ng-zorro-antd/modal' import { NzMessageService } from 'ng-zorro-antd/message' import { NzDrawerRef, NzDrawerService } from 'ng-zorro-antd/drawer' import { lastValueFrom } from 'rxjs' import { FormValidators, Utils } from 'app/utils' import { SelectUserByOrgComponent, buildCateTree } from 'app/components' import { ASSET_STATUS, MAX_PAGE_SIZE } from 'app/constants' @Component({ selector: 'app-stockaking-job', standalone: true, imports: [SharedModule, SelectUserByOrgComponent], templateUrl: './stockaking-job.component.html', styleUrl: './stockaking-job.component.less', }) export class StockakingJobComponent { constructor( private modal: NzModalService, private msg: NzMessageService, private drawer: NzDrawerService, private api: ApiService, private fb: FormBuilder, ) {} table = new TableOption(this.fetchData.bind(this)) queryForm!: FormGroup createForm!: FormGroup drawerRef?: NzDrawerRef @ViewChild('drawerFooterTpl') drawerFooterTpl!: TemplateRef @ViewChild('formContentTpl') formContentTpl!: TemplateRef ASSET_STATUS = ASSET_STATUS assetCategoryTree: NzSafeAny[] = [] positionTree: NzSafeAny[] = [] warehouse: NzSafeAny[] = [] companyTree: NzSafeAny[] = [] orgTree: NzSafeAny[] = [] initCreateForm() { this.createForm = this.fb.group({ stocktakingJobId: [], name: ['', [FormValidators.required('请输入')]], notes: ['', []], stocktakingUserId: [[], []], head: [[], []], fullStocktaking: [1, []], stocktakingStartDate: [null, []], stocktakingEndDate: [null, []], categoryId: [null, []], warehouseId: [null, []], positionId: [null, []], ownCompanyId: [null, []], useOrganizationId: [null, []], useUserId: [null, []], assetStatus: [0, []], }) } initQueryForm() { this.queryForm = this.fb.group({ name: [], status: [], notes: [], }) } ngOnInit(): void { this.table .setConfig({ selectable: true, rowKey: 'stocktakingJobId', }) .setColumn([ { key: 'name', title: '名称' }, { key: 'fullStocktaking', title: '全员盘点' }, { key: 'status', title: '盘点状态' }, { key: '_head', title: '负责人', width: '150px' }, { key: '_stocktakingUser', title: '盘点人', width: '150px' }, { key: 'countPending', title: '待盘点', width: '100px' }, { key: 'countCompleted', title: '已盘点', width: '100px' }, { key: 'countLoss', title: '盘亏', width: '100px' }, { key: 'countSurplus', title: '盘盈', width: '100px' }, { key: 'countException', title: '异常数据', width: '100px' }, { key: '_category', title: '资产分类' }, { key: '_ownCompany', title: '所属公司' }, { key: '_useOrganization', title: '使用公司/部门' }, { key: '_useUser', title: '保管人', width: '150px' }, { key: '_warehouse', title: '仓库' }, { key: '_position', title: '存放位置' }, { key: 'stocktakingStartDate', title: '购置开始日期', width: '180px' }, { key: 'stocktakingEndDate', title: '购置结束日期', width: '180px' }, { key: 'createTime', title: '创建时间', width: '180px' }, { key: 'createUser', title: '创建人', width: '150px' }, { key: 'notes', title: '备注' }, ]) .setRowOperate([ { title: '明细', onClick: this.onCreate.bind(this) }, // { title: '详情', onClick: this.onDetail.bind(this) }, { title: '编辑', onClick: this.onCreate.bind(this) }, { title: '删除', onClick: this.deleteItem.bind(this) }, ]) this.initQueryForm() this.initCreateForm() this.api.getBasicCategoryTree().subscribe((res) => { this.assetCategoryTree = Utils.buildTree(res.body, 'categoryId', 'categoryName') }) this.api.getBasicPositionTree().subscribe((res) => { this.positionTree = Utils.buildTree(res.body, 'positionId', 'name') }) this.api.getBasicWarehousePage({ pageSize: MAX_PAGE_SIZE, pageNum: 1 }).subscribe((r) => { this.warehouse = r.body.rows.map((i: NzSafeAny) => { return { label: i.name, value: i.warehouseId, } }) }) this.api.getOrgTree().subscribe((res) => { const c: NzSafeAny[] = [] const o: NzSafeAny[] = [] res.body.forEach((i: NzSafeAny) => { if (i.organizationType === '0') { c.push(i) } if (['1', '0'].includes(i.organizationType)) { o.push(i) } }) this.orgTree = Utils.buildTree(o, 'organizationId', 'organizationName') this.companyTree = Utils.buildTree(c, 'organizationId', 'organizationName') }) } fetchData(p: {}, q: AnyObject) { return this.api.getStocktakingJobPage({ ...p, ...q }) } onCreate(data?: NzSafeAny) { if (data) { this.createForm.patchValue({ ...data, stocktakingUserId: data._stocktakingUser?.userId ? [data._stocktakingUser?.userId] : [], head: data._head?.userId ? [data._head?.userId] : [], useUserId: data._useUser?.userId ? [data._useUser?.userId] : [], categoryId: data._category?.categoryId + '', positionId: data._position?.positionId + '', ownCompanyId: data._ownCompany?.organizationId + '', useOrganizationId: data._useOrganization?.organizationId + '', warehouseId: data._warehouse?.warehouseId, }) } this.drawerRef = this.drawer.create({ nzTitle: data ? '编辑盘点任务' : '新增盘点任务', nzContent: this.formContentTpl, nzFooter: this.drawerFooterTpl, nzWidth: 600, nzOnCancel: this.onCancel.bind(this), }) } onDetail(data: NzSafeAny) { this.drawerRef = this.drawer.create({ nzTitle: data ? '编辑盘点任务' : '新增盘点任务', nzContent: this.formContentTpl, nzFooter: this.drawerFooterTpl, nzWidth: 600, nzOnCancel: this.onCancel.bind(this), }) } onConfirm() { if (FormValidators.validateFormGroup(this.createForm)) { const { value } = this.createForm this.api .saveStocktakingJob({ ...value, stocktakingUserId: value.stocktakingUserId?.[0], head: value.head?.[0], useUserId: value.useUserId?.[0], }) .subscribe((res) => { this.msg.success(res.desc) this.onCancel() this.table.ref.reload() }) } } async onCancel() { this.drawerRef?.close() this.createForm.reset({ fullStocktaking: 1, }) } deleteItem(item: NzSafeAny) { this.modal.confirm({ nzTitle: '警告', nzContent: '是否要删除该盘点任务?', nzOnOk: async () => { const res = await lastValueFrom(this.api.deleteStocktakingJob([item.stocktakingJobId])) this.msg.success(res.desc) this.table.ref.reload() }, }) } onBegin() { const ids = Array.from(this.table.ref.selected).map((i) => Number(i)) this.modal.confirm({ nzTitle: '警告', nzContent: '是否要开始该盘点任务?', nzOnOk: async () => { const res = await lastValueFrom(this.api.beginStocktakingJob(ids)) this.msg.success(res.desc) this.table.ref.reload() }, }) } onStop() { const ids = Array.from(this.table.ref.selected).map((i) => Number(i)) this.modal.confirm({ nzTitle: '警告', nzContent: '是否要结束该盘点任务?', nzOnOk: async () => { const res = await lastValueFrom(this.api.stopStocktakingJob(ids)) this.msg.success(res.desc) this.table.ref.reload() }, }) } }