import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, CodeClassModel, RhDynamicTableFieldDto, RhTableQueryOptionDto, RhTableButtonInfoDto, RhBaseDto, OpMode, RhSelectItem, IRHInterfaceLocation, RhSafeAny } from 'rh-base/model'; import { MsgHelper, FormHelper, RhApiUrlsService } from 'rh-base/core'; import { RhPortType } from 'rh-base/model'; import { DynamicFormComponent } from 'rh-base/shared'; import { RhConfigManageService } from '../config-manage.service'; import { CodingRuleInput } from '../config-manage-model'; import { UntypedFormGroup, Validators, UntypedFormBuilder } from '@angular/forms'; @Component({ selector: 'rh-code-rule-manage', templateUrl: './code-rule-manage.component.html', styleUrls: ['./code-rule-manage.component.less'] }) export class RhCodeRuleManageComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; // editDto: RhBaseDto = {}; dataLoading = false; dataresult!: DataResultT; isShowCodeEditModal = false; isShowSampleModal = false; defaultFieldDataset: RhDynamicTableFieldDto[] = []; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as CodingRuleInput)), new RhTableButtonInfoDto('codeEdit', '规则编辑', 'default', false, 'setting', false, null, (data) => this.openCodeEditModal(data as RhBaseDto) ), new RhTableButtonInfoDto('codeSample', '编码示例', 'default', false, 'experiment', false, null, (data) => this.openSampleModal(data as RhBaseDto) ) ]; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openEditTab(), true) ]; selectedItem: RhBaseDto | null = null; groupNameItems: RhSelectItem[] = []; configNameItems: RhSelectItem[] = []; editDto!: CodingRuleInput; editForm!: UntypedFormGroup; submitLoading = false; sampleGenerateInterface: IRHInterfaceLocation = { interfaceType: 'post', port: this.apiUrl.getPortNum(RhPortType.rdp), controllerName: 'CodingRuleWebApi', interfaceName: 'GeneratingCode' }; constructor(private operator: RhConfigManageService, private fb: UntypedFormBuilder, private apiUrl: RhApiUrlsService) { // } ngOnInit(): void { this.getConfigGroupNameItems(); } openEditTab(data?: CodingRuleInput) { if (data) { this.editDto = data; this.editDto.OpSign = OpMode.OpEdit; } else { this.editDto = this.initEditDto(); } this.createEditForm(this.editDto); this.selectedIndex = 1; } closeEditTab() { this.selectedIndex = 0; } openCodeEditModal(data: RhBaseDto) { this.selectedItem = data; this.isShowCodeEditModal = true; } closeCodeEditModal() { this.selectedItem = null; this.isShowCodeEditModal = false; this.refreshDatas(void 0); } closeSampleModal() { this.isShowSampleModal = false; } openSampleModal(data: RhBaseDto) { this.selectedItem = null; this.selectedItem = data; this.isShowSampleModal = true; } setDatas(dataset: DataResultT) { this.dataresult = dataset; } dataLoadingChange(status: boolean) { this.dataLoading = status; } 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); } } initEditDto(): CodingRuleInput { // 使用构造函数初始化对象 const dto = CodingRuleInput.create(); dto.OpSign = OpMode.OpAdd; return dto; } createEditForm(dto: CodingRuleInput) { this.editForm = this.fb.group({ /** 模块键值 */ ModuleKey: [dto.ModuleKey, [Validators.required]], /** 模块名称 */ ModuleName: [dto.ModuleName, [Validators.required]], /** 业务键值 */ BusinessKey: [dto.BusinessKey, [Validators.required]], /** 业务名称 */ BusinessName: [dto.BusinessName, [Validators.required]] }); this.moduleKeyChange(dto.ModuleKey as string); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } this.submitLoading = true; FormHelper.YGSubmitForm(this.editDto, this.editForm, (dto: RhSafeAny) => { this.operator.StoreCodingRuleData(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); } moduleKeyChange(moduleKey: string) { if (moduleKey) { this.getConfigNameItems(moduleKey); const data = this.groupNameItems.find((f) => f.Value === moduleKey); if (data) { this.editForm.get('ModuleName')?.setValue(data.Text); } else { this.editForm.get('ModuleName')?.setValue(null); } } else { this.configNameItems = []; this.editForm.get('ModuleName')?.setValue(null); } } businessKeyChange(businessKey: string) { if (businessKey) { const data = this.configNameItems.find((f) => f.Value === businessKey); if (data) { this.editForm.get('BusinessName')?.setValue(data.Text); } else { this.editForm.get('BusinessName')?.setValue(null); } } else { this.editForm.get('BusinessName')?.setValue(null); } } 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 = []; } }); } }