import { RhEsopNewService } from './../../esop-new.service'; import { Component, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { NzTabComponent } from 'ng-zorro-antd/tabs'; import { DataResultT, RhBaseDto, RhSafeAny, RhSelectItem, RhTableButtonInfoDto, RhTableQueryOptionDto } from 'rh-base/model'; import { MsgHelper, RhApiUrlsService } from 'rh-base/core'; import { DocumentInfoDto, DocumentInfoQueryDto, ESopBindType, ESopBindTypeEnum, ESopMaterialDto, ESopProcessDto, ESopProcessRoutingDto, ReplaceESopDocumentInput } from './../../../mdp-model'; import { DynamicFormComponent } from 'rh-base/shared'; type type = 'bind' | 'replace'; type UploadFunType = | 'UploadESopFileForMaterial' | 'UploadESopFileForBom' | 'UploadESopFileForProcessRoutingEntry' | 'UploadESopFileForProcessRouting' | 'UploadESopFileForProcess'; type ReplaceFunType = | 'ReplaceMaterialDocument' | 'ReplaceESopBomDocument' | 'ReplaceESopProcessRoutingDocument' | 'ReplaceESopProcessRoutingEntryDocument' | 'ReplaceESopProcessDocument'; @Component({ selector: 'rh-bind-file-modal', templateUrl: './rh-bind-file-modal.component.html', styleUrls: ['./rh-bind-file-modal.component.css'] }) export class RhBindFileModalComponent implements OnInit { @ViewChild('queryBingFileFormCmt', { static: false }) queryBingFileFormCmt!: DynamicFormComponent; /** 需要换绑定或者替换或者上传文件的数据 */ @Input() rhSelectedDatas: RhSafeAny[] = []; /** 来自 ESopBindType = "Bom" | "ProcessRouting" | "ProcessRoutingEntry" | "Material" */ @Input() rhBindFileType: ESopBindType = 'Material'; /** 动作 type = "bind" | "replace" bind=upload */ @Input() rhType: type = 'bind'; /** 弹窗关闭 */ @Output() rhClose = new EventEmitter(); /** tab改变 */ @Output() rhTabChange = new EventEmitter(); /** 上传调用的方法 默认为物料上传方法 */ @Input() rhUploadFun: UploadFunType = 'UploadESopFileForMaterial'; /** 替换调用的方法 默认为物料替换方法 */ @Input() rhReplaceFun: ReplaceFunType = 'ReplaceMaterialDocument'; // 默认查询参数 defaultQueryDto!: DocumentInfoQueryDto; // eslint-disable-next-line @typescript-eslint/no-inferrable-types selectedIndex: number = 0; selectedDatas: DocumentInfoDto[] = []; dataResult!: DataResultT; // eslint-disable-next-line @typescript-eslint/no-inferrable-types dataLoading: boolean = false; // sop种类数组 sopFiletypes: RhSelectItem[] = []; // 表单对象 bindFileForm!: UntypedFormGroup; // 绑定时 二次确认弹窗 isShowBindSelectSOPModal = false; // 表行内按钮 editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'bindFileSop', '绑定', 'default', false, 'edit', false, null, (data) => this.bindFileBtnFun(data as DocumentInfoDto), true, false ) ]; sopBtnUploading = false; // 选择的文件 selectFileData: RhSafeAny = {}; constructor(private rhEsopNewService: RhEsopNewService, private fb: UntypedFormBuilder, private apiUrlService: RhApiUrlsService) {} ngOnInit(): void { this.initParams(); this.initForm(); this.getSopTypes(); } /** * 创建表单 */ initForm() { this.bindFileForm = this.fb.group({ /** Sop类型 */ // SopType: [null, [Validators.required]] SopType: [this.rhSelectedDatas.length === 1 ? this.rhSelectedDatas[0].SopType : null, [Validators.required]] }); } /** * 初始化参数 */ initParams() { if (this.rhSelectedDatas.length > 0) { const queryDto = DocumentInfoQueryDto.create(); this.defaultQueryDto = queryDto; } } /** * 查询数据 * @param data */ resultDataChange(data: DataResultT) { this.dataResult = data; } dataLoadingChange(bol: boolean) { this.dataLoading = bol; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } private getDatas(option?: RhTableQueryOptionDto) { if (this.queryBingFileFormCmt) { this.queryBingFileFormCmt.submitDefaultForm(null, option); } } /** * 关闭sop选择弹窗 * @param event */ closeModal(event: { type: boolean }) { if (event.type) { this.sopBtnUploading = true; if (!this.bindFileForm.value.SopType) { MsgHelper.ShowWarningMessage('请选择sop类型!'); this.sopBtnUploading = false; return; } if (this.rhType === 'bind') { this.bindFile(this.selectFileData); } else { this.replaceFile(this.selectFileData); } } else { this.isShowBindSelectSOPModal = false; } } /** * tab变化 * @param event */ selectedChange(event: { index: number; tab: NzTabComponent }) { this.rhTabChange.emit(event); } /** * 上传文件成功 */ uploadFileSuccess() { this.rhClose.emit(); } /** * 绑定事件 * @param event */ bindFileBtnFun(event: RhSafeAny) { this.isShowBindSelectSOPModal = true; this.selectFileData = event; } /** * 绑定文件 * @param event * @returns */ bindFile(event: RhSafeAny) { // 判断是tab几 switch (this.rhBindFileType) { case ESopBindTypeEnum.Material: this.bindsMaterial(event); break; case ESopBindTypeEnum.ProcessRouting: this.bindsProcessRouting(event); break; case ESopBindTypeEnum.Process: this.bindsProcess(event); break; } } /** * 物料绑定 * @param data */ bindsMaterial(data: RhBaseDto) { const binds: ESopMaterialDto[] = []; this.rhSelectedDatas.forEach((f: RhBaseDto) => { const bind = ESopMaterialDto.create(); bind.MaterialCode = f['MaterialCode']; bind.MaterialName = f['MaterialName']; bind.SopType = this.bindFileForm.value.SopType; bind.DocumentInfoName = data['DocumentInfoName']; bind.DocumentInfoCode = data['DocumentInfoCode']; binds.push(bind); }); this.rhEsopNewService.ESopMaterialBind(binds).subscribe((result) => { if (result.Success) { MsgHelper.ShowInfoMessage('绑定成功!'); this.rhClose.emit(); } else { MsgHelper.ShowErrorMessage('绑定失败!' + result.Message); } this.sopBtnUploading = false; }); } /** * 工艺路线绑定 * @param data */ bindsProcessRouting(data: RhBaseDto) { const binds: ESopProcessRoutingDto[] = []; this.rhSelectedDatas.forEach((f: RhBaseDto) => { const bind = ESopProcessRoutingDto.create(); bind.ProcessRoutingCode = f['ProcessRoutingCode']; bind.ProcessRoutingName = f['ProcessRoutingName']; bind.MaterialCode = f['MaterialCode']; bind.MaterialName = f['MaterialName']; bind.SopType = this.bindFileForm.value.SopType; bind.DocumentInfoName = data['DocumentInfoName']; bind.DocumentInfoCode = data['DocumentInfoCode']; binds.push(bind); }); this.rhEsopNewService.ESopProcessRoutingBind(binds).subscribe((result) => { if (result.Success) { MsgHelper.ShowInfoMessage('绑定成功!'); this.rhClose.emit(); } else { MsgHelper.ShowErrorMessage('绑定失败!' + result.Message); } this.sopBtnUploading = false; }); } /** * 工序绑定 * @param data */ bindsProcess(data: RhBaseDto) { const binds: ESopProcessDto[] = []; this.rhSelectedDatas.forEach((f: RhBaseDto) => { const bind = ESopProcessDto.create(); bind.ProcessCode = f['ProcessCode']; bind.ProcessName = f['ProcessName']; bind.SopType = this.bindFileForm.value.SopType; bind.DocumentInfoName = data['DocumentInfoName']; bind.DocumentInfoCode = data['DocumentInfoCode']; binds.push(bind); }); this.rhEsopNewService.ESopProcessBind(binds).subscribe((result) => { if (result.Success) { MsgHelper.ShowInfoMessage('绑定成功!'); this.rhClose.emit(); } else { MsgHelper.ShowErrorMessage('绑定失败!' + result.Message); } this.sopBtnUploading = false; }); } /** * 替换文件 * @param event */ replaceFile(event: DocumentInfoDto) { const params = ReplaceESopDocumentInput.create(); switch (this.rhBindFileType) { case ESopBindTypeEnum.Material: params.replaceItems.eSopMaterialDtos = this.rhSelectedDatas; break; case ESopBindTypeEnum.Bom: params.replaceItems.eSopBomDtos = this.rhSelectedDatas; break; case ESopBindTypeEnum.ProcessRouting: params.replaceItems.eSopProcessRoutingDtos = this.rhSelectedDatas; break; case ESopBindTypeEnum.ProcessRoutingEntry: params.replaceItems.eSopProcessRoutingEntryDtos = this.rhSelectedDatas; break; case ESopBindTypeEnum.Process: params.replaceItems.eSopProcessDtos = this.rhSelectedDatas; break; } // this.rhSelectedDatas.forEach((f: RhBaseDto) => { // params.replaceItems.eSopMaterialDtos.push() // // TODO 未完成带修改 // // params.replaceItems.push( // // new ESopDocumentOutDto(f.MaterialCode, f.MateriName, f.SopType, this.rhBindFileType, f.DocumentInfoCode, f.DocumentInfoName) // // ); // }); params.newDocumentCode = event.DocumentInfoCode; params.newDocumentName = event.DocumentInfoName; params.newSopType = this.bindFileForm.value.SopType; this.rhEsopNewService.ReplaceDocmentBind(params).subscribe((result) => { result.forEach((element) => { if (element.Attach?.['length'] > 0) { if (element.Success) { MsgHelper.ShowInfoMessage('替换成功!'); this.rhClose.emit(); } else { MsgHelper.ShowErrorMessage('替换失败!' + element.Message); } } }); this.sopBtnUploading = false; }); } /** * 查询sop的种类 */ getSopTypes() { this.apiUrlService .getSettingEntry({ moduleKey: 'SopConfiguration', groupKey: 'SopConfiguration', paraCode: 'SopFileType' }) .subscribe((result) => { if (result.Success) { this.sopFiletypes = result.Attach; } }); } }