From 21e523a821598d348b48e27d1dfc1f4f34c95f37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Wed, 1 Mar 2023 18:23:29 +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 --- .../CodeGenTemplate/TplControllers.txt | 4 +- .../wwwroot/CodeGenTemplate/TplDto.txt | 6 +- .../wwwroot/CodeGenTemplate/TplIService.txt | 9 +-- .../wwwroot/CodeGenTemplate/TplModel.txt | 2 +- .../wwwroot/CodeGenTemplate/TplRepository.txt | 6 +- .../wwwroot/CodeGenTemplate/TplService.txt | 11 ++-- ZR.CodeGenerator/CodeGenerateOption.cs | 8 +-- ZR.CodeGenerator/CodeGeneratorTool.cs | 62 +++++++++---------- 8 files changed, 51 insertions(+), 57 deletions(-) diff --git a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplControllers.txt b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplControllers.txt index 0dfb9c6..60182ac 100644 --- a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplControllers.txt +++ b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplControllers.txt @@ -4,8 +4,8 @@ using Infrastructure.Enums; using Infrastructure.Model; using Mapster; using Microsoft.AspNetCore.Mvc; -using ${options.ModelsNamespace}.Dto; -using ${options.ModelsNamespace}.Models; +using ${options.DtosNamespace}; +using ${options.ModelsNamespace}.${options.SubNamespace}; using ${options.IServicsNamespace}.${options.SubNamespace}.I${options.SubNamespace}Service; using ${options.ApiControllerNamespace}.Extensions; using ${options.ApiControllerNamespace}.Filters; diff --git a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplDto.txt b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplDto.txt index 2db4e58..94c0916 100644 --- a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplDto.txt +++ b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplDto.txt @@ -1,14 +1,12 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using Newtonsoft.Json; -using ${options.ModelsNamespace}.Dto; -using ${options.ModelsNamespace}.Models; +//using ${options.ModelsNamespace}.${options.SubNamespace}; $if(replaceDto.ShowBtnExport) using MiniExcelLibs.Attributes; $end -namespace ${options.DtosNamespace}.Dto +namespace ${options.DtosNamespace} { /// /// ${genTable.FunctionName}查询对象 diff --git a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplIService.txt b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplIService.txt index bc22796..0359963 100644 --- a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplIService.txt +++ b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplIService.txt @@ -1,7 +1,7 @@ using System; -using ${options.ModelsNamespace}; -using ${options.ModelsNamespace}.Dto; -using ${options.ModelsNamespace}.Models; +using ${options.BaseNamespace}Model; +using ${options.DtosNamespace}; +using ${options.ModelsNamespace}.${options.SubNamespace}; using System.Collections.Generic; namespace ${options.IServicsNamespace}.${options.SubNamespace}.I${options.SubNamespace}Service @@ -22,7 +22,8 @@ $end int Add${replaceDto.ModelTypeName}(${replaceDto.ModelTypeName} parm); int Update${replaceDto.ModelTypeName}(${replaceDto.ModelTypeName} parm); - +$if(replaceDto.ShowBtnTruncate) void Truncate${replaceDto.ModelTypeName}(); +$end } } diff --git a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplModel.txt b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplModel.txt index 634c6d7..024af8b 100644 --- a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplModel.txt +++ b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplModel.txt @@ -2,7 +2,7 @@ using SqlSugar; using System.Collections.Generic; -namespace ${options.ModelsNamespace}.Models +namespace ${options.ModelsNamespace}.${options.SubNamespace} { /// /// ${genTable.FunctionName},数据实体对象 diff --git a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplRepository.txt b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplRepository.txt index cce84fe..f020491 100644 --- a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplRepository.txt +++ b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplRepository.txt @@ -1,7 +1,5 @@ -using System; -using Infrastructure.Attribute; -using ${options.RepositoriesNamespace}.System; -using ${options.ModelsNamespace}.Models; +using Infrastructure.Attribute; +using ${options.ModelsNamespace}.${options.SubNamespace}; namespace ${options.RepositoriesNamespace} { diff --git a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplService.txt b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplService.txt index 4213795..30b21fd 100644 --- a/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplService.txt +++ b/ZR.Admin.WebApi/wwwroot/CodeGenTemplate/TplService.txt @@ -1,10 +1,10 @@ using System; using SqlSugar; using Infrastructure.Attribute; -using ${options.ModelsNamespace}; -using ${options.ModelsNamespace}.Dto; -using ${options.ModelsNamespace}.Models; -using ${options.IRepositoriesNamespace}; +using ${options.BaseNamespace}Model; +using ${options.DtosNamespace}; +using ${options.ModelsNamespace}.${options.SubNamespace}; +using ${options.RepositoriesNamespace}; using ${options.IServicsNamespace}.${options.SubNamespace}.I${options.SubNamespace}Service; using System.Linq; $if(genTable.TplCategory == "tree") @@ -120,7 +120,7 @@ ${end} //return response; return Update(model, true); } - +$if(replaceDto.ShowBtnTruncate) /// /// 清空${genTable.FunctionName} /// @@ -129,6 +129,7 @@ ${end} { Truncate(); } +$end #endregion } } \ No newline at end of file diff --git a/ZR.CodeGenerator/CodeGenerateOption.cs b/ZR.CodeGenerator/CodeGenerateOption.cs index 4a16dca..ca906e2 100644 --- a/ZR.CodeGenerator/CodeGenerateOption.cs +++ b/ZR.CodeGenerator/CodeGenerateOption.cs @@ -18,10 +18,10 @@ /// 输入输出数据实体名称空间 /// public string DtosNamespace { get; set; } - /// - /// 仓储接口命名空间 - /// - public string IRepositoriesNamespace { get; set; } + ///// + ///// 仓储接口命名空间 + ///// + //public string IRepositoriesNamespace { get; set; } /// /// 仓储实现名称空间 /// diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs index cb2d062..80514de 100644 --- a/ZR.CodeGenerator/CodeGeneratorTool.cs +++ b/ZR.CodeGenerator/CodeGeneratorTool.cs @@ -19,10 +19,6 @@ namespace ZR.CodeGenerator public class CodeGeneratorTool { private static readonly string CodeTemplateDir = "CodeGenTemplate"; - /// - /// 代码生成器配置 - /// - private static CodeGenerateOption _option = new CodeGenerateOption(); /// /// 代码生成器入口方法 @@ -30,26 +26,14 @@ namespace ZR.CodeGenerator /// public static void Generate(GenerateDto dto) { - _option.BaseNamespace = dto.GenTable.BaseNameSpace; - _option.SubNamespace = dto.GenTable.ModuleName.FirstUpperCase(); - _option.DtosNamespace = _option.BaseNamespace + "Model"; - _option.ModelsNamespace = _option.BaseNamespace + "Model"; - _option.RepositoriesNamespace = _option.BaseNamespace + "Repository"; - _option.IRepositoriesNamespace = _option.BaseNamespace + "Repository"; - _option.IServicsNamespace = _option.BaseNamespace + "Service"; - _option.ServicesNamespace = _option.BaseNamespace + "Service"; - _option.ApiControllerNamespace = _option.BaseNamespace + "Admin.WebApi"; - var vuePath = AppSettings.GetConfig("gen:vuePath"); dto.VueParentPath = dto.VueVersion == 3 ? "ZRAdmin-vue" : "ZR.Vue"; if (!vuePath.IsEmpty()) { dto.VueParentPath = vuePath; } - dto.GenOptions = _option; + dto.GenOptions = GenerateOption(dto.GenTable); ; - string PKName = "Id"; - string PKType = "int"; ReplaceDto replaceDto = new() { ModelTypeName = dto.GenTable.ClassName,//表名对应C# 实体类名 @@ -61,6 +45,8 @@ namespace ZR.CodeGenerator ShowBtnExport = dto.GenTable.Options.CheckedBtn.Any(f => f == 4), ShowBtnView = dto.GenTable.Options.CheckedBtn.Any(f => f == 5), ShowBtnTruncate = dto.GenTable.Options.CheckedBtn.Any(f => f == 6), + PKName = "Id", + PKType = "int" }; //循环表字段信息 @@ -68,8 +54,8 @@ namespace ZR.CodeGenerator { if (dbFieldInfo.IsPk || dbFieldInfo.IsIncrement) { - PKName = dbFieldInfo.CsharpField; - PKType = dbFieldInfo.CsharpType; + replaceDto.PKName = dbFieldInfo.CsharpField; + replaceDto.PKType = dbFieldInfo.CsharpType; } if (dbFieldInfo.HtmlType.Equals(GenConstants.HTML_IMAGE_UPLOAD) || dbFieldInfo.HtmlType.Equals(GenConstants.HTML_FILE_UPLOAD)) { @@ -86,9 +72,7 @@ namespace ZR.CodeGenerator dbFieldInfo.CsharpFieldFl = dbFieldInfo.CsharpField.FirstLowerCase(); } - replaceDto.PKName = PKName; - replaceDto.PKType = PKType; - replaceDto.FistLowerPk = PKName.FirstLowerCase(); + replaceDto.FistLowerPk = replaceDto.PKName.FirstLowerCase(); InitJntTemplate(dto, replaceDto); GenerateModels(replaceDto, dto); @@ -122,6 +106,23 @@ namespace ZR.CodeGenerator } } + private static CodeGenerateOption GenerateOption(GenTable genTable) + { + CodeGenerateOption _option = new() + { + BaseNamespace = genTable.BaseNameSpace, + SubNamespace = genTable.ModuleName.FirstUpperCase() + }; + _option.DtosNamespace = _option.BaseNamespace + "Model.Dto"; + _option.ModelsNamespace = _option.BaseNamespace + "Model"; + _option.RepositoriesNamespace = _option.BaseNamespace + "Repository"; + //_option.IRepositoriesNamespace = _option.BaseNamespace + "Repository"; + _option.IServicsNamespace = _option.BaseNamespace + "Service"; + _option.ServicesNamespace = _option.BaseNamespace + "Service"; + _option.ApiControllerNamespace = _option.BaseNamespace + "Admin.WebApi"; + return _option; + } + #region 读取模板 /// @@ -134,8 +135,8 @@ namespace ZR.CodeGenerator var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplModel.txt"); var tplDto = JnHelper.ReadTemplate(CodeTemplateDir, "TplDto.txt"); - string fullPath = Path.Combine(_option.ModelsNamespace, "Models", _option.SubNamespace, replaceDto.ModelTypeName + ".cs"); - string fullPathDto = Path.Combine(_option.ModelsNamespace, "Dto", _option.SubNamespace, $"{replaceDto.ModelTypeName}Dto.cs"); + string fullPath = Path.Combine(generateDto.GenOptions.ModelsNamespace, "Models", generateDto.GenOptions.SubNamespace, replaceDto.ModelTypeName + ".cs"); + string fullPathDto = Path.Combine(generateDto.GenOptions.ModelsNamespace, "Dto", generateDto.GenOptions.SubNamespace, replaceDto.ModelTypeName + "Dto.cs"); generateDto.GenCodes.Add(new GenCode(1, "Model.cs", fullPath, tpl.Render())); generateDto.GenCodes.Add(new GenCode(2, "Dto.cs", fullPathDto, tplDto.Render())); @@ -150,7 +151,7 @@ namespace ZR.CodeGenerator { var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplRepository.txt"); var result = tpl.Render(); - var fullPath = Path.Combine(_option.RepositoriesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Repository.cs"); + var fullPath = Path.Combine(generateDto.GenOptions.RepositoriesNamespace, generateDto.GenOptions.SubNamespace, $"{replaceDto.ModelTypeName}Repository.cs"); generateDto.GenCodes.Add(new GenCode(3, "Repository.cs", fullPath, result)); } @@ -163,8 +164,8 @@ namespace ZR.CodeGenerator var tpl = JnHelper.ReadTemplate(CodeTemplateDir, "TplService.txt"); var tpl2 = JnHelper.ReadTemplate(CodeTemplateDir, "TplIService.txt"); - var fullPath = Path.Combine(_option.ServicesNamespace, _option.SubNamespace, $"{replaceDto.ModelTypeName}Service.cs"); - var fullPath2 = Path.Combine(_option.IServicsNamespace, _option.SubNamespace, $"I{_option.SubNamespace}Service", $"I{replaceDto.ModelTypeName}Service.cs"); + var fullPath = Path.Combine(generateDto.GenOptions.ServicesNamespace, generateDto.GenOptions.SubNamespace, $"{replaceDto.ModelTypeName}Service.cs"); + var fullPath2 = Path.Combine(generateDto.GenOptions.IServicsNamespace, generateDto.GenOptions.SubNamespace, $"I{generateDto.GenOptions.SubNamespace}Service", $"I{replaceDto.ModelTypeName}Service.cs"); generateDto.GenCodes.Add(new GenCode(4, "Service.cs", fullPath, tpl.Render())); generateDto.GenCodes.Add(new GenCode(4, "IService.cs", fullPath2, tpl2.Render())); @@ -179,7 +180,7 @@ namespace ZR.CodeGenerator tpl.Set("QueryCondition", replaceDto.QueryCondition); var result = tpl.Render(); - var fullPath = Path.Combine(_option.ApiControllerNamespace, "Controllers", _option.SubNamespace, $"{replaceDto.ModelTypeName}Controller.cs"); + var fullPath = Path.Combine(generateDto.GenOptions.ApiControllerNamespace, "Controllers", generateDto.GenOptions.SubNamespace, $"{replaceDto.ModelTypeName}Controller.cs"); generateDto.GenCodes.Add(new GenCode(5, "Controller.cs", fullPath, result)); } @@ -406,11 +407,6 @@ namespace ZR.CodeGenerator return sTempDatatype; } - public static bool IsNumber(string tableDataType) - { - string[] arr = new string[] { "int", "long" }; - return arr.Any(f => f.Contains(GetCSharpDatatype(tableDataType))); - } #endregion #region 初始化信息