import { MaterialStoragePtyDto, MaterialSnPtyDto, DataImportDto, TemplateInfoDto } from './../../mdp-model'; import { Component, OnInit, ViewChild } from '@angular/core'; import { OpMode, RhBaseDto, DataResultT, RhTableButtonInfoDto, RhSafeAny, RhTableQueryOptionDto, CodeClassModel, RhDynamicTableFieldDto, RhSelectItem, OpResult, RhDynamicComponentFieldType } from 'rh-base/model'; import { MsgHelper, ObjectHelper, RhStorageService, RhApiUrlsService, FileHelper } from 'rh-base/core'; import { RhPortType, RhPortLocalFieldType } from 'rh-base/model'; import { DynamicFormComponent } from 'rh-base/shared'; import { RhMaterialDefineService } from '../material-define.service'; import { MaterialDocumentDto } from '../../mdp-model'; import { Observable, Observer } from 'rxjs'; import { NzCheckBoxOptionInterface } from 'ng-zorro-antd/checkbox'; import { NzModalService } from 'ng-zorro-antd/modal'; import { NzTreeNode } from 'ng-zorro-antd/tree'; import { NzUploadFile } from 'ng-zorro-antd/upload'; const MATERIAL_SPECIFICATION_MAP = { IsMaterial: 'IsMaterial', IsSemiFinishedProduct: 'IsSemiFinishedProduct', IsProduct: 'IsProduct' }; const MATERIAL_SPECIFICATION_DATAS = [ { label: '原材料', value: 'IsMaterial' }, { label: '半成品', value: 'IsSemiFinishedProduct' }, { label: '成品', value: 'IsProduct' } ]; @Component({ selector: 'rh-material-document-manage', templateUrl: './material-document-manage.component.html', styleUrls: ['./material-document-manage.component.less'] }) export class RhMaterialDocumentManageComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; dataresult!: DataResultT; defaultFieldDataset: RhDynamicComponentFieldType[] = []; dataLoading = false; docDto: RhBaseDto = {}; //物料树内选中的物料 selectedMaterial: RhSafeAny; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as RhBaseDto), true), new RhTableButtonInfoDto('eye', '查看详情', 'default', false, 'eye', false, null, (data) => this.openDetailTab(data as RhBaseDto), true) ]; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openAddTab(), true), new RhTableButtonInfoDto( 'delete', '删除', 'primary', true, 'delete', false, null, (data) => this.deleteDatas(data as MaterialDocumentDto[]), false, true ) ]; extraButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('download', '下载模板', 'default', false, 'download', false, null, () => this.downloadTemplate(), true, false), new RhTableButtonInfoDto('import', '导入Excel', 'default', false, 'import', false, null, () => this.openUploadFileModal(), true) ]; // *******扩展******* ptyData!: MaterialStoragePtyDto; ptyDataLoading = false; // *******条码验证********* snData!: MaterialSnPtyDto & { OpSign: number }; snDataLoading = false; // 辅助 isAddTab = false; isEditTab = false; isDeatilTab = false; isShowEditBModal = false; // 管道数据源 measureUnits: RhSelectItem[] = []; // 上传Excel isShowUploadModal = false; uploadDatas: RhSafeAny; uploadFileList: NzUploadFile[] = []; uploading = false; // 导入的数据 importDatas: RhSafeAny[] = []; //数据导入页面 isOpenImportTab = false; isAddAfterDeleteAll = false; tableWidth = '0px'; // 导入数据返回的表头信息 dataHeadFields: { FieldName: string; FieldDescription: string }[] = []; businessTypeCode = 'MaterialDocumentAndExtPty'; moduleCode = 'MDP'; defaultQueryDto: RhSafeAny = {}; checkOptions: NzCheckBoxOptionInterface[] = MATERIAL_SPECIFICATION_DATAS; /********************************************维护物料类别******************************************** */ isShowMaterialCategoryModal = false; editMaterialCategoryDto: RhBaseDto = {}; materialCategorys: RhSelectItem[] = []; constructor( private apiUrls: RhApiUrlsService, public operator: RhMaterialDefineService, private modal: NzModalService, private storage: RhStorageService ) {} ngOnInit() { // this.getMeasureUnitSelectItems(); } // getValueChange() { // this.defaultQueryDto = { ...this.defaultQueryDto, ...this.collapseQueryDto }; // setTimeout(() => { // this.getDatas(); // }); // } setDatas(dataset: DataResultT) { this.dataresult = dataset; if (this.dataresult.Success && this.dataresult.Attach?.length) { if (this.docDto && this.docDto.OpSign === OpMode.OpEdit) { this.docDto = this.dataresult.Attach.find((f) => f['MaterialCode'] === this.docDto['MaterialCode']) as RhBaseDto; } else if (this.docDto?.OpSign === OpMode.OpAdd && this.defaultQueryDto?.MaterialCode) { this.docDto = this.dataresult.Attach.find((f) => f['MaterialCode'] === this.defaultQueryDto.MaterialCode) as RhBaseDto; } } } setInitFieldDataset(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]; } dataLoadingChange(status: boolean) { this.dataLoading = status; } /** 下载模板 */ downloadTemplate() { MsgHelper.ShowGlobalLoadingMessage('正在下载,请稍后。。。'); const dto = TemplateInfoDto.create(); dto.BusinessTypeCode = this.businessTypeCode; dto.ModuleKey = this.moduleCode; const url = this.apiUrls.GetApiOperateUrl(RhPortType.rdp, 'DataImportExportApi', 'TemplateDownload', RhPortLocalFieldType.rdp); FileHelper.downloadFileViaPost(url, dto, `${name}下载模板${new Date().getTime()}.xlsx`); } /** * 新建 */ openAddTab() { this.docDto = {}; this.docDto.OpSign = OpMode.OpAdd; this.isAddTab = true; this.isEditTab = false; this.isDeatilTab = false; this.selectedIndex = 1; } openEditTab(item: RhBaseDto) { this.docDto = item; this.docDto.OpSign = OpMode.OpEdit; this.getMaterialStoragePtyData(item['MaterialCode']); this.getMaterialSnPtyData(item['MaterialCode']); this.isAddTab = false; this.isEditTab = true; this.isDeatilTab = false; this.selectedIndex = 1; } openDetailTab(item: RhBaseDto) { this.docDto = item; this.getMaterialStoragePtyData(item['MaterialCode']); this.getMaterialSnPtyData(item['MaterialCode']); this.isAddTab = false; this.isEditTab = false; this.isDeatilTab = true; this.selectedIndex = 1; } closeAddTab() { this.isAddTab = false; this.selectedIndex = 0; } closeEditTab() { this.isEditTab = false; this.selectedIndex = 0; } closeDetailTab() { this.isDeatilTab = false; this.selectedIndex = 0; } onMaterialSpecificationChange(datas: NzCheckBoxOptionInterface[]) { const attachProp: RhSafeAny = {}; datas.forEach((ele) => { attachProp[MATERIAL_SPECIFICATION_MAP[ele.value as keyof typeof MATERIAL_SPECIFICATION_MAP]] = ele.checked ? true : null; }); this.defaultQueryDto = { ...this.defaultQueryDto, ...attachProp }; } /** 获取物料扩展信息 */ private getMaterialStoragePtyData(materialCode: string) { this.ptyData = MaterialStoragePtyDto.create(); this.ptyData.MaterialCode = materialCode; this.ptyDataLoading = true; this.operator.GetMaterialStoragePtyData(materialCode).subscribe( (result) => { if (result.Success) { this.ptyData = result.Attach; // 编辑和查看 this.ptyData.OpSign = OpMode.OpEdit; } else { // this.ptyData = null; // 不存在则新增 this.ptyData.OpSign = OpMode.OpAdd; // MsgHelper.ShowWarningMessage(`获取物料扩展信息失败!${result.Message}`); } this.ptyDataLoading = false; }, () => { // this.ptyData = null; this.ptyDataLoading = false; } ); } /** 获取条码信息 */ private getMaterialSnPtyData(materialCode: string) { this.snDataLoading = true; this.operator.GetMaterialSnPtyData(materialCode).subscribe( (result) => { if (result.Success) { this.snData = result.Attach as RhSafeAny; } else { this.snData = {} as RhSafeAny; this.snData.MaterialCode = materialCode; } this.snData.OpSign = OpMode.OpEdit; this.snDataLoading = false; }, () => { this.snDataLoading = false; } ); } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } getDatas(option?: RhTableQueryOptionDto, materialCode?: string) { if (this.queryFormCmt) { this.defaultQueryDto = this.updateQueryDto(materialCode); setTimeout(() => { this.queryFormCmt.submitDefaultForm(null, option); }); } } // onStoreDatas(result: DataResultT) { // if (result && result.Success) { // const msg = this.docDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; // MsgHelper.ShowSuccessMessage(`${msg}数据成功`); // if (this.docDto.OpSign === OpMode.OpEdit) { // this.selectedIndex = 0; // } // this.docDto = ObjectHelper.createRhBaseDto(); // this.getDatas(); // } else { // MsgHelper.ShowWarningMessage(`数据存储失败!${result.Message}`); // } // } deleteDatas(selectDatas: MaterialDocumentDto[]) { MsgHelper.ShowDeleteConfirm('删除后数据无法恢复,是否确定?', () => { this.modal.confirm({ nzTitle: '警告!', nzContent: '若删除此项,将会同时删除BOM信息,该操作不可逆,是否确认?', nzOnOk: () => { this.titleButtonDataset[1].loading = true; this.titleButtonDataset = [...this.titleButtonDataset]; this.operator.RemoveMaterialDocumentDatas(selectDatas).subscribe( (result) => { if (result.Success) { this.getDatas(); MsgHelper.ShowSuccessMessage('删除成功!'); } else { MsgHelper.ShowWarningMessage(`删除失败!${result.Message}`); } this.titleButtonDataset[1].loading = false; this.titleButtonDataset = [...this.titleButtonDataset]; }, () => { this.titleButtonDataset[1].loading = false; this.titleButtonDataset = [...this.titleButtonDataset]; } ); }, nzOkType: 'primary', nzOkDanger: true, nzIconType: 'warning' }); }); } openEditBModal() { MsgHelper.ShowConfirmModal('是否确定编辑', '修改会同步更新BOM信息', () => { this.isShowEditBModal = true; }); } closeEditBModal() { this.isShowEditBModal = false; } onStoreBDatas(result: DataResultT) { if (result && result.Success) { MsgHelper.ShowSuccessMessage('更新物料档案及其BOM信息成功!'); this.closeEditBModal(); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`更新物料档案及其BOM信息失败!${result.Message}`); } } // 上传Excel openUploadFileModal() { this.isShowUploadModal = true; this.uploadDatas = DataImportDto.create(); const session = this.storage.getUserSession(); this.uploadDatas.FactoryCode = session?.User?.FactoryCode || 'Default'; this.uploadDatas.BusinessCode = this.businessTypeCode; this.uploadDatas.ModuleCode = this.moduleCode; } closeUploadFileModal() { this.uploadFileList = []; this.isShowUploadModal = false; } openImportTab() { this.isOpenImportTab = true; this.isAddAfterDeleteAll = false; this.selectedIndex = 1; } closeImportTab() { this.isOpenImportTab = false; this.selectedIndex = 0; } 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 = () => { this.uploadDatas.FileStreamString = reader.result as RhSafeAny; // 只有一个文件 this.uploadFileList = [file]; observer.next(false); observer.complete(); }; reader.onerror = (e) => { MsgHelper.ShowWarningModal(`读取文件失败!${e}`); observer.next(false); observer.complete(); }; }); }; handleImport() { this.uploading = true; 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.closeUploadFileModal(); // this.getDatas(); this.openImportTab(); } else { MsgHelper.ShowWarningModal(`文件导入失败!${result.Message}`); } this.uploading = false; }, () => (this.uploading = false) ); } /** 清空数据 */ 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'; } // 保存数据 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 { // const dto = DataStoreDto.create(); // dto.BusinessCode = this.businessTypeCode; // dto.DataGroup = this.importDatas; // result = await this.operator.DataStore(dto).toPromise(); result = (await this.operator.StoreMaterialDocumentAndExtPtyDatas(this.importDatas).toPromise()) as OpResult; } catch (error) { MsgHelper.ShowErrorMessage(`保存错误!${error}`); } if (result) { if (result.Success) { MsgHelper.ShowSuccessMessage('保存成功!'); this.importDatas = []; this.getDatas(); this.closeImportTab(); } else { MsgHelper.ShowWarningMessage(`保存失败!${result.Message}`); } } } /*******************************新增维护项目组**************************************** */ /** * 获取项目分组 */ getItemGroups() { this.apiUrls.getSettingEntry({ moduleKey: 'MDP', groupKey: 'MaterialManagement', paraCode: 'MaterialCategory' }).subscribe((result) => { if (result.Success) { this.materialCategorys = result.Attach; } }); } beforeSubmit = (data: RhBaseDto) => { data['SortId'] = this.materialCategorys.length + 1; return true; }; openMaterialCategoryModal() { this.isShowMaterialCategoryModal = true; this.getItemGroups(); } closeMaterialCategoryModal() { this.isShowMaterialCategoryModal = false; this.editMaterialCategoryDto = {}; } onStoreMaterialCategoryDatas(result: DataResultT) { if (result && result.Success) { // const msg = this.editDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; MsgHelper.ShowSuccessMessage(`数据存储成功`); // if (this.editDto.OpSign === OpMode.OpEdit) { // this.selectedIndex = 0; // } this.closeMaterialCategoryModal(); this.editMaterialCategoryDto = ObjectHelper.createRhBaseDto(); } else { MsgHelper.ShowWarningMessage(`数据线存储失败${result.Message}`); } } SelectedMaterialCode(node: NzTreeNode) { if (node && node.origin['customData']) { const data = node.origin['customData']; if (node.level === 1 && node.isSelected) { this.selectedMaterial = data; this.getDatas(); } } else { this.selectedMaterial = null; this.defaultQueryDto = this.updateQueryDto(); } } private updateQueryDto(materialCode?: string) { const queryDto: RhSafeAny = {}; if (this.selectedMaterial?.MaterialCode) { queryDto.MaterialCode = `$${this.selectedMaterial.MaterialCode}`; } else if (materialCode) { queryDto.MaterialCode = materialCode; } else { queryDto.MaterialCode = null; } return queryDto; } }