diff --git a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs index 7cffe87..c1cd221 100644 --- a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs @@ -1,15 +1,21 @@ using Infrastructure; using Infrastructure.Attribute; using Infrastructure.Enums; +using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; +using System; using System.Collections.Generic; 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.Generate; using ZR.Model.Vo; +using ZR.Service.System.IService; namespace ZR.Admin.WebApi.Controllers { @@ -20,6 +26,13 @@ namespace ZR.Admin.WebApi.Controllers 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; + } /// /// 获取所有数据库的信息 @@ -57,15 +70,15 @@ namespace ZR.Admin.WebApi.Controllers /// /// /// - [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); + //[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)); - } + // return SUCCESS(_CodeGeneraterService.GetColumnInfo(dbName, tableName)); + //} /// /// 代码生成器 @@ -86,5 +99,125 @@ namespace ZR.Admin.WebApi.Controllers 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); + //TODO 带做 删除表 + return SUCCESS(""); + } + + /// + /// 导入表 + /// + /// + /// + /// + [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 item in tableNames) + { + var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, item); + if (tabInfo != null) + { + GenTable genTable = new() + { + TableName = item, + TableComment = tabInfo.Description, + ClassName = CodeGeneratorTool.GetModelClassName(item), + CreateBy = userName, + CreateTime = DateTime.Now + }; + int rows = GenTableService.InsertGenTable(genTable); + if (rows > 0) + { + //保存列信息 + List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, item); + List genTableColumns = new(); + long tableId = 0; + foreach (var column in dbColumnInfos) + { + tableId = column.TableId; + + GenTableColumn genTableColumn = new() + { + ColumnName = column.DbColumnName, + ColumnComment = column.ColumnDescription, + IsPk = column.IsPrimarykey, + ColumnType = column.DataType, + TableId = rows, + TableName = item, + CsharpType = TableMappingHelper.GetPropertyDatatype(column.DataType), + CsharpField = column.DbColumnName.Substring(0, 1).ToUpper() + column.DbColumnName[1..], + IsRequired = column.IsNullable, + IsIncrement = column.IsIdentity, + CreateBy = userName, + CreateTime = DateTime.Now, + IsInsert = true, + IsEdit = true, + IsList = true, + IsQuery = false + }; + genTableColumns.Add(genTableColumn); + } + + GenTableColumnService.DeleteGenTableColumn(tableId); + GenTableColumnService.InsertGenTableColumn(genTableColumns); + } + } + } + + return SUCCESS(1); + } } } diff --git a/ZR.Admin.WebApi/Template/ServiceTemplate.txt b/ZR.Admin.WebApi/Template/ServiceTemplate.txt index 1c88cf6..340390a 100644 --- a/ZR.Admin.WebApi/Template/ServiceTemplate.txt +++ b/ZR.Admin.WebApi/Template/ServiceTemplate.txt @@ -9,7 +9,6 @@ using System.Text; using ZR.Common; using {ModelsNamespace}.Models; using {IRepositoriesNamespace}; -using {ServicesNamespace}.IService; namespace {ServicesNamespace}.Business { diff --git a/ZR.CodeGenerator/CodeGenerateTemplate.cs b/ZR.CodeGenerator/CodeGenerateTemplate.cs index fff483e..4e9ac44 100644 --- a/ZR.CodeGenerator/CodeGenerateTemplate.cs +++ b/ZR.CodeGenerator/CodeGenerateTemplate.cs @@ -42,10 +42,16 @@ namespace ZR.CodeGenerator if (!dbFieldInfo.IsNullable && !dbFieldInfo.IsIdentity) { vueViewEditFromRuleContent += $" {dbFieldInfo.DbColumnName}: [\n"; - vueViewEditFromRuleContent += $" {{ required: true, message:\"请输入{dbFieldInfo.ColumnDescription}\", trigger: \"blur\"}},\n"; + vueViewEditFromRuleContent += $" {{ required: true, message: '请输入{dbFieldInfo.ColumnDescription}', trigger: \"blur\"}},\n"; //vueViewEditFromRuleContent += " { min: 2, max: 50, message: \"长度在 2 到 50 个字符\", trigger:\"blur\" }\n"; vueViewEditFromRuleContent += " ],\n"; } + else if (TableMappingHelper.IsNumber(dbFieldInfo.DataType)) + { + vueViewEditFromRuleContent += $" {dbFieldInfo.DbColumnName}: [\n"; + vueViewEditFromRuleContent += $" {{ type: 'number', message: '{dbFieldInfo.DbColumnName}必须为数字值', trigger: \"blur\"}},\n"; + vueViewEditFromRuleContent += " ],\n"; + } return vueViewEditFromRuleContent; } @@ -98,24 +104,26 @@ namespace ZR.CodeGenerator //图片 vueViewFromContent += $" \n"; vueViewFromContent += $" \n"; - vueViewFromContent += $" \n"; - vueViewFromContent += " \n"; - vueViewFromContent += " \n"; - vueViewFromContent += $" \n"; + vueViewFromContent += $" \n"; + vueViewFromContent += " \n"; + vueViewFromContent += " \n"; + vueViewFromContent += $" \n"; vueViewFromContent += " \n"; } else if (CodeGeneratorTool.radioFiled.Any(f => columnName.Contains(f)) && (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint" || dbFieldInfo.DataType == "int")) { - vueViewFromContent += $" "; + vueViewFromContent += $" \n"; vueViewFromContent += $" \n"; - vueViewFromContent += " {{dict.dictLabel}}\n"; + vueViewFromContent += " \n"; + vueViewFromContent += " \n"; vueViewFromContent += " \n"; vueViewFromContent += " \n"; } else { + string inputNumTxt = TableMappingHelper.IsNumber(dbFieldInfo.DataType) ? ".number" : ""; vueViewFromContent += $" \n"; - vueViewFromContent += $" \n"; + vueViewFromContent += $" \n"; vueViewFromContent += " \n"; } diff --git a/ZR.CodeGenerator/Service/CodeGeneraterService.cs b/ZR.CodeGenerator/Service/CodeGeneraterService.cs index f22a3bf..1e5612d 100644 --- a/ZR.CodeGenerator/Service/CodeGeneraterService.cs +++ b/ZR.CodeGenerator/Service/CodeGeneraterService.cs @@ -33,10 +33,27 @@ namespace ZR.CodeGenerator.Service { tableList = tableList.Where(f => f.Name.ToLower().Contains(tableName.ToLower())).ToList(); } + tableList = tableList.Where(f => !new string[] { "gen", "sys_" }.Contains(f.Name)).ToList(); pager.TotalNum = tableList.Count; return tableList.Skip(pager.PageSize * (pager.PageNum - 1)).Take(pager.PageSize).OrderBy(f => f.Name).ToList(); } + /// + /// 获取单表数据 + /// + /// + /// + /// + public DbTableInfo GetTableInfo(string dbName, string tableName) + { + var tableList = GetSugarDbContext(dbName).DbMaintenance.GetTableInfoList(true); + if (!string.IsNullOrEmpty(tableName)) + { + return tableList.Where(f => f.Name.ToLower() == (tableName.ToLower())).FirstOrDefault(); + } + + return null; + } /// /// 获取列信息 /// diff --git a/ZR.CodeGenerator/TableMappingHelper.cs b/ZR.CodeGenerator/TableMappingHelper.cs index 44f8e38..302b0ec 100644 --- a/ZR.CodeGenerator/TableMappingHelper.cs +++ b/ZR.CodeGenerator/TableMappingHelper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using Infrastructure.Extensions; using ZR.Common.Extension; @@ -108,5 +109,11 @@ namespace ZR.CodeGenerator.CodeGenerator } return sTempDatatype; } + + public static bool IsNumber(string tableDataType) + { + string[] arr = new string[] { "int", "long" }; + return arr.Any(f => f.Replace("?", "").Contains(GetPropertyDatatype(tableDataType))); + } } } diff --git a/ZR.Model/System/Generate/GenTable.cs b/ZR.Model/System/Generate/GenTable.cs new file mode 100644 index 0000000..1d024e7 --- /dev/null +++ b/ZR.Model/System/Generate/GenTable.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ZR.Model.System.Generate +{ + /// + /// 代码生成表 + /// + [SqlSugar.SugarTable("gen_table")] + public class GenTable + { + [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] + public int TableId { get; set; } + public string TableName { get; set; } + public string TableComment { get; set; } + public string SubTableName { get; set; } + public string SubTableFkName { get; set; } + public string ClassName { get; set; } + public string TplCategory { get; set; } + public string PackageName { get; set; } + public string ModuleName { get; set; } + public string BusinessName { get; set; } + public string FunctionName { get; set; } + public string FunctionAuthor { get; set; } + public string GenType { get; set; } + public string Options { get; set; } + + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] + public string CreateBy { get; set; } + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] + public DateTime CreateTime { get; set; } + } +} diff --git a/ZR.Model/System/Generate/GenTableColumn.cs b/ZR.Model/System/Generate/GenTableColumn.cs new file mode 100644 index 0000000..eae7a08 --- /dev/null +++ b/ZR.Model/System/Generate/GenTableColumn.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ZR.Model.System.Generate +{ + /// + /// 代码生成表字段 + /// + [SqlSugar.SugarTable("gen_table_column")] + public class GenTableColumn + { + [SqlSugar.SugarColumn(IsIdentity = true, IsPrimaryKey = true)] + public int ColumnId { get; set; } + public string ColumnName { get; set; } + public int TableId { get; set; } + public string TableName { get; set; } + public string ColumnComment { get; set; } + public string ColumnType { get; set; } + public string CsharpType { get; set; } + public string CsharpField { get; set; } + public bool IsPk { get; set; } + public bool IsRequired { get; set; } + public bool IsIncrement { get; set; } + /// + /// 是否插入 + /// + public bool IsInsert { get; set; } + /// + /// 是否需要编辑 + /// + public bool IsEdit { get; set; } + /// + /// isList + /// + public bool IsList { get; set; } + public bool IsQuery { get; set; } + public int Sort { get; set; } + + public string CreateBy { get; set; } + public DateTime CreateTime { get; set; } + } +} diff --git a/ZR.Service/BaseService.cs b/ZR.Service/BaseService.cs index f2d3830..23c9f61 100644 --- a/ZR.Service/BaseService.cs +++ b/ZR.Service/BaseService.cs @@ -65,7 +65,7 @@ namespace ZR.Service } /// - /// 添加或更新数据 + /// 添加或更新数据,不推荐使用了 /// /// List /// diff --git a/ZR.Service/System/GenTableService.cs b/ZR.Service/System/GenTableService.cs new file mode 100644 index 0000000..b0c3ac9 --- /dev/null +++ b/ZR.Service/System/GenTableService.cs @@ -0,0 +1,114 @@ +using Infrastructure.Attribute; +using Infrastructure.Extensions; +using Infrastructure.Model; +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZR.Model.System.Generate; +using ZR.Service.System.IService; + +namespace ZR.Service.System +{ + /// + /// 代码生成表 + /// + [AppService(ServiceType = typeof(IGenTableService), ServiceLifetime = LifeTime.Transient)] + public class GenTableService : BaseService, IGenTableService + { + /// + /// 删除表 + /// + /// + /// + public int DeleteGenTable(GenTable table) + { + return Db.Deleteable().Where(f => f.TableName == table.TableName).ExecuteCommand(); + } + + /// + /// 获取表信息 + /// + /// + /// + public GenTable GetGenTableInfo(long tableId) + { + return GetId(tableId); + } + + /// + /// 查询代码生成表信息 + /// + /// + /// + /// + public PagedInfo GetGenTables(GenTable genTable, Model.PagerInfo pagerInfo) + { + var predicate = Expressionable.Create(); + predicate = predicate.AndIF(genTable.TableName.IfNotEmpty(), it => it.TableName.Contains(genTable.TableName)); + + return GetPages(predicate.ToExpression(), pagerInfo); + } + + /// + /// 插入代码生成表 + /// + /// + /// + public int InsertGenTable(GenTable table) + { + var db = Db; + DeleteGenTable(table); + return db.Insertable(table).ExecuteReturnIdentity(); + } + + /// + /// 获取表数据 + /// + /// + /// + public List SelectDbTableListByNamess(string[] tableNames) + { + throw new NotImplementedException(); + } + } + + /// + /// 代码生成表列 + /// + [AppService(ServiceType = typeof(IGenTableColumnService), ServiceLifetime = LifeTime.Transient)] + public class GenTableColumnService : BaseService, IGenTableColumnService + { + /// + /// 删除表字段 + /// + /// + /// + public int DeleteGenTableColumn(long tableId) + { + return Db.Deleteable().Where(f => f.TableId == tableId).ExecuteCommand(); + } + + /// + /// 获取表所有字段 + /// + /// + /// + public List GenTableColumns(long tableId) + { + return GetAll().OrderBy(x => x.Sort).ToList(); + } + + /// + /// 插入表字段 + /// + /// + /// + public int InsertGenTableColumn(List tableColumn) + { + return Db.Insertable(tableColumn).ExecuteCommand(); + } + } +} diff --git a/ZR.Service/System/IService/IGenTableService.cs b/ZR.Service/System/IService/IGenTableService.cs new file mode 100644 index 0000000..91aabd2 --- /dev/null +++ b/ZR.Service/System/IService/IGenTableService.cs @@ -0,0 +1,30 @@ +using Infrastructure.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ZR.Model.System.Generate; + +namespace ZR.Service.System.IService +{ + public interface IGenTableService + { + List SelectDbTableListByNamess(string[] tableNames); + + int InsertGenTable(GenTable tables); + + int DeleteGenTable(GenTable table); + PagedInfo GetGenTables(GenTable genTable, Model.PagerInfo pagerInfo); + GenTable GetGenTableInfo(long tableId); + } + + public interface IGenTableColumnService + { + int InsertGenTableColumn(List tableColumn); + + int DeleteGenTableColumn(long tableId); + + List GenTableColumns(long tableId); + } +} diff --git a/ZR.Vue/src/api/tool/gen.js b/ZR.Vue/src/api/tool/gen.js index 93d7a27..747e7b8 100644 --- a/ZR.Vue/src/api/tool/gen.js +++ b/ZR.Vue/src/api/tool/gen.js @@ -30,7 +30,7 @@ export function codeGetDBList() { /** * 获取数据库表 */ -export function codeGetTableList(data) { +export function listDbTable(data) { return request({ url: 'tool/gen/getTableList', method: 'get', @@ -53,14 +53,32 @@ export async function codeGenerator(data) { * @param {*} data * @returns */ -export function queryColumnInfo(data) { +export function queryColumnInfo(tableId) { return request({ - url: 'tool/gen/getColumnInfo', + url: 'tool/gen/Column/' + tableId, method: 'GET', - params: data, }) } + +// 查询表详细信息 +export function getGenTable(params) { + return request({ + url: 'tool/gen/getGenTable', + method: 'get', + params: params + }) +} +// 导入表 +export function importTable(data) { + return request({ + url: '/tool/gen/importTable', + method: 'post', + params: data + }) +} + + // /** // * // * 数据库解密 diff --git a/ZR.Vue/src/views/tool/gen/editTable.vue b/ZR.Vue/src/views/tool/gen/editTable.vue new file mode 100644 index 0000000..80e7549 --- /dev/null +++ b/ZR.Vue/src/views/tool/gen/editTable.vue @@ -0,0 +1,225 @@ + + diff --git a/ZR.Vue/src/views/tool/gen/genInfoForm.vue b/ZR.Vue/src/views/tool/gen/genInfoForm.vue new file mode 100644 index 0000000..27d6a22 --- /dev/null +++ b/ZR.Vue/src/views/tool/gen/genInfoForm.vue @@ -0,0 +1,300 @@ + + diff --git a/ZR.Vue/src/views/tool/gen/importTable.vue b/ZR.Vue/src/views/tool/gen/importTable.vue new file mode 100644 index 0000000..93abb0d --- /dev/null +++ b/ZR.Vue/src/views/tool/gen/importTable.vue @@ -0,0 +1,132 @@ + + + diff --git a/ZR.Vue/src/views/tool/gen/index.vue b/ZR.Vue/src/views/tool/gen/index.vue index 49df8e1..7b9855c 100644 --- a/ZR.Vue/src/views/tool/gen/index.vue +++ b/ZR.Vue/src/views/tool/gen/index.vue @@ -1,23 +1,23 @@ diff --git a/ZRAdmin.xml b/ZRAdmin.xml index 8133d63..cd83250 100644 --- a/ZRAdmin.xml +++ b/ZRAdmin.xml @@ -27,6 +27,47 @@ + + + 代码自动生成 + + + + + 接口 + + + + + 查询列表 + + + + + + 查询详情 + + + + + + + 添加 + + + + + + 更新 + + + + + + 删除 + + + 代码生成 @@ -47,14 +88,6 @@ 分页信息 - - - 获取表格列 - - - - - 代码生成器 @@ -62,6 +95,36 @@ 数据传输对象 + + + 获取表详细信息 + + + 分页信息 + + + + + 查询表字段列表 + + + + + + + 代码生成删除 + + + + + + + 导入表 + + + + + 心跳 diff --git a/document/admin-sqlserver.sql b/document/admin-sqlserver.sql index f7f556f..49a10ca 100644 Binary files a/document/admin-sqlserver.sql and b/document/admin-sqlserver.sql differ