import { InspectUploadCode, SimpleTreeDto } from '../../qmp-model'; import { FormHelper, RhApiUrlsService, MsgHelper } from 'rh-base/core'; import { RhSelectItem, RhDynamicInterfaceDto } from 'rh-base/model'; import { Component, OnInit, ViewChild, Input, Output, EventEmitter } from '@angular/core'; import { IncomingInspectOrderDataCollectionDto, IncomingInspectItemDataCollectionDto, IncomingInspectGroupDataCollectionDto, IncomingInspectItemDataValueDto, IQualityUploadTypeDto, RhInspectConfigType } from '../../qmp-model'; import { UntypedFormGroup, Validators, UntypedFormBuilder } from '@angular/forms'; import { debounce } from 'lodash'; import { RhInspectGroupItemDisplayComponent } from '../inspect-typin/inspect-group-item-display/inspect-group-item-display.component'; import { isNotNil } from 'ng-zorro-antd/core/util'; import { NzInputNumberComponent } from 'ng-zorro-antd/input-number'; @Component({ selector: 'rh-incoming-item-mode-inspect', templateUrl: './incoming-item-mode-inspect.component.html', styleUrls: ['./incoming-item-mode-inspect.component.less'] }) export class RhIncomingItemModeInspectComponent implements OnInit { @ViewChild('inspectValueInput', { static: false }) inspectValueInput!: NzInputNumberComponent; @ViewChild('goodQtyInput', { static: false }) goodQtyInput!: NzInputNumberComponent; @ViewChild('badQtyInput', { static: false }) badQtyInput!: NzInputNumberComponent; @ViewChild(RhInspectGroupItemDisplayComponent) groupItemDisplayComp!: RhInspectGroupItemDisplayComponent; @Input() rhUploadType: 'IqcAttachFileModule' | 'OqcAttachFileModule' = 'IqcAttachFileModule'; @Input() rhData!: IncomingInspectOrderDataCollectionDto; /** 数据暂存 */ @Output() rhTempStorage = new EventEmitter(); @Output() rhClose = new EventEmitter(); @Input() rhInspectCategory: 'incommingInspect' | 'productInspect' = 'incommingInspect'; @Input() rhInspectSetting!: RhInspectConfigType; // dataloading = false; // itemDataCollectionDataset_Source: IncomingInspectItemDataCollectionDto[] = []; groupCollectionDataset!: IncomingInspectGroupDataCollectionDto[]; currentGroupCollectionDataset!: IncomingInspectGroupDataCollectionDto; /** (第二层模型数据) */ itemDataCollectionDataset: IncomingInspectItemDataCollectionDto[] = []; /** 当前正在录入数据的产品 */ currentInputProduct!: IncomingInspectItemDataValueDto; /** 当前正在录入数据的检验项目 */ currentInputInspectItem: IncomingInspectItemDataCollectionDto | null = null; /** 检验模式数据 */ inspectModeDatas: RhSelectItem[] = []; /** 检验方法数据 */ inspectMethodDatas: RhSelectItem[] = []; entryForm!: UntypedFormGroup; entryDto!: IncomingInspectItemDataValueDto; entryStoreLoading = false; /** 定量模式下的判定结果 */ judgeResult!: boolean; // 产品列表 editInspectDataset: IncomingInspectItemDataValueDto[] = []; indexOne!: number; indexTwo!: number; inspectGroupDataset: RhSelectItem[] = []; currentInspectGroup!: string; isShowUploadModal = false; isShowPreviewModal = false; //附件录入相关 uploadCode!: InspectUploadCode | string; selectFileData!: IQualityUploadTypeDto; // 更改跳转逻辑,根据项目组、项目、产品index依次跳转; groupIndex = 0; itemIndex = 0; productIndex = 0; // SN录入 @Input() rhSnGetInterface!: RhDynamicInterfaceDto; // 延迟50ms计算结果,延迟时间不能太大,否则会导致录入过快,赋值方法没有执行 judgeInspectResultDebounce = debounce((data: string) => this._judgeInspectResult(data), 50); constructor(public fb: UntypedFormBuilder, public apiUrl: RhApiUrlsService) { // } ngOnInit(): void { this.getInspectMethod(); this.getInspectMode(); this.groupCollectionDataset = this.rhData.GroupDataCollectionDtos || []; this.entryDto = this.initEntryDto(); // console.log(this.rhData); this.rhData.IsItemEntry = true; // this.itemDataCollectionDataset_Source = this.rhData?.ItemDataCollectionDtos || []; // this.itemDataCollectionDataset = [...this.itemDataCollectionDataset_Source]; this.inspectGroupDataset = this.rhData.GroupDataCollectionDtos?.map( (ele) => new RhSelectItem(ele.InspectItemGroupName as string, ele.InspectItemGroupCode) ) as RhSelectItem[]; this.currentGroupCollectionDataset = this.groupCollectionDataset[0]; this.itemDataCollectionDataset = this.currentGroupCollectionDataset?.ItemDataCollectionDtos || []; if (this.itemDataCollectionDataset?.length) { // 默认选中第一条检验项目 this.itemDataCollectionDataset.forEach((ele) => (ele.select = false)); this.itemDataCollectionDataset[0].select = true; this.currentInputInspectItem = this.itemDataCollectionDataset[0]; // 初始化产品列表 this.editInspectDataset = this.currentInputInspectItem.ItemDataValueDtos || []; this.currentInputProduct = this.editInspectDataset[0]; this.entryDto = this.currentInputInspectItem?.ItemDataValueDtos?.[0] as IncomingInspectItemDataValueDto; } else { this.currentInputInspectItem = null; this.editInspectDataset = []; } this.createEntryForm(this.entryDto); this.focusValueInput(); } // 初始化检验产品列表、数据录入form initEditInspectDatas(datas: IncomingInspectItemDataValueDto[]) { datas.forEach((ele) => (ele.select = false)); datas[0].select = true; this.currentInputProduct = datas[0]; this.resetEntryForm(); // 如果是定性分析,需要处理合格数量,不良数量和检验结果; if (this.currentInputInspectItem?.InspectMode === 'Qualitative') { const goodQty = this.currentInputInspectItem?.GoodQty || 0; const badQty = this.currentInputInspectItem?.BadQty || 0; const result = badQty > 0 ? false : true; this.currentInputProduct.InspectResult = result; this.currentInputProduct['GoodQty'] = goodQty; this.currentInputProduct['BadQty'] = badQty; } this.setValueToEntryForm(this.currentInputProduct); } initEntryDto(): IncomingInspectItemDataValueDto { const temp = IncomingInspectItemDataValueDto.create(); // temp.InspectResult = true; return temp; } // 定量form createEntryForm(dto: IncomingInspectItemDataValueDto) { this.entryForm = this.fb.group({ /** 检验数据 */ InspectData: [dto.InspectData], SN: [dto.SN], /** 检验结果 */ InspectResult: [dto.InspectResult], // 以下两个字段针对的是定性项目; GoodQty: [this.currentInputInspectItem?.GoodQty || 0], BadQty: [this.currentInputInspectItem?.BadQty || 0] }); } resetEntryForm() { this.entryForm.get('InspectResult')?.patchValue(null); this.entryForm.get('InspectData')?.patchValue(null); } closeDataEnrtyModal() { this.rhClose.emit(this.rhData); } closeUploadModal() { this.isShowUploadModal = false; } openUploadModal(code: string) { this.uploadCode = code; this.selectFileData = this.initUploadFileData(code as InspectUploadCode); this.isShowUploadModal = true; } closePreviewModal() { this.isShowPreviewModal = false; } openPreviewModal(code: string) { this.uploadCode = code; this.selectFileData = this.initUploadFileData(code as InspectUploadCode); this.isShowPreviewModal = true; } // 定量模式下根据输入的检验数据,判定检验结果 judgeInspectResult(data: string) { // FIXME: 添加防抖动函数 this.judgeInspectResultDebounce(data); } private _judgeInspectResult(data: string) { if (isNotNil(data) && this.currentInputInspectItem) { // 给选中模型赋值 this.indexOne = this.itemDataCollectionDataset.findIndex( (ele) => ele.InspectOrderEntryCode === this.currentInputInspectItem?.InspectOrderEntryCode ); this.indexTwo = this.currentInputInspectItem.ItemDataValueDtos?.findIndex((ele) => ele.SortId === this.currentInputProduct.SortId); // this.collectionData.ItemDataCollectionDtos[this.indexOne].ItemDataValueDtos[this.indexTwo].InspectData = data.toString(); this.itemDataCollectionDataset[this.indexOne].ItemDataValueDtos[this.indexTwo].InspectData = data as string; // 判断检验数据结果 if ( Number.parseFloat(data) >= (this.currentInputInspectItem.MinValue as number) && Number.parseFloat(data) <= (this.currentInputInspectItem.MaxValue as number) ) { this.judgeResult = true; } else { this.judgeResult = false; } this.entryForm.get('InspectResult')?.patchValue(this.judgeResult); // this.collectionData.ItemDataCollectionDtos[this.indexOne].ItemDataValueDtos[this.indexTwo].InspectResult = this.judgeResult; this.itemDataCollectionDataset[this.indexOne].ItemDataValueDtos[this.indexTwo].InspectResult = this.judgeResult; // this.itemDataCollectionDataset = this.currentGroupCollectionDataset.ItemDataCollectionDtos; } } judgeQualitativeInspectResult(data: number) { this.entryForm.get('InspectResult')?.patchValue(data > 0 ? false : true); } selectProductChange(dto: IncomingInspectItemDataValueDto) { // this.resetEntryForm(); this.setValueToEntryForm(dto); } setValueToEntryForm(dto: IncomingInspectItemDataValueDto) { this.focusValueInput(); dto.select = true; // dto.InspectResult = dto.selectInspectItemChange === void 0 || dto.InspectResult === null ? true : dto.InspectResult; // 定量检测将检验数据设置为必填 if (this.currentInputInspectItem?.InspectMode === 'Quantitative') { this.entryForm.get('InspectData')?.setValidators(Validators.required); } else { this.entryForm.get('InspectData')?.clearValidators(); } FormHelper.SetTValueToForm(dto, this.entryForm); if (this.currentInputInspectItem?.InspectMode === 'Qualitative') { // 将所有sn都赋值给sn input const snStr = this.editInspectDataset?.length ? this.editInspectDataset .filter((ele) => ele.SN) .map((ele) => ele.SN) .join(',') : null; this.entryForm.get('SN')?.patchValue(snStr); } if (this.currentInputInspectItem?.InspectMode === 'Quantitative') { // this.judgeInspectResult(dto.InspectData); } } // 检验结果赋值 setInspectResult(result: boolean) { if (result !== void 0 && result !== null && this.itemDataCollectionDataset.length) { /** 当前正在录入的检验项目 */ this.indexOne = this.itemDataCollectionDataset.findIndex( (ele) => ele.InspectItemCode === this.currentInputInspectItem?.InspectItemCode ); /** 当前正在录入的产品 */ this.indexTwo = this.currentInputInspectItem?.ItemDataValueDtos.findIndex( (ele) => ele.SortId === this.currentInputProduct.SortId ) as number; // this.collectionData.ItemDataCollectionDtos[this.indexOne].ItemDataValueDtos[this.indexTwo].InspectResult = result; this.itemDataCollectionDataset[this.indexOne].ItemDataValueDtos[this.indexTwo].InspectResult = result; // this.itemDataCollectionDataset = this.currentGroupCollectionDataset.ItemDataCollectionDtos; // 自动跳转 if (this.rhInspectSetting?.autoSkip) { this.switchNextProduct(); } } } // 切换下一个产品数据录入 switchNextProduct() { if (!this.submitEntryForm()) { return; } // 搜索没有检测过的产品 const index = this.editInspectDataset.findIndex((ele) => !ele.check); if (index === -1) { this.editInspectDataset.forEach((ele) => (ele.check = false)); // 防止检验结果还没计算出来,就已经跳转了 setTimeout(() => { this.groupItemDisplayComp?.switchNext(); }, 100); // // 检测下一个检测项目 // this.currentInputInspectItem.check = true; // this.currentInputInspectItem.select = false; // // 搜索没有检测过的检验项目 // const inspectIndex = this.itemDataCollectionDataset.findIndex((ele) => !ele.check); // if (inspectIndex === -1) { // // 搜索有没有下一个检验分组 // // FIXME: `this.currentGroupCollectionDataset`为undefined // this.currentGroupCollectionDataset.select = false; // this.currentGroupCollectionDataset.check = true; // const groupIndex = this.groupCollectionDataset.findIndex((ele) => !ele.check); // if (groupIndex !== -1) { // // TODO: // // this.onInspectGroupChange(this.groupCollectionDataset[groupIndex].InspectItemGroupCode); // } else { // MsgHelper.ShowInfoMessage('请确认所有项目和产品是否都检验完成'); // this.currentInputInspectItem = null; // } // // MsgHelper.ShowConfirmModal('是否关闭数据录入界面?', '所有项目和产品检验完成,是否关闭数据录入界面', () => { // // this.closeDataEnrtyModal(); // // }); // } else { // // TODO: // // this.selectInspectItemChange(this.itemDataCollectionDataset[inspectIndex]); // } } else { // this.editInspectDatas[index].select = false; // this.editInspectDatas[index + 1].select = true; // 检测下一检验产品 this.currentInputProduct.select = false; this.currentInputProduct.check = true; this.currentInputProduct = this.editInspectDataset[index]; this.setValueToEntryForm(this.currentInputProduct); } this.focusValueInput(); } selectInspectItemChange(node: SimpleTreeDto) { this.currentInspectGroup = node.parent?.origin.InspectItemGroupCode; this.currentInputInspectItem = node.origin; // this.currentInputInspectItem.select = true; // 切换检验项目时要更新产品数据信息 this.editInspectDataset = this.currentInputInspectItem?.ItemDataValueDtos as IncomingInspectItemDataValueDto[]; this.currentGroupCollectionDataset = this.groupCollectionDataset.find( (ele) => ele.InspectItemGroupCode === this.currentInspectGroup ) as IncomingInspectGroupDataCollectionDto; this.itemDataCollectionDataset = this.currentGroupCollectionDataset.ItemDataCollectionDtos || []; this.initEditInspectDatas(this.editInspectDataset); } // 数据暂存 storeEntryData() { if (!this.submitEntryForm()) { return; } this.rhTempStorage.emit(this.rhData); } focusGoodQtyInput() { if (this.goodQtyInput) { this.goodQtyInput.focus(); this.goodQtyInput.inputElement.nativeElement.select(); } } // onInspectGroupChange(value: string) { // this.currentInspectGroup = value; // this.currentGroupCollectionDataset = this.groupCollectionDataset.find((ele) => ele.InspectItemGroupCode === value); // if (this.currentGroupCollectionDataset) { // this.itemDataCollectionDataset = this.currentGroupCollectionDataset.ItemDataCollectionDtos || []; // if (this.itemDataCollectionDataset?.length) { // this.selectInspectItemChange(this.itemDataCollectionDataset[0]); // } // } // } onSelectSnChange(data: RhSelectItem[]) { if (this.currentInputInspectItem?.InspectMode === 'Quantitative') { if (!data.length) { return; } // 定量赋值sn this.entryForm.get('SN')?.patchValue(data[0].Value); } else { if (data?.length > this.editInspectDataset?.length) { MsgHelper.ShowWarningMessage(`选择SN数量不能大于样品数量!`); return; } // 定性赋值不良数量 this.entryForm.get('BadQty')?.patchValue(data.length || 0); this.entryForm.get('GoodQty')?.patchValue((this.currentInputInspectItem?.SampleQty as number) - (data.length || 0)); // 找出对应数量的几个产品,赋值目前的SN; if (data?.length) { this.entryForm.get('SN')?.patchValue(data.map((ele) => ele.Value).join(',')); data.forEach((ele, index) => { this.editInspectDataset[index].SN = ele.Value as string; }); } else { this.editInspectDataset.forEach((ele) => (ele.SN = null)); } } } // 获取检验模式数据源用于数据格式化 private getInspectMode() { this.apiUrl .getSettingEntry({ moduleKey: 'QualityManagement', groupKey: 'AuxiliaryBasic', paraCode: 'InspectMode' }) .subscribe((result) => { if (result && result.Success) { this.inspectModeDatas = result.Attach; } else { this.inspectModeDatas = []; } }); } // 获取检验方法数据源用于数据格式化 private getInspectMethod() { this.apiUrl .getSettingEntry({ moduleKey: 'QualityManagement', groupKey: 'AuxiliaryBasic', paraCode: 'InspectMethod' }) .subscribe((result) => { if (result && result.Success) { this.inspectMethodDatas = result.Attach; } else { this.inspectMethodDatas = []; } }); } private focusValueInput() { setTimeout(() => { if (this.inspectValueInput) { this.inspectValueInput.focus(); this.inspectValueInput.inputElement.nativeElement.select(); } if (this.badQtyInput) { this.badQtyInput.focus(); this.badQtyInput.inputElement.nativeElement.select(); } }, 300); } initUploadFileData(type: InspectUploadCode): IQualityUploadTypeDto { const temp: IQualityUploadTypeDto = { InspectOrderCode: this.rhData.InspectOrderCode as string }; switch (type) { case InspectUploadCode.InspectOrderItemGroup: temp.InspectItemGroupCode = this.currentInspectGroup; break; case InspectUploadCode.InspectOrderEntry: temp.InspectItemGroupCode = this.currentInspectGroup; temp.InspectOrderEntryCode = this.currentInputInspectItem?.InspectOrderEntryCode as string; break; case InspectUploadCode.InspectOrderSample: temp.InspectItemGroupCode = this.currentInspectGroup; temp.InspectOrderEntryCode = this.currentInputInspectItem?.InspectOrderEntryCode as string; temp.SortId = this.currentInputProduct.SortId as number; break; default: throw new Error('错误的附件上传Upload Code'); } return temp; } private submitEntryForm(): boolean { FormHelper.ValidateBeforeSubmit(this.entryForm); if (this.entryForm.invalid) { return false; } // 定性模式下需要判定合格数量+不良数量等于总数量 const inspectDto = this.entryForm.value; if (inspectDto.SN && this.currentInputInspectItem?.InspectMode === 'Quantitative') { this.currentInputProduct.SN = inspectDto.SN; } if ( this.currentInputInspectItem?.InspectMode === 'Qualitative' && inspectDto?.GoodQty + inspectDto?.BadQty !== this.editInspectDataset?.length ) { MsgHelper.ShowWarningMessage('良品数数量加不良数量不等于产品总数,请检查后重试!'); return false; } this.currentInputProduct.check = true; // 定性检测要赋值合格数量和不合格数量 同时将所有检验产品都设置为已检验 if (this.currentInputInspectItem?.InspectMode === 'Qualitative') { this.currentInputInspectItem.GoodQty = inspectDto.GoodQty || 0; this.currentInputInspectItem.BadQty = inspectDto.BadQty || 0; this.currentInputInspectItem.ItemDataValueDtos.forEach((ele, index) => { ele.check = true; if (index < inspectDto.BadQty) { ele.InspectResult = false; } else { ele.InspectResult = true; } }); } return true; } }