From 06ec7b0ddc136771d4ab9a5e35eb22aa61307a08 Mon Sep 17 00:00:00 2001 From: izory <599854767@qq.com> Date: Sun, 19 Sep 2021 20:50:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E7=94=9F?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/OptionsSetting.cs | 3 - README.md | 6 +- .../Controllers/CodeGeneratorController.cs | 6 +- ZR.CodeGenerator/CodeGeneratorTool.cs | 88 ++++++++----------- ZR.CodeGenerator/DbProvider.cs | 4 +- ZR.CodeGenerator/GenConstants.cs | 6 ++ ZR.CodeGenerator/Model/ReplaceDto.cs | 20 +++-- 7 files changed, 67 insertions(+), 66 deletions(-) diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs index a48177c..0d9790bd 100644 --- a/Infrastructure/OptionsSetting.cs +++ b/Infrastructure/OptionsSetting.cs @@ -9,9 +9,6 @@ namespace Infrastructure public static string ConnAdmin = "conn_zrAdmin"; public static string DbType = "conn_admin_Type"; public static string DbKey = "DbKey"; - public static string Gen_conn = "gen:conn"; - public static string Gen_conn_dbType = "gen:dbType"; - public static string Gen_author = "gen:author"; public string Conn_Admin { get; set; } public string AppName { get; set; } diff --git a/README.md b/README.md index 0983811..61387d4 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Vue版前端技术栈 :基于vue、vuex、vue-router 、vue-cli 、axios 和 e ## 🍄快速启动 需要安装:VS2019(最新版)、npm或yarn(最新版) -准备工作:将document文件夹下面的admin.sql脚本导入到数据库中,修改ZRAdmin项目里面的Conn_Admin数据库连接字符串以及DbType选择对应的数据库类型,目前仅MySQL验证了 +准备工作:将document文件夹下面的admin.sql脚本导入到数据库中,修改ZR.Admin.WebApi项目里面配置文件中的conn_zrAdmin数据库连接字符串以及DbType选择对应的数据库类型,目前仅支持MySQL、SQL server 启动后台:打开项目根目录的startup.bat即可启动(数据库默认MySQL) 启动前端:打开ZR.Vue文件夹,运行npm install命令,再运行npm run serve启动 浏览器访问:http://localhost:8887 (默认前端端口为:8887,后台端口为:8888) @@ -92,6 +92,10 @@ Vue版前端技术栈 :基于vue、vuex、vue-router 、vue-cli 、axios 和 e + + + + ## 🎉优势 diff --git a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs index ec0b67a..ceb967c 100644 --- a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs +++ b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs @@ -183,7 +183,7 @@ namespace ZR.Admin.WebApi.Controllers ModuleName = "bus", ClassName = CodeGeneratorTool.GetClassName(tableName), BusinessName = CodeGeneratorTool.GetClassName(tableName), - FunctionAuthor = ConfigUtils.Instance.GetConfig(OptionsSetting.Gen_author), + FunctionAuthor = ConfigUtils.Instance.GetConfig(GenConstants.Gen_author), FunctionName = tabInfo.Description, TableName = tableName, TableComment = tabInfo.Description, @@ -211,8 +211,8 @@ namespace ZR.Admin.WebApi.Controllers IsIncrement = column.IsIdentity, Create_by = userName, Create_time = DateTime.Now, - IsInsert = true, - IsEdit = true, + IsInsert = !column.IsIdentity && !column.IsPrimarykey, + IsEdit = !column.IsIdentity && !column.IsPrimarykey, IsList = true, IsQuery = false, HtmlType = GenConstants.HTML_INPUT diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index 822db9c..8ff4d6e 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -1,11 +1,9 @@ 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 @@ -64,9 +62,9 @@ namespace ZR.CodeGenerator public static void GenerateSingle(List listField, GenTable tableInfo, GenerateDto dto) { var modelTypeName = tableInfo.ClassName;//表名对应C# 实体类名 - var primaryKey = "id";//主键 - string keyTypeName = "int";//主键数据类型 + string PKName = "id"; + string PKType = "int"; string modelContent = "";//数据库模型字段 string InputDtoContent = "";//输入模型 //string outputDtoContent = "";//输出模型 @@ -74,32 +72,25 @@ namespace ZR.CodeGenerator string vueViewListContent = string.Empty;//Vue列表输出内容 string vueViewFormContent = string.Empty;//Vue表单输出内容 string vueViewEditFromContent = string.Empty;//Vue变量输出内容 - string vueViewEditFromBindContent = string.Empty;//Vue显示初始化输出内容 - string vueViewSaveBindContent = string.Empty;//Vue保存时输出内容 string vueViewEditFromRuleContent = string.Empty;//Vue数据校验 string vueJsMethod = string.Empty;//Vue js自定义方法 + //循环表字段信息 foreach (GenTableColumn dbFieldInfo in listField) { string columnName = dbFieldInfo.ColumnName; - if (dbFieldInfo.ColumnType == "bool" || dbFieldInfo.ColumnType == "tinyint") - { - vueViewEditFromContent += $" {columnName}: 'true',\n"; - } - else + if (dbFieldInfo.IsInsert || dbFieldInfo.IsEdit) { vueViewEditFromContent += $" {columnName}: undefined,\n"; } - - //主键 if (dbFieldInfo.IsPk || dbFieldInfo.IsIncrement) { - primaryKey = columnName.Substring(0, 1).ToUpper() + columnName[1..]; - keyTypeName = dbFieldInfo.CsharpType; + PKName = dbFieldInfo.CsharpField; + PKType = dbFieldInfo.CsharpType; } //编辑字段 - if (dbFieldInfo.IsEdit && (!dbFieldInfo.IsPk || !dbFieldInfo.IsIncrement)) + if (dbFieldInfo.IsEdit) { updateColumn += $" {dbFieldInfo.CsharpField} = parm.{dbFieldInfo.CsharpField},\n"; } @@ -112,8 +103,8 @@ namespace ZR.CodeGenerator InputDtoContent += CodeGenerateTemplate.GetDtoContent(dbFieldInfo); } ReplaceDto replaceDto = new(); - replaceDto.KeyTypeName = keyTypeName; - replaceDto.PrimaryKey = primaryKey; + replaceDto.PKName = PKName; + replaceDto.PKType = PKType; replaceDto.ModelTypeName = modelTypeName; replaceDto.ModelProperty = modelContent; replaceDto.TableName = tableInfo.TableName; @@ -121,10 +112,10 @@ namespace ZR.CodeGenerator replaceDto.InputDtoProperty = InputDtoContent; replaceDto.updateColumn = updateColumn; replaceDto.VueJsMethod = vueJsMethod; - replaceDto.VueViewEditFormContent = vueViewEditFromContent; - replaceDto.VueViewFormContent = vueViewFormContent; + replaceDto.VueViewEditFormHtml = vueViewEditFromContent; + replaceDto.VueViewFormHtml = vueViewFormContent; replaceDto.VueViewEditFormRuleContent = vueViewEditFromRuleContent; - replaceDto.VueViewListContent = vueViewListContent; + replaceDto.VueViewListHtml = vueViewListContent; if (dto.genFiles.Contains(1)) { @@ -161,13 +152,8 @@ namespace ZR.CodeGenerator /// /// 生成Models文件 /// - /// 命名空间 - /// 类名 - /// 表名称 - /// 表描述 - /// 数据库表实体内容 - /// 主键数据类型 - /// 如果目标文件存在,是否覆盖。默认为false + /// + /// 替换实体 private static Tuple GenerateModels(ReplaceDto replaceDto, GenerateDto generateDto) { var parentPath = ".."; @@ -187,7 +173,7 @@ namespace ZR.CodeGenerator .Replace("{ModelsNamespace}", _option.ModelsNamespace) .Replace("{ModelTypeName}", replaceDto.ModelTypeName) .Replace("{TableNameDesc}", replaceDto.TableDesc) - .Replace("{KeyTypeName}", replaceDto.KeyTypeName) + .Replace("{KeyTypeName}", replaceDto.PKName) .Replace("{PropertyName}", replaceDto.ModelProperty) .Replace("{TableName}", replaceDto.TableName); WriteAndSave(fullPath, content); @@ -199,7 +185,7 @@ namespace ZR.CodeGenerator /// 生成InputDto文件 /// /// - /// + /// 替换实体 private static Tuple GenerateInputDto(ReplaceDto replaceDto, GenerateDto generateDto) { var parentPath = ".."; @@ -218,7 +204,6 @@ namespace ZR.CodeGenerator .Replace("{DtosNamespace}", _option.DtosNamespace) .Replace("{ModelsNamespace}", _option.ModelsNamespace) .Replace("{TableNameDesc}", replaceDto.TableDesc) - .Replace("{KeyTypeName}", replaceDto.KeyTypeName) .Replace("{PropertyName}", replaceDto.InputDtoProperty) .Replace("{ModelTypeName}", replaceDto.ModelTypeName); WriteAndSave(fullPath, content); @@ -231,11 +216,8 @@ namespace ZR.CodeGenerator /// /// 生成Repository层代码文件 /// - /// - /// - /// 表名 - /// - /// 如果目标文件存在,是否覆盖。默认为false + /// + /// 替换实体 private static Tuple GenerateRepository(ReplaceDto replaceDto, GenerateDto generateDto) { var parentPath = ".."; @@ -254,8 +236,8 @@ namespace ZR.CodeGenerator .Replace("{RepositoriesNamespace}", _option.RepositoriesNamespace) .Replace("{ModelTypeName}", replaceDto.ModelTypeName) .Replace("{TableNameDesc}", replaceDto.TableDesc) - .Replace("{TableName}", replaceDto.TableName) - .Replace("{KeyTypeName}", replaceDto.KeyTypeName); + .Replace("{TableName}", replaceDto.TableName); + WriteAndSave(fullPath, content); return Tuple.Create(fullPath, content); } @@ -266,6 +248,8 @@ namespace ZR.CodeGenerator /// /// 生成IService文件 /// + /// + /// 替换实体 private static Tuple GenerateIService(ReplaceDto replaceDto, GenerateDto generateDto) { var parentPath = ".."; @@ -284,8 +268,8 @@ namespace ZR.CodeGenerator .Replace("{DtosNamespace}", _option.DtosNamespace) .Replace("{IServicsNamespace}", _option.IServicsNamespace) .Replace("{RepositoriesNamespace}", _option.RepositoriesNamespace) - .Replace("{ModelTypeName}", replaceDto.ModelTypeName) - .Replace("{KeyTypeName}", replaceDto.KeyTypeName); + .Replace("{ModelTypeName}", replaceDto.ModelTypeName); + WriteAndSave(fullPath, content); return Tuple.Create(fullPath, content); } @@ -313,8 +297,8 @@ namespace ZR.CodeGenerator .Replace("{TableNameDesc}", replaceDto.TableDesc) .Replace("{ModelsNamespace}", _option.ModelsNamespace) .Replace("{ServicesNamespace}", _option.ServicesNamespace) - .Replace("{ModelTypeName}", replaceDto.ModelTypeName) - .Replace("{KeyTypeName}", replaceDto.KeyTypeName); + .Replace("{ModelTypeName}", replaceDto.ModelTypeName); + WriteAndSave(fullPath, content); return Tuple.Create(fullPath, content); } @@ -345,9 +329,9 @@ namespace ZR.CodeGenerator .Replace("{TableDesc}", replaceDto.TableDesc) .Replace("{ModelName}", replaceDto.ModelTypeName) .Replace("{Permission}", replaceDto.ModelTypeName.ToLower()) - .Replace("{PrimaryKey}", replaceDto.PrimaryKey) + .Replace("{PrimaryKey}", replaceDto.PKName) .Replace("{UpdateColumn}", replaceDto.updateColumn) - .Replace("{KeyTypeName}", replaceDto.KeyTypeName); + .Replace("{KeyTypeName}", replaceDto.PKType); WriteAndSave(fullPath, content); return Tuple.Create(fullPath, content); } @@ -372,15 +356,15 @@ namespace ZR.CodeGenerator var content = ReadTemplate("VueTemplate.txt"); content = content .Replace("{fileClassName}", FirstLowerCase(replaceDto.ModelTypeName)) - .Replace("{VueViewListContent}", replaceDto.VueViewListContent)//查询 table列 - .Replace("{VueViewFormContent}", replaceDto.VueViewFormContent)//添加、修改表单 + .Replace("{VueViewListContent}", replaceDto.VueViewListHtml)//查询 table列 + .Replace("{VueViewFormContent}", replaceDto.VueViewFormHtml)//添加、修改表单 .Replace("{ModelTypeName}", replaceDto.ModelTypeName) .Replace("{Permission}", replaceDto.ModelTypeName.ToLower()) - .Replace("{VueViewEditFormContent}", replaceDto.VueViewEditFormContent) + .Replace("{VueViewEditFormContent}", replaceDto.VueViewEditFormHtml) .Replace("{vueJsMethod}", replaceDto.VueJsMethod) //.Replace("{VueViewEditFromBindContent}", vueViewEditFromBindContent) //.Replace("{VueViewSaveBindContent}", vueViewSaveBindContent) - .Replace("{primaryKey}", FirstLowerCase(replaceDto.PrimaryKey)) + .Replace("{primaryKey}", FirstLowerCase(replaceDto.PKName)) .Replace("{VueViewEditFormRuleContent}", replaceDto.VueViewEditFormRuleContent);//添加、修改表单验证规则 WriteAndSave(fullPath, content); @@ -412,17 +396,17 @@ namespace ZR.CodeGenerator /// public static string GetClassName(string tableName) { - bool autoRemovePre = ConfigUtils.Instance.GetAppConfig("gen:autoPre", false); - string tablePrefix = ConfigUtils.Instance.GetAppConfig("gen:tablePrefix"); + bool autoRemovePre = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_autoPre, false); + string tablePrefix = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_tablePrefix); if (!string.IsNullOrEmpty(tablePrefix) && autoRemovePre) { - string[] searcList = tablePrefix.Split(",",StringSplitOptions.RemoveEmptyEntries); + 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(), ""); + tableName = tableName.Replace(searcList[i], ""); } } } diff --git a/ZR.CodeGenerator/DbProvider.cs b/ZR.CodeGenerator/DbProvider.cs index 28d5291..db30380 100644 --- a/ZR.CodeGenerator/DbProvider.cs +++ b/ZR.CodeGenerator/DbProvider.cs @@ -22,8 +22,8 @@ namespace ZR.CodeGenerator /// public SqlSugarScope GetSugarDbContext(string dbName = "") { - string connStr = ConfigUtils.Instance.GetConfig(OptionsSetting.Gen_conn); - int dbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.Gen_conn_dbType, 0); + string connStr = ConfigUtils.Instance.GetConfig(GenConstants.Gen_conn); + int dbType = ConfigUtils.Instance.GetAppConfig(GenConstants.Gen_conn_dbType, 0); connStr = connStr.Replace("{database}", dbName); if (string.IsNullOrEmpty(dbName)) { diff --git a/ZR.CodeGenerator/GenConstants.cs b/ZR.CodeGenerator/GenConstants.cs index df1e440..237a61e 100644 --- a/ZR.CodeGenerator/GenConstants.cs +++ b/ZR.CodeGenerator/GenConstants.cs @@ -11,6 +11,12 @@ namespace ZR.CodeGenerator /// public class GenConstants { + public static string Gen_conn = "gen:conn"; + public static string Gen_conn_dbType = "gen:dbType"; + public static string Gen_author = "gen:author"; + public static string Gen_autoPre = "gen:autoPre"; + public static string Gen_tablePrefix = "gen:tablePrefix"; + /** 单表(增删改查) */ public static string TPL_CRUD = "crud"; diff --git a/ZR.CodeGenerator/Model/ReplaceDto.cs b/ZR.CodeGenerator/Model/ReplaceDto.cs index 07f0c91..e1a16b5 100644 --- a/ZR.CodeGenerator/Model/ReplaceDto.cs +++ b/ZR.CodeGenerator/Model/ReplaceDto.cs @@ -18,11 +18,11 @@ namespace ZR.CodeGenerator.Model /// /// 主键字段 /// - public string PrimaryKey { get; set; } + public string PKName { get; set; } /// /// 主键类型 /// - public string KeyTypeName { get; set; } + public string PKType { get; set; } /// /// 控制器权限 /// @@ -52,9 +52,19 @@ namespace ZR.CodeGenerator.Model public string InputDtoProperty { get; set; } //vue、api - public string VueViewEditFormContent { get; set; } - public string VueViewListContent { get; set; } - public string VueViewFormContent { get; set; } + public string VueViewEditFormHtml { get; set; } + /// + /// 前端列表查询html + /// + public string VueViewListHtml { get; set; } + /// + /// 前端添加、编辑表格html + /// + public string VueViewFormHtml { get; set; } + /// + /// 前端搜索表单html + /// + public string VueQueryFormHtml { get; set; } public string VueJsMethod { get; set; } public string VueViewEditFormRuleContent { get; set; } }