import { RhConfigManageService } from './../../config-manage.service'; import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { DateHelper, FileHelper, RhAppConfigService } from 'rh-base/core'; import { CodeClassModel, CodeCsExampleModel, CodeDomModelCodeTemplate, CodeJsonExampleModel, CodeTsExampleModel, CodeTSModel, RhAppConfigDto, RhProjectConfigDto, RhSafeAny } from 'rh-base/model'; import { CodeDomainModelCreateOption, CodeDomainModelFullTpl, CodeEFDomainModel, CodeEFDomainModelCreateOption, CodeFormExampleModel, DataDicServerConfigDto, DataDicServerConfigQueryDto, PrimaryKeyInfo, YGTableColumnInfo, YGTableInfo } from './../../config-manage-model'; @Component({ selector: 'app-backend-code-generate', templateUrl: './backend-code-generate.component.html', styleUrls: ['./backend-code-generate.component.less'] }) export class BackendCodeGenerateComponent implements OnInit { @ViewChild('editInput') inputRef!: ElementRef; constructor(private operator: RhConfigManageService, public appconfigSer: RhAppConfigService) { this.appconfig = this.appconfigSer.appConfig; } get projectConfig(): RhProjectConfigDto { return this.appconfigSer.projectConfig; } selectedTabIndex = 0; // selectTenant = 'IMayHaveTenant'; selectAuditLog = 'ICreationAudited'; isCollapsed = false; // serverConfigs: DataDicServerConfigDto[] = []; primaryKesTypes!: PrimaryKeyInfo[]; serverList: DataDicServerConfigDto[] = []; selectedServeConfig!: DataDicServerConfigDto; databaseList: DataDicServerConfigDto[] = []; selectedDatabaseConfig!: DataDicServerConfigDto; tableinfoes: YGTableInfo[] = []; activedTableInfo!: YGTableInfo; tableNamesOfactivedTableInfo: string[] = []; tableNamesOfactivedTableInfoBackup!: string[]; filterTblName!: string; selectedTableName!: string; codeModelCreateOption!: CodeEFDomainModelCreateOption; codeEFModelTpl: CodeEFDomainModel | RhSafeAny | null = null; editCache: Record = {}; dataset: YGTableColumnInfo[] = []; domainModelDlls: CodeDomModelCodeTemplate[] = []; selectedDomainModelDll: string | null = null; codeClassModels: CodeClassModel[] = []; filterTableName = ''; displayData = [...this.codeClassModels]; createDomainModelOption: CodeDomainModelCreateOption | null = null; csCodeModel: CodeClassModel | null = null; tsCodeModel: CodeTSModel | null = null; tsCodeExampleModel: CodeTsExampleModel | null = null; csCodeExampleModel: CodeCsExampleModel | null = null; jsonCodeExampleModel: CodeJsonExampleModel | null = null; formCodeExampleTpl: CodeFormExampleModel | null = null; tableCodeExampleTpl: CodeTsExampleModel | null = null; selectedTplTabIndex = 0; appconfig: RhAppConfigDto; // settingDataset: DataDicServerConfigDto[]=[]; isShowSettingModal = false; // currentModal: CodeDomainModelCreateOption; ngOnInit() { this.getCodeEFCreateConfig(); this.initCodeModelCreateOption(); } initCodeModelCreateOption(): void { this.codeModelCreateOption = new CodeEFDomainModelCreateOption('', '', '', '', false, true, true, false, false, '', null, null); this.createDomainModelOption = new CodeDomainModelCreateOption('', false, '', '', ''); } getCodeEFCreateConfig(): void { // const temp = DataDicServerConfigDto.create(); // temp.ServerName = this.appconfig.ServerIp; // this.serverList.push(temp); const queryDto = DataDicServerConfigQueryDto.create(); queryDto.Mode = 8; this.operator.GetDataDicServerConfigDatas(queryDto).subscribe((result) => { if (result && result.Success) { this.serverConfigs = result.Attach; } // this.primaryKesTypes = data.primaryKeys; result.Attach.forEach((dataitem) => { const serverItem = this.serverList.find((e) => e.ServerName === dataitem.ServerName); if (!serverItem) { this.serverList.push(dataitem); } }); }); } getServerConfig(): void { if (this.selectedServeConfig && this.serverConfigs.length) { // let tempdatas = []; this.databaseList = this.serverConfigs.filter((e) => e.ServerName === this.selectedServeConfig.ServerName); // this.projectConfig.TooltikDatabaseList.forEach((ele) => { // // const temp = tempdatas.find((a) => a.DatabaseName === ele); // const temp = CodeServerConfig.create(); // temp.ServerName = this.appconfig.ServerIp; // temp.DatabaseName = ele; // this.databaseList.push(temp); // }); // this.databaseList = this.projectConfig.TooltikDatabaseList; this.databaseList = [...this.databaseList]; } } getDatabaseConfig(): void { if (this.selectedDatabaseConfig) { this.operator.GetTableInfoes(this.selectedDatabaseConfig).subscribe((datas: YGTableInfo[]) => { this.tableinfoes = datas; }); } } getTableNames(table: YGTableInfo): void { this.activedTableInfo = table; this.tableNamesOfactivedTableInfo = table.TableNames as string[]; this.tableNamesOfactivedTableInfoBackup = [...this.tableNamesOfactivedTableInfo]; } searchTableName(): void { if (this.filterTblName && this.filterTblName !== null) { this.tableNamesOfactivedTableInfo = this.tableNamesOfactivedTableInfoBackup.filter((f) => f.indexOf(this.filterTblName) !== -1); } else { this.tableNamesOfactivedTableInfo = [...this.tableNamesOfactivedTableInfoBackup]; } } selectTable(tableName: string): void { this.selectedTableName = tableName; this.codeModelCreateOption.TableDescription = ''; this.codeModelCreateOption.StoreName = ''; this.codeModelCreateOption.EntityName = tableName; this.codeModelCreateOption.TableName = tableName; this.codeModelCreateOption.ServerConfig = this.selectedDatabaseConfig; this.selectedTabIndex = 0; this.dataset = []; this.editCache = {}; this.codeEFModelTpl = null; this.operator.GetTableColumnInfos(this.codeModelCreateOption).subscribe((data: CodeEFDomainModelCreateOption) => { this.codeModelCreateOption = data; this.dataset = data.TableColumnInfos as YGTableColumnInfo[]; this.updateEditCache(); }); } // private setAuditLogStatus( // isCreate: boolean, // isAudit: boolean, // isFullAudit: boolean // ): void { // this.codeModelCreateOption.IsImpICreationAudited = isCreate; // this.codeModelCreateOption.IsImpIAudited = isAudit; // this.codeModelCreateOption.IsImpIFullAudited = isFullAudit; // } // private setTenantStatus(isMay: boolean, isMust: boolean): void { // this.codeModelCreateOption.IsImpIMayHaveTenant = isMay; // this.codeModelCreateOption.IsImpIMustHaveTenant = isMust; // } // // 获取实体和Dto代码模型 // getEntityAndDtoCodeTpl(): void { // if (this.selectTenant === 'IMayHaveTenant') { // this.setTenantStatus(true, false); // } else { // this.setTenantStatus(false, true); // } // if (this.selectAuditLog === 'ICreationAudited') { // this.setAuditLogStatus(true, false, false); // } else if (this.selectAuditLog === 'IAudited') { // this.setAuditLogStatus(false, true, false); // } else if (this.selectAuditLog === 'IFullAudited') { // this.setAuditLogStatus(false, false, true); // } // this.codeModelCreateOption.TableColumnInfos = this.dataset; // this.codeModelCreateOption.ServerConfig = this.selectedDatabaseConfig; // // console.log(this.codeModelCreateOption); // this.operator.GetCodeEFDomainModelTpl(this.codeModelCreateOption).subscribe( // (data: CodeEFDomainModel) => { // this.codeEFModelTpl = data; // this.selectedTabIndex = 1; // }, // (error) => { // // eslint-disable-next-line no-console // console.log(error); // } // ); // } updateEditCache(): void { let id = 1; this.dataset.forEach((item) => { item.Id = id; if (!this.editCache[item.ColumnName as string]) { this.editCache[item.ColumnName as string] = { edit: false, data: item }; } id = id + 1; }); } startEdit(field: string): void { this.editCache[field].edit = true; setTimeout(() => { if (this.inputRef) { this.inputRef.nativeElement.focus(); this.inputRef.nativeElement.select(); } }, 100); } cancelEdit(field: string): void { this.editCache[field].edit = false; } saveEdit($event: RhSafeAny, field: string) { if ($event.keyCode === 27) { this.cancelEdit(field); } if ($event.keyCode === 13) { const index = this.dataset.findIndex((item) => item.ColumnName === field); const editedData = this.editCache[field].data; this.dataset[index] = editedData; this.editCache[field].edit = false; const id = editedData.Id + 1; const next = this.dataset.find((e) => e.Id === id); if (next) { // this.startEdit(next.ColumnName); } } } GetDomainModelDlls(): void { this.operator.GetDomainModelDlls().subscribe((datas: CodeDomModelCodeTemplate[]) => { this.domainModelDlls = datas; }); } GetCodeClassModels(): void { if (this.selectedDomainModelDll !== null) { this.operator.GetCodeClassModels(this.selectedDomainModelDll).subscribe((datas: CodeClassModel[]) => { this.codeClassModels = datas; this.displayData = [...this.codeClassModels]; }); } } SearchTableValue(): void { if (this.filterTableName === null) { this.displayData = this.codeClassModels; } else { this.displayData = this.codeClassModels.filter((f) => f.ClassName.indexOf(this.filterTableName) !== -1); } } SelectClassModel(m: CodeClassModel): void { this.csCodeModel = m; this.createDomainModelOption!.FormName = m.ClassName + 'Form'; this.createDomainModelOption!.TableName = m.ClassName + 'Table'; // 给可观察对象发送信息 this.operator.sendModalInfo(m); } GetCodeTpl(): void { const m: CodeClassModel = this.csCodeModel as CodeClassModel; this.createDomainModelOption!.ClassName = m.ClassName; this.createDomainModelOption!.AssemblyName = this.selectedDomainModelDll; this.operator.GetCodeDomainModelFullTpl(this.createDomainModelOption!).subscribe((data: CodeDomainModelFullTpl) => { this.tsCodeModel = data.TSCode as CodeTSModel; this.tsCodeExampleModel = data.TsExampleCode as CodeTsExampleModel; this.csCodeExampleModel = data.CsExampleCode as CodeCsExampleModel; this.jsonCodeExampleModel = data.JsonExampleCode as CodeJsonExampleModel; this.formCodeExampleTpl = data.FormModel as CodeFormExampleModel; this.tableCodeExampleTpl = data.TableModel as CodeTsExampleModel; this.selectedTplTabIndex = 1; }); } exportTableData() { FileHelper.exportTableData(this.dataset, 'editRowTable', this.setFileName()); } setFileName(): string { const format = DateHelper.format(new Date(Date.now()), 'yyMMddHHmmss'); const fileName = `data_${format}`; return fileName; } closeSettingModal() { this.isShowSettingModal = false; } openSettingModal() { this.isShowSettingModal = true; } setServerConfigChanges(s: DataDicServerConfigDto[]) { this.serverConfigs = s; this.serverList = []; if (this.serverConfigs.length) { this.serverConfigs.forEach((dataitem) => { const serverItem = this.serverList.find((e) => e.ServerName === dataitem.ServerName); if (!serverItem) { this.serverList.push(dataitem); } }); } } }