import { Component, OnInit, ViewChild } from '@angular/core'; import { DataResultT, RhSelectableDto, RhBaseDto, RhTableButtonInfoDto, CodeClassModel, RhDynamicTableFieldDto, OpMode, RhTableQueryOptionDto, RhSelectItem, RhSafeAny } from 'rh-base/model'; import { MsgHelper, RhApiUrlsService, FileHelper, FormHelper } from 'rh-base/core'; import { RhPortType, RhPortLocalFieldType } from 'rh-base/model'; import { DynamicFormComponent } from 'rh-base/shared'; import { RhConfigManageService } from '../config-manage.service'; import { ImportStructureMetadataQueryDto, ModelDto, ImportStructureMetadataDto } from '../config-manage-model'; import { UntypedFormGroup, Validators, UntypedFormBuilder } from '@angular/forms'; @Component({ selector: 'rh-import-template-manage', templateUrl: './import-template-manage.component.html', styleUrls: ['./import-template-manage.component.less'] }) export class RhImportTemplateManageComponent implements OnInit { @ViewChild('queryForm', { static: false }) queryFormCmt!: DynamicFormComponent; selectedIndex = 0; dataLoading = false; dataresult!: DataResultT; defaultFieldDataset: RhDynamicTableFieldDto[] = []; // editDto: RhBaseDto = {}; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openEditTab(), true), new RhTableButtonInfoDto( 'createTemplate', '生成自定义模板', 'primary', false, 'file-sync', false, null, (datas) => this.createTemplate(datas as RhBaseDto[]), false ) ]; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as ImportStructureMetadataDto), true ) ]; extraButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'syncMetadataDatas', '元数据初始化', 'default', false, 'sync', false, null, (datas) => this.syncMetadataDatas(datas as RhBaseDto[]), true, true ) ]; modules: RhSelectItem[] = []; businesses: ModelDto[] = []; editFormBusinesses: ModelDto[] = []; activeModule: string | null = null; activeBusiness: string | null = null; businessLoading = false; editFormBusinessesLoading = false; defaultQueryDto = {}; checkIsUseSign!: boolean; editDto!: ImportStructureMetadataDto; editForm!: UntypedFormGroup; submitLoading = false; constructor(private apiUrls: RhApiUrlsService, private operator: RhConfigManageService, private fb: UntypedFormBuilder) { // } ngOnInit(): void { this.getModules(); } dataLoadingChange(status: boolean) { this.dataLoading = status; } setDatas(dataset: DataResultT) { this.dataresult = dataset; } setInitFieldDataset(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]; } openEditTab(data?: ImportStructureMetadataDto) { if (data) { this.editDto = data; this.editDto.OpSign = OpMode.OpEdit; this.changeModuleKey(this.editDto.ModuleKey as string); } else { this.editDto = this.initEditDto(); this.editDto.OpSign = OpMode.OpAdd; } this.createEditForm(this.editDto); this.selectedIndex = 1; } closeEditTab() { this.selectedIndex = 0; } refreshDatas(option: RhTableQueryOptionDto) { this.getDatas(option); } private getDatas(option?: RhTableQueryOptionDto) { if (this.queryFormCmt) { this.queryFormCmt.submitDefaultForm(null, option); } } getModules() { // GetSelectItems修改为GetBusinessTypeSelectItems this.operator.GetBusinessTypeSelectItems().subscribe((result) => { if (result.Success) { this.modules = result.Attach; } }); } checkIsUseClick($event: RhSafeAny) { if ($event.target.checked === true) { this.checkIsUseSign = true; } else { this.checkIsUseSign = false; } } getBusinesses(mod: string) { this.defaultQueryDto = { ModuleKey: mod, BusinessKey: null }; setTimeout(() => { //this.getDatas(); }); if (mod) { const queryDto = ImportStructureMetadataQueryDto.create(); queryDto.ModuleKey = mod; queryDto.Mode = 8; this.businessLoading = true; this.operator.GetBusinessKeys(queryDto).subscribe( (result) => { if (result.Success) { this.businesses = result.Attach; } else { this.businesses = []; } this.businessLoading = false; }, () => { this.businessLoading = false; } ); } else { this.businesses = []; } } getCheckedData(item: ModelDto) { if (item) { this.activeBusiness = item.BusinessKey as string; } else { this.activeBusiness = null; } this.defaultQueryDto = { ModuleKey: this.activeModule, BusinessKey: this.activeBusiness, IsUse: this.checkIsUseSign }; setTimeout(() => { this.getDatas(); }); } createTemplate(datas: RhBaseDto[]) { if (datas && datas.length) { const key = datas[0]['BusinessKey']; const name = datas[0]['BusinessName']; if (datas.every((data) => data['BusinessKey'] === key)) { const url = this.apiUrls.GetApiOperateUrl( RhPortType.mdp, 'TemplateManagementApi', 'CustomBuildImportTemplate', RhPortLocalFieldType.mdp ); FileHelper.downloadFileViaPost(url, datas, `${name}导入模板${new Date().getTime()}.xlsx`); setTimeout(() => { this.titleButtonDataset[1].loading = true; this.titleButtonDataset = [...this.titleButtonDataset]; }, 1000); } else { MsgHelper.ShowWarningMessage('选中的数据的业务键值必须一致!'); } } } //元数据初始化 syncMetadataDatas(datas: RhBaseDto[]) { //是否选择了业务名称 if (this.dataresult?.Success && this.dataresult?.Attach?.length) { MsgHelper.ShowConfirmModal('是否继续', '确定要元数据初始化吗?', () => { MsgHelper.ShowLoadingMessage('元数据初始化中,请稍后....', 1000); const key = this.dataresult?.Attach[0]['BusinessKey']; if (datas.every((data) => data['BusinessKey'] === key)) { this.operator.SyncMetadataDatas(key).subscribe((result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage('元数据初始化成功'); } else { MsgHelper.ShowWarningModal(`元数据初始化失败!${result.Message}`); } }); } else { MsgHelper.ShowWarningMessage('选中的数据的业务键值必须一致!'); } }); } else { MsgHelper.ShowConfirmModal('是否继续', '确定要全部元数据初始化吗?', () => { const key = ''; MsgHelper.ShowLoadingMessage('元数据初始化中,请稍后....', 21000); this.operator.SyncMetadataDatas(key).subscribe((result) => { if (result && result.Success) { MsgHelper.ShowSuccessMessage('全部元数据初始化成功'); } else { MsgHelper.ShowWarningModal(`元数据初始化失败!${result.Message}`); } }); }); } } // 编辑表单 initEditDto(): ImportStructureMetadataDto { // 使用构造函数初始化对象 const dto = ImportStructureMetadataDto.create(); // 默认值 dto.IsUse = true; dto.OpSign = OpMode.OpAdd; return dto; } createEditForm(dto: ImportStructureMetadataDto) { 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]], /** 字段名称 */ FieldName: [dto.FieldName, [Validators.required]], /** 字段表示 */ FieldDisplayName: [dto.FieldDisplayName], /** 排序ID */ SortId: [dto.SortId, [Validators.required]], /** 是否启用 */ IsUse: [dto.IsUse], /** 是否必填 */ IsRequired: [dto.IsRequired] }); } submitEditForm($event: Event) { if ($event) { $event.preventDefault(); } FormHelper.YGSubmitForm(this.editDto, this.editForm, (dto) => { this.submitLoading = true; this.operator.StoreImportTemplateStructureMetadataData(dto as ImportStructureMetadataDto).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); } // change changeModuleName(moduleName: string) { const data = this.modules.find((f) => f.Text === moduleName); if (data) { this.editForm.get('ModuleKey')?.setValue(data.Value); } else { this.editForm.get('ModuleKey')?.setValue(null); } } changeModuleKey(moduleKey: string) { if (moduleKey) { const queryDto = ImportStructureMetadataQueryDto.create(); queryDto.Mode = 8; queryDto.ModuleKey = moduleKey; this.editFormBusinessesLoading = true; this.operator.GetBusinessKeys(queryDto).subscribe( (result) => { if (result.Success) { this.editFormBusinesses = result.Attach; } else { this.editFormBusinesses = []; } this.editFormBusinessesLoading = false; }, () => { this.editFormBusinessesLoading = false; } ); } else { this.editFormBusinesses = []; } } changeBusinessName(businessName: string) { const data = this.editFormBusinesses.find((f) => f.BusinessName === businessName); if (data) { this.editForm.get('BusinessKey')?.setValue(data.BusinessKey); } else { this.editForm.get('BusinessKey')?.setValue(null); } } }