using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Enums; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Filters; using ZR.CodeGenerator; using ZR.CodeGenerator.CodeGenerator; using ZR.CodeGenerator.Model; using ZR.CodeGenerator.Service; using ZR.Common; using ZR.Model; using ZR.Model.System.Dto; using ZR.Model.System.Generate; using ZR.Model.Vo; using ZR.Service.System.IService; namespace ZR.Admin.WebApi.Controllers { /// /// 代码生成 /// [Route("tool/gen")] public class CodeGeneratorController : BaseController { private CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService(); private IGenTableService GenTableService; private IGenTableColumnService GenTableColumnService; public CodeGeneratorController(IGenTableService genTableService, IGenTableColumnService genTableColumnService) { GenTableService = genTableService; GenTableColumnService = genTableColumnService; } /// /// 获取所有数据库的信息 /// /// [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 vm = new VMPageResult(list, pager); return SUCCESS(vm); } /// /// 获取表格列 /// /// /// /// //[HttpGet("getColumnInfo")] //[ActionPermissionFilter(Permission = "tool:gen:list")] //public IActionResult QueryColumnInfo(string dbName, string tableName) //{ // if (string.IsNullOrEmpty(dbName) || string.IsNullOrEmpty(tableName)) // return ToRespose(ResultCode.PARAM_ERROR); // return SUCCESS(_CodeGeneraterService.GetColumnInfo(dbName, tableName)); //} /// /// 代码生成器 /// /// 数据传输对象 /// [HttpPost("genCode")] [Log(Title = "代码生成", BusinessType = BusinessType.GENCODE)] [ActionPermissionFilter(Permission = "tool:gen:code")] public IActionResult Generate([FromBody] GenerateDto dto) { if (string.IsNullOrEmpty(dto.tableName) || dto.TableId <= 0) { throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); } var genTableInfo = GenTableService.GetGenTableInfo(dto.TableId); var getTableColumn = GenTableColumnService.GenTableColumns(dto.TableId); genTableInfo.Columns = getTableColumn; DbTableInfo dbTableInfo = new() { Name = dto.tableName }; CodeGeneratorTool.Generate(genTableInfo, dto); return SUCCESS(dbTableInfo); } /// /// 获取表详细信息 /// /// /// 分页信息 /// [HttpGet("getGenTable")] public IActionResult GetGenTable(string tableName, PagerInfo pagerInfo) { //if (string.IsNullOrEmpty(tableName)) //{ // throw new CustomException(ResultCode.CUSTOM_ERROR, "请求参数为空"); //} //查询原表数据,部分字段映射到代码生成表字段 var rows = GenTableService.GetGenTables(new GenTable() { TableName = tableName }, pagerInfo); return SUCCESS(rows); } /// /// 查询表字段列表 /// /// /// [HttpGet("column/{tableId}")] public IActionResult GetColumnList(long tableId) { var tableColumns = GenTableColumnService.GenTableColumns(tableId); var tableInfo = GenTableService.GetGenTableInfo(tableId); return SUCCESS(new { result = tableColumns, info = tableInfo }); } /// /// 删除代码生成 /// /// /// [Log(Title = "代码生成", BusinessType = BusinessType.DELETE)] [HttpDelete("{tableIds}")] [ActionPermissionFilter(Permission = "tool:gen:remove")] public IActionResult Remove(string tableIds) { long[] tableId = Tools.SpitLongArrary(tableIds); GenTableService.DeleteGenTableByIds(tableId); return SUCCESS(1); } /// /// 导入表 /// /// /// /// [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); string userName = User.Identity.Name; foreach (var tableName in tableNames) { var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, tableName); if (tabInfo != null) { GenTable genTable = new() { BaseNameSpace = "ZR.", ModuleName = "bus", ClassName = CodeGeneratorTool.GetClassName(tableName), BusinessName = CodeGeneratorTool.GetClassName(tableName), FunctionAuthor = ConfigUtils.Instance.GetConfig("gen:author"), FunctionName = tabInfo.Description, TableName = tableName, TableComment = tabInfo.Description, Create_by = userName, }; int rows = GenTableService.ImportGenTable(genTable); if (rows > 0) { //保存列信息 List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName); List genTableColumns = new(); foreach (var column in dbColumnInfos) { GenTableColumn genTableColumn = new() { ColumnName = CodeGeneratorTool.FirstLowerCase(column.DbColumnName), ColumnComment = column.ColumnDescription, IsPk = column.IsPrimarykey, ColumnType = column.DataType, TableId = rows, TableName = tableName, CsharpType = TableMappingHelper.GetPropertyDatatype(column.DataType), CsharpField = column.DbColumnName.Substring(0, 1).ToUpper() + column.DbColumnName[1..], IsRequired = column.IsNullable, IsIncrement = column.IsIdentity, Create_by = userName, Create_time = DateTime.Now, IsInsert = true, IsEdit = true, IsList = true, IsQuery = false, HtmlType = GenConstants.HTML_INPUT }; if (CodeGeneratorTool.imageFiled.Any(f => column.DbColumnName.ToLower().Contains(f.ToLower()))) { genTableColumn.HtmlType = GenConstants.HTML_IMAGE_UPLOAD; } if (genTableColumn.CsharpType.ToLower().Contains("datetime")) { genTableColumn.HtmlType = GenConstants.HTML_DATETIME; } if (CodeGeneratorTool.radioFiled.Any(f => column.DbColumnName.Contains(f))) { genTableColumn.HtmlType = GenConstants.HTML_RADIO; } if (column.Length > 200) { genTableColumn.HtmlType = GenConstants.HTML_TEXTAREA; } genTableColumns.Add(genTableColumn); } GenTableColumnService.DeleteGenTableColumnByTableName(tableName); GenTableColumnService.InsertGenTableColumn(genTableColumns); } } } return SUCCESS(1); } /// /// 代码生成保存 /// /// [HttpPut()] //[Log(Title = "代码生成", BusinessType = BusinessType.UPDATE)] [ActionPermissionFilter(Permission = "tool:gen:edit")] public IActionResult EditSave([FromBody]GenTableDto genTableDto) { if (genTableDto == null) throw new CustomException("请求参数错误"); var genTable = genTableDto.Adapt().ToUpdate(); int rows = GenTableService.UpdateGenTable(genTable); if (rows > 0) { GenTableColumnService.UpdateGenTableColumn(genTable.Columns); } return SUCCESS(rows); } } }