using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Enums; using Infrastructure.Extensions; using Mapster; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using SqlSugar; using System; using System.Collections.Generic; using System.IO; using ARW.Admin.WebApi.Extensions; using ARW.Admin.WebApi.Filters; using ARW.CodeGenerator; using ARW.CodeGenerator.Model; using ARW.CodeGenerator.Service; using ARW.Common; using ARW.Model; using ARW.Model.System.Dto; using ARW.Model.System.Generate; using ARW.Service.System.IService; namespace ARW.Admin.WebApi.Controllers { /// /// 代码生成 /// [Verify] [Route("tool/gen")] public class CodeGeneratorController : BaseController { private readonly CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService(); private readonly IGenTableService GenTableService; private readonly IGenTableColumnService GenTableColumnService; private readonly IWebHostEnvironment WebHostEnvironment; public CodeGeneratorController( IGenTableService genTableService, IGenTableColumnService genTableColumnService, IWebHostEnvironment webHostEnvironment) { GenTableService = genTableService; GenTableColumnService = genTableColumnService; WebHostEnvironment = webHostEnvironment; } /// /// 获取所有数据库的信息 /// /// [HttpGet("getDbList")] [ActionPermissionFilter(Permission = "tool:gen:list")] public IActionResult GetListDataBase() { var dbList = _CodeGeneraterService.GetAllDataBases(); var defaultDb = dbList.Count > 0 ? dbList[0] : null; return SUCCESS(new { dbList, defaultDb }); } /// ///获取所有表根据数据库名 /// /// 数据库名 /// 表名 /// 分页信息 /// [HttpGet("getTableList")] [ActionPermissionFilter(Permission = "tool:gen:list")] public IActionResult FindListTable(string dbName, string? tableName, PagerInfo pager) { List list = _CodeGeneraterService.GetAllTables(dbName, tableName, pager); var page = new PagedInfo { TotalPage = pager.TotalPage, TotalNum = pager.TotalNum, PageSize = pager.PageSize, PageIndex = pager.PageNum, Result = list }; return SUCCESS(page); } /// /// 查询生成表数据 /// /// 表名 /// 分页信息 /// [HttpGet("list")] [ActionPermissionFilter(Permission = "tool:gen:list")] public IActionResult GetGenTable(string? tableName, PagerInfo pagerInfo) { //查询原表数据,部分字段映射到代码生成表字段 var rows = GenTableService.GetGenTables(new GenTable() { TableName = tableName }, pagerInfo); return SUCCESS(rows); } /// /// 修改代码生成业务查询 /// /// genTable表id /// [HttpGet("{tableId}")] [ActionPermissionFilter(Permission = "tool:gen:query")] public IActionResult GetColumnList(long tableId) { var tableInfo = GenTableService.GetGenTableInfo(tableId); var tables = GenTableService.GetGenTableAll(); if (tableInfo != null) { tableInfo.Columns = GenTableColumnService.GenTableColumns(tableId); } return SUCCESS(new { info = tableInfo, tables }); } /// /// 根据表id查询表列 /// /// genTable表id /// [HttpGet("column/{tableId}")] [ActionPermissionFilter(Permission = "tool:gen:query")] public IActionResult GetTableColumnList(long tableId) { var tableColumns = GenTableColumnService.GenTableColumns(tableId); return SUCCESS(new { columns = tableColumns }); } /// /// 删除代码生成 /// /// /// [Log(Title = "代码生成", BusinessType = BusinessType.DELETE)] [HttpDelete("{tableIds}")] [ActionPermissionFilter(Permission = "tool:gen:remove")] public IActionResult Remove(string tableIds) { long[] tableId = Tools.SpitLongArrary(tableIds); int result = GenTableService.DeleteGenTableByIds(tableId); return SUCCESS(result); } /// /// 导入表结构(保存) /// /// /// /// [HttpPost("importTable")] [Log(Title = "代码生成", BusinessType = BusinessType.IMPORT)] [ActionPermissionFilter(Permission = "tool:gen:import")] public IActionResult ImportTableSave(string tables, string dbName) { if (string.IsNullOrEmpty(tables)) { throw new CustomException("表不能为空"); } string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries); int result = 0; foreach (var tableName in tableNames) { var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, tableName); if (tabInfo != null) { GenTable genTable = CodeGeneratorTool.InitTable(dbName, HttpContext.GetName(), tableName, tabInfo?.Description); genTable.TableId = GenTableService.ImportGenTable(genTable); if (genTable.TableId > 0) { //保存列信息 List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName); List genTableColumns = CodeGeneratorTool.InitGenTableColumn(genTable, dbColumnInfos); GenTableColumnService.DeleteGenTableColumnByTableName(tableName); GenTableColumnService.InsertGenTableColumn(genTableColumns); genTable.Columns = genTableColumns; result++; } } } return ToResponse(result); } /// /// 修改保存代码生成业务 /// /// 请求参数实体 /// [HttpPut] [Log(Title = "代码生成", BusinessType = BusinessType.GENCODE, IsSaveRequestData = false)] [ActionPermissionFilter(Permission = "tool:gen:edit")] public IActionResult EditSave([FromBody] GenTableDto genTableDto) { if (genTableDto == null) throw new CustomException("请求参数错误"); if (genTableDto.BusinessName.Equals(genTableDto.ModuleName, StringComparison.OrdinalIgnoreCase)) { return ToResponse(ResultCode.CUSTOM_ERROR, "模块名不能和业务名一样"); } var genTable = genTableDto.Adapt().ToUpdate(HttpContext); //将前端额外参数转成字符串存入Options中 genTable.Options = genTableDto.Params.Adapt(); DbResult result = GenTableService.UseTran(() => { int rows = GenTableService.UpdateGenTable(genTable); if (rows > 0) { GenTableColumnService.UpdateGenTableColumn(genTable.Columns); } }); return SUCCESS(result.IsSuccess); } /// /// 预览代码 /// /// /// /// [HttpPost("preview/{tableId}")] [ActionPermissionFilter(Permission = "tool:gen:preview")] public IActionResult Preview(long tableId = 0, int VueVersion = 0) { GenerateDto dto = new(); dto.TableId = tableId; dto.VueVersion = VueVersion; if (dto == null || dto.TableId <= 0) { throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0); dto.GenTable = genTableInfo; dto.IsPreview = true; //生成代码 CodeGeneratorTool.Generate(dto); return SUCCESS(dto.GenCodes); } /// /// 生成代码(下载方式) /// /// 数据传输对象 /// [HttpPost("genCode")] [Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)] [ActionPermissionFilter(Permission = "tool:gen:code")] public IActionResult CodeGenerate([FromBody] GenerateDto dto) { if (dto?.TableId <= 0) { throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0); dto.GenTable = genTableInfo; //自定义路径 if (genTableInfo.GenType == "1") { string tempPath = WebHostEnvironment.ContentRootPath; var parentPath = Directory.GetParent(tempPath)?.Parent?.FullName; dto.GenType = "1"; //代码生成文件夹路径 dto.GenCodePath = genTableInfo.GenPath.IsEmpty() ? parentPath : genTableInfo.GenPath; } else { dto.GenType = "0"; dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode"); dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd")); } //生成压缩包 string zipReturnFileName = $"ARWAdmin.NET-{genTableInfo.TableComment}-{DateTime.Now:MMddHHmmss}.zip"; //生成代码到指定文件夹 CodeGeneratorTool.Generate(dto); //下载文件 FileHelper.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName); return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName }); } /// /// 生成Api代码(下载方式) /// /// 数据传输对象 /// [HttpPost("genCodeApi")] [Log(Title = "Api代码生成", BusinessType = BusinessType.GENCODE)] [ActionPermissionFilter(Permission = "tool:gen:codeApi")] public IActionResult CodeGenerateApi([FromBody] GenerateDto dto) { if (dto?.TableId <= 0) { throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); genTableInfo.Columns = GenTableColumnService.GenTableColumns(dto.TableId); dto.DbType = AppSettings.GetAppConfig("gen:dbType", 0); dto.GenTable = genTableInfo; //自定义路径 if (genTableInfo.GenType == "1") { string tempPath = WebHostEnvironment.ContentRootPath; var parentPath = Directory.GetParent(tempPath)?.Parent?.FullName; dto.GenType = "1"; //代码生成文件夹路径 dto.GenCodePath = genTableInfo.GenPath.IsEmpty() ? parentPath : genTableInfo.GenPath; } else { dto.GenType = "0"; dto.ZipPath = Path.Combine(WebHostEnvironment.WebRootPath, "Generatecode"); dto.GenCodePath = Path.Combine(dto.ZipPath, DateTime.Now.ToString("yyyyMMdd")); } //生成压缩包 string zipReturnFileName = $"ARWAdmin.NET-{genTableInfo.TableComment}-{DateTime.Now:MMddHHmmss}.zip"; //生成代码到指定文件夹 CodeGeneratorTool.GenerateApi(dto); //下载文件 FileHelper.ZipGenCode(dto.ZipPath, dto.GenCodePath, zipReturnFileName); return SUCCESS(new { path = "/Generatecode/" + zipReturnFileName, fileName = dto.ZipFileName }); } /// /// 同步数据库 /// /// /// /// [ActionPermissionFilter(Permission = "tool:gen:edit")] [Log(Title = "代码生成", BusinessType = BusinessType.UPDATE)] [HttpGet("synchDb/{tableId}")] public IActionResult SynchDb(string tableName, long tableId = 0) { if (string.IsNullOrEmpty(tableName) || tableId <= 0) throw new CustomException("参数错误"); GenTable table = GenTableService.GetGenTableInfo(tableId); if (table == null) { throw new CustomException("原表不存在"); } List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(table.DbName, tableName); List dbTableColumns = CodeGeneratorTool.InitGenTableColumn(table, dbColumnInfos); GenTableService.SynchDb(tableId, table, dbTableColumns); return SUCCESS(true); } } }