import { RhAacService } from '../../../aac.service'; import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, FactoryInfoDto, DevProfileSettingCopyDto, DevProfileSettingDto, RhSelectItem, RhSafeAny, DevProfileSettingQueryDto, RhBaseDto, RhDynamicComponentFieldType } from 'rh-base/model'; import { MsgHelper, FormHelper, RhStorageService } from 'rh-base/core'; import { DynamicFormComponent, DynamicTableComponent } from 'rh-base/shared'; import { UntypedFormGroup, UntypedFormBuilder, Validators } from '@angular/forms'; const PROJECT_MAP_DATAS: RhSelectItem[] = [ new RhSelectItem('操作产品端数据', 'RH_Root'), new RhSelectItem('操作工控端数据', 'ITM_Root'), new RhSelectItem('操作WMS数据', 'RW_Root') ]; type OperationType = 'copy' | 'replace'; interface IQuickCopyDto { FactoryCode: string; NewFactoryCode: string; operationType: OperationType; } @Component({ selector: 'rh-multi-factory-dynamic-data-snippet', templateUrl: './multi-factory-dynamic-data-snippet.component.html', styleUrls: ['./multi-factory-dynamic-data-snippet.component.less'] }) export class MultiFactoryDynamicDataSnippetComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; @ViewChild('DisplayTable', { static: false }) displayTable!: DynamicTableComponent; dataLoading = false; dataresult!: DataResultT; defaultFieldDataset: RhDynamicComponentFieldType[] = []; titleButtonDataset: RhTableButtonInfoDto[] = []; extraButtonDataset = [ new RhTableButtonInfoDto( 'export', '导出', 'default', false, 'export', false, null, (datas) => this.exportDatas(datas as DevProfileSettingDto[]), false, void 0, void 0, null ), new RhTableButtonInfoDto( 'copy', '复制到...', 'default', false, 'copy', false, null, (datas) => { this.openFactoryModal(datas as DevProfileSettingDto[]); this.operationType = 'copy'; }, false ), new RhTableButtonInfoDto( 'diff', '替换到...', 'default', false, 'diff', false, null, (datas) => { this.openFactoryModal(datas as DevProfileSettingDto[]); this.operationType = 'replace'; }, false ), new RhTableButtonInfoDto( 'delete', '删除数据', 'primary', true, 'delete', true, null, (datas) => { this._deleteDevProfileDats(datas as RhBaseDto[]); }, false ) ]; copyLoading = false; isShowFactoryModal = false; selectedDatas!: DevProfileSettingDto[]; selectFactoryData: FactoryInfoDto | null = null; isShowCopyModal = false; copyDto!: IQuickCopyDto; copyForm!: UntypedFormGroup; selectCopyProject!: RhTableButtonInfoDto | RhSafeAny; queryDto!: Record; operationType: OperationType = 'copy'; scrollIndex = 0; constructor(public operator: RhAacService, public fb: UntypedFormBuilder, public storageSer: RhStorageService) { // } ngOnInit(): void { this.titleButtonDataset = this.initTitleButtonDatas(); const currentFactory = this.storageSer?.getUserSession()?.User?.FactoryCode; this.queryDto = { FactoryCode: currentFactory as string }; this.copyDto = { FactoryCode: '', NewFactoryCode: '', operationType: this.operationType }; this.createCopyForm(this.copyDto); } createCopyForm(dto: IQuickCopyDto) { this.copyForm = this.fb.group({ FactoryCode: [dto.FactoryCode, [Validators.required]], NewFactoryCode: [dto.NewFactoryCode, [Validators.required]], operationType: [dto.operationType, [Validators.required]] }); } submitCopyForm(event: Event) { if (event) { event.preventDefault(); } FormHelper.YGSubmitForm(this.copyDto, this.copyForm, (dto) => { if (dto?.FactoryCode === dto?.NewFactoryCode) { MsgHelper.ShowWarningMessage(`源工厂和目标工厂不能相同,请修改后重试!`); return; } this.quickCopyDynamicDatas(dto as IQuickCopyDto, this.operationType); }); } resetCopyForm(event: Event) { if (event) { event.preventDefault(); } this.copyForm.reset(); } closeFactoryModal() { this.isShowFactoryModal = false; this.operationType = 'copy'; } openFactoryModal(datas: DevProfileSettingDto[]) { this.isShowFactoryModal = true; this.selectedDatas = datas; } closeCopyModal() { this.isShowCopyModal = false; this.copyDto = { NewFactoryCode: '', FactoryCode: '', operationType: this.operationType }; this.copyForm.reset(this.copyDto); } openCopyModal(...args: RhSafeAny[]) { this.selectCopyProject = args[1]; this.isShowCopyModal = true; this.operationType = 'copy'; } confirmSelect() { if (!this.selectFactoryData || !this.selectedDatas?.length) { MsgHelper.ShowWarningMessage(`工厂未选择或没有需要复制的数据!`); return; } this.copyDynamicDatas(this.selectFactoryData.FactoryCode as string, this.selectedDatas, this.operationType, () => this.closeFactoryModal() ); } copyProjectDatas() { // } exportDatas(datas: DevProfileSettingDto[]) { // MsgHelper.ShowGlobalLoadingMessage(`正在导出数据,请稍候...`); this.operator.ExportToDevProfileConfigFile(datas); } locateScrollIndex() { if (this.displayTable) { this.displayTable.locateScrollIndex(); } } onSelectFactoryData(data: FactoryInfoDto[]) { if (data?.length === 1) { this.selectFactoryData = data[0]; } else { this.selectFactoryData = null; } } onOperationTypeChange(operationType: OperationType) { this.operationType = operationType; } setDatas(dataset: DataResultT) { this.dataresult = dataset; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } } dataLoadingChange(status: boolean) { this.dataLoading = status; } setInitFieldDatset(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]; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } //调用子组件中的接口 private getDatas(option?: RhTableQueryOptionDto) { if (this.queryFormCmt) { this.queryFormCmt.submitDefaultForm(null, option); } } private initTitleButtonDatas() { const tempDatas: RhTableButtonInfoDto[] = []; PROJECT_MAP_DATAS.forEach((ele) => { const temp = new RhTableButtonInfoDto( ele.Value as string, ele.Text, 'primary', false, 'project', false, null, (...args: RhSafeAny[]) => this.openCopyModal(...args), true ); tempDatas.push(temp); }); return tempDatas; } private getDynamicDatas(factoryCode: string) { const queryDto = DevProfileSettingQueryDto.create(); queryDto.FactoryCode = factoryCode; queryDto.FeatureCode = this.selectCopyProject.name; queryDto.Mode = 8; const dynamicDataPromise: Promise = new Promise((resolve, reject) => { this.operator.GetDevProfileSettingDatas(queryDto).subscribe( (result) => { if (result?.Success) { resolve(result.Attach); } else { MsgHelper.ShowWarningModal(`源工厂无对应配置数据!${result.Message}`); resolve([]); } }, () => { MsgHelper.ShowWarningModal(`获取源工厂工厂配置数据失败!`); reject(); } ); }); return dynamicDataPromise; } private async quickCopyDynamicDatas(dto: IQuickCopyDto, operationType: OperationType) { this.copyLoading = true; const sourceDatas = await this.getDynamicDatas(dto.FactoryCode); if (sourceDatas?.length) { this.copyDynamicDatas(dto.NewFactoryCode, sourceDatas, operationType, () => this.closeCopyModal()); } } private copyDynamicDatas(newFactoryCode: string, datas: DevProfileSettingDto[], operationType: OperationType, cb: () => void) { const tempData = DevProfileSettingCopyDto.create(); tempData.NewFactoryCode = newFactoryCode; tempData.Settings = datas; if (!this.copyLoading) { this.copyLoading = true; } MsgHelper.ShowGlobalLoadingMessage(`正在操作,数据量较大,请耐心等待。。。`, 0); this._copyDevProfileSettings(tempData, operationType).subscribe( (result) => { if (result.Success) { MsgHelper.ShowSuccessMessage(`数据${operationType === 'copy' ? '复制' : '替换'}成功!`); this.getDatas(); if (cb) { cb(); } } else { MsgHelper.ShowWarningModal(`数据${operationType === 'copy' ? '复制' : '替换'}失败!${result.Message}`); } this.copyLoading = false; MsgHelper.CloseGlobalLoadingMessage(); }, () => { MsgHelper.CloseGlobalLoadingMessage(); this.copyLoading = false; } ); } private _copyDevProfileSettings(tempData: DevProfileSettingCopyDto, operationType: OperationType) { if (operationType === 'copy') { return this.operator.CopyDevProfileSettings(tempData); } else { return this.operator.ModifyDevProfileSettingFactoryCode(tempData); } } private _deleteDevProfileDats(datas: RhBaseDto[]) { MsgHelper.ShowGlobalLoadingMessage(`正在删除数据,请稍候。。。`, 0); this.operator.RemoveDevProfileSettingDatas(datas).subscribe( (result) => { if (result?.Success) { MsgHelper.ShowSuccessMessage(`数据删除成功!`); this.getDatas(); } else { MsgHelper.ShowWarningModal(`数据删除失败!${result.Message}`); } MsgHelper.CloseGlobalLoadingMessage(); }, () => { MsgHelper.CloseGlobalLoadingMessage(); } ); } }