import { UntypedFormGroup, UntypedFormBuilder, Validators, AbstractControl } from '@angular/forms'; import { MsgHelper, RhApiUrlsService, FormHelper } from 'rh-base/core'; import { Component, OnInit, Input, ViewChild, ElementRef, AfterViewInit, Output, EventEmitter } from '@angular/core'; import { RhBaseDto, RhDynamicInterfaceDto, RhBaseQueryDto, DataResultT, RhTableButtonInfoDto, RhSafeAny } from 'rh-base/model'; import { RhIqcManagementService } from '../../iqc-management/iqc-management.service'; import { IqcMaterialInspectOrderSNPoolDto, IqcMaterialInspectOrderSNPoolQueryDto } from '../../qmp-model'; @Component({ selector: 'rh-inspect-sn-record', templateUrl: './inspect-sn-record.component.html', styleUrls: ['./inspect-sn-record.component.less'] }) export class RhInspectSnRecordComponent implements OnInit, AfterViewInit { @ViewChild('SnInput', { static: false }) inputRef!: ElementRef; @Input() rhGetInterfaceInfo!: RhDynamicInterfaceDto; @Input() rhRemoveInterfaceInfo!: RhDynamicInterfaceDto; @Input() rhStoreInterfaceInfo!: RhDynamicInterfaceDto; // 选中的检验单对象 @Input() rhData!: RhBaseDto; @Output() rhClose = new EventEmitter(); /** 针对不同类别的检验类型,需要做不同的处理 * @description `OQC`需要验证箱号和`SN`是否匹配,这个后端已经做了处理 * `OBA`提交form有个`22条码`字段; */ @Input() rhSnInspectCategory: 'IQC' | 'OQC' | 'OBA' = 'OBA'; dataResult!: DataResultT; dataResultLoading = false; snQueryInterfaceInfo!: RhDynamicInterfaceDto; snQueryDto!: RhBaseQueryDto; recordForm!: UntypedFormGroup | RhSafeAny; recordDto!: IqcMaterialInspectOrderSNPoolDto; recordLoading = false; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'delete', '删除', 'primary', true, 'delete', true, null, (datas) => this.deleteDatas(datas as IqcMaterialInspectOrderSNPoolDto[]), false, true ) ]; extraButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('refresh', '刷新', 'default', false, 'sync', false, null, () => this.getSnDatas(), true, true) ]; snDuplicate = (control: AbstractControl) => { const value = control.value; if (!value) { return { error: true, required: true }; } else if (this.dataResult?.Attach && this.dataResult.Attach.findIndex((ele) => ele.SN === value) !== -1) { return { error: true, duplicate: true }; } else { return null; } }; constructor(public operator: RhIqcManagementService, public apiUrls: RhApiUrlsService, public fb: UntypedFormBuilder) { // } ngOnInit(): void { this.recordDto = IqcMaterialInspectOrderSNPoolDto.create(); this.recordDto.InspectOrderCode = this.rhData['InspectOrderCode']; this.createRecordForm(this.recordDto); this.getSnDatas(() => this.focusSnInput()); } ngAfterViewInit() { this.focusSnInput(); } close() { this.rhClose.emit(); } createRecordForm(dto: IqcMaterialInspectOrderSNPoolDto) { this.recordForm = this.fb.group({ SN: [dto.SN, [Validators.required, this.snDuplicate]], ContainerSN: [dto.ContainerSN], /** 22条码,只有OBA检验任务单才有 */ RelateSN: [dto.RelateSN] }); // 根据不同的检验类别处理控件的必填性 if (this.rhSnInspectCategory === 'OBA') { this.recordForm.get('ContainerSN')?.setValidators(Validators.required); this.recordForm.get('RelateSN')?.setValidators(Validators.required); } else { this.recordForm.get('ContainerSN')?.clearValidators(); this.recordForm.get('RelateSN')?.clearValidators(); } } submitRecordForm(event: Event) { if (event) { event.preventDefault(); } FormHelper.YGSubmitForm(this.recordDto, this.recordForm, (dto) => { this.apiUrls.httpHandler(this.rhStoreInterfaceInfo, dto).subscribe((result) => { if (result.Success) { MsgHelper.ShowSuccessMessage(`SN添加成功!`); this.getSnDatas(() => this.focusSnInput()); } else { MsgHelper.ShowWarningModal(`SN添加失败!${result.Message}`); } }); }); } resetRecordForm(event: Event) { if (event) { event.preventDefault(); } this.recordForm.reset(); } deleteDatas(datas: IqcMaterialInspectOrderSNPoolDto[]) { this.apiUrls.httpHandler(this.rhRemoveInterfaceInfo, datas).subscribe((result) => { if (result?.Success) { MsgHelper.ShowSuccessMessage(`SN删除成功!`); this.getSnDatas(); } else { MsgHelper.ShowWarningModal(`SN删除失败!${result.Message}`); } }); } getSnDatas(cb?: () => void) { const queryDto = IqcMaterialInspectOrderSNPoolQueryDto.create(); queryDto.InspectOrderCode = this.rhData['InspectOrderCode']; this.dataResultLoading = true; // this.operator.GetIqcMaterialInspectOrderSNPoolDatas(queryDto).subscribe( // (result) => { // if (result.Success) { // this.dataResult = result; // } else { // result.Attach = result.Attach || []; // this.dataResult = result; // MsgHelper.ShowWarningMessage(`查询无SN数据!${result.Message}`); // } // this.dataResultLoading = false; // if (cb) { // cb(); // } // }, // () => (this.dataResultLoading = false) // ); this.apiUrls.httpHandler(this.rhGetInterfaceInfo, queryDto).subscribe( (result) => { if (result.Success) { this.dataResult = result; } else { result.Attach = result.Attach || []; this.dataResult = result; MsgHelper.ShowWarningMessage(`查询无SN数据!${result.Message}`); } this.dataResultLoading = false; if (cb) { cb(); } }, () => (this.dataResultLoading = false) ); } private focusSnInput() { setTimeout(() => { if (this.inputRef) { this.inputRef.nativeElement.focus(); this.inputRef.nativeElement.select(); } }, 200); } // private initSnQueryInterfaceInfo() { // const temp = RhDynamicInterfaceDto.create(); // temp.port = this.apiUrls.getPortNum(RhPortType.emp); // temp.interfaceType = 'post'; // temp.controllerName = 'IqcMaterialInspectApi'; // temp.interfaceName = 'GetIqcMaterialInspectOrderSNPoolDatas'; // return temp; // } }