import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, RhBaseDto, OpMode, RhSafeAny, RhLoginedUserDto, OpResult, RhDynamicComponentFieldType } from 'rh-base/model'; import { RhAuxiliaryBasicInformationService } from '../auxiliary-basic-information.service'; import { DataImportInput, InspectItemDto } from '../../qmp-model'; import { MsgHelper, ObjectHelper, RhStorageService } from 'rh-base/core'; import { DynamicFormComponent, RhSharedService } from 'rh-base/shared'; import { NzUploadFile } from 'ng-zorro-antd/upload'; import { NzModalService } from 'ng-zorro-antd/modal'; import { firstValueFrom, Observable, Observer } from 'rxjs'; import { UntypedFormGroup, Validators } from '@angular/forms'; import { format } from 'date-fns'; @Component({ selector: 'rh-inspect-item-management', templateUrl: './inspect-item-management.component.html', styleUrls: ['./inspect-item-management.component.less'] }) export class RhInspectItemManagementComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; editDto: RhBaseDto = {}; dataLoading = false; dataresult!: DataResultT; isShowEditModal = false; defaultFieldDataset: RhDynamicComponentFieldType[] = []; editFormVisibleSetting = {}; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as RhBaseDto)) ]; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openEditTab(), true) ]; extraButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('examine', '审核', 'default', false, 'check-circle', false, null, () => this.auditedInspectItemDatas(), false), new RhTableButtonInfoDto( 'Cancel', '反审核', 'primary', true, 'close-circle', false, null, () => this.cancelAuditedInspectItemDatas(), false ), new RhTableButtonInfoDto('import', '导入Excel', 'default', false, 'import', false, null, () => this.openImportModal(), true) ]; collapseQueryDto = {}; defaultQueryDto = {}; // 添加分组 groupAddDto: RhBaseDto = {}; isShowGroupAddModal = false; // 当前登陆用户 currentUser!: RhLoginedUserDto; // 审核按钮 selectedDatas: RhSelectableDto[] = []; selecedItemDatas: InspectItemDto[] = []; tabType: string | null = null; // 数据导入 isShowUploadDrawer = false; /** 上传的文件数据模型列表 */ uploadDatas: RhSafeAny; uploadFileList: NzUploadFile[] = []; uploading = false; importDatas: RhSafeAny[] = []; // 导入数据返回的表头信息 dataHeadFields: { FieldName: string; FieldDescription: string }[] = []; tableWidth = '0px'; isAddAfterDeleteAll = false; constructor( public sharedSer: RhSharedService, public storageSer: RhStorageService, private operator: RhAuxiliaryBasicInformationService, private modal: NzModalService ) { // } ngOnInit() { this.currentUser = this.storageSer.getUserSession()?.User as RhLoginedUserDto; this.initGroupAddDto(); this.editDto = this.initEditDto(); } doSomething(form: UntypedFormGroup) { if (this.editDto['InspectMode'] === 'Quantitative') { form.get('UpperTolerance')?.setValidators(Validators.required); form.get('LowerTolerance')?.setValidators(Validators.required); } else { form.get('UpperTolerance')?.clearValidators(); form.get('UpperTolerance')?.patchValue(null); form.get('LowerTolerance')?.clearValidators(); form.get('LowerTolerance')?.patchValue(null); } } openEditTab(data?: RhBaseDto) { if (data) { this.editDto = data; this.editFormVisibleSetting = { UpperTolerance: data['InspectMode'] == 'Quantitative' ? true : false, LowerTolerance: data['InspectMode'] == 'Quantitative' ? true : false }; this.editDto.OpSign = OpMode.OpEdit; } else { this.editDto = this.initEditDto(); this.editDto['InspectItemCode'] = 'I' + format(new Date(Date.now()), 'yy.MM.dd.HHmmss').toString().replace(/\./g, ''); this.editDto.OpSign = OpMode.OpAdd; } this.selectedIndex = 1; this.tabType = 'edit'; } closeEditTab() { this.selectedIndex = 0; this.tabType = null; } setDatas(dataset: DataResultT) { this.dataresult = dataset; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } } dataLoadingChange(status: boolean) { this.dataLoading = status; } private initEditDto(): RhBaseDto { const editDto: RhBaseDto = {}; editDto.OpSign = OpMode.OpAdd; return editDto; } onStoreData(result: DataResultT) { if (result && result.Success) { const msg = this.editDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; MsgHelper.ShowSuccessMessage(`${msg}数据成功!`); if (this.editDto.OpSign === OpMode.OpEdit) { this.selectedIndex = 0; this.tabType = null; } this.editDto = ObjectHelper.createRhBaseDto(); const oldValue = result.httpRequestPara as InspectItemDto; let tempDto: RhSafeAny = this.initEditDto(); tempDto = oldValue; tempDto['InspectItemCode'] = 'I' + format(new Date(Date.now()), 'yy.MM.dd.HHmmss').toString().replace(/\./g, ''); tempDto['InspectItemName'] = null; tempDto['StandardValue'] = null; this.editDto = tempDto; this.getDatas(); } else { MsgHelper.ShowWarningModal(`存储数据失败!${result.Message}`); } } setInitFieldDatset(item: CodeClassModel) { const tempDatas: RhDynamicComponentFieldType[] = []; item.CodeProperties.forEach((ele) => { const temp = RhDynamicTableFieldDto.create(); temp.name = ele.PropertyName; temp.displayName = ele.Descirption; tempDatas.push(temp); }); this.defaultFieldDataset = [...tempDatas]; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } //调用子组件中的接口 private getDatas(option?: RhTableQueryOptionDto) { if (this.queryFormCmt) { this.queryFormCmt.submitDefaultForm(null, option); } } initDefaultQueryDto(dto: RhSafeAny) { this.collapseQueryDto = dto; this.defaultQueryDto = { ...this.defaultQueryDto, ...this.collapseQueryDto }; } getValueChange() { this.defaultQueryDto = { ...this.defaultQueryDto, ...this.collapseQueryDto }; setTimeout(() => { this.getDatas(); }); } // 添加分组 openGroupAddModal() { this.isShowGroupAddModal = true; } closeGroupAddModal() { this.isShowGroupAddModal = false; } initGroupAddDto() { this.groupAddDto.OpSign = OpMode.OpAdd; this.groupAddDto['ModuleKey'] = 'QualityManagement'; this.groupAddDto['GroupKey'] = 'AuxiliaryBasic'; this.groupAddDto['ParameterCode'] = 'InspectItemGroup'; this.groupAddDto['ParameterType'] = 'String'; } onStoreGroupData(result: DataResultT) { if (result && result.Success) { MsgHelper.ShowSuccessMessage(`分组新建成功!`); this.initGroupAddDto(); } else { MsgHelper.ShowWarningModal(`分组新建失败!${result.Message}`); } } // ********** 审核和反审核 ********** // 检验项目审核 auditedInspectItemDatas() { const dto = this.selectedDatas.find((ele) => ele['DataStatus'] !== 'Created'); if (dto) { MsgHelper.ShowWarningMessage(`选中的数据中有状态不为 “创建” 的数据,审核失败!`); } else { this.operator.AuditedInspectItemDatas(this.selecedItemDatas).subscribe((result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage(`审核成功`); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`审核失败!${result.Message}`); } }); } } // 检验项目反审核 cancelAuditedInspectItemDatas() { if (this.selectedDatas.some((ele) => ele['DataStatus'] !== 'Audited')) { MsgHelper.ShowWarningMessage(`选中的数据中有状态不为 “已审核” 的数据,反审核失败!`); } else { this.operator.CancelAuditedInspectItemDatas(this.selecedItemDatas).subscribe((result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage(`反审核成功`); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`反审核失败!${result.Message}`); } }); } } selectedDatasChange(dtos: RhSelectableDto[]) { if (dtos) { this.selecedItemDatas = []; this.selectedDatas = dtos; this.selectedDatas.forEach((ele) => { this.selecedItemDatas.push(ele as InspectItemDto); }); } else { this.selecedItemDatas = []; } } //#region ********** 数据导入 ********** //打开文件上传modal openImportModal() { this.isShowUploadDrawer = true; } closeImportModal() { this.isShowUploadDrawer = false; this.uploadFileList = []; } beforeUpload = (file: NzUploadFile) => { if (this.uploadFileList.length >= 1) { MsgHelper.ShowWarningMessage('每次只能上传一个文件!'); return false; } return new Observable((observer: Observer) => { const reader = new FileReader(); reader.readAsDataURL(file as RhSafeAny); reader.onload = () => { const importQueryDto = DataImportInput.create(); importQueryDto.FileStreamString = reader.result as any; this.uploadDatas = importQueryDto; // 只有一个文件 this.uploadFileList = this.uploadFileList.concat(file); // this.uploadFileList = [file]; observer.next(false); observer.complete(); }; reader.onerror = (e) => { MsgHelper.ShowWarningModal(`读取文件失败!${e}`); observer.next(false); observer.complete(); }; }); }; handleImport() { this.uploading = true; this.uploadDatas.BusinessCode = 'InspectItemInfoImporter'; this.operator.DataImport(this.uploadDatas).subscribe( (result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage('文件导入成功!'); this.importDatas = result.Attach; this.dataHeadFields = result.DataHeadFields as RhSafeAny; this.tableWidth = this.dataHeadFields.length * 120 + 100 + 'px'; this.closeImportModal(); this.getDatas(); this.openImportTab(); } else { MsgHelper.ShowWarningModal(`文件导入失败!${result.Message}`); } this.uploading = false; }, () => (this.uploading = false) ); } // 文件导入tab openImportTab() { this.tabType = 'import'; this.isAddAfterDeleteAll = false; this.selectedIndex = 1; } closeImportTab() { this.tabType === null; this.selectedIndex = 0; } changeInspectMode(mode: string, form: UntypedFormGroup) { if (mode === 'Quantitative') { form.get('UpperTolerance')?.setValidators(Validators.required); form.get('LowerTolerance')?.setValidators(Validators.required); } else { form.get('UpperTolerance')?.clearValidators(); form.get('UpperTolerance')?.patchValue(null); form.get('LowerTolerance')?.clearValidators(); form.get('LowerTolerance')?.patchValue(null); } } // 保存数据 async storeImportedDatas() { if (this.importDatas.some((f) => !f.Success)) { MsgHelper.ShowWarningMessage('请修改错误或重新导入后保存!'); return; } let result: OpResult | null = null; if (this.isAddAfterDeleteAll) { this.importDatas.forEach((f) => (f.OpSign = OpMode.OpAddAfterDeleteAll)); } else { this.importDatas.forEach((f) => (f.OpSign = OpMode.OpAdd)); } try { result = await firstValueFrom(this.operator.StoreInspectItemDatas(this.importDatas)); } catch (error) { MsgHelper.ShowErrorMessage(`保存错误!${error}`); } if (result) { if (result.Success) { MsgHelper.ShowSuccessMessage('保存成功!'); this.importDatas = []; this.closeImportTab(); } else { MsgHelper.ShowWarningMessage(`保存失败!${result.Message}`); } } } // 清空数据 clearImportedPlanDatas() { MsgHelper.ShowConfirmModal('是否继续', '是否清空所有导入的模板数据?', () => { this.importDatas = []; MsgHelper.ShowSuccessMessage('数据清空成功!'); }); } changeIsAddAfterDeleteAll(value: boolean) { if (value) { this.modal.confirm({ nzTitle: '警告!', nzContent: '若勾选此项,将会进行不可逆的删除,是否确认?', nzOnCancel: () => { this.isAddAfterDeleteAll = false; }, nzOkType: 'primary', nzOkDanger: true, nzIconType: 'warning' }); } } removeImportTempData(index: number) { MsgHelper.ShowConfirmModal('是否继续', '是否移除选中的模板数据?', () => { this.importDatas.splice(index, 1); this.importDatas = [...this.importDatas]; }); } getDataType(data: string) { if (data) { const tmp = data.toString().trim().toLowerCase(); const dateReg = new RegExp(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?/, 'i'); if (['true', 'false'].includes(tmp)) { return 'boolean'; } else if (dateReg.test(data)) { return 'date'; } } return 'string'; } //#endregion // }