From 2d722579455627da8ad15b2ed52b263c3a0e74ac Mon Sep 17 00:00:00 2001 From: izory <791736813@qq.com> Date: Wed, 8 Sep 2021 07:48:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90Model=E3=80=81Dto=E7=9A=84?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.Admin.WebApi/Template/InputDtoTemplate.txt | 20 ++ ZR.Admin.WebApi/Template/ModelTemplate.txt | 2 +- ZR.Admin.WebApi/ZR.Admin.WebApi.csproj | 3 + ZR.CodeGenerator/CodeGeneratorTool.cs | 194 ++++++++++-------- 4 files changed, 127 insertions(+), 92 deletions(-) create mode 100644 ZR.Admin.WebApi/Template/InputDtoTemplate.txt diff --git a/ZR.Admin.WebApi/Template/InputDtoTemplate.txt b/ZR.Admin.WebApi/Template/InputDtoTemplate.txt new file mode 100644 index 0000000..f38f62b --- /dev/null +++ b/ZR.Admin.WebApi/Template/InputDtoTemplate.txt @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using {ModelsNamespace}; + +namespace {DtosNamespace} +{ + /// + /// {TableNameDesc}输入对象模型 + /// + public class {ModelTypeName}Dto + { +{PropertyName} + } + + public class {ModelTypeName}QueryDto: PagerInfo + { + + + } +} diff --git a/ZR.Admin.WebApi/Template/ModelTemplate.txt b/ZR.Admin.WebApi/Template/ModelTemplate.txt index 47fafe1..23e36c5 100644 --- a/ZR.Admin.WebApi/Template/ModelTemplate.txt +++ b/ZR.Admin.WebApi/Template/ModelTemplate.txt @@ -9,6 +9,6 @@ namespace {ModelsNamespace} [SqlSugar.SugarTable("{TableName}")] public class {ModelTypeName} { -{ModelContent} +{PropertyName} } } diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj index ed4e7c1..5bbdc8a 100644 --- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj +++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj @@ -57,6 +57,9 @@ TextTemplatingFileGenerator Controller.cs + + Always + Always diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index f390fa7..5162a9b 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -39,7 +39,7 @@ namespace ZR.CodeGenerator public static void Generate(string dbName, string baseNamespace, DbTableInfo dbTableInfo, string replaceTableNameStr, bool ifExsitedCovered = false) { _option.BaseNamespace = baseNamespace; - _option.DtosNamespace = baseNamespace + "ZR.Model.Dto"; + _option.DtosNamespace = baseNamespace + "ZR.Model"; _option.ModelsNamespace = baseNamespace + "ZR.Model"; //_option.IRepositoriesNamespace = baseNamespace + ".IRepositorie"; _option.RepositoriesNamespace = baseNamespace + "ZR.Repository"; @@ -86,14 +86,8 @@ namespace ZR.CodeGenerator public static void GenerateSingle(List listField, DbTableInfo tableInfo, bool ifExsitedCovered = false) { var modelsNamespace = _option.ModelsNamespace; - var modelTypeName = tableInfo.Name;//表名 + var modelTypeName = GetModelName(tableInfo.Name); ;//表名 var modelTypeDesc = tableInfo.Description;//表描述 - if (!string.IsNullOrEmpty(_option.ReplaceTableNameStr)) - { - modelTypeName = modelTypeName.Replace(_option.ReplaceTableNameStr.ToString(), ""); - } - modelTypeName = modelTypeName.Replace("_", ""); - modelTypeName = modelTypeName.Substring(0, 1).ToUpper() + modelTypeName.Substring(1); string keyTypeName = "string";//主键数据类型 string modelcontent = "";//数据库模型字段 @@ -121,104 +115,75 @@ namespace ZR.CodeGenerator } modelcontent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r"; - //主键 - //if (dbFieldInfo.IsIdentity) + //if (dbFieldInfo.DataType == "string") //{ - //keyTypeName = dbFieldInfo.DataType; - //outputDtocontent += " /// \n"; - //outputDtocontent += string.Format(" /// 设置或获取{0}\n", dbFieldInfo.ColumnDescription); - //outputDtocontent += " /// \n"; - - //outputDtocontent += string.Format(" [SqlSugar.SugarColumn(IsIdentity = true, IsPrimaryKey = true)]\n"); - //outputDtocontent += string.Format(" public {0} {1}", TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType), columnName); - //outputDtocontent += " { get; set; }\n\r"; + // outputDtocontent += string.Format(" [MaxLength({0})]\n", dbFieldInfo.FieldMaxLength); //} - // else //非主键 + //outputDtocontent += string.Format(" public {0} {1}", dbFieldInfo.DataType, columnName); + //outputDtocontent += " { get; set; }\n\r"; + //if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint") //{ - //modelcontent += " /// \n"; - //modelcontent += string.Format(" /// 设置或获取{0}\n", dbFieldInfo.ColumnDescription); - //modelcontent += " /// \n"; - ////if (dbFieldInfo.DataType == "string") - ////{ - //// modelcontent += string.Format(" [MaxLength({0})]\n", dbFieldInfo.FieldMaxLength); - ////} - //modelcontent += string.Format(" public {0} {1}", TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType), columnName); - //modelcontent += " { get; set; }\n\r"; + // vueViewListContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription); + // vueViewListContent += " \n"; + // vueViewListContent += " \n"; - //outputDtocontent += " /// \n"; - //outputDtocontent += string.Format(" /// 设置或获取{0}\n", dbFieldInfo.ColumnDescription); - //outputDtocontent += " /// \n"; - //if (dbFieldInfo.DataType == "string") - //{ - // outputDtocontent += string.Format(" [MaxLength({0})]\n", dbFieldInfo.FieldMaxLength); - //} - //outputDtocontent += string.Format(" public {0} {1}", dbFieldInfo.DataType, columnName); - //outputDtocontent += " { get; set; }\n\r"; - //if (dbFieldInfo.DataType == "bool" || dbFieldInfo.DataType == "tinyint") - //{ + // vueViewFromContent += string.Format(" ", dbFieldInfo.ColumnDescription, columnName); + // vueViewFromContent += string.Format(" \n", columnName); + // vueViewFromContent += " \n"; + // vueViewFromContent += " \n"; + // vueViewFromContent += " \n"; + // vueViewFromContent += " \n"; - // vueViewListContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription); - // vueViewListContent += " \n"; - // vueViewListContent += " \n"; + // vueViewEditFromContent += string.Format(" {0}: 'true',\n", columnName); + // vueViewEditFromBindContent += string.Format(" this.editFrom.{0} = res.ResData.{0}+''\n", columnName); + //} + //else + //{ + // vueViewListContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription); - // vueViewFromContent += string.Format(" ", dbFieldInfo.ColumnDescription, columnName); - // vueViewFromContent += string.Format(" \n", columnName); - // vueViewFromContent += " \n"; - // vueViewFromContent += " \n"; - // vueViewFromContent += " \n"; - // vueViewFromContent += " \n"; + // vueViewFromContent += string.Format(" \n", dbFieldInfo.ColumnDescription, columnName); + // vueViewFromContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription); + // vueViewFromContent += " \n"; + // vueViewEditFromContent += string.Format(" {0}: '',\n", columnName); + // vueViewEditFromBindContent += string.Format(" this.editFrom.{0} = res.ResData.{0}\n", columnName); + //} + //vueViewSaveBindContent += string.Format(" '{0}':this.editFrom.{0},\n", columnName); + //if (!dbFieldInfo.IsNullable) + //{ + // vueViewEditFromRuleContent += string.Format(" {0}: [\n", columnName); + // vueViewEditFromRuleContent += " {"; + // vueViewEditFromRuleContent += string.Format("required: true, message:\"请输入{0}\", trigger: \"blur\"", dbFieldInfo.ColumnDescription); + // vueViewEditFromRuleContent += "},\n { min: 2, max: 50, message: \"长度在 2 到 50 个字符\", trigger:\"blur\" }\n"; + // vueViewEditFromRuleContent += " ],\n"; + //} - // vueViewEditFromContent += string.Format(" {0}: 'true',\n", columnName); - // vueViewEditFromBindContent += string.Format(" this.editFrom.{0} = res.ResData.{0}+''\n", columnName); - //} - //else - //{ - // vueViewListContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription); - - // vueViewFromContent += string.Format(" \n", dbFieldInfo.ColumnDescription, columnName); - // vueViewFromContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription); - // vueViewFromContent += " \n"; - // vueViewEditFromContent += string.Format(" {0}: '',\n", columnName); - // vueViewEditFromBindContent += string.Format(" this.editFrom.{0} = res.ResData.{0}\n", columnName); - //} - //vueViewSaveBindContent += string.Format(" '{0}':this.editFrom.{0},\n", columnName); - //if (!dbFieldInfo.IsNullable) - //{ - // vueViewEditFromRuleContent += string.Format(" {0}: [\n", columnName); - // vueViewEditFromRuleContent += " {"; - // vueViewEditFromRuleContent += string.Format("required: true, message:\"请输入{0}\", trigger: \"blur\"", dbFieldInfo.ColumnDescription); - // vueViewEditFromRuleContent += "},\n { min: 2, max: 50, message: \"长度在 2 到 50 个字符\", trigger:\"blur\" }\n"; - // vueViewEditFromRuleContent += " ],\n"; - //} - } //if (!inputDtoNoField.Contains(columnName) || columnName == "Id") //{ - // InputDtocontent += " /// \n"; - // InputDtocontent += string.Format(" /// 设置或获取{0}\n", dbFieldInfo.ColumnDescription); - // InputDtocontent += " /// \n"; - // //if (dbFieldInfo.FieldType == "string") - // //{ - // // InputDtocontent += string.Format(" [MaxLength({0})]\n", dbFieldInfo.FieldMaxLength); - // //} - // InputDtocontent += string.Format(" public {0} {1}", dbFieldInfo.DataType, columnName); - // InputDtocontent += " { get; set; }\n\r"; + InputDtocontent += " /// \n"; + InputDtocontent += string.Format(" /// 设置或获取{0}\n", dbFieldInfo.ColumnDescription); + InputDtocontent += " /// \n"; + //if (dbFieldInfo.FieldType == "string") + //{ + // InputDtocontent += string.Format(" [MaxLength({0})]\n", dbFieldInfo.FieldMaxLength); + //} + InputDtocontent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r"; //} // - //} + } GenerateModels(modelsNamespace, modelTypeName, tableInfo.Name, modelcontent, modelTypeDesc, keyTypeName, ifExsitedCovered); + GenerateInputDto(modelsNamespace, modelTypeName, modelTypeDesc, InputDtocontent, keyTypeName, ifExsitedCovered); //GenerateIRepository(modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered); //GenerateRepository(modelTypeName, modelTypeDesc, tableInfo.TableName, keyTypeName, ifExsitedCovered); //GenerateIService(modelsNamespace, modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered); //GenerateService(modelsNamespace, modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered); //GenerateOutputDto(modelTypeName, modelTypeDesc, outputDtocontent, ifExsitedCovered); - //GenerateInputDto(modelsNamespace, modelTypeName, modelTypeDesc, InputDtocontent, keyTypeName, ifExsitedCovered); //GenerateControllers(modelTypeName, modelTypeDesc, keyTypeName, ifExsitedCovered); //GenerateVueViews(modelTypeName, modelTypeDesc, vueViewListContent, vueViewFromContent, vueViewEditFromContent, vueViewEditFromBindContent, vueViewSaveBindContent, vueViewEditFromRuleContent, ifExsitedCovered); } @@ -238,13 +203,15 @@ namespace ZR.CodeGenerator /// 如果目标文件存在,是否覆盖。默认为false private static void GenerateModels(string modelsNamespace, string modelTypeName, string tableName, string modelContent, string modelTypeDesc, string keyTypeName, bool ifExsitedCovered = false) { - var parentPath = "..\\"; - var servicesPath = parentPath + _option.BaseNamespace + "\\" + modelsNamespace; + var parentPath = ".."; + //../ZR.Model + var servicesPath = parentPath + "\\" + _option.BaseNamespace + "\\" + modelsNamespace; if (!Directory.Exists(servicesPath)) { - servicesPath = parentPath + _option.ModelsNamespace; + //servicesPath = parentPath + "\\" + _option.ModelsNamespace; Directory.CreateDirectory(servicesPath); } + // ../ZR.Model/Models/User.cs var fullPath = servicesPath + "\\Models\\" + modelTypeName + ".cs"; if (File.Exists(fullPath) && !ifExsitedCovered) return; @@ -254,14 +221,59 @@ namespace ZR.CodeGenerator .Replace("{ModelTypeName}", modelTypeName) .Replace("{TableNameDesc}", modelTypeDesc) .Replace("{KeyTypeName}", keyTypeName) - .Replace("{ModelContent}", modelContent) + .Replace("{PropertyName}", modelContent) .Replace("{TableName}", tableName); WriteAndSave(fullPath, content); } + + + /// + /// 生成InputDto文件 + /// + /// + /// + /// + /// + /// + /// 如果目标文件存在,是否覆盖。默认为false + private static void GenerateInputDto(string modelsNamespace, string modelTypeName, string modelTypeDesc, string modelContent, string keyTypeName, bool ifExsitedCovered = false) + { + var parentPath = ".."; + var servicesPath = parentPath + "\\" + _option.BaseNamespace + "\\" + modelsNamespace; + if (!Directory.Exists(servicesPath)) + { + //servicesPath = parentPath + "\\" + _option.BaseNamespace + "\\Dtos"; + Directory.CreateDirectory(servicesPath); + } + // ../ZR.Model/Dto/User.cs + var fullPath = servicesPath + "\\Dto\\" + modelTypeName + "Dto.cs"; + if (File.Exists(fullPath) && !ifExsitedCovered) + return; + var content = ReadTemplate("InputDtoTemplate.txt"); + content = content + .Replace("{DtosNamespace}", _option.DtosNamespace) + .Replace("{ModelsNamespace}", modelsNamespace) + .Replace("{TableNameDesc}", modelTypeDesc) + .Replace("{KeyTypeName}", keyTypeName) + .Replace("{PropertyName}", modelContent) + .Replace("{ModelTypeName}", modelTypeName); + WriteAndSave(fullPath, content); + } #endregion #region 帮助方法 + private static string GetModelName(string modelTypeName) + { + if (!string.IsNullOrEmpty(_option.ReplaceTableNameStr)) + { + modelTypeName = modelTypeName.Replace(_option.ReplaceTableNameStr.ToString(), ""); + } + modelTypeName = modelTypeName.Replace("_", ""); + modelTypeName = modelTypeName.Substring(0, 1).ToUpper() + modelTypeName.Substring(1); + return modelTypeName; + } + /// /// 从代码模板中读取内容 ///