import { debounceTime } from 'rxjs/operators'; import { Subscription } from 'rxjs'; import { ConfigGroupDatasDto } from './../../syc-models'; import { RhConfigManageService } from './../config-manage.service'; import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, OpMode, RhSelectItem, RhSafeAny } from 'rh-base/model'; import { MsgHelper, FormHelper } from 'rh-base/core'; import { DynamicFormComponent } from 'rh-base/shared'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; @Component({ selector: 'rh-group-data-config', templateUrl: './group-data-config.component.html', styleUrls: ['./group-data-config.component.less'] }) export class RhGroupDataConfigComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; // editDto: RhBaseDto = {}; dataLoading = false; dataresult!: DataResultT; isShowEditModal = false; defaultFieldDataset: RhDynamicTableFieldDto[] = []; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as ConfigGroupDatasDto)) ]; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openEditTab(), true) ]; editDto!: ConfigGroupDatasDto; editForm!: UntypedFormGroup; submitLoading = false; groupNameItems: RhSelectItem[] = []; configNameItems: RhSelectItem[] = []; groupNameChangeSub: Subscription | null = null; groupKeyChangeSub: Subscription | null = null; displayNameChangeSub: Subscription | null = null; constructor(private operator: RhConfigManageService, public fb: UntypedFormBuilder) { // } ngOnInit(): void { this.editDto = this.initEditDto(); this.createEditForm(this.editDto); } ngOnDestroy() { this.cancelFormSub(); } setDatas(dataset: DataResultT) { this.dataresult = dataset; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } } dataLoadingChange(status: boolean) { this.dataLoading = status; } // onStoreData(result: DataResultT) { // 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: RhDynamicTableFieldDto[] = []; 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); } } openEditTab(data?: ConfigGroupDatasDto) { this.getConfigGroupNameItems(); if (data) { this.editDto = data; this.editDto.OpSign = OpMode.OpEdit; } else { this.editDto = this.initEditDto(); // this.editDto.OpSign = OpMode.OpAdd; } this.createEditForm(this.editDto); this.selectedIndex = 1; this.setFormSub(); } closeEditTab() { this.selectedIndex = 0; this.cancelFormSub(); } initEditDto(): ConfigGroupDatasDto { // 使用构造函数初始化对象 const dto = ConfigGroupDatasDto.create(); // 默认值 dto.Enable = true; dto.SortOrder = 1; dto.EditionId = 1; dto.OpSign = OpMode.OpAdd; return dto; } createEditForm(dto: ConfigGroupDatasDto) { this.editForm = this.fb.group({ /** 分组键 */ GroupKey: [dto.GroupKey, [Validators.required]], /** 分组名称 */ GroupName: [dto.GroupName, [Validators.required]], /** 键值 */ Name: [dto.Name, [Validators.required]], /** 名称 */ DisplayName: [dto.DisplayName, [Validators.required]], /** 排序号 */ SortOrder: [dto.SortOrder], /** 版本号 */ EditionId: [dto.EditionId], /** 产品名称 */ ProductName: [dto.ProductName, [Validators.required]], /** 是否启用 */ Enable: [dto.Enable] }); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } this.submitLoading = true; FormHelper.YGSubmitForm(this.editDto, this.editForm, (dto: RhSafeAny) => { if (dto.Enable) { dto.IsEnable = 1; } else { dto.IsEnable = 0; } this.submitLoading = true; this.operator.StoreGroupConfigDatas(dto).subscribe( (result) => { 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 = this.initEditDto(); this.getDatas(); } else { MsgHelper.ShowWarningModal(`存储数据失败!${result.Message}`); } this.submitLoading = false; }, () => { this.submitLoading = false; } ); }); } resetEditForm($event: Event) { if ($event) { $event.preventDefault(); } this.editDto = this.initEditDto(); this.editForm.reset(this.editDto); } // ********************辅助数据************************************ getConfigGroupNameItems() { this.operator.GetConfigGroupNameItems().subscribe((result) => { if (result.Success) { this.groupNameItems = result.Attach; } else { this.groupNameItems = []; } }); } getConfigNameItems(groupKey: string) { this.operator.GetConfigNameItems(groupKey).subscribe((result) => { if (result.Success) { this.configNameItems = result.Attach; } else { this.configNameItems = []; } }); } // change subscribeChangeGroupName() { this.groupNameChangeSub = this.editForm .get('GroupName') ?.valueChanges.pipe(debounceTime(500)) .subscribe((groupName) => { const data = this.groupNameItems.find((f) => f.Text === groupName); if (data) { this.editForm.get('GroupKey')?.setValue(data.Value); } else { this.editForm.get('GroupKey')?.setValue(null); } }) as Subscription; } subscribeChangeGroupKey() { this.groupKeyChangeSub = this.editForm .get('GroupKey') ?.valueChanges.pipe(debounceTime(500)) .subscribe((groupKey) => { this.getConfigNameItems(groupKey); }) as Subscription; } subscribeChangeDisplayName() { this.displayNameChangeSub = this.editForm .get('DisplayName') ?.valueChanges.pipe(debounceTime(500)) .subscribe((displayName) => { const data = this.configNameItems.find((f) => f.Text === displayName); if (data) { this.editForm.get('Name')?.setValue(data.Value); } else { this.editForm.get('Name')?.setValue(null); } }) as Subscription; } setFormSub() { this.subscribeChangeGroupKey(); this.subscribeChangeGroupName(); this.subscribeChangeDisplayName(); } cancelFormSub() { if (this.groupNameChangeSub) { this.groupNameChangeSub.unsubscribe(); } if (this.groupKeyChangeSub) { this.groupKeyChangeSub.unsubscribe(); } if (this.displayNameChangeSub) { this.displayNameChangeSub.unsubscribe(); } } }