import { RhProductionPlanManagementService } from './../production-plan-management.service'; import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, RhBaseDto, OpMode, RhSafeAny, RhLoginedUserDto, WithNil, OpResult } from 'rh-base/model'; import { MsgHelper, ObjectHelper, RhApiUrlsService, RhStorageService, FileHelper } from 'rh-base/core'; import { RhPortType, RhPortLocalFieldType } from 'rh-base/model'; import { DynamicFormComponent } from 'rh-base/shared'; import { DataImportInput, ProductionPlanImportDto, ProductProcessRouteDto, ProductProcessRouteQueryDto, TemplateInfoDto, View_ProductProcessDto, View_ProductProcessQueryDto, WorkShopLineExtProperyQueryDto, WorkShopLineProductionParameter } from '../../ppm-model'; import { NzUploadFile, UploadFilter } from 'ng-zorro-antd/upload'; import { Observable, Observer } from 'rxjs'; import { UntypedFormGroup, Validators } from '@angular/forms'; @Component({ selector: 'rh-create-production-plan', templateUrl: './create-production-plan.component.html', styleUrls: ['./create-production-plan.component.less'] }) export class RhCreateProductionPlanComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; editDto: RhSafeAny = {}; dataLoading = false; dataresult!: DataResultT; defaultFieldDataset: RhSafeAny = []; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openEditTab(), true), new RhTableButtonInfoDto('planloader', '计划导入', 'default', false, 'import', false, null, () => this.openImportTab(), true) ]; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as RhBaseDto), true) ]; tabType = 'edit'; factoryCode!: WithNil; currentUser!: WithNil; // 计划导入tab isShowImportTab = false; // 模板下载 isShowTempleteDownloadModal = false; templateMesFileName!: WithNil; MesDataset: TemplateInfoDto[] = []; displayMesDataset: TemplateInfoDto[] = []; mesDataLoading = false; // ******** excel导入 ******** isShowImportFlesModal = false; // 上传文件 isShowUploadDrawer = false; isDirectoryUploadEnable = false; uploading = false; uploadFileList: NzUploadFile[] = []; uploadDatas!: WithNil; // 导入的数据 importDatas: RhSafeAny[] = []; // 返回的表头信息 dataHeadFields: { FieldName: string; FieldDescription: string }[] = []; tableWidth = '0px'; 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; } } ]; // 生产模式 businessType!: string; /** 计划日期 */ startDate!: Date; endDate!: Date; rangeDate: Date[] = []; // 产品工艺路线信息 productProcessDatas: View_ProductProcessDto[] = []; productProcessList: View_ProductProcessDto[] = []; // 产品绑定工艺路线信息 productBindProcessDatas: ProductProcessRouteDto[] = []; productBindProcessList: ProductProcessRouteDto[] = []; productFieldDataNew = { placeholder: '产品代码', relatedComponentName: 'editForm', config: { type: 'product', displayMode: 1, relatedModelDatas: [ { sourceName: 'MaterialCode', mappedName: 'ProductCode' }, { sourceName: 'MaterialName', mappedName: 'ProductName' }, { sourceName: 'MaterialSpecification', mappedName: 'ProductSpecification' } ], relatedFieldDatas: [ { sourceName: 'MaterialCode', mappedName: 'ProductCode' }, { sourceName: 'MaterialName', mappedName: 'ProductName' }, { sourceName: 'MaterialSpecification', mappedName: 'ProductSpecification' } ] } }; workshopFieldDataNew = { placeholder: '车间产线', relatedComponentName: 'editForm', config: { type: 'workshop', displayMode: 1 // relatedModelDatas: [ // { sourceName: 'MaterialCode', mappedName: 'ProductCode' }, // { sourceName: 'MaterialName', mappedName: 'ProductName' }, // { sourceName: 'MaterialSpecification', mappedName: 'ProductSpecification' } // ], // relatedFieldDatas: [ // { sourceName: 'MaterialCode', mappedName: 'ProductCode' }, // { sourceName: 'MaterialName', mappedName: 'ProductName' }, // { sourceName: 'MaterialSpecification', mappedName: 'ProductSpecification' } // ] } }; // 车间产线配置参数 workshopParaData!: WorkShopLineProductionParameter; isShowProductRouting = false; constructor(private operator: RhProductionPlanManagementService, private apiUrls: RhApiUrlsService, private storage: RhStorageService) { // } ngOnInit() { this.currentUser = this.storage.getUserSession()?.User; this.factoryCode = this.storage.getUserSession()?.User?.FactoryCode; this.initMesDatas(); // 获取产品工艺路线信息 this.getProductProcessDatas(); // 获取产品绑定工艺路线信息 this.getProductBindingProcessRouteDatas(); } getProductProcessDatas() { const queryDto = View_ProductProcessQueryDto.create(); queryDto.Mode = 8; this.operator.GetProductProcessDatas(queryDto).subscribe((result) => { if (result && result.Success) { this.productProcessDatas = result.Attach; } else { this.productProcessDatas = []; } }); } getProductBindingProcessRouteDatas() { const queryDto = ProductProcessRouteQueryDto.create(); queryDto.Mode = 8; this.operator.GetProductBindingProcessRouteDatas(queryDto).subscribe((result) => { if (result && result.Success) { this.productBindProcessDatas = result.Attach; } else { this.productBindProcessDatas = []; } }); } // 产品相关信息赋值 setProductRelateDatas(code: string) { // 传入的数据为产品代码 if (code) { this.productProcessList = this.productProcessDatas.filter((ele) => ele.ProductCode === code); // 根据bom编码去重 const rr = new Map(); this.productProcessList = this.productProcessList.filter((ele) => !rr.has(ele.ProductBomCode) && rr.set(ele.ProductBomCode, 1)); this.productBindProcessList = this.productBindProcessDatas.filter((ele) => ele.ProductCode === code); } else { this.productProcessList = []; this.productBindProcessList = []; } if (this.editDto.OpSign === 1) { this.editDto.ProcessRoutingCode = null; this.editDto.ProductBomCode = null; } this.editDto = { ...this.editDto }; } async refreshProductBindProcess(formGroup: UntypedFormGroup) { const queryDto = ProductProcessRouteQueryDto.create(); queryDto.Mode = 8; const res = await this.operator.GetProductBindingProcessRouteDatas(queryDto).toPromise(); if (res && res.Success) { this.productBindProcessDatas = res.Attach; const code = formGroup.get('ProductCode')?.value; this.setProductRelateDatas(code); } else { MsgHelper.ShowErrorMessage('刷新失败!' + res?.Message); } } openEditTab(data?: RhSafeAny) { this.tabType = 'edit'; // if (data) { this.rangeDate[0] = data.PlanStartTime; this.rangeDate[1] = data.PlanEndTime; this.setProductRelateDatas(data.ProductCode); this.editDto = data; this.editDto.OpSign = OpMode.OpEdit; } else { this.editDto = {}; this.editDto.OpSign = OpMode.OpAdd; } this.editDto.PlanScheduler = this.currentUser?.UserName; this.selectedIndex = 1; } closeEditTab() { // this.selectedIndex = 0; } setDatas(dataset: DataResultT | OpResult) { this.dataresult = dataset as DataResultT; } dataLoadingChange(status: boolean) { this.dataLoading = status; } onStoreData(result: DataResultT | OpResult) { if (result && result.Success) { const msg = this.editDto.OpSign === OpMode.OpAdd ? '新增' : '编辑'; MsgHelper.ShowSuccessMessage(`${msg}数据成功!`); if (this.editDto.OpSign === OpMode.OpEdit) { this.selectedIndex = 0; } this.editDto = ObjectHelper.createRhBaseDto(); this.getDatas(); } else { MsgHelper.ShowWarningModal(`存储数据失败!${result.Message}`); } } setInitFieldDatset(item: CodeClassModel) { const tempDatas: RhSafeAny = []; item.CodeProperties.forEach((ele) => { const temp = RhDynamicTableFieldDto.create(); temp.name = ele.PropertyName; temp.displayName = ele.Descirption; tempDatas.push(temp); }); this.defaultFieldDataset = [...tempDatas]; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } //调用子组件中的接口 private getDatas(option?: RhTableQueryOptionDto) { if (this.queryFormCmt) { this.queryFormCmt.submitDefaultForm(null, option); } } setStartAndEndDate(dates: Date[]) { if (!dates || dates?.length < 2) { return; } this.startDate = new Date(dates[0]); this.endDate = new Date(dates[1]); } dataHandler = (data: RhSafeAny) => { data.PlanStartTime = this.startDate; data.PlanEndTime = this.endDate; return true; }; // 获取车间产线配置数据 getWorkShopLineProductionParameterData(code: string, formGroup: UntypedFormGroup) { this.editDto = { ...this.editDto }; const queryDto = WorkShopLineExtProperyQueryDto.create(); queryDto.OrganizationStructureCode = code; this.operator.GetWorkShopLineProductionParameterData(queryDto).subscribe((result) => { if (result && result.Success) { this.workshopParaData = result.Attach; if ( this.workshopParaData.ProductionMode === 'DiscreteMode' || (this.workshopParaData.ProductionMode === 'PlanDispatch' && this.workshopParaData.DispatchingObject === 'ProcessDispatch') ) { this.isShowProductRouting = true; } else { this.isShowProductRouting = false; } } else { this.workshopParaData = WorkShopLineProductionParameter.create(); this.isShowProductRouting = false; } if (formGroup) { if (this.isShowProductRouting) { formGroup.get('ProcessRoutingCode')?.setValidators(Validators.required); } else { formGroup.get('ProcessRoutingCode')?.clearValidators(); formGroup.get('ProcessRoutingCode')?.setValue(null); } } }); } //#region ********** 计划导入 ********** openImportTab() { this.tabType = 'import'; this.isShowImportTab = true; this.selectedIndex = 1; } closeImportTab() { this.isShowImportTab = false; this.selectedIndex = 0; } /********** 计划导入模板下载 **********/ async initMesDatas() { const result = await this.getMesDatas(); if (result && result.Success) { // this.MesDataset = result.Attach.filter((ele) => ele.BusinessTypeCode === 'ProductionPlanOrder'); this.MesDataset = result.Attach.filter((ele) => ele.BusinessTypeCode?.match('ProductionPlanImport')); this.displayMesDataset = [...this.MesDataset]; // console.log(this.displayMesDataset); } else { this.MesDataset = []; this.displayMesDataset = []; } } async getMesDatas() { let result = null; this.mesDataLoading = true; try { result = await this.operator.GetTemplateInfoDatasRdp(this.factoryCode || '', 'MES').toPromise(); } catch (error) { MsgHelper.ShowWarningMessage(`接口出错!${error}`); } finally { this.mesDataLoading = false; } return result; } openTempleteDownloadModal() { this.isShowTempleteDownloadModal = true; } closeTempleteDownloadModal() { this.isShowTempleteDownloadModal = false; } // 下载模板 downloadMesTemplate(item: TemplateInfoDto) { if (item) { const url = this.apiUrls.GetApiOperateUrl(RhPortType.rdp, 'DataImportExportApi', 'TemplateDownload', RhPortLocalFieldType.mdp); const extendName = item.FilePath?.split('.')[item.FilePath?.split('.').length - 1]; FileHelper.downloadFileViaPost(url, item, item.TemplateName + '.' + extendName); } else { MsgHelper.ShowWarningMessage('模板不存在,无法下载!'); } } /********** excel导入 **********/ // 导入excel openImportFlesModal() { this.uploadDatas = DataImportInput.create(); this.uploadDatas.BusinessCode = 'ProductionPlanOrder'; this.uploadDatas.FactoryCode = this.factoryCode; this.uploadDatas.OperatorCode = this.currentUser?.UserName; this.uploadDatas.OperatorName = this.currentUser?.DisplayName; this.isShowImportFlesModal = true; } closeImportFlesModal() { this.isShowImportFlesModal = false; this.uploadFileList = []; this.uploadDatas = null; } 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.uploadDatas) { this.uploadDatas.FileStreamString = reader.result as RhSafeAny; } // 只有一个文件 // 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(); }; }); }; handleUpload() { if (!this.businessType || !this.uploadDatas) { MsgHelper.ShowWarningMessage(`请先选择生产模式!`); return; } else { this.uploadDatas.BusinessCode = this.businessType; } 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.closeImportFlesModal(); this.openImportTab(); } else { MsgHelper.ShowWarningModal(`文件上传失败!${result.Message}`); } this.uploading = false; }, () => (this.uploading = false) ); } // 计划保存 storeImportedPlanDatas() { if (this.importDatas.some((f) => !f.Success)) { MsgHelper.ShowWarningMessage('请修改错误后重新导入!'); } else { this.operator.CreateProductionPlanOrders(this.importDatas).subscribe( (result) => { if (result.Success) { MsgHelper.ShowSuccessMessage('保存成功!'); this.importDatas = []; } else { MsgHelper.ShowWarningMessage(`保存失败!${result.Message}`); } }, () => { MsgHelper.ShowErrorMessage(`保存错误`); } ); } } 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'; } /** 清空数据 */ clearImportedPlanDatas() { MsgHelper.ShowConfirmModal('是否继续', '是否清空所有导入的模板数据?', () => { this.importDatas = []; MsgHelper.ShowSuccessMessage('数据清空成功!'); }); } removeImportTempData(index: number) { MsgHelper.ShowConfirmModal('是否继续', '是否移除选中的数据?', () => { this.importDatas.splice(index, 1); this.importDatas = [...this.importDatas]; }); } //#endregion 计划导入 // }