import { Component, OnInit, ViewChild } from '@angular/core'; import { UntypedFormGroup, UntypedFormBuilder, Validators } from '@angular/forms'; import { NzTreeNode, NzTreeComponent, NzFormatEmitEvent, NzTreeService } from 'ng-zorro-antd/tree'; import { RhAuxiliaryDataDictionaryDto, OpMode, RhSafeAny } from 'rh-base/model'; import { FormHelper, MsgHelper, RhTreeHelper } from 'rh-base/core'; import { RhSycService } from './../../syc.service'; import { ConfigGroupDatasDto, AuxiliaryDataDictionaryQueryDto } from './../../syc-models'; @Component({ selector: 'rh-auxiliary-dictionary', templateUrl: './auxiliary-dictionary.component.html', styleUrls: ['./auxiliary-dictionary.component.less'] }) export class RhAuxiliaryDictionaryComponent implements OnInit { RhAuxiliaryDataDictionaryDtoForm!: UntypedFormGroup; dataLoading = false; fFormData: RhAuxiliaryDataDictionaryDto | null = null; submitBusy = false; treeNodes: NzTreeNode[] = []; selectedTreeNode: NzTreeNode | null = null; selectedDataDic: RhAuxiliaryDataDictionaryDto | null = null; opMode!: number; @ViewChild('dataDicTree', { static: false }) dataDicTree!: NzTreeComponent; moduleList: ConfigGroupDatasDto[] = []; selectedModuleItem: ConfigGroupDatasDto | null = null; groupList: RhAuxiliaryDataDictionaryDto[] = []; constructor(private fb: UntypedFormBuilder, private operator: RhSycService, public treeSer: NzTreeService) {} ngOnInit() { this.fFormData = this.initRhAuxiliaryDataDictionaryDto(); this.createRhAuxiliaryDataDictionaryDtoForm(this.fFormData); this.initTreeNodes(); this.loadModuleList(); } initTreeNodes(): void { this.operator.GetAuxiliaryDicTreeNodes(new AuxiliaryDataDictionaryQueryDto(null, null, null, -1, 'PP')).subscribe((dr) => { if (dr.Success) { this.treeNodes = RhTreeHelper.ToNzTreeNodes(dr.Attach, this.treeSer); } }); } createRhAuxiliaryDataDictionaryDtoForm(dto: RhAuxiliaryDataDictionaryDto): void { this.RhAuxiliaryDataDictionaryDtoForm = this.fb.group({ ModuleKey: [dto.ModuleKey, [Validators.required]], ModuleName: [dto.ModuleName, [Validators.required]], GroupKey: [dto.GroupKey, [Validators.required]], GroupName: [dto.GroupName, [Validators.required]], Name: [dto.Name, [Validators.required]], DisplayName: [dto.DisplayName, [Validators.required]], ParentId: [dto.ParentId, [Validators.required]], LevelLayer: [{ value: dto.LevelLayer, disabled: true }], SortOrder: [dto.SortOrder], IsEnable: [dto.IsEnable], EditionId: [dto.EditionId, [Validators.required]], ProductName: [dto.ProductName, [Validators.required]] }); } initRhAuxiliaryDataDictionaryDto(): RhAuxiliaryDataDictionaryDto { return new RhAuxiliaryDataDictionaryDto( null, null, null, null, null, null, null, null, null, null, 1, null, null, OpMode.OpAdd, null, null ); } loadModuleList(): void { this.operator.GetConfigGroupDatas('ModuleName').subscribe((dr) => { if (dr.Success) { this.moduleList = dr.Attach; } }); } loadGroupList(moduleKey: string): void { this.operator.GetConfigGroupList(moduleKey).subscribe((dr) => { if (dr.Attach) { this.groupList = dr.Attach; } }); } setModuleNameChange(moduleName: string): void { if (this.RhAuxiliaryDataDictionaryDtoForm !== null) { const item = this.moduleList.find((f) => f.DisplayName === moduleName); if (item !== undefined) { this.selectedModuleItem = item; this.RhAuxiliaryDataDictionaryDtoForm.get('ModuleKey')!.setValue(item.Name); this.loadGroupList(item.Name); } else { this.RhAuxiliaryDataDictionaryDtoForm.get('ModuleKey')!.setValue(''); } } } setGroupNameChange(groupName: string): void { const item = this.groupList.find((f) => f.ModuleKey === this.selectedModuleItem?.Name && f.GroupName === groupName); if (this.RhAuxiliaryDataDictionaryDtoForm !== null) { if (item !== undefined) { { this.RhAuxiliaryDataDictionaryDtoForm.get('GroupKey')!.setValue(item.GroupKey); } } else { this.RhAuxiliaryDataDictionaryDtoForm.get('GroupKey')!.setValue(''); } } } submitForm($event: Event): void { $event.preventDefault(); FormHelper.YGSubmitForm( this.fFormData as RhAuxiliaryDataDictionaryDto, this.RhAuxiliaryDataDictionaryDtoForm, (dto: RhSafeAny) => { this.submitBusy = true; dto.IsEnable = dto.IsEnable ? 1 : 0; this.operator.StoreAuxiliaryDicConfigDatas(dto).subscribe((result) => { FormHelper.HandleOpResult(result, (r) => { const menu = r.Attach; if (this.opMode === 1) { this.addTreeNode(this.selectedTreeNode?.parentNode as NzTreeNode, menu); } else if (this.opMode === 2) { this.addTreeNode(this.selectedTreeNode as NzTreeNode, menu); } else if (this.opMode === -1 && this.selectedTreeNode) { this.selectedTreeNode.title = dto.DisplayName; this.selectedTreeNode.origin['customData'] = menu; } this.loadGroupList(dto.ModuleKey); this.resetForm($event); }); this.submitBusy = false; }); } ); } resetForm($event: Event): void { $event.preventDefault(); this.submitBusy = false; this.RhAuxiliaryDataDictionaryDtoForm.reset(this.initRhAuxiliaryDataDictionaryDto()); } addDicDataNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } const dicData = this.initRhAuxiliaryDataDictionaryDto(); dicData.ParentId = this.selectedDataDic?.ParentId; dicData.ProductName = this.selectedDataDic?.ProductName; dicData.LevelLayer = this.selectedTreeNode?.level; dicData.GroupKey = this.selectedDataDic?.GroupKey; dicData.GroupName = this.selectedDataDic?.GroupName; FormHelper.SetTValueToForm(dicData, this.RhAuxiliaryDataDictionaryDtoForm, ['ModuleKey', 'ModuleName']); this.opMode = 1; } addChildDicDataNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } const dicData = this.initRhAuxiliaryDataDictionaryDto(); dicData.ParentId = this.selectedDataDic?.IdKey; dicData.ProductName = this.selectedDataDic?.ProductName; dicData.LevelLayer = (this.selectedTreeNode?.level as number) + 1; dicData.GroupKey = this.selectedDataDic?.GroupKey; dicData.GroupName = this.selectedDataDic?.GroupName; FormHelper.SetTValueToForm(dicData, this.RhAuxiliaryDataDictionaryDtoForm); this.opMode = 2; } removeDicDataNode(): void { if (!this.checkIsSelectedTreeNode()) { return; } MsgHelper.ShowDeleteConfirm( '删除该数据节点,则与该节点数据所含有子节点数据将一并删除,同时系统的此功能将停止使用,您确定要删除吗?', () => { this.operator.RemoveAuxiliaryDicConfigDatas(this.selectedDataDic as RhAuxiliaryDataDictionaryDto).subscribe((result) => { const parentNode = this.selectedTreeNode?.parentNode; this.selectedTreeNode?.remove(); if (parentNode && parentNode.children.length > 0) { parentNode.isExpanded = true; } else if (parentNode) { parentNode.isLeaf = true; } this.createRhAuxiliaryDataDictionaryDtoForm(this.initRhAuxiliaryDataDictionaryDto()); }); }, () => { return true; } ); this.opMode = -1; } checkIsSelectedTreeNode(): boolean { if (this.selectedTreeNode === null) { MsgHelper.ShowWarningModal('请先选中数据节点!'); return false; } return true; } onSelectTreeNode($event: NzFormatEmitEvent): void { const treeNode = $event.node; if (treeNode?.isSelected) { this.selectedTreeNode = treeNode; const dicData = treeNode.origin['customData']; this.selectedDataDic = dicData; this.opMode = -1; FormHelper.SetTValueToForm(dicData, this.RhAuxiliaryDataDictionaryDtoForm); } else { this.selectedTreeNode!.isSelected = false; this.selectedTreeNode = null; this.fFormData = this.initRhAuxiliaryDataDictionaryDto(); this.createRhAuxiliaryDataDictionaryDtoForm(this.fFormData); } } addTreeNode(treeNode: NzTreeNode, data: RhAuxiliaryDataDictionaryDto): void { const node = new NzTreeNode( { title: data.DisplayName || '', key: data.Name || '', isLeaf: true, customData: data }, null, this.dataDicTree.nzTreeService ); node.isExpanded = true; treeNode.isLeaf = false; treeNode.addChildren([node]); treeNode.isExpanded = true; } }