using CoreICodeGenerationService = MiaoYu.Core.CodeGenerator.Services.ICodeGenerationService; using CoreDataSourceDto = MiaoYu.Core.CodeGenerator.Models.DataSourceDto; using CorePagingView = MiaoYu.Core.CodeGenerator.Models.PagingView; using CoreGenFormDto = MiaoYu.Core.CodeGenerator.Models.GenFormDto; namespace MiaoYu.Api.Admin.Controllers.DevelopmentTools; /// /// 代码生成器控制器 /// [ControllerDescriptor(MenuId = "31")] public class CodeGenerationController : AdminControllerBase { public CodeGenerationController(CoreICodeGenerationService defaultService) : base(defaultService) { } /// /// 获取所有数据库列表 /// /// [ActionDescriptor(DisplayName = "获取数据库列表")] [HttpGet] public List GetDatabasesAsync() { return _defaultService.GetAllDataSources(); } /// /// 获取列表 /// /// /// /// /// [ActionDescriptor(DisplayName = "查询列表")] [HttpPost("{size}/{page}")] public CorePagingView FindListAsync([FromRoute] int size, [FromRoute] int page, [FromBody] CoreGenFormDto search) { return _defaultService.GetGenContextDtos(page, size, search); } /// /// 获取代码 根据 表名 和 类型 /// /// /// [ActionDescriptor(DisplayName = "获取代码")] [HttpPost] public async Task GetCodeAsync([FromBody] CoreGenFormDto genFormDto) { var code = await _defaultService.GetCodeByTypeAndTableNameAsync(genFormDto); //var lowCodeTableInfos = new List(); if (!string.IsNullOrWhiteSpace(genFormDto.TableName)) { var table = _defaultService.GetGenContextDtoByTableName( genFormDto.TableName, genFormDto.DataBase); //lowCodeTableInfos = table.TableInfos; } return new { code, //appTableInfos = lowCodeTableInfos.Select(w => new //{ // w.Name=, // w.DefaultValue, // w.MaxLength, // w.Position, // w.DbTypeTextFull, // w.Comment, // CsTypeName = w.CsType.Name, // w.IsPrimary, // w.IsIdentity, // w.IsNullable //}).ToList() }; } /// /// 下载当前代码 /// /// /// [ActionDescriptor(DisplayName = "下载当前代码")] [HttpPost] public async Task DownloadAsync([FromBody] CoreGenFormDto genFormDto) { var (codeBytes, contentType, fileName) = await _defaultService.DownloadAsync(genFormDto); HttpContext.DownLoadFile(codeBytes, contentType, fileName); } /// /// 创建代码文件 /// /// /// [ActionDescriptor(DisplayName = "创建代码文件")] [HttpPost] public async Task DownloadAllAsync([FromBody] CoreGenFormDto genFormDto) { var (codeBytes, contentType, fileName) = await _defaultService.DownloadAllAsync(genFormDto); HttpContext.DownLoadFile(codeBytes, contentType, fileName); } /// /// 数据库字典文件 /// /// [ActionDescriptor(DisplayName = "生成数据库字典文件")] [HttpPost] public void CreateDataDictionary() { var data = _defaultService.CreateDataDictionary(); var fileName = $"{(string.IsNullOrWhiteSpace(data.dataBase) ? "" : data.dataBase + "_")}数据库设计{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"; HttpContext.DownLoadFile(data.excel, Tools.GetFileContentType[".xlsx"], fileName); } /// /// 生成代码并自动导入项目 /// /// /// [ActionDescriptor(DisplayName = "生成代码并自动导入项目")] [HttpPost] public async Task AutoImprotProjectAsync([FromBody] CoreGenFormDto genFormDto) { await _defaultService.AutoImprotProjectAsync(genFormDto); return true; } }