From 18d8051d659da1f04c6c700c6a5484b378b76cb8 Mon Sep 17 00:00:00 2001
From: izory <791736813@qq.com>
Date: Tue, 7 Sep 2021 18:37:21 +0800
Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E4=BB=A3=E7=A0=81=E7=94=9F?=
=?UTF-8?q?=E6=88=90=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Infrastructure/OptionsSetting.cs | 1 +
.../Controllers/CodeGeneratorController.cs | 54 +-
ZR.Admin.WebApi/Template/ModelTemplate.txt | 14 +
ZR.Admin.WebApi/ZR.Admin.WebApi.csproj | 4 +
ZR.CodeGenerator/CodeGenerateOption.cs | 54 ++
ZR.CodeGenerator/CodeGeneratorTool.cs | 322 ++++++++++++
ZR.CodeGenerator/DbProvider.cs | 30 ++
.../Service/CodeGeneraterService.cs | 88 ++++
ZR.CodeGenerator/TableMappingHelper.cs | 2 +
ZR.CodeGenerator/ZR.CodeGenerator.csproj | 11 +-
ZR.Model/CodeGenerator/DbFieldInfo.cs | 1 +
ZR.Model/Models/README.txt | 1 +
ZR.Repository/DbProvider/SugarDbContext.cs | 17 +
.../System/CodeGeneratorRepository.cs | 59 ++-
ZR.Service/IService/ICodeGeneratorService.cs | 16 +-
ZR.Service/System/CodeGeneratorService.cs | 112 +++--
ZR.Vue/src/api/monitor/operlog.js | 14 +-
ZR.Vue/src/layout/components/Navbar.vue | 8 +-
ZR.Vue/src/views/dashboard/bigdata.vue | 218 --------
.../views/dashboard/bigdata/IviewCircle.vue | 88 ----
.../views/dashboard/bigdata/chart-options.js | 471 ------------------
.../src/views/dashboard/bigdata/head_bg.png | Bin 7881 -> 0 bytes
.../src/views/dashboard/bigdata/layout.less | 197 --------
ZR.Vue/src/views/monitor/cache/index.vue | 151 ------
ZR.Vue/src/views/monitor/online/index.vue | 128 -----
ZR.Vue/src/views/monitor/operlog/index.vue | 5 +-
ZR.Vue/src/views/tool/index.vue | 130 +++--
ZRAdmin.xml | 12 +-
28 files changed, 786 insertions(+), 1422 deletions(-)
create mode 100644 ZR.Admin.WebApi/Template/ModelTemplate.txt
create mode 100644 ZR.CodeGenerator/CodeGenerateOption.cs
create mode 100644 ZR.CodeGenerator/CodeGeneratorTool.cs
create mode 100644 ZR.CodeGenerator/DbProvider.cs
create mode 100644 ZR.CodeGenerator/Service/CodeGeneraterService.cs
create mode 100644 ZR.Model/Models/README.txt
delete mode 100644 ZR.Vue/src/views/dashboard/bigdata.vue
delete mode 100644 ZR.Vue/src/views/dashboard/bigdata/IviewCircle.vue
delete mode 100644 ZR.Vue/src/views/dashboard/bigdata/chart-options.js
delete mode 100644 ZR.Vue/src/views/dashboard/bigdata/head_bg.png
delete mode 100644 ZR.Vue/src/views/dashboard/bigdata/layout.less
delete mode 100644 ZR.Vue/src/views/monitor/cache/index.vue
delete mode 100644 ZR.Vue/src/views/monitor/online/index.vue
diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs
index 4aa2b2e..91d7963 100644
--- a/Infrastructure/OptionsSetting.cs
+++ b/Infrastructure/OptionsSetting.cs
@@ -7,6 +7,7 @@ namespace Infrastructure
public class OptionsSetting
{
public static string ConnAdmin = "Conn_admin";
+ public static string Conn = "ConnDynamic";
public static string DbType = "DbType";
public static string DbKey = "DbKey";
diff --git a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs
index e4ba737..7248560 100644
--- a/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs
+++ b/ZR.Admin.WebApi/Controllers/CodeGeneratorController.cs
@@ -1,10 +1,14 @@
using Infrastructure;
+using Infrastructure.Attribute;
+using Infrastructure.Enums;
using Infrastructure.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
+using ZR.CodeGenerator;
+using ZR.CodeGenerator.Service;
using ZR.Model;
using ZR.Model.CodeGenerator;
using ZR.Model.Vo;
@@ -19,11 +23,12 @@ namespace ZR.Admin.WebApi.Controllers
[Route("codeGenerator")]
public class CodeGeneratorController : BaseController
{
- public ICodeGeneratorService CodeGeneratorService;
- public CodeGeneratorController(ICodeGeneratorService codeGeneratorService)
- {
- CodeGeneratorService = codeGeneratorService;
- }
+ //public ICodeGeneratorService CodeGeneratorService;
+ //public CodeGeneratorController(ICodeGeneratorService codeGeneratorService)
+ //{
+ // CodeGeneratorService = codeGeneratorService;
+ //}
+ private CodeGeneraterService _CodeGeneraterService = new CodeGeneraterService();
///
/// 获取所有数据库的信息
@@ -34,38 +39,53 @@ namespace ZR.Admin.WebApi.Controllers
//[NoPermissionRequired]
public IActionResult GetListDataBase()
{
- List listTable = CodeGeneratorService.GetAllDataBases("SqlServer");
-
- return SUCCESS(listTable);
+ return SUCCESS(_CodeGeneraterService.GetAllDataBases());
}
///
///获取所有表根据数据名
///
- /// 数据库名
- /// 表名
+ /// 数据库名
+ /// 表名
/// 分页信息
///
[HttpGet("FindListTable")]
- public IActionResult FindListTable(string enCode, string keywords, PagerInfo pager)
+ public IActionResult FindListTable(string dbName, string tableName, PagerInfo pager)
{
- if (string.IsNullOrEmpty(enCode))
+ if (string.IsNullOrEmpty(dbName))
{
- return ToRespose(ResultCode.PARAM_ERROR);
+ dbName = "ZrAdmin";
}
- List list = CodeGeneratorService.GetTablesWithPage(keywords, enCode, pager);
- var vm = new VMPageResult(list, pager);
+ List list = _CodeGeneraterService.GetAllTables(dbName, tableName, pager);
+ var vm = new VMPageResult(list, pager);
return SUCCESS(vm);
}
///
- /// 生成代码
+ /// 代码生成器
///
+ ///
+ /// 要生成代码的表
+ /// 项目命名空间
+ /// 要删除表名的字符串用英文逗号","隔开
///
[HttpGet("Generate")]
- public IActionResult Generate()
+ [Log(Title = "代码生成", BusinessType = BusinessType.OTHER)]
+ public IActionResult Generate(string dbName, string baseSpace, string tables, string replaceTableNameStr)
{
+ if (string.IsNullOrEmpty(baseSpace))
+ {
+ throw new CustomException(ResultCode.CUSTOM_ERROR, "命名空间不能为空");
+ }
+ string[] tableList = tables.Split(",");
+ List tableInfos = new List();// CodeGeneratorService.GetAllTables(tables);
+ foreach (var item in tableList)
+ {
+ tableInfos.Add(new DbTableInfo() { TableName = item });
+ }
+
+ CodeGeneratorTool.Generate(dbName, baseSpace, tableInfos, replaceTableNameStr, true);
return SUCCESS(null);
}
diff --git a/ZR.Admin.WebApi/Template/ModelTemplate.txt b/ZR.Admin.WebApi/Template/ModelTemplate.txt
new file mode 100644
index 0000000..47fafe1
--- /dev/null
+++ b/ZR.Admin.WebApi/Template/ModelTemplate.txt
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+
+namespace {ModelsNamespace}
+{
+ ///
+ /// {TableNameDesc},数据实体对象
+ ///
+ [SqlSugar.SugarTable("{TableName}")]
+ public class {ModelTypeName}
+ {
+{ModelContent}
+ }
+}
diff --git a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj
index 9f82d37..ed4e7c1 100644
--- a/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj
+++ b/ZR.Admin.WebApi/ZR.Admin.WebApi.csproj
@@ -32,6 +32,7 @@
+
@@ -56,6 +57,9 @@
TextTemplatingFileGenerator
Controller.cs
+
+ Always
+
diff --git a/ZR.CodeGenerator/CodeGenerateOption.cs b/ZR.CodeGenerator/CodeGenerateOption.cs
new file mode 100644
index 0000000..abb237f
--- /dev/null
+++ b/ZR.CodeGenerator/CodeGenerateOption.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ZR.CodeGenerator
+{
+ public class CodeGenerateOption
+ {
+ ///
+ /// 项目命名空间
+ ///
+ public string BaseNamespace { get; set; }
+ ///
+ /// 数据实体命名空间
+ ///
+ public string ModelsNamespace { get; set; }
+ ///
+ /// 输入输出数据实体名称空间
+ ///
+ public string DtosNamespace { get; set; }
+ ///
+ /// 仓储接口命名空间
+ ///
+ public string IRepositoriesNamespace { get; set; }
+ ///
+ /// 仓储实现名称空间
+ ///
+ public string RepositoriesNamespace { get; set; }
+ ///
+ /// 服务接口命名空间
+ ///
+ public string IServicsNamespace { get; set; }
+ ///
+ /// 服务接口实现命名空间
+ ///
+ public string ServicesNamespace { get; set; }
+
+ ///
+ /// Api控制器命名空间
+ ///
+ public string ApiControllerNamespace { get; set; }
+
+ ///
+ /// 去掉的表头字符
+ ///
+ public string ReplaceTableNameStr { get; set; }
+ ///
+ /// 要生数据的表,用“,”分割
+ ///
+ public string TableList { get; set; }
+ }
+}
diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs
new file mode 100644
index 0000000..dc84deb
--- /dev/null
+++ b/ZR.CodeGenerator/CodeGeneratorTool.cs
@@ -0,0 +1,322 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using ZR.CodeGenerator.CodeGenerator;
+using ZR.CodeGenerator.Service;
+using ZR.Model;
+
+namespace ZR.CodeGenerator
+{
+ ///
+ /// 代码生成器。
+ ///
+ /// 根据指定的实体域名空间生成Repositories和Services层的基础代码文件。
+ ///
+ ///
+ public class CodeGeneratorTool
+ {
+ ///
+ /// 代码生成器配置
+ ///
+ private static CodeGenerateOption _option = new CodeGenerateOption();
+ ///
+ /// InputDto输入实体是不包含字段
+ ///
+ private static string inputDtoNoField = "DeleteMark,CreatorTime,CreatorUserId,CompanyId,DeptId,LastModifyTime,LastModifyUserId,DeleteTime,DeleteUserId,";
+
+ ///
+ /// 代码生成器入口方法
+ ///
+ ///
+ /// 要生成代码的表
+ ///
+ ///
+ /// 要删除表名称的字符
+ /// 是否替换现有文件,为true时替换
+ public static void Generate(string dbName, string baseNamespace, List listTable, string replaceTableNameStr, bool ifExsitedCovered = false)
+ {
+ _option.DtosNamespace = baseNamespace + ".Dtos";
+ _option.ModelsNamespace = baseNamespace + ".Model";
+ _option.IRepositoriesNamespace = baseNamespace + ".IRepositories";
+ _option.RepositoriesNamespace = baseNamespace + ".Repositories";
+ _option.IServicsNamespace = baseNamespace + ".IServices";
+ _option.ServicesNamespace = baseNamespace + ".Services";
+ _option.ApiControllerNamespace = baseNamespace + "Api";
+ _option.ReplaceTableNameStr = replaceTableNameStr;
+ //_option.TableList = listTable;
+ _option.BaseNamespace = baseNamespace;
+
+ CodeGeneraterService codeGeneraterService = new CodeGeneraterService();
+ //List listTable = dbExtractor.GetWhereTables(_option.TableList);
+ string profileContent = string.Empty;
+ foreach (DbTableInfo dbTableInfo in listTable)
+ {
+ List listField = codeGeneraterService.GetColumnInfo(dbName, dbTableInfo.TableName);
+ GenerateSingle(listField, dbTableInfo, ifExsitedCovered);
+ string tableName = dbTableInfo.TableName;
+ if (!string.IsNullOrEmpty(_option.ReplaceTableNameStr))
+ {
+ string[] rel = _option.ReplaceTableNameStr.Split(';');
+ for (int i = 0; i < rel.Length; i++)
+ {
+ if (!string.IsNullOrEmpty(rel[i].ToString()))
+ {
+ tableName = tableName.Replace(rel[i].ToString(), "");
+ }
+ }
+ }
+ tableName = tableName.Substring(0, 1).ToUpper() + tableName.Substring(1);
+ profileContent += string.Format(" CreateMap<{0}, {0}OutputDto>();\n", tableName);
+ profileContent += string.Format(" CreateMap<{0}InputDto, {0}>();\n", tableName);
+ }
+
+ //GenerateDtoProfile(_option.ModelsNamespace, profileContent, ifExsitedCovered);
+ }
+
+
+
+ ///
+ /// 单表生成代码
+ ///
+ /// 表字段集合
+ /// 表信息
+ /// 如果目标文件存在,是否覆盖。默认为false
+ public static void GenerateSingle(List listField, DbTableInfo tableInfo, bool ifExsitedCovered = false)
+ {
+ var modelsNamespace = _option.ModelsNamespace;
+ var modelTypeName = tableInfo.TableName;//表名
+ var modelTypeDesc = tableInfo.Description;//表描述
+ if (!string.IsNullOrEmpty(_option.ReplaceTableNameStr))
+ {
+ string[] rel = _option.ReplaceTableNameStr.Split(';');
+ for (int i = 0; i < rel.Length; i++)
+ {
+ if (!string.IsNullOrEmpty(rel[i].ToString()))
+ {
+ modelTypeName = modelTypeName.Replace(rel[i].ToString(), "");
+ }
+ }
+ }
+ modelTypeName = modelTypeName.Substring(0, 1).ToUpper() + modelTypeName.Substring(1);
+ string keyTypeName = "string";//主键数据类型
+ string modelcontent = "";//数据库模型字段
+ string InputDtocontent = "";//输入模型
+ string outputDtocontent = "";//输出模型
+ string vueViewListContent = string.Empty;//Vue列表输出内容
+ string vueViewFromContent = string.Empty;//Vue表单输出内容
+ string vueViewEditFromContent = string.Empty;//Vue变量输出内容
+ string vueViewEditFromBindContent = string.Empty;//Vue显示初始化输出内容
+ string vueViewSaveBindContent = string.Empty;//Vue保存时输出内容
+ string vueViewEditFromRuleContent = string.Empty;//Vue数据校验
+
+ foreach (SqlSugar.DbColumnInfo dbFieldInfo in listField)
+ {
+ string columnName = dbFieldInfo.DbColumnName.Substring(0, 1).ToUpper() + dbFieldInfo.DbColumnName.Substring(1);
+
+ modelcontent += " /// \n";
+ modelcontent += ($" /// {dbFieldInfo.ColumnDescription}\n");
+ modelcontent += " /// \n";
+ if (dbFieldInfo.IsIdentity || dbFieldInfo.IsPrimarykey)
+ {
+ modelcontent += $" [SugarColumn(IsPrimaryKey = {dbFieldInfo.IsPrimarykey}, IsIdentity = {dbFieldInfo.IsIdentity})]\n";
+ }
+ modelcontent += $" public {TableMappingHelper.GetPropertyDatatype(dbFieldInfo.DataType)} {columnName} {{ get; set; }}\n\r";
+
+ //主键
+ if (dbFieldInfo.IsIdentity)
+ {
+ 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";
+ }
+ else //非主键
+ {
+ 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";
+
+
+ //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")
+ //{
+
+ // vueViewListContent += string.Format(" \n", columnName, dbFieldInfo.ColumnDescription);
+ // vueViewListContent += " \n";
+ // vueViewListContent += string.Format(" ", columnName);
+ // vueViewListContent += "{{ ";
+ // vueViewListContent += string.Format("scope.row.{0}===true?'启用':'禁用' ", columnName);
+ // vueViewListContent += "}}\n";
+ // vueViewListContent += " \n";
+ // vueViewListContent += " \n";
+
+ // vueViewFromContent += string.Format(" ", dbFieldInfo.ColumnDescription, columnName);
+ // vueViewFromContent += string.Format(" \n", columnName);
+ // vueViewFromContent += " 是\n";
+ // vueViewFromContent += " 否\n";
+ // vueViewFromContent += " \n";
+ // vueViewFromContent += " \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";
+ //}
+ //
+ }
+ GenerateModels(modelsNamespace, modelTypeName, tableInfo.TableName, modelcontent, modelTypeDesc, 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);
+ }
+
+
+ #region 生成Model
+
+ ///
+ /// 生成Models文件
+ ///
+ /// 命名空间
+ /// 类名
+ /// 表名称
+ /// 表描述
+ /// 数据库表实体内容
+ /// 主键数据类型
+ /// 如果目标文件存在,是否覆盖。默认为false
+ private static void GenerateModels(string modelsNamespace, string modelTypeName, string tableName, string modelContent, string modelTypeDesc, string keyTypeName, bool ifExsitedCovered = false)
+ {
+ var path = AppDomain.CurrentDomain.BaseDirectory;
+ //path = path.Substring(0, path.IndexOf("\\bin"));
+ var parentPath = path.Substring(0, path.LastIndexOf("\\"));
+ var servicesPath = parentPath + "\\" + _option.BaseNamespace + "\\" + modelsNamespace;
+ if (!Directory.Exists(servicesPath))
+ {
+ servicesPath = parentPath + "\\" + _option.BaseNamespace + "\\Models";
+ Directory.CreateDirectory(servicesPath);
+ }
+ var fullPath = servicesPath + "\\" + modelTypeName + ".cs";
+ if (File.Exists(fullPath) && !ifExsitedCovered)
+ return;
+ var content = ReadTemplate("ModelTemplate.txt");
+ content = content
+ .Replace("{ModelsNamespace}", modelsNamespace)
+ .Replace("{ModelTypeName}", modelTypeName)
+ .Replace("{TableNameDesc}", modelTypeDesc)
+ .Replace("{KeyTypeName}", keyTypeName)
+ .Replace("{ModelContent}", modelContent)
+ .Replace("{TableName}", tableName);
+ WriteAndSave(fullPath, content);
+ }
+ #endregion
+
+ #region 帮助方法
+
+ ///
+ /// 从代码模板中读取内容
+ ///
+ /// 模板名称,应包括文件扩展名称。比如:template.txt
+ ///
+ private static string ReadTemplate(string templateName)
+ {
+ var path = AppDomain.CurrentDomain.BaseDirectory;
+ string fullName = $"{path}\\Template\\{templateName}";
+ string temp = fullName;
+ string str = "";
+ if (!File.Exists(temp))
+ {
+ return str;
+ }
+ StreamReader sr = null;
+ try
+ {
+ sr = new StreamReader(temp);
+ str = sr.ReadToEnd(); // 读取文件
+ }
+ catch { }
+ sr?.Close();
+ sr?.Dispose();
+ return str;
+
+ }
+
+ ///
+ /// 写文件
+ ///
+ ///
+ ///
+ private static void WriteAndSave(string fileName, string content)
+ {
+ //实例化一个文件流--->与写入文件相关联
+ using var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write);
+ //实例化一个StreamWriter-->与fs相关联
+ using var sw = new StreamWriter(fs);
+ //开始写入
+ sw.Write(content);
+ //清空缓冲区
+ sw.Flush();
+ //关闭流
+ sw.Close();
+ fs.Close();
+ }
+
+ #endregion
+ }
+}
diff --git a/ZR.CodeGenerator/DbProvider.cs b/ZR.CodeGenerator/DbProvider.cs
new file mode 100644
index 0000000..c4a5265
--- /dev/null
+++ b/ZR.CodeGenerator/DbProvider.cs
@@ -0,0 +1,30 @@
+using Infrastructure;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ZR.CodeGenerator
+{
+ public class DbProvider
+ {
+
+ public SqlSugarClient GetSugarDbContext(string dbName)
+ {
+ string connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.Conn).Replace("{DbName}", dbName);
+ int dbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.DbType, 0);
+
+ return new SqlSugarClient(new List()
+ {
+ new ConnectionConfig(){
+ ConnectionString = connStr,
+ DbType = (DbType)dbType,
+ IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
+ InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
+ },
+ });
+ }
+ }
+}
diff --git a/ZR.CodeGenerator/Service/CodeGeneraterService.cs b/ZR.CodeGenerator/Service/CodeGeneraterService.cs
new file mode 100644
index 0000000..6dc3b1d
--- /dev/null
+++ b/ZR.CodeGenerator/Service/CodeGeneraterService.cs
@@ -0,0 +1,88 @@
+using Infrastructure;
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using ZR.Model;
+using ZR.Model.CodeGenerator;
+
+namespace ZR.CodeGenerator.Service
+{
+ public class CodeGeneraterService: DbProvider
+ {
+ /////
+ ///// 获取表所有列
+ /////
+ /////
+ /////
+ //public List GetAllColumns(string tableName)
+ //{
+ // var dbType = ConfigUtils.Instance.GetConfig("CodeGenDbType");
+ // if (tableName == null)
+ // throw new ArgumentException(nameof(tableName));
+ // List list = new List();
+ // if (dbType == "1")
+ // {
+ // list = CodeGeneratorRepository.GetAllColumns(tableName);
+ // }
+ // return list;
+ //}
+
+ ///
+ /// 获取所有数据库名
+ ///
+ ///
+ public List GetAllDataBases()
+ {
+ var dbType = ConfigUtils.Instance.GetConfig("CodeGenDbType");
+ List list = new List();
+ if (dbType == "1")
+ {
+ var db = GetSugarDbContext("ZrAdmin");
+ var templist = db.DbMaintenance.GetDataBaseList(db);
+ templist.ForEach(item =>
+ {
+ list.Add(new DataBaseInfo() { DbName = item });
+ });
+ }
+ else if (dbType == "0")
+ {
+ // list = mssqlExtractor.GetAllDataBases();
+ }
+ return list;
+ }
+
+ ///
+ /// 获取所有表
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public List GetAllTables(string dbName, string tableName, PagerInfo pager)
+ {
+ var tableList = GetSugarDbContext(dbName).DbMaintenance.GetTableInfoList(true);
+ if (!string.IsNullOrEmpty(tableName))
+ {
+ tableList = tableList.Where(f => f.Name.Contains(tableName)).ToList();
+ }
+ pager.TotalNum = tableList.Count;
+ return tableList.Skip(pager.PageSize * (pager.PageNum - 1)).Take(pager.PageSize).OrderBy(f => f.Name).ToList();
+ }
+
+ ///
+ /// 获取列信息
+ ///
+ ///
+ ///
+ ///
+ public List GetColumnInfo(string dbName, string tableName)
+ {
+ return GetSugarDbContext(dbName).DbMaintenance.GetColumnInfosByTableName(tableName, true);
+ }
+
+ }
+}
diff --git a/ZR.CodeGenerator/TableMappingHelper.cs b/ZR.CodeGenerator/TableMappingHelper.cs
index e0c9daa..44f8e38 100644
--- a/ZR.CodeGenerator/TableMappingHelper.cs
+++ b/ZR.CodeGenerator/TableMappingHelper.cs
@@ -34,6 +34,8 @@ namespace ZR.CodeGenerator.CodeGenerator
public static string GetClassNamePrefix(string tableName)
{
string[] arr = tableName.Split('_');
+ if (arr.Length <= 0) return tableName;
+
StringBuilder sb = new StringBuilder();
for (int i = 1; i < arr.Length; i++)
{
diff --git a/ZR.CodeGenerator/ZR.CodeGenerator.csproj b/ZR.CodeGenerator/ZR.CodeGenerator.csproj
index 948f805..d93648b 100644
--- a/ZR.CodeGenerator/ZR.CodeGenerator.csproj
+++ b/ZR.CodeGenerator/ZR.CodeGenerator.csproj
@@ -12,7 +12,16 @@
-
+
+
+
+
+
+
+
+
+ PreserveNewest
+
diff --git a/ZR.Model/CodeGenerator/DbFieldInfo.cs b/ZR.Model/CodeGenerator/DbFieldInfo.cs
index f176895..f5e145e 100644
--- a/ZR.Model/CodeGenerator/DbFieldInfo.cs
+++ b/ZR.Model/CodeGenerator/DbFieldInfo.cs
@@ -10,6 +10,7 @@ namespace ZR.Model
///
public class DbFieldInfo
{
+ public string TableName { get; set; }
///
/// 初始化
///
diff --git a/ZR.Model/Models/README.txt b/ZR.Model/Models/README.txt
new file mode 100644
index 0000000..39e9872
--- /dev/null
+++ b/ZR.Model/Models/README.txt
@@ -0,0 +1 @@
+此文件夹用于存放业务代码数据库实体类
\ No newline at end of file
diff --git a/ZR.Repository/DbProvider/SugarDbContext.cs b/ZR.Repository/DbProvider/SugarDbContext.cs
index 2aa9b4e..68f7afe 100644
--- a/ZR.Repository/DbProvider/SugarDbContext.cs
+++ b/ZR.Repository/DbProvider/SugarDbContext.cs
@@ -42,6 +42,7 @@ namespace ZR.Repository.DbProvider
//调式代码 用来打印SQL
Db.Aop.OnLogExecuting = (sql, pars) =>
{
+ Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine("【SQL语句】" + sql.ToLower() + "\r\n" + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
};
//出错打印日志
@@ -51,5 +52,21 @@ namespace ZR.Repository.DbProvider
Console.WriteLine();
};
}
+
+ public SqlSugarClient GetSugarDbContext(string dbName)
+ {
+ string connStr = ConfigUtils.Instance.GetConnectionStrings(OptionsSetting.Conn).Replace("{DbName}", dbName);
+ int dbType = ConfigUtils.Instance.GetAppConfig(OptionsSetting.DbType, 0);
+
+ return new SqlSugarClient(new List()
+ {
+ new ConnectionConfig(){
+ ConnectionString = connStr,
+ DbType = (DbType)dbType,
+ IsAutoCloseConnection = true,//开启自动释放模式和EF原理一样
+ InitKeyType = InitKeyType.Attribute,//从特性读取主键和自增列信息
+ },
+ });
+ }
}
}
diff --git a/ZR.Repository/System/CodeGeneratorRepository.cs b/ZR.Repository/System/CodeGeneratorRepository.cs
index 7455f89..b5d5f97 100644
--- a/ZR.Repository/System/CodeGeneratorRepository.cs
+++ b/ZR.Repository/System/CodeGeneratorRepository.cs
@@ -13,33 +13,62 @@ namespace ZR.Repository.System
public class CodeGeneratorRepository : BaseRepository
{
///
- /// 获取数据库信息
+ /// 获取数据库
///
///
- public List GetAllDataBaseInfos()
+ public List GetAllDb()
{
- return Db.Ado.SqlQuery("select name as DbName from master..sysdatabases ");
+ //return Db.Ado.SqlQuery("select name as DbName from master..sysdatabases ");
+ var list = Db.DbMaintenance.GetDataBaseList(Db);
+ List dataBases = new List();
+ list.ForEach(item =>
+ {
+ dataBases.Add(new DataBaseInfo() { DbName = item });
+ });
+ return dataBases;
}
///
- /// 获取所有的表
+ /// 根据数据库名获取所有的表
///
///
///
///
///
- public List GetAllTables(string dbName, string tableName, PagerInfo pager)
+ public List GetAllTables(string dbName, string tableName, PagerInfo pager)
{
- string sql = $"SELECT name as TableName FROM {dbName}..SysObjects Where XType='U'";
- int total = 0;
- var list = Db.SqlQueryable(sql)
- //.WithCache(60 * 10)
- .WhereIF(!string.IsNullOrEmpty(tableName), it => it.TableName.Contains(tableName))
- .AddParameters(new { dbName })
- .OrderBy(x => x.TableName)
- .ToPageList(pager.PageNum, pager.PageSize, ref total);
- pager.TotalNum = total;
- return list;
+ var tableList = GetSugarDbContext(dbName).DbMaintenance.GetTableInfoList(true);
+ if (!string.IsNullOrEmpty(tableName))
+ {
+ tableList = tableList.Where(f => f.Name.Contains(tableName)).ToList();
+ }
+ pager.TotalNum = tableList.Count;
+ return tableList.Skip(pager.PageSize * (pager.PageNum - 1)).Take(pager.PageSize).OrderBy(f => f.Name).ToList();
+ }
+
+ ///
+ /// 获取表格列信息
+ ///
+ ///
+ ///
+ ///
+ public List GetColumnInfo(string dbName, string tableName)
+ {
+ return GetSugarDbContext(dbName).DbMaintenance.GetColumnInfosByTableName(tableName, true);
+ }
+
+
+ ///
+ /// 获取当前数据库表名
+ ///
+ ///
+ ///
+ public List GetAllTables(string[] tabList)
+ {
+ string sql = @"SELECT tbs.name as TableName ,ds.value as Description FROM sys.tables tbs
+ left join sys.extended_properties ds on ds.major_id=tbs.object_id and ds.minor_id=0";
+
+ return Db.SqlQueryable(sql).WhereIF(tabList.Length > 0, x => tabList.Contains(x.TableName)).ToList();
}
}
}
diff --git a/ZR.Service/IService/ICodeGeneratorService.cs b/ZR.Service/IService/ICodeGeneratorService.cs
index db7798e..2e713a9 100644
--- a/ZR.Service/IService/ICodeGeneratorService.cs
+++ b/ZR.Service/IService/ICodeGeneratorService.cs
@@ -8,10 +8,16 @@ using ZR.Model.CodeGenerator;
namespace ZR.Service.IService
{
- public interface ICodeGeneratorService
- {
- List GetAllDataBases(string dbType);
+ //public interface ICodeGeneratorService
+ //{
+ // List GetAllDataBases();
- List GetTablesWithPage(string tablename, string dbName, PagerInfo info);
- }
+ // List GetTablesWithPage(string tablename, string dbName, PagerInfo info);
+
+ // List GetAllColumns(string tableName);
+
+ // List GetAllTables(string tabList);
+
+ // List GetColumnInfo(string dbName, string tableName);
+ //}
}
diff --git a/ZR.Service/System/CodeGeneratorService.cs b/ZR.Service/System/CodeGeneratorService.cs
index c6b9112..7c8446e 100644
--- a/ZR.Service/System/CodeGeneratorService.cs
+++ b/ZR.Service/System/CodeGeneratorService.cs
@@ -15,53 +15,85 @@ namespace ZR.Service.System
///
///
///
- [AppService(ServiceType = typeof(ICodeGeneratorService), ServiceLifetime = LifeTime.Transient)]
- public class CodeGeneratorService: ICodeGeneratorService
+ //[AppService(ServiceType = typeof(ICodeGeneratorService), ServiceLifetime = LifeTime.Transient)]
+ public class CodeGeneratorService //: ICodeGeneratorService
{
- public CodeGeneratorRepository CodeGeneratorRepository;
+ //public CodeGeneratorRepository CodeGeneratorRepository;
- public CodeGeneratorService(CodeGeneratorRepository codeGeneratorRepository)
- {
- CodeGeneratorRepository = codeGeneratorRepository;
- }
+ //public CodeGeneratorService(CodeGeneratorRepository codeGeneratorRepository)
+ //{
+ // CodeGeneratorRepository = codeGeneratorRepository;
+ //}
+
+ /////
+ ///// 获取表所有列
+ /////
+ /////
+ /////
+ //public List GetAllColumns(string tableName)
+ //{
+ // var dbType = ConfigUtils.Instance.GetConfig("CodeGenDbType");
+ // if (tableName == null)
+ // throw new ArgumentException(nameof(tableName));
+ // List list = new List();
+ // if (dbType == "1")
+ // {
+ // list = CodeGeneratorRepository.GetAllColumns(tableName);
+ // }
+ // return list;
+ //}
+
+ /////
+ ///// 获取所有数据库名
+ /////
+ /////
+ //public List GetAllDataBases()
+ //{
+ // var dbType = ConfigUtils.Instance.GetConfig("CodeGenDbType");
+ // List list = new List();
+ // if (dbType == "1")
+ // {
+ // list = CodeGeneratorRepository.GetAllDb();
+ // }
+ // else if (dbType == "0")
+ // {
+ // // list = mssqlExtractor.GetAllDataBases();
+ // }
+ // return list;
+ //}
+
+ //public List GetAllTables(string tableStrs)
+ //{
+ // string[] tabList = tableStrs.Split(",");
+
+ // return CodeGeneratorRepository.GetAllTables(tabList);
+ //}
///
- /// 获取所有数据库名
+ /// 获取列信息
///
+ ///
+ ///
///
- public List GetAllDataBases(string dbType)
- {
- List list = new List();
- if (dbType.Contains("SqlServer"))
- {
- list = CodeGeneratorRepository.GetAllDataBaseInfos();
- }
- else if (dbType.Contains("MySql"))
- {
- // list = mssqlExtractor.GetAllDataBases();
- }
- return list;
- }
+ //public List GetColumnInfo(string dbName, string tableName)
+ //{
+ // return CodeGeneratorRepository.GetColumnInfo(dbName, tableName);
+ //}
- public List GetTablesWithPage(string tablename, string dbName, PagerInfo pager)
- {
- var dbType = ConfigUtils.Instance.GetConfig("CodeGenDbType");
- List list = new List();
- if (dbType == "1")
- {
- list = CodeGeneratorRepository.GetAllTables(dbName, tablename, pager);
- }
- else if (dbType.Contains("MySql"))
- {
- //list = mysqlExtractor.GetAllTables(this.dbName, tablename, fieldNameToSort, isDescending, info);
- }
- //if (!string.IsNullOrEmpty(tablename))
- //{
- // list = list.Where(f => f.TableName.Contains(tablename)).ToList();
- //}
-
- return list;
- }
+ //public List GetTablesWithPage(string tablename, string dbName, PagerInfo pager)
+ //{
+ // var dbType = ConfigUtils.Instance.GetConfig("CodeGenDbType");
+ // List list = new List();
+ // if (dbType == "1")
+ // {
+ // list = CodeGeneratorRepository.GetAllTables(dbName, tablename, pager);
+ // }
+ // else if (dbType == "0")
+ // {
+ // //list = mysqlExtractor.GetAllTables(this.dbName, tablename, fieldNameToSort, isDescending, info);
+ // }
+ // return list;
+ //}
}
}
diff --git a/ZR.Vue/src/api/monitor/operlog.js b/ZR.Vue/src/api/monitor/operlog.js
index 07e27ab..58def23 100644
--- a/ZR.Vue/src/api/monitor/operlog.js
+++ b/ZR.Vue/src/api/monitor/operlog.js
@@ -26,10 +26,10 @@ export function cleanOperlog() {
}
// 导出操作日志
-export function exportOperlog(query) {
- return request({
- url: '/monitor/operlog/export',
- method: 'get',
- params: query
- })
-}
+// export function exportOperlog(query) {
+// return request({
+// url: '/monitor/operlog/export',
+// method: 'get',
+// params: query
+// })
+// }
diff --git a/ZR.Vue/src/layout/components/Navbar.vue b/ZR.Vue/src/layout/components/Navbar.vue
index 2d0df08..1e36156 100644
--- a/ZR.Vue/src/layout/components/Navbar.vue
+++ b/ZR.Vue/src/layout/components/Navbar.vue
@@ -6,16 +6,13 @@
-
-
-
-
+
+
+
+
+
+ 预览
+ 生成代码
+
+