diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs index fb677bb..c66f988 100644 --- a/Infrastructure/OptionsSetting.cs +++ b/Infrastructure/OptionsSetting.cs @@ -11,6 +11,8 @@ namespace Infrastructure public static string DbType = "DbType"; public static string CodeGenDbType = "CodeGenDbType"; public static string DbKey = "DbKey"; + public static string Gen_conn = "gen:conn"; + public static string Gen_conn_dbType = "gen:dbType"; public string Conn_Admin { get; set; } diff --git a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs index c1cd221..4287484 100644 --- a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs @@ -6,6 +6,8 @@ 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; @@ -13,6 +15,7 @@ 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; @@ -90,12 +93,15 @@ namespace ZR.Admin.WebApi.Controllers [ActionPermissionFilter(Permission = "tool:gen:code")] public IActionResult Generate([FromBody] GenerateDto dto) { - if (string.IsNullOrEmpty(dto.tableName)) + 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(dbTableInfo, dto); + CodeGeneratorTool.Generate(genTableInfo, dto); return SUCCESS(dbTableInfo); } @@ -133,7 +139,7 @@ namespace ZR.Admin.WebApi.Controllers } /// - /// 代码生成删除 + /// 删除代码生成 /// /// /// @@ -143,8 +149,9 @@ namespace ZR.Admin.WebApi.Controllers public IActionResult Remove(string tableIds) { long[] tableId = Tools.SpitLongArrary(tableIds); - //TODO 带做 删除表 - return SUCCESS(""); + + GenTableService.DeleteGenTableByIds(tableId); + return SUCCESS(1); } /// @@ -165,53 +172,73 @@ namespace ZR.Admin.WebApi.Controllers string[] tableNames = tables.Split(',', StringSplitOptions.RemoveEmptyEntries); string userName = User.Identity.Name; - foreach (var item in tableNames) + foreach (var tableName in tableNames) { - var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, item); + var tabInfo = _CodeGeneraterService.GetTableInfo(dbName, tableName); if (tabInfo != null) { GenTable genTable = new() { - TableName = item, + 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, - ClassName = CodeGeneratorTool.GetModelClassName(item), - CreateBy = userName, - CreateTime = DateTime.Now + Create_by = userName, }; - int rows = GenTableService.InsertGenTable(genTable); + int rows = GenTableService.ImportGenTable(genTable); if (rows > 0) { //保存列信息 - List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, item); + List dbColumnInfos = _CodeGeneraterService.GetColumnInfo(dbName, tableName); List genTableColumns = new(); - long tableId = 0; foreach (var column in dbColumnInfos) { - tableId = column.TableId; - GenTableColumn genTableColumn = new() { - ColumnName = column.DbColumnName, + ColumnName = CodeGeneratorTool.FirstLowerCase(column.DbColumnName), ColumnComment = column.ColumnDescription, IsPk = column.IsPrimarykey, ColumnType = column.DataType, TableId = rows, - TableName = item, + TableName = tableName, 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, + Create_by = userName, + Create_time = DateTime.Now, IsInsert = true, IsEdit = true, IsList = true, - IsQuery = false + 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.DeleteGenTableColumn(tableId); + GenTableColumnService.DeleteGenTableColumnByTableName(tableName); GenTableColumnService.InsertGenTableColumn(genTableColumns); } } @@ -219,5 +246,25 @@ namespace ZR.Admin.WebApi.Controllers 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); + } } } diff --git a/ZR.Admin.WebApi/Extensions/EntityExtension.cs b/ZR.Admin.WebApi/Extensions/EntityExtension.cs index c472ed5..2dfb6b3 100644 --- a/ZR.Admin.WebApi/Extensions/EntityExtension.cs +++ b/ZR.Admin.WebApi/Extensions/EntityExtension.cs @@ -49,27 +49,27 @@ namespace ZR.Admin.WebApi.Extensions return source; } - //public static TSource ToUpdate(this TSource source, UserSessionVM userSession) - //{ - // var types = source.GetType(); + public static TSource ToUpdate(this TSource source, HttpContext context = null) + { + var types = source.GetType(); - // if (types.GetProperty("UpdateTime") != null) - // { - // types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null); - // } + if (types.GetProperty("UpdateTime") != null) + { + types.GetProperty("UpdateTime").SetValue(source, DateTime.Now, null); + } - // if (types.GetProperty("UpdateID") != null) - // { - // types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null); - // } + //if (types.GetProperty("UpdateID") != null) + //{ + // types.GetProperty("UpdateID").SetValue(source, userSession.UserID, null); + //} - // if (types.GetProperty("UpdateName") != null) - // { - // types.GetProperty("UpdateName").SetValue(source, userSession.UserName, null); - // } + if (types.GetProperty("UpdateBy") != null) + { + types.GetProperty("UpdateBy").SetValue(source,context.GetName(), null); + } - // return source; - //} + return source; + } } } diff --git a/ZR.CodeGenerator/CodeGenerateOption.cs b/ZR.CodeGenerator/CodeGenerateOption.cs index abb237f..a3bad7f 100644 --- a/ZR.CodeGenerator/CodeGenerateOption.cs +++ b/ZR.CodeGenerator/CodeGenerateOption.cs @@ -49,6 +49,6 @@ namespace ZR.CodeGenerator /// /// 要生数据的表,用“,”分割 /// - public string TableList { get; set; } + //public string TableList { get; set; } } } diff --git a/ZR.CodeGenerator/CodeGenerateTemplate.cs b/ZR.CodeGenerator/CodeGenerateTemplate.cs index 4e9ac44..56af855 100644 --- a/ZR.CodeGenerator/CodeGenerateTemplate.cs +++ b/ZR.CodeGenerator/CodeGenerateTemplate.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using ZR.CodeGenerator.CodeGenerator; +using ZR.Model.System.Generate; namespace ZR.CodeGenerator { @@ -20,11 +21,11 @@ namespace ZR.CodeGenerator /// /// /// - public static string GetVueJsMethod(DbColumnInfo dbColumnInfo) + public static string GetVueJsMethod(GenTableColumn dbColumnInfo) { - string columnName = CodeGeneratorTool.FirstLowerCase(dbColumnInfo.DbColumnName); + string columnName = dbColumnInfo.ColumnName; string js = ""; - if (CodeGeneratorTool.imageFiled.Any(f => columnName.Contains(f))) + if (dbColumnInfo.HtmlType.Equals(GenConstants.HTML_IMAGE_UPLOAD)) { js += $"handleUpload{columnName}Success(res, file) {{\n"; js += $" this.form.{columnName} = URL.createObjectURL(file.raw);\n"; @@ -35,21 +36,20 @@ namespace ZR.CodeGenerator } //rules - public static string GetFormRules(DbColumnInfo dbFieldInfo) + public static string GetFormRules(GenTableColumn dbFieldInfo) { string vueViewEditFromRuleContent = ""; //Rule 规则验证 - if (!dbFieldInfo.IsNullable && !dbFieldInfo.IsIdentity) + if (!dbFieldInfo.IsPk && !dbFieldInfo.IsIncrement) { - vueViewEditFromRuleContent += $" {dbFieldInfo.DbColumnName}: [\n"; - vueViewEditFromRuleContent += $" {{ required: true, message: '请输入{dbFieldInfo.ColumnDescription}', trigger: \"blur\"}},\n"; - //vueViewEditFromRuleContent += " { min: 2, max: 50, message: \"长度在 2 到 50 个字符\", trigger:\"blur\" }\n"; + vueViewEditFromRuleContent += $" {dbFieldInfo.ColumnName}: [\n"; + vueViewEditFromRuleContent += $" {{ required: true, message: '请输入{dbFieldInfo.ColumnComment}', trigger: \"blur\"}},\n"; vueViewEditFromRuleContent += " ],\n"; } - else if (TableMappingHelper.IsNumber(dbFieldInfo.DataType)) + else if (TableMappingHelper.IsNumber(dbFieldInfo.ColumnType) && dbFieldInfo.IsRequired) { - vueViewEditFromRuleContent += $" {dbFieldInfo.DbColumnName}: [\n"; - vueViewEditFromRuleContent += $" {{ type: 'number', message: '{dbFieldInfo.DbColumnName}必须为数字值', trigger: \"blur\"}},\n"; + vueViewEditFromRuleContent += $" {dbFieldInfo.ColumnName}: [\n"; + vueViewEditFromRuleContent += $" {{ type: 'number', message: '{dbFieldInfo.ColumnName}必须为数字值', trigger: \"blur\"}},\n"; vueViewEditFromRuleContent += " ],\n"; } @@ -57,49 +57,49 @@ namespace ZR.CodeGenerator } //model 属性 - public static string GetModelTemplate(DbColumnInfo dbFieldInfo) + public static string GetModelTemplate(GenTableColumn dbFieldInfo) { - string columnName = dbFieldInfo.DbColumnName.Substring(0, 1).ToUpper() + dbFieldInfo.DbColumnName[1..]; var modelcontent = ""; modelcontent += " /// \n"; - modelcontent += $" /// 描述 :{dbFieldInfo.ColumnDescription}\n"; - modelcontent += $" /// 空值 :{dbFieldInfo.IsNullable}\n"; - modelcontent += $" /// 默认 :{dbFieldInfo.DefaultValue}\n"; + modelcontent += $" /// 描述 :{dbFieldInfo.ColumnComment}\n"; + modelcontent += $" /// 空值 :{dbFieldInfo.IsRequired}\n"; modelcontent += " /// \n"; - if (dbFieldInfo.IsIdentity || dbFieldInfo.IsPrimarykey) + if (dbFieldInfo.IsPk || dbFieldInfo.IsIncrement) { - modelcontent += $" [SqlSugar.SugarColumn(IsPrimaryKey = {dbFieldInfo.IsPrimarykey.ToString().ToLower()}, IsIdentity = {dbFieldInfo.IsIdentity.ToString().ToLower()})]\n"; + modelcontent += $" [SqlSugar.SugarColumn(IsPrimaryKey = {dbFieldInfo.IsPk.ToString().ToLower()}, IsIdentity = {dbFieldInfo.IsIncrement.ToString().ToLower()})]\n"; } - modelcontent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r"; + modelcontent += $" public {dbFieldInfo.CsharpType} {dbFieldInfo.CsharpField} {{ get; set; }}\n\r"; return modelcontent; } //DTO model - public static string GetDtoContent(DbColumnInfo dbFieldInfo) + public static string GetDtoContent(GenTableColumn dbFieldInfo) { - string columnName = dbFieldInfo.DbColumnName.Substring(0, 1).ToUpper() + dbFieldInfo.DbColumnName[1..]; string InputDtoContent = ""; - InputDtoContent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r"; + if (dbFieldInfo.IsInsert || dbFieldInfo.IsEdit) + { + InputDtoContent += $" public {dbFieldInfo.CsharpType} {dbFieldInfo.CsharpField} {{ get; set; }}\n\r"; + } return InputDtoContent; } //form-item - public static string GetVueViewFormContent(DbColumnInfo dbFieldInfo) + public static string GetVueViewFormContent(GenTableColumn dbFieldInfo) { - string columnName = CodeGeneratorTool.FirstLowerCase(dbFieldInfo.DbColumnName); - string labelName = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnDescription, columnName); + string columnName = dbFieldInfo.ColumnName; + string labelName = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, columnName); string vueViewFromContent = ""; - string labelDisabled = dbFieldInfo.IsIdentity ? ":disabled=\"true\"" : ""; - string placeHolder = dbFieldInfo.IsIdentity ? "" : $"请输入{CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnDescription, columnName)}"; + string labelDisabled = dbFieldInfo.IsPk ? ":disabled=\"true\"" : ""; + string placeHolder = dbFieldInfo.IsIncrement ? "" : $"请输入{labelName}"; - if (dbFieldInfo.DataType == "datetime") + if (dbFieldInfo.HtmlType == GenConstants.HTML_DATETIME) { //时间 vueViewFromContent += $" \n"; vueViewFromContent += $" \n"; vueViewFromContent += " \n"; } - else if (CodeGeneratorTool.imageFiled.Any(f => columnName.Contains(f))) + else if (dbFieldInfo.HtmlType == GenConstants.HTML_IMAGE_UPLOAD) { //图片 vueViewFromContent += $" \n"; @@ -110,7 +110,7 @@ namespace ZR.CodeGenerator vueViewFromContent += $" \n"; vueViewFromContent += " \n"; } - else if (CodeGeneratorTool.radioFiled.Any(f => columnName.Contains(f)) && (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint" || dbFieldInfo.DataType == "int")) + else if (dbFieldInfo.HtmlType == GenConstants.HTML_RADIO) { vueViewFromContent += $" \n"; vueViewFromContent += $" \n"; @@ -119,10 +119,16 @@ namespace ZR.CodeGenerator vueViewFromContent += " \n"; vueViewFromContent += " \n"; } + else if (dbFieldInfo.HtmlType == GenConstants.HTML_TEXTAREA) + { + vueViewFromContent += $" \n"; + vueViewFromContent += $" \n"; + vueViewFromContent += " \n"; + } else { - string inputNumTxt = TableMappingHelper.IsNumber(dbFieldInfo.DataType) ? ".number" : ""; - vueViewFromContent += $" \n"; + string inputNumTxt = TableMappingHelper.IsNumber(dbFieldInfo.CsharpType) ? ".number" : ""; + vueViewFromContent += $" \n"; vueViewFromContent += $" \n"; vueViewFromContent += " \n"; } @@ -131,14 +137,18 @@ namespace ZR.CodeGenerator } //table-column - public static string GetTableColumn(DbColumnInfo dbFieldInfo) + public static string GetTableColumn(GenTableColumn dbFieldInfo) { - string columnName = CodeGeneratorTool.FirstLowerCase(dbFieldInfo.DbColumnName); - string label = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnDescription, columnName); + string columnName = dbFieldInfo.ColumnName; + string label = CodeGeneratorTool.GetLabelName(dbFieldInfo.ColumnComment, columnName); string vueViewListContent = ""; - string showToolTip = dbFieldInfo.DataType.Contains("varchar") ? ":show-overflow-tooltip=\"true\"" : ""; + string showToolTip = dbFieldInfo.ColumnType.Contains("varchar") ? ":show-overflow-tooltip=\"true\"" : ""; + if (!dbFieldInfo.IsQuery) + { + return vueViewListContent; + } - if (CodeGeneratorTool.imageFiled.Any(f => columnName.ToLower().Contains(f))) + if (dbFieldInfo.HtmlType.Equals(GenConstants.HTML_IMAGE_UPLOAD)) { vueViewListContent += $" \n"; vueViewListContent += " \n"; vueViewListContent += " \n"; } - else if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint") - { - vueViewListContent += $" \n"; - vueViewListContent += " \n"; - vueViewListContent += " \n"; - } + //else if (dbFieldInfo.HtmlType.Equals(GenConstants.HTML_RADIO)) + //{ + // vueViewListContent += $" \n"; + // vueViewListContent += " \n"; + // vueViewListContent += " \n"; + //} else { //table-column - vueViewListContent += $" \n"; + vueViewListContent += $" \n"; } return vueViewListContent; } diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index 6538bcd..f25a97f 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -1,10 +1,12 @@ -using SqlSugar; +using Infrastructure; +using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using ZR.CodeGenerator.Model; using ZR.CodeGenerator.Service; +using ZR.Model.System.Generate; namespace ZR.CodeGenerator { @@ -33,7 +35,7 @@ namespace ZR.CodeGenerator /// /// /// - public static void Generate(DbTableInfo dbTableInfo, GenerateDto dto) + public static void Generate(GenTable dbTableInfo, GenerateDto dto) { _option.BaseNamespace = "ZR."; //_option.TableList = listTable; @@ -46,9 +48,9 @@ namespace ZR.CodeGenerator _option.ServicesNamespace = _option.BaseNamespace + "Service"; _option.ApiControllerNamespace = _option.BaseNamespace + "Admin.WebApi"; - CodeGeneraterService codeGeneraterService = new(); - List listField = codeGeneraterService.GetColumnInfo(dto.dbName, dbTableInfo.Name); - GenerateSingle(listField, dbTableInfo, dto); + //CodeGeneraterService codeGeneraterService = new(); + //List listField = codeGeneraterService.GetColumnInfo(dto.dbName, dbTableInfo.TableName); + GenerateSingle(dbTableInfo?.Columns, dbTableInfo, dto); //GenerateDtoProfile(_option.ModelsNamespace, profileContent, ifExsitedCovered); } @@ -59,9 +61,9 @@ namespace ZR.CodeGenerator /// 表字段集合 /// 表信息 /// - public static void GenerateSingle(List listField, DbTableInfo tableInfo, GenerateDto dto) + public static void GenerateSingle(List listField, GenTable tableInfo, GenerateDto dto) { - var modelTypeName = GetModelClassName(tableInfo.Name);//表名对应C# 实体类名 + var modelTypeName = tableInfo.ClassName;//表名对应C# 实体类名 var primaryKey = "id";//主键 string keyTypeName = "int";//主键数据类型 @@ -77,26 +79,24 @@ namespace ZR.CodeGenerator string vueViewEditFromRuleContent = string.Empty;//Vue数据校验 string vueJsMethod = string.Empty;//Vue js自定义方法 - foreach (DbColumnInfo dbFieldInfo in listField) + foreach (GenTableColumn dbFieldInfo in listField) { - string columnName = FirstLowerCase(dbFieldInfo.DbColumnName); + string columnName = dbFieldInfo.ColumnName; - if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint") + if (dbFieldInfo.ColumnType == "bool" || dbFieldInfo.ColumnType == "tinyint") { vueViewEditFromContent += $" {columnName}: 'true',\n"; - //vueViewEditFromBindContent += $" this.form.{columnName} = res.data.{0}+''\n"; } else { vueViewEditFromContent += $" {columnName}: undefined,\n"; - //vueViewEditFromBindContent += $" {columnName}: row.{columnName},\n"; } //vueViewSaveBindContent += string.Format(" '{0}':this.editFrom.{0},\n", columnName); //主键 - if (dbFieldInfo.IsIdentity || dbFieldInfo.IsPrimarykey) + if (dbFieldInfo.IsPk || dbFieldInfo.IsIncrement) { primaryKey = columnName.Substring(0, 1).ToUpper() + columnName[1..]; - keyTypeName = dbFieldInfo.DataType; + keyTypeName = dbFieldInfo.CsharpType; } else { @@ -104,7 +104,6 @@ namespace ZR.CodeGenerator updateColumn += $" {tempColumnName} = parm.{tempColumnName},\n"; } - dbFieldInfo.DbColumnName = columnName; modelContent += CodeGenerateTemplate.GetModelTemplate(dbFieldInfo); vueViewFormContent += CodeGenerateTemplate.GetVueViewFormContent(dbFieldInfo); vueJsMethod += CodeGenerateTemplate.GetVueJsMethod(dbFieldInfo); @@ -117,8 +116,8 @@ namespace ZR.CodeGenerator replaceDto.PrimaryKey = primaryKey; replaceDto.ModelTypeName = modelTypeName; replaceDto.ModelProperty = modelContent; - replaceDto.TableName = tableInfo.Name; - replaceDto.TableDesc = tableInfo.Description; + replaceDto.TableName = tableInfo.TableName; + replaceDto.TableDesc = tableInfo.TableComment; replaceDto.InputDtoProperty = InputDtoContent; replaceDto.updateColumn = updateColumn; replaceDto.VueJsMethod = vueJsMethod; @@ -407,19 +406,29 @@ namespace ZR.CodeGenerator /// /// 如果有前缀替换将前缀替换成空,替换下划线"_"为空再将首字母大写 + /// 表名转换成C#类名 /// - /// + /// /// - public static string GetModelClassName(string modelTypeName) + public static string GetClassName(string tableName) { - if (!string.IsNullOrEmpty(_option.ReplaceTableNameStr)) + bool autoRemovePre = ConfigUtils.Instance.GetAppConfig("gen:autoPre", false); + string tablePrefix = ConfigUtils.Instance.GetAppConfig("gen:tablePrefix"); + + if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre) { - modelTypeName = modelTypeName.Replace(_option.ReplaceTableNameStr.ToString(), ""); + string[] searcList = tablePrefix.Split(",",StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < searcList.Length; i++) + { + if (!string.IsNullOrEmpty(searcList[i].ToString())) + { + tableName = tableName.Replace(searcList[i].ToString(), ""); + } + } } - modelTypeName = modelTypeName.Replace("_", ""); - modelTypeName = modelTypeName.Substring(0, 1).ToUpper() + modelTypeName[1..]; - return modelTypeName; + return tableName.Substring(0, 1).ToUpper() + tableName[1..].Replace("_", ""); } + /// /// 首字母转小写,输出前端 /// diff --git a/ZR.CodeGenerator/DbProvider.cs b/ZR.CodeGenerator/DbProvider.cs index 56525f3..3da9e12 100644 --- a/ZR.CodeGenerator/DbProvider.cs +++ b/ZR.CodeGenerator/DbProvider.cs @@ -19,8 +19,8 @@ namespace ZR.CodeGenerator /// public SqlSugarScope GetSugarDbContext(string dbName = "") { - string connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.Conn).Replace("{database}", dbName); - int dbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.CodeGenDbType, 0); + string connStr = ConfigUtils.Instance.GetConfig(OptionsSetting.Gen_conn).Replace("{database}", dbName); + int dbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.Gen_conn_dbType, 0); if (string.IsNullOrEmpty(dbName)) { connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.ConnAdmin); diff --git a/ZR.CodeGenerator/GenConstants.cs b/ZR.CodeGenerator/GenConstants.cs new file mode 100644 index 0000000..df1e440 --- /dev/null +++ b/ZR.CodeGenerator/GenConstants.cs @@ -0,0 +1,119 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ZR.CodeGenerator +{ + /// + /// 代码生成常量 + /// + public class GenConstants + { + /** 单表(增删改查) */ + public static string TPL_CRUD = "crud"; + + /** 树表(增删改查) */ + public static string TPL_TREE = "tree"; + + /** 主子表(增删改查) */ + public static string TPL_SUB = "sub"; + + /** 树编码字段 */ + public static string TREE_CODE = "treeCode"; + + /** 树父编码字段 */ + public static string TREE_PARENT_CODE = "treeParentCode"; + + /** 树名称字段 */ + public static string TREE_NAME = "treeName"; + + /** 上级菜单ID字段 */ + public static string PARENT_MENU_ID = "parentMenuId"; + + /** 上级菜单名称字段 */ + public static string PARENT_MENU_NAME = "parentMenuName"; + + /** 数据库字符串类型 */ + public static string[] COLUMNTYPE_STR = { "char", "varchar", "nvarchar", "varchar2" }; + + /** 数据库文本类型 */ + public static string[] COLUMNTYPE_TEXT = { "tinytext", "text", "mediumtext", "longtext" }; + + /** 数据库时间类型 */ + public static string[] COLUMNTYPE_TIME = { "datetime", "time", "date", "timestamp" }; + + /** 数据库数字类型 */ + public static string[] COLUMNTYPE_NUMBER = { "tinyint", "smallint", "mediumint", "int", "number", "integer", + "bit", "bigint", "float", "double", "decimal" }; + + /** 页面不需要编辑字段 */ + public static string[] COLUMNNAME_NOT_EDIT = { "id", "create_by", "create_time", "del_flag" }; + + /** 页面不需要显示的列表字段 */ + public static string[] COLUMNNAME_NOT_LIST = { "id", "create_by", "create_time", "del_flag", "update_by", + "update_time" }; + + /** 页面不需要查询字段 */ + public static string[] COLUMNNAME_NOT_QUERY = { "id", "create_by", "create_time", "del_flag", "update_by", + "update_time", "remark" }; + + /** Entity基类字段 */ + public static string[] BASE_ENTITY = { "createBy", "createTime", "updateBy", "updateTime", "remark" }; + + /** Tree基类字段 */ + public static string[] TREE_ENTITY = { "parentName", "parentId", "orderNum", "ancestors", "children" }; + + /** 文本框 */ + public static string HTML_INPUT = "input"; + + /** 文本域 */ + public static string HTML_TEXTAREA = "textarea"; + + /** 下拉框 */ + public static string HTML_SELECT = "select"; + + /** 单选框 */ + public static string HTML_RADIO = "radio"; + + /** 复选框 */ + public static string HTML_CHECKBOX = "checkbox"; + + /** 日期控件 */ + public static string HTML_DATETIME = "datetime"; + + /** 图片上传控件 */ + public static string HTML_IMAGE_UPLOAD = "imageUpload"; + + /** 文件上传控件 */ + public static string HTML_FILE_UPLOAD = "fileUpload"; + + /** 富文本控件 */ + public static string HTML_EDITOR = "editor"; + + /** 字符串类型 */ + public static string TYPE_STRING = "string"; + + /** 整型 */ + public static string TYPE_INTEGER = "Integer"; + + /** 长整型 */ + public static string TYPE_LONG = "Long"; + + /** 浮点型 */ + public static string TYPE_DOUBLE = "Double"; + + /** 高精度计算类型 */ + public static string TYPE_BIGDECIMAL = "BigDecimal"; + + /** 时间类型 */ + public static string TYPE_DATE = "Date"; + + /** 模糊查询 */ + public static string QUERY_LIKE = "LIKE"; + + /** 需要 */ + public static string REQUIRE = "1"; + } +} \ No newline at end of file diff --git a/ZR.CodeGenerator/Model/GenerateDto.cs b/ZR.CodeGenerator/Model/GenerateDto.cs index 161db2a..15bb219 100644 --- a/ZR.CodeGenerator/Model/GenerateDto.cs +++ b/ZR.CodeGenerator/Model/GenerateDto.cs @@ -8,6 +8,7 @@ namespace ZR.CodeGenerator.Model { public class GenerateDto { + public long TableId { get; set; } public string[] queryColumn { get; set; } /// /// diff --git a/ZR.CodeGenerator/TableMappingHelper.cs b/ZR.CodeGenerator/TableMappingHelper.cs index 302b0ec..2f6c1cd 100644 --- a/ZR.CodeGenerator/TableMappingHelper.cs +++ b/ZR.CodeGenerator/TableMappingHelper.cs @@ -55,45 +55,45 @@ namespace ZR.CodeGenerator.CodeGenerator case "number": case "integer": case "smallint": - sTempDatatype = "int?"; + sTempDatatype = "int"; break; case "bigint": - sTempDatatype = "long?"; + sTempDatatype = "long"; break; case "tinyint": - sTempDatatype = "byte?"; + sTempDatatype = "byte"; break; case "numeric": case "real": - sTempDatatype = "Single?"; + sTempDatatype = "Single"; break; case "float": - sTempDatatype = "float?"; + sTempDatatype = "float"; break; case "decimal": case "numer(8,2)": - sTempDatatype = "decimal?"; + sTempDatatype = "decimal"; break; case "bit": - sTempDatatype = "bool?"; + sTempDatatype = "bool"; break; case "date": case "datetime": case "datetime2": case "smalldatetime": - sTempDatatype = "DateTime?"; + sTempDatatype = "DateTime"; break; case "money": case "smallmoney": - sTempDatatype = "double?"; + sTempDatatype = "double"; break; case "char": @@ -113,7 +113,7 @@ namespace ZR.CodeGenerator.CodeGenerator public static bool IsNumber(string tableDataType) { string[] arr = new string[] { "int", "long" }; - return arr.Any(f => f.Replace("?", "").Contains(GetPropertyDatatype(tableDataType))); + return arr.Any(f => f.Contains(GetPropertyDatatype(tableDataType))); } } } diff --git a/ZR.Model/System/Dto/GenTableDto.cs b/ZR.Model/System/Dto/GenTableDto.cs new file mode 100644 index 0000000..42367d9 --- /dev/null +++ b/ZR.Model/System/Dto/GenTableDto.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Text; +using ZR.Model.System.Generate; + +namespace ZR.Model.System.Dto +{ + public class GenTableDto + { + 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 BaseNameSpace { 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; } + public List Columns { get; set; } + } + + public class GenTableColumnDto + { + 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; } + ///// + ///// 是否必填(1是) + ///// + //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 string HtmlType { get; set; } + ///// + ///// 查询类型(等于、不等于、大于、小于、范围) + ///// + //public string QueryType { get; set; } = "EQ"; + //public int Sort { get; set; } + } +} diff --git a/ZR.Model/System/Generate/GenTable.cs b/ZR.Model/System/Generate/GenTable.cs index 1d024e7..0737c33 100644 --- a/ZR.Model/System/Generate/GenTable.cs +++ b/ZR.Model/System/Generate/GenTable.cs @@ -8,7 +8,7 @@ namespace ZR.Model.System.Generate /// 代码生成表 /// [SqlSugar.SugarTable("gen_table")] - public class GenTable + public class GenTable: SysBase { [SqlSugar.SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int TableId { get; set; } @@ -18,7 +18,7 @@ namespace ZR.Model.System.Generate public string SubTableFkName { get; set; } public string ClassName { get; set; } public string TplCategory { get; set; } - public string PackageName { get; set; } + public string BaseNameSpace { get; set; } public string ModuleName { get; set; } public string BusinessName { get; set; } public string FunctionName { get; set; } @@ -26,9 +26,9 @@ namespace ZR.Model.System.Generate 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; } + + /** 表列信息 */ + [SqlSugar.SugarColumn(IsIgnore = true)] + public List Columns { get; set; } } } diff --git a/ZR.Model/System/Generate/GenTableColumn.cs b/ZR.Model/System/Generate/GenTableColumn.cs index eae7a08..3206d37 100644 --- a/ZR.Model/System/Generate/GenTableColumn.cs +++ b/ZR.Model/System/Generate/GenTableColumn.cs @@ -1,4 +1,5 @@ -using System; +using Newtonsoft.Json; +using System; using System.Collections.Generic; using System.Text; @@ -8,19 +9,32 @@ namespace ZR.Model.System.Generate /// 代码生成表字段 /// [SqlSugar.SugarTable("gen_table_column")] - public class GenTableColumn + public class GenTableColumn: SysBase { [SqlSugar.SugarColumn(IsIdentity = true, IsPrimaryKey = true)] public int ColumnId { get; set; } public string ColumnName { get; set; } + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] public int TableId { get; set; } + + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] public string TableName { get; set; } public string ColumnComment { get; set; } + + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] public string ColumnType { get; set; } public string CsharpType { get; set; } public string CsharpField { get; set; } + /// + /// 是否主键(1是) + /// + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] public bool IsPk { get; set; } + /// + /// 是否必填(1是) + /// public bool IsRequired { get; set; } + [SqlSugar.SugarColumn(IsOnlyIgnoreUpdate = true)] public bool IsIncrement { get; set; } /// /// 是否插入 @@ -35,9 +49,14 @@ namespace ZR.Model.System.Generate /// public bool IsList { get; set; } public bool IsQuery { get; set; } + /// + /// 显示类型(文本框、文本域、下拉框、复选框、单选框、日期控件) + /// + public string HtmlType { get; set; } + /// + /// 查询类型(等于、不等于、大于、小于、范围) + /// + public string QueryType { get; set; } = "EQ"; public int Sort { get; set; } - - public string CreateBy { get; set; } - public DateTime CreateTime { get; set; } } } diff --git a/ZR.Model/System/SysBase.cs b/ZR.Model/System/SysBase.cs index 07d07f7..ced566a 100644 --- a/ZR.Model/System/SysBase.cs +++ b/ZR.Model/System/SysBase.cs @@ -39,7 +39,6 @@ namespace ZR.Model.System /// [Computed]计算属性,打上此标签,对象地insert,update等操作会忽略此列 /// [SugarColumn(IsIgnore = true)] - //[Computed] [JsonIgnore] public DateTime? BeginTime { get; set; } @@ -47,7 +46,6 @@ namespace ZR.Model.System /// 用于搜索使用 /// [SugarColumn(IsIgnore = true)] - //[Computed] [JsonIgnore] public DateTime? EndTime { get; set; } } diff --git a/ZR.Service/System/GenTableService.cs b/ZR.Service/System/GenTableService.cs index b0c3ac9..78689e1 100644 --- a/ZR.Service/System/GenTableService.cs +++ b/ZR.Service/System/GenTableService.cs @@ -5,8 +5,6 @@ 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; @@ -18,14 +16,31 @@ namespace ZR.Service.System [AppService(ServiceType = typeof(IGenTableService), ServiceLifetime = LifeTime.Transient)] public class GenTableService : BaseService, IGenTableService { + public IGenTableColumnService GenTableColumnService; + public GenTableService(IGenTableColumnService genTableColumnService) + { + GenTableColumnService = genTableColumnService; + } + /// /// 删除表 /// - /// + /// 需要删除的表id /// - public int DeleteGenTable(GenTable table) + public int DeleteGenTableByIds(long[] tableIds) { - return Db.Deleteable().Where(f => f.TableName == table.TableName).ExecuteCommand(); + Db.Deleteable().Where(f => tableIds.Contains(f.TableId)).ExecuteCommand(); + return GenTableColumnService.DeleteGenTableColumn(tableIds); + } + + /// + /// 删除表根据表名 + /// + /// + /// + public int DeleteGenTableByTbName(string tableName) + { + return Db.Deleteable().Where(f => f.TableName == tableName).ExecuteCommand(); } /// @@ -57,11 +72,15 @@ namespace ZR.Service.System /// /// /// - public int InsertGenTable(GenTable table) + public int ImportGenTable(GenTable table) { var db = Db; - DeleteGenTable(table); - return db.Insertable(table).ExecuteReturnIdentity(); + table.Create_time = db.GetDate(); + //导入前删除现有表 + //DeleteGenTableByIds(new long[] { table.TableId }); + DeleteGenTableByTbName(table.TableName); + + return db.Insertable(table).IgnoreColumns(ignoreNullColumn: true).ExecuteReturnIdentity(); } /// @@ -73,6 +92,13 @@ namespace ZR.Service.System { throw new NotImplementedException(); } + + public int UpdateGenTable(GenTable genTable) + { + var db = Db; + genTable.Update_time = db.GetDate(); + return db.Updateable(genTable).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommand(); + } } /// @@ -88,7 +114,26 @@ namespace ZR.Service.System /// public int DeleteGenTableColumn(long tableId) { - return Db.Deleteable().Where(f => f.TableId == tableId).ExecuteCommand(); + return DeleteGenTableColumn(new long[] { tableId }); + } + /// + /// 根据表id批量删除表字段 + /// + /// + /// + public int DeleteGenTableColumn(long[] tableId) + { + return Db.Deleteable().Where(f => tableId.Contains(f.TableId)).ExecuteCommand(); + } + + /// + /// 根据表名删除字段 + /// + /// + /// + public int DeleteGenTableColumnByTableName(string tableName) + { + return Db.Deleteable().Where(f => f.TableName == tableName).ExecuteCommand(); } /// @@ -98,7 +143,7 @@ namespace ZR.Service.System /// public List GenTableColumns(long tableId) { - return GetAll().OrderBy(x => x.Sort).ToList(); + return Db.Queryable().Where(f => f.TableId == tableId).OrderBy(x => x.Sort).ToList(); } /// @@ -108,7 +153,17 @@ namespace ZR.Service.System /// public int InsertGenTableColumn(List tableColumn) { - return Db.Insertable(tableColumn).ExecuteCommand(); + return Db.Insertable(tableColumn).IgnoreColumns(x => new { x.Remark}).ExecuteCommand(); + } + + /// + /// 批量更新表字段 + /// + /// + /// + public int UpdateGenTableColumn(List tableColumn) + { + return Db.Updateable(tableColumn).IgnoreColumns(x => new { x.Remark }).ExecuteCommand(); } } } diff --git a/ZR.Service/System/IService/IGenTableService.cs b/ZR.Service/System/IService/IGenTableService.cs index 91aabd2..7adc936 100644 --- a/ZR.Service/System/IService/IGenTableService.cs +++ b/ZR.Service/System/IService/IGenTableService.cs @@ -12,19 +12,23 @@ namespace ZR.Service.System.IService { List SelectDbTableListByNamess(string[] tableNames); - int InsertGenTable(GenTable tables); + int ImportGenTable(GenTable tables); - int DeleteGenTable(GenTable table); + int DeleteGenTableByIds(long[] tableIds); + int DeleteGenTableByTbName(string tableName); PagedInfo GetGenTables(GenTable genTable, Model.PagerInfo pagerInfo); GenTable GetGenTableInfo(long tableId); + int UpdateGenTable(GenTable genTable); } public interface IGenTableColumnService - { + { int InsertGenTableColumn(List tableColumn); int DeleteGenTableColumn(long tableId); - + int DeleteGenTableColumn(long[] tableIds); + int DeleteGenTableColumnByTableName(string tableName); List GenTableColumns(long tableId); + int UpdateGenTableColumn(List tableColumn); } } diff --git a/ZR.Vue/src/api/tool/gen.js b/ZR.Vue/src/api/tool/gen.js index 747e7b8..0033408 100644 --- a/ZR.Vue/src/api/tool/gen.js +++ b/ZR.Vue/src/api/tool/gen.js @@ -77,6 +77,22 @@ export function importTable(data) { params: data }) } +// 删除表数据 +export function delTable(tableId) { + return request({ + url: '/tool/gen/' + tableId, + method: 'delete' + }) +} + +// 修改代码生成表信息 +export function updateGenTable(data) { + return request({ + url: '/tool/gen/', + method: 'put', + data: data + }) +} // /** diff --git a/ZR.Vue/src/views/tool/gen/editTable.vue b/ZR.Vue/src/views/tool/gen/editTable.vue index 80e7549..18481f3 100644 --- a/ZR.Vue/src/views/tool/gen/editTable.vue +++ b/ZR.Vue/src/views/tool/gen/editTable.vue @@ -182,6 +182,7 @@ export default { treeParentCode: genTable.treeParentCode, parentMenuId: genTable.parentMenuId, }; + console.log(genTable) updateGenTable(genTable).then((res) => { this.msgSuccess(res.msg); if (res.code === 200) { diff --git a/ZR.Vue/src/views/tool/gen/genInfoForm.vue b/ZR.Vue/src/views/tool/gen/genInfoForm.vue index 27d6a22..0a0edb7 100644 --- a/ZR.Vue/src/views/tool/gen/genInfoForm.vue +++ b/ZR.Vue/src/views/tool/gen/genInfoForm.vue @@ -11,18 +11,18 @@ - + @@ -52,7 +52,7 @@ 生成功能名 - + @@ -60,7 +60,7 @@ - + - + @@ -126,12 +119,7 @@ - + @@ -144,12 +132,7 @@ - + @@ -162,12 +145,7 @@ - + @@ -183,12 +161,7 @@ - + @@ -201,12 +174,7 @@ - + @@ -223,15 +191,15 @@ export default { props: { info: { type: Object, - default: null + default: null, }, tables: { type: Array, - default: null + default: null, }, menus: { type: Array, - default: [] + default: [], }, }, data() { @@ -239,28 +207,28 @@ export default { subColumns: [], rules: { tplCategory: [ - { required: true, message: "请选择生成模板", trigger: "blur" } + { required: true, message: "请选择生成模板", trigger: "blur" }, ], packageName: [ - { required: true, message: "请输入生成包路径", trigger: "blur" } + { required: true, message: "请输入生成包路径", trigger: "blur" }, ], moduleName: [ - { required: true, message: "请输入生成模块名", trigger: "blur" } + { required: true, message: "请输入生成模块名", trigger: "blur" }, ], businessName: [ - { required: true, message: "请输入生成业务名", trigger: "blur" } + { required: true, message: "请输入生成业务名", trigger: "blur" }, ], functionName: [ - { required: true, message: "请输入生成功能名", trigger: "blur" } + { required: true, message: "请输入生成功能名", trigger: "blur" }, ], - } + }, }; }, created() {}, watch: { - 'info.subTableName': function(val) { + "info.subTableName": function (val) { this.setSubTableColumns(val); - } + }, }, methods: { /** 转换菜单数据结构 */ @@ -271,18 +239,18 @@ export default { return { id: node.menuId, label: node.menuName, - children: node.children + children: node.children, }; }, /** 选择子表名触发 */ subSelectChange(value) { - this.info.subTableFkName = ''; + this.info.subTableFkName = ""; }, /** 选择生成模板触发 */ tplSelectChange(value) { - if(value !== 'sub') { - this.info.subTableName = ''; - this.info.subTableFkName = ''; + if (value !== "sub") { + this.info.subTableName = ""; + this.info.subTableFkName = ""; } }, /** 设置关联外键 */ @@ -294,7 +262,7 @@ export default { break; } } - } - } + }, + }, }; diff --git a/ZR.Vue/src/views/tool/gen/index.vue b/ZR.Vue/src/views/tool/gen/index.vue index 7b9855c..4ce79bb 100644 --- a/ZR.Vue/src/views/tool/gen/index.vue +++ b/ZR.Vue/src/views/tool/gen/index.vue @@ -14,12 +14,12 @@ - --> + - + --> 查询 刷新 @@ -32,10 +32,10 @@ - 删除 + 删除 - + diff --git a/ZRAdmin.xml b/ZRAdmin.xml index cd83250..8c3e75a 100644 --- a/ZRAdmin.xml +++ b/ZRAdmin.xml @@ -27,47 +27,6 @@ - - - 代码自动生成 - - - - - 接口 - - - - - 查询列表 - - - - - - 查询详情 - - - - - - - 添加 - - - - - - 更新 - - - - - - 删除 - - - 代码生成 @@ -112,7 +71,7 @@ - 代码生成删除 + 删除代码生成 @@ -125,6 +84,12 @@ + + + 代码生成保存 + + + 心跳 diff --git a/document/admin-sqlserver.sql b/document/admin-sqlserver.sql index 49a10ca..30a7332 100644 Binary files a/document/admin-sqlserver.sql and b/document/admin-sqlserver.sql differ