import { RHFeaturesDto, RHFeaturesQueryDto, RhMenuFeaturesQueryDto, RhMenuFeaturesDto, RhMenusDto, OpMode } from 'rh-base/model'; import { MsgHelper, FormHelper, ArrayHelper } from 'rh-base/core'; import { Component, OnInit, Output, Input, EventEmitter, OnChanges, SimpleChanges } from '@angular/core'; import { NzTreeNode } from 'ng-zorro-antd/tree'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { RhAacService } from './../../../aac.service'; @Component({ selector: 'rh-feature-menu-edit-snippet', templateUrl: './feature-menu-edit-snippet.component.html', styleUrls: ['./feature-menu-edit-snippet.component.less'] }) export class FeatureMenuEditSnippetComponent implements OnInit, OnChanges { @Input() rhTreeNode!: NzTreeNode; @Output() rhTreeNodeChange = new EventEmitter(); @Output() rhClose = new EventEmitter(); menuDto!: RhMenusDto; dataset: RhMenuFeaturesDto[] = []; dataLoading = false; // selectMenuFeature: MenuFeaturesDto; editForm!: UntypedFormGroup; editDto: RhMenuFeaturesDto | null = null; submitLoading = false; featureDataset: RHFeaturesDto[] = []; /** 是否自定义键值 */ customize = false; constructor(private operator: RhAacService, private fb: UntypedFormBuilder) {} ngOnInit() { this.menuDto = this.rhTreeNode.origin['customData']; this.getDatas(); this.getFeatureDataset(); this.editDto = this.initEditFormDto(); this.createEditForm(this.editDto); } ngOnChanges(changes: SimpleChanges) { const { rhTreeNode } = changes; if (rhTreeNode && !rhTreeNode.isFirstChange && rhTreeNode.currentValue) { this.menuDto = this.rhTreeNode.origin['customData']; this.getDatas(); } } initEditFormDto(): RhMenuFeaturesDto { // 使用构造函数初始化对象 const dto = RhMenuFeaturesDto.create(); dto.MenuName = this.menuDto.Name; dto.OpSign = OpMode.OpAdd; return dto; } createEditForm(dto: RhMenuFeaturesDto) { this.editForm = this.fb.group({ /** 菜单键值 */ MenuName: [dto.MenuName, [Validators.required]], /** 功能键值 */ FeatureName: [dto.FeatureName, [Validators.required]], /** 功能名称 */ FeatureDisplayName: [dto.FeatureDisplayName, [Validators.required]], /** 功能图标 */ FeatureIcon: [dto.FeatureIcon], /** 控制器名称 */ ApiControllerName: [dto.ApiControllerName], /** 操作名称 */ ApiActionName: [dto.ApiActionName], customize: [this.customize] }); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.editDto as RhMenuFeaturesDto, this.editForm, (dto) => { this.operator.StoreMenuFeaturesData(dto as RhMenuFeaturesDto).subscribe((result) => { FormHelper.HandleOpResult( result, () => { this.getDatas(); if (dto?.OpSign === OpMode.OpAdd) { MsgHelper.ShowSuccessMessage('功能菜单添加成功!'); } else { MsgHelper.ShowSuccessMessage('功能菜单编辑成功!'); } this.resetEditForm(null); }, () => { MsgHelper.ShowWarningModal(`存储信息失败!${result.Message}`); } ); }); }); } resetEditForm($event: Event | null) { if ($event) { $event.preventDefault(); } this.editDto = this.initEditFormDto(); this.editForm.reset(this.editDto); this.dataset.forEach((ele) => (ele.select = false)); } addData() { this.resetEditForm(null); } onSelectFeatureChange(name: string) { const target = this.featureDataset.find((ele) => ele.Name === name); if (target) { // this.editForm.get('FeatureName').patchValue(target.Name); this.editForm?.get('FeatureDisplayName')?.patchValue(target.DisplayName); } } onSelectFeatureMenu(item: RhMenuFeaturesDto) { this.dataset.forEach((ele) => { if (ele === item) { ele.select = !ele.select; if (item.select) { this.editDto = item; this.editDto.OpSign = OpMode.OpEdit; this.editForm.patchValue(this.editDto); } else { this.editDto = null; } } else { ele.select = false; } }); } removeFeatureMenu(item: RhMenuFeaturesDto) { MsgHelper.ShowConfirmModal('是否删除?', '是否删除功能菜单?', () => { this.operator.RemoveMenuFeaturesData(item).subscribe((result) => { if (result && result.Success) { this.dataset = ArrayHelper.Remove(this.dataset, (ele) => ele.FeatureName === item.FeatureName); MsgHelper.ShowSuccessMessage('功能菜单删除成功!'); } else { MsgHelper.ShowWarningModal(`功能菜单删除失败!${result.Message}`); } }); }); } onCustomizeChange(status: boolean) { this.customize = status; } getDatas() { const queryDto = RhMenuFeaturesQueryDto.create(); queryDto.MenuName = this.menuDto.Name; queryDto.Mode = 8; this.dataLoading = true; this.operator.GetMenuFeaturesDatas(queryDto).subscribe( (result) => { if (result && result.Success) { this.dataset = result.Attach; } else { MsgHelper.ShowInfoMessage(`获取功能菜单列表失败!${result.Message}`); this.dataset = []; } this.dataLoading = false; }, () => { this.dataLoading = false; this.dataset = []; } ); } closeEditTab() { this.resetEditForm(null); this.rhClose.emit(true); } /** 数据源 */ private getFeatureDataset() { const queryDto = RHFeaturesQueryDto.create(); this.operator.GetFeaturesDatas(queryDto).subscribe((result) => { if (result && result.Success) { this.featureDataset = result.Attach; } else { MsgHelper.ShowWarningMessage(`获取功能数据列表失败!${result.Message}`); } }); } }