import { UntypedFormGroup } from '@angular/forms'; import { SampleSchemaEntryQueryDto, SampleSchemaDto, DataImportInput } from './../../qmp-model'; import { Component, OnInit, ViewChild } from '@angular/core'; import { RhAuxiliaryBasicInformationService } from '../auxiliary-basic-information.service'; import { DataResultT, RhSelectableDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, RhBaseDto, OpMode, RhSafeAny, RhDynamicInterfaceDto, RhSelectItem, OpResult, RhDynamicComponentFieldType } from 'rh-base/model'; import { MsgHelper, ObjectHelper, RhApiUrlsService } from 'rh-base/core'; import { RhPortType } from 'rh-base/model'; import { DynamicFormComponent } from 'rh-base/shared'; import { NzUploadFile } from 'ng-zorro-antd/upload'; import { NzModalService } from 'ng-zorro-antd/modal'; import { firstValueFrom, Observable, Observer } from 'rxjs'; @Component({ selector: 'rh-sample-schema-management', templateUrl: './sample-schema-management.component.html', styleUrls: ['./sample-schema-management.component.less'] }) export class RhSampleSchemaManagementComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; editDto: RhBaseDto = {}; dataLoading = false; dataresult!: DataResultT; isShowEditModal = false; tabType = 'edit'; defaultFieldDataset: RhDynamicComponentFieldType[] = []; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as RhBaseDto)), new RhTableButtonInfoDto( 'details', '方案明细', 'default', false, 'unordered-list', false, null, (data) => this.getSampleSchemaEntryDatas(data as RhBaseDto), true, true ) ]; 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.auditedSampleSchemaDatas(), false ), new RhTableButtonInfoDto( 'Cancel', '反审核', 'primary', true, 'close-circle', false, null, () => this.cancelAuditedSampleSchemaDatas(), false ), new RhTableButtonInfoDto('import', '导入Excel', 'default', false, 'import', false, null, () => this.openImportModal(), true) ]; selectData!: RhBaseDto; startDate!: Date; endDate!: Date; // rangeDate: Date[] = []; //#region 物料表体相关 entryDatasInterfaceInfo!: RhDynamicInterfaceDto; entryDatasQueryDto!: SampleSchemaEntryQueryDto; entrySelectedIndex = 0; entryEditDto: RhBaseDto = {}; isShowEntryEditTab = false; entryTitleButton: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('newEntry', '新建', 'primary', false, 'file', false, null, () => this.openEntryDataEditTab(), true) ]; entryEditButton: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('editEntry', '编辑', 'default', false, 'edit', false, null, (data) => this.openEntryDataEditTab(data as RhBaseDto) ) ]; //#endregion // // 抽样类型 sampleTypes: RhSelectItem[] = []; selectedSampleType = ''; // 抽样方案 sampleSchemas: RhSelectItem[] = []; // 审核 selectedDatas: SampleSchemaDto[] = []; // 数据导入 isShowUploadDrawer = false; /** 上传的文件数据模型列表 */ uploadDatas: RhSafeAny; uploadFileList: NzUploadFile[] = []; uploading = false; importDatas: RhSafeAny[] = []; // 导入数据返回的表头信息 dataHeadFields: { FieldName: string; FieldDescription: string }[] = []; tableWidth = '0px'; isAddAfterDeleteAll = false; constructor(private apiUrl: RhApiUrlsService, private operator: RhAuxiliaryBasicInformationService, private modal: NzModalService) { // } ngOnInit() { this.entryDatasInterfaceInfo = this.getEntryDatasInterfaceInfo(); this.entryDatasQueryDto = SampleSchemaEntryQueryDto.create(); this.getSampleTypes(); // this.getSampleSchemas(); } //#region ********** 表头crud相关 ********** openEditTab(data?: RhBaseDto) { // this.rangeDate = []; if (data) { this.editDto = data; // this.rangeDate[0] = data.ValidStartDate; // this.rangeDate[1] = data.ValidEndDate; this.editDto['ValidDateRange'] = [data['ValidStartDate'], data['ValidEndDate']]; this.editDto.OpSign = OpMode.OpEdit; } else { this.editDto = {}; this.editDto.OpSign = OpMode.OpAdd; } this.tabType = 'edit'; this.selectedIndex = 1; } closeEditTab() { this.selectedIndex = 0; } setDatas(dataset: DataResultT) { this.dataresult = dataset; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } } dataLoadingChange(status: boolean) { this.dataLoading = status; } 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.editDto = ObjectHelper.createRhBaseDto(); 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); } } setStartAndEndDate(dates: Date[]) { if (!dates || dates?.length < 2) { return; } this.startDate = new Date(dates[0]); this.endDate = new Date(dates[1]); // console.log(this.startDate, this.endDate); } dataHandler = (data: RhBaseDto) => { data['ValidStartDate'] = this.startDate; data['ValidEndDate'] = this.endDate; return true; }; //#endregion ********** 表头crud相关 ********** // //#region ********** 表体crud相关 ********** // 获取明细表体数据查询接口的接口信息 getEntryDatasInterfaceInfo(): RhDynamicInterfaceDto { const dto = RhDynamicInterfaceDto.create(); // 端口类型获取对应的端口号 const portNum = this.apiUrl.getPortNum(RhPortType.emp); dto.interfaceType = 'post'; dto.port = portNum; dto.controllerName = 'AuxiliaryBasicInfoApi'; dto.interfaceName = 'GetSampleSchemaEntryDatas'; return dto; } // 获取物料明细表体数据 getSampleSchemaEntryDatas(dto: RhBaseDto) { this.selectData = dto; this.entryDatasQueryDto.SampleSchemaCode = dto['SampleSchemaCode']; this.entryDatasQueryDto.AqlValue = dto['AqlValue']; this.openEntryDatasTab(); } openEntryDatasTab() { this.tabType = 'entry'; this.selectedIndex = 1; } closeEntryDatasTab() { if (this.entrySelectedIndex) { this.closeEntryEditTab(); } this.selectedIndex = 0; } // 新建/编辑 openEntryDataEditTab(data?: RhBaseDto) { if (data) { this.entryEditDto = data; this.entryEditDto.OpSign = OpMode.OpEdit; } else { this.entryEditDto = {}; ObjectHelper.MapT(this.selectData, this.entryEditDto); this.entryEditDto['AqlValue'] = this.selectData['AqlValue']; this.entryEditDto.OpSign = OpMode.OpAdd; } this.entrySelectedIndex = 1; } closeEntryEditTab() { this.entrySelectedIndex = 0; } onStoreEntryData(result: DataResultT) { if (result && result.Success) { const msg = this.entryEditDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; MsgHelper.ShowSuccessMessage(`${msg}数据成功!`); if (this.entryEditDto.OpSign === OpMode.OpEdit) { this.entrySelectedIndex = 0; } this.entryEditDto = ObjectHelper.createRhBaseDto(); // this.getDatas(); this.entryDatasInterfaceInfo = { ...this.entryDatasInterfaceInfo }; } else { MsgHelper.ShowWarningModal(`存储数据失败!${result.Message}`); } } // 当选择国检的时候新建 选择抽样方案编码 赋值抽样方案名称 SampleSchemaCodeChange(e: { value: RhBaseDto; item: RhBaseDto; controlName: string }, formCroup: UntypedFormGroup) { if (e.controlName === 'SampleSchemaCode') { formCroup.get('SampleSchemaName')?.setValue(e?.item?.['Text'] || null); } } //#endregion ********** 表体crud相关 ********** // // 获取抽样类型 getSampleTypes() { this.apiUrl .getSettingEntry({ moduleKey: 'QualityManagement', groupKey: 'AuxiliaryBasic', paraCode: 'SampleType' }) .subscribe((result) => { if (result && result.Success) { this.sampleTypes = result.Attach; } else { this.sampleTypes = []; } }); } // 判断抽样类型 sampleTypeChange(type: string) { this.selectedSampleType = type; } // 获取抽样方案 getSampleSchemas() { this.apiUrl .getSettingEntry({ moduleKey: 'QualityManagement', groupKey: 'AuxiliaryBasic', paraCode: 'SampleSchema' }) .subscribe((result) => { if (result && result.Success) { this.sampleSchemas = result.Attach; } else { this.sampleSchemas = []; } }); } // 抽样方案审核 auditedSampleSchemaDatas() { const dto = this.selectedDatas.find((ele) => ele.DataStatus !== 'Created'); if (dto) { MsgHelper.ShowWarningMessage(`选中的数据中有状态不为 “创建” 的数据,审核失败!`); } else { this.operator.AuditedSampleSchemaDatas(this.selectedDatas).subscribe((result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage(`审核成功`); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`审核失败!${result.Message}`); } }); } } // 抽样方案反审核 cancelAuditedSampleSchemaDatas() { if (this.selectedDatas.some((ele) => ele.DataStatus !== 'Audited')) { MsgHelper.ShowWarningMessage(`选中的数据中有状态不为 “已审核” 的数据,反审核失败!`); } else { this.operator.CancelAuditedSampleSchemaDatas(this.selectedDatas).subscribe((result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage(`反审核成功`); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`反审核失败!${result.Message}`); } }); } } selectedDatasChange(dtos: RhSelectableDto[]) { if (dtos) { this.selectedDatas = []; dtos.forEach((ele) => { this.selectedDatas.push(ele as SampleSchemaDto); }); } else { this.selectedDatas = []; } } //#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 RhSafeAny; 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 = 'SampleSchemaImporter'; 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; } // 保存数据 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.StoreSampleSchemaDatas(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 // }