import { TemplateInfoDto, DataStoreDto, DataImportDto } from './../../syc-models'; import { Component, OnInit } from '@angular/core'; import { RhSafeAny, OpResult, OpMode, DataResultT, RhTableButtonInfoDto } from 'rh-base/model'; import { RhStorageService, RhApiUrlsService, FileHelper, MsgHelper, noop } from 'rh-base/core'; import { RhPortType, RhPortLocalFieldType } from 'rh-base/model'; import { NzUploadFile, UploadFilter } from 'ng-zorro-antd/upload'; import { firstValueFrom, Observable, Observer } from 'rxjs'; import { RhConfigManageService } from '../config-manage.service'; import { TemplateUpdataDto } from '../config-manage-model'; import { NzModalService } from 'ng-zorro-antd/modal'; import { NzTabChangeEvent } from 'ng-zorro-antd/tabs'; @Component({ selector: 'rh-batch-import-config', templateUrl: './batch-import-config.component.html', styleUrls: ['./batch-import-config.component.less'] }) export class RhBatchImportConfigComponent implements OnInit { // 模式管理:MES=0;MDP=1 displayMode = 0; // 根据用户获取对应工厂信息 factoryCode: string | null = null; // Mdp dataresult!: DataResultT; selectTpl!: TemplateInfoDto; dataLoading = false; // Mes // mesDataresult: DataResultT; // mesDataLoading = false; // 筛选 templateFileName: string | null = null; //业务代码筛选 businessTypeCode: string | null = null; // 上传文件 isShowUploadDrawer = false; isDirectoryUploadEnable = false; uploading = false; uploadFileList: NzUploadFile[] = []; uploadDatas: DataImportDto | null = null; //大数据导入 importBigData!: DataImportDto; //导入文件名 importFileName!: string; isImportBigDatas = false; // 替换文件 isChangeTemplate = false; uploadTpl: TemplateUpdataDto | null = null; // 文件过滤 uploadFilter: UploadFilter[] = [ { name: 'fileType', fn: (fileList: NzUploadFile[]) => { const filterFileList = fileList.filter((f) => /\.xlsx$/.test(f.name.toLocaleLowerCase())); if (fileList.length !== filterFileList.length) { MsgHelper.ShowWarningMessage('请上传xlsx格式的文件!'); return filterFileList; } return fileList; } } ]; // 导入的数据 importDatas: RhSafeAny[] = []; // 返回的表头信息 dataHeadFields: { FieldName: string; FieldDescription: string }[] = []; tableWidth = '0px'; selectedIndex = 0; isOpenImportTab = false; isAddAfterDeleteAll = false; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'upload', '导入数据', 'default', false, 'cloud-upload', false, null, (data) => this.openUploadFileDrawer(data as TemplateInfoDto), true, false, 'green' ), new RhTableButtonInfoDto( 'import', '大数据导入', 'default', false, 'cloud-server', false, null, (data) => this.openImportFileDrawer(data as TemplateInfoDto), true, false, 'green' ), new RhTableButtonInfoDto( 'download', '下载模板', 'default', false, 'download', false, null, (data) => this.downloadTemplate(data as TemplateInfoDto), true, false, '#1890ff' ), new RhTableButtonInfoDto( 'change', '上传模板', 'default', false, 'upload', false, null, (data) => this.openUploadFileDrawer(data as TemplateInfoDto, true), true, false, 'orange' ) ]; // mesEditButtonDataset: RhTableButtonInfoDto[] = [ // new RhTableButtonInfoDto( // 'download', // '下载', // 'default', // 'download', // false, // null, // (data) => this.downloadMesTemplate(data as TemplateInfoDto), // true, // false, // '#1890ff' // ), // new RhTableButtonInfoDto( // 'upload', // '上传', // 'default', // 'cloud-upload', // false, // null, // (data) => this.openUploadFileDrawer(data as TemplateInfoDto), // true, // false, // 'green' // ), // new RhTableButtonInfoDto( // 'change', // '替换', // 'default', // 'sync', // false, // null, // (data) => this.openUploadFileDrawer(data as TemplateInfoDto, true), // true, // false, // 'red' // ) // ]; constructor( private operator: RhConfigManageService, private storage: RhStorageService, private apiUrls: RhApiUrlsService, private modal: NzModalService ) {} ngOnInit(): void { this.factoryCode = this.storage.getUserSession()?.User?.FactoryCode as string; this.getTemplateInfoDatas(); } getTemplateInfoDatas() { const temp = this.displayMode === 0 ? 'MDP' : 'MES'; this.dataLoading = true; this.operator.GetTemplateInfoDatas(this.factoryCode as string, temp).subscribe( (result) => { if (!result.Success) { MsgHelper.ShowWarningMessage(`查询数据失败!${result.Message}`); } else if (this.templateFileName) { const templateFileNameInput = this.templateFileName.trim(); const displayDataset = result.Attach.filter((f) => f.TemplateName?.includes(templateFileNameInput)); result.Attach = displayDataset; result.TotalCount = displayDataset.length; if (this.businessTypeCode) { const businessTypeCodeInput = this.businessTypeCode.trim(); const displayDataset = result.Attach.filter( (f) => f.TemplateName?.includes(templateFileNameInput) && f.BusinessTypeCode?.includes(businessTypeCodeInput) ); result.Attach = displayDataset; result.TotalCount = displayDataset.length; } } else if (this.businessTypeCode) { const businessTypeCodeInput = this.businessTypeCode.trim(); const displayDataset = result.Attach.filter((f) => f.BusinessTypeCode?.includes(businessTypeCodeInput)); result.Attach = displayDataset; result.TotalCount = displayDataset.length; if (this.templateFileName) { const templateFileNameInput = this.templateFileName.trim(); const displayDataset = result.Attach.filter( (f) => f.TemplateName?.includes(templateFileNameInput) && f.BusinessTypeCode?.includes(businessTypeCodeInput) ); result.Attach = displayDataset; result.TotalCount = displayDataset.length; } } this.dataresult = result; this.dataLoading = false; }, () => { this.dataLoading = false; } ); } resetTemplateInfoDatas() { this.templateFileName = null; this.businessTypeCode = null; } /** 下载模板 */ downloadTemplate(item: TemplateInfoDto) { // const dto = TemplateDownloadDto.create(); // dto.BusinessTypeCode = item.BusinessTypeCode; MsgHelper.ShowGlobalLoadingMessage('正在下载,请稍后。。。'); const url = this.apiUrls.GetApiOperateUrl(RhPortType.rdp, 'DataImportExportApi', 'TemplateDownload', RhPortLocalFieldType.rdp); const extendName = item.FilePath?.split('.')[item.FilePath.split('.').length - 1]; FileHelper.downloadFileViaPost(url, item, item.TemplateName + '.' + extendName); } /******************************上传数据*********************************/ openUploadFileDrawer(item: TemplateInfoDto, isChange = false) { this.selectTpl = item; this.isChangeTemplate = isChange; if (this.isChangeTemplate) { this.uploadTpl = TemplateUpdataDto.create(); this.uploadTpl.BusinessTypeCode = item.BusinessTypeCode; this.uploadTpl.FactoryCode = this.factoryCode; this.uploadTpl.ModuleKey = item.ModuleKey; } else { this.uploadDatas = DataImportDto.create(); this.uploadDatas.BusinessCode = item.BusinessTypeCode; this.uploadDatas.FactoryCode = this.factoryCode; this.uploadDatas.ModuleKey = item.ModuleKey; } this.isShowUploadDrawer = true; } closeUploadFileDrawer() { this.isShowUploadDrawer = false; this.uploadFileList = []; this.uploadDatas = null; this.uploadTpl = null; this.isImportBigDatas = false; } 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 = () => { if (this.isChangeTemplate && this.uploadTpl) { this.uploadTpl.FileStream = reader.result as RhSafeAny; this.uploadTpl.FileName = file.name; const nameList = this.uploadTpl.FileName.split('.'); this.uploadTpl.FileExtensionName = nameList[nameList.length - 1]; } else { if (this.isImportBigDatas) { this.importFileName = file.name; this.importBigData.FileStreamString = reader.result as RhSafeAny; } else { this.uploadDatas ? (this.uploadDatas.FileStreamString = reader.result as RhSafeAny) : noop(); } } // 只有一个文件 // 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(); }; }); }; async handleUpload() { let result: RhSafeAny; try { this.uploading = true; if (this.isChangeTemplate) { result = await this.operator.ChangeTemplate(this.uploadTpl as TemplateUpdataDto).toPromise(); } else { result = await this.operator.DataImport(this.uploadDatas as DataImportDto).toPromise(); } if (result && result.Success) { MsgHelper.ShowSuccessMessage('文件上传成功!'); this.closeUploadFileDrawer(); if (!this.isChangeTemplate) { this.importDatas = result.Attach; this.dataHeadFields = result.DataHeadFields; this.tableWidth = this.dataHeadFields.length * 120 + 100 + 'px'; this.openImportTab(); } else { this.getTemplateInfoDatas(); } } else { MsgHelper.ShowWarningModal(`文件上传失败!${result.Message}`); } } catch (error) { MsgHelper.ShowErrorMessage('接口发生错误!'); } finally { this.uploading = false; } } /******************************保存数据***********************************/ openImportTab() { this.isOpenImportTab = true; this.isAddAfterDeleteAll = false; this.selectedIndex = 1; } closeImportTab() { this.isOpenImportTab = false; } /** 清空数据 */ clearImportedPlanDatas() { MsgHelper.ShowConfirmModal('是否继续', '是否清空所有导入的模板数据?', () => { this.importDatas = []; MsgHelper.ShowSuccessMessage('数据清空成功!'); }); } removeImportTempData(index: number) { MsgHelper.ShowConfirmModal('是否继续', '是否移除选中的模板数据?', () => { this.importDatas.splice(index, 1); this.importDatas = [...this.importDatas]; }); } async storeImportedDatas() { if (this.importDatas.some((f) => !f.Success)) { MsgHelper.ShowWarningMessage('请修改错误或重新导入后保存!'); return; } const businessType = this.selectTpl.BusinessTypeCode; 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 = businessType; dto.DataGroup = this.importDatas; result = await firstValueFrom(this.operator.DataStore(dto)); } catch (error) { MsgHelper.ShowErrorMessage(`保存错误!${error}`); } if (result) { if (result.Success) { MsgHelper.ShowSuccessMessage('保存成功!'); this.importDatas = []; this.closeImportTab(); } else { MsgHelper.ShowWarningMessage(`保存失败!${result.Message}`); } } } 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'; } changeIsAddAfterDeleteAll(value: boolean) { if (value) { this.modal.confirm({ nzTitle: '警告!', nzContent: '若勾选此项,将会进行不可逆的删除,是否确认?', nzOnCancel: () => { this.isAddAfterDeleteAll = false; }, nzOkType: 'primary', nzOkDanger: true, nzIconType: 'warning' }); } } displayModeChange(event: NzTabChangeEvent) { this.getTemplateInfoDatas(); this.selectedIndex = 0; this.templateFileName = null; this.businessTypeCode = null; } /*************************************大数据导入*********************************************************** */ openImportFileDrawer(item: TemplateInfoDto, isChange = false) { this.isImportBigDatas = true; this.selectTpl = item; this.isChangeTemplate = isChange; // if (this.isChangeTemplate) { // this.uploadTpl = TemplateUpdataDto.create(); // this.uploadTpl.BusinessTypeCode = item.BusinessTypeCode; // this.uploadTpl.FactoryCode = item.FactoryCode; // this.uploadTpl.ModuleKey = item.ModuleKey; // } else { this.importBigData = DataImportDto.create(); this.importBigData.BusinessCode = item.BusinessTypeCode; this.importBigData.FactoryCode = this.storage.getFactoryCode(); this.importBigData.ModuleKey = item.ModuleKey; // } this.isShowUploadDrawer = true; } // 导入 async handleImport() { let result: RhSafeAny; try { this.uploading = true; // MDP if (this.displayMode === 0) { result = await this.operator.BigDataImport(this.importBigData).toPromise(); } else if (this.displayMode === 1) { // MES result = await this.operator.BigDataImportMes(this.importBigData).toPromise(); } if (result && ((result as RhSafeAny).OpSuccess || result.Success)) { if ((result as RhSafeAny).FilePath) { MsgHelper.ShowSuccessMessage('检测到异常数据,异常数据未导入,其他数据导入成功'); const extendName = result.FilePath.split('\\')[result.FilePath.split('\\').length - 1]; if (this.displayMode === 0) { const url = this.apiUrls.GetApiOperateUrl( RhPortType.mdp, 'DataImportExportApi', 'GetImportFaildData', RhPortLocalFieldType.mdp ); FileHelper.downloadFileViaPost(url, result, extendName); } else if (this.displayMode === 1) { const url = this.apiUrls.GetApiOperateUrl( RhPortType.emp, 'DataImportExportApi', 'GetImportFaildData', RhPortLocalFieldType.emp ); FileHelper.downloadFileViaPost(url, result, extendName); } } else { MsgHelper.ShowSuccessMessage('数据导入成功!'); } this.closeUploadFileDrawer(); } else { MsgHelper.ShowWarningMessage(`数据导入失败${result.Message}`); } } catch (error) { MsgHelper.ShowErrorMessage(`出错了! ${error}`); } finally { this.uploading = false; } } }