From 2c287f66f354004538db7c615af556e06fee795c 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: Sun, 18 Jun 2023 21:36:03 +0800
Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90Csh?=
=?UTF-8?q?arp=E6=95=B0=E6=8D=AE=E7=B1=BB=E5=9E=8B=E6=94=B9=E4=B8=BA?=
=?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Infrastructure/OptionsSetting.cs | 18 ++++-
ZR.Admin.WebApi/appsettings.json | 14 +++-
ZR.CodeGenerator/CodeGeneratorTool.cs | 58 +++++++++-----
ZR.CodeGenerator/GenConstants.cs | 104 ++++++++++++++++----------
4 files changed, 133 insertions(+), 61 deletions(-)
diff --git a/Infrastructure/OptionsSetting.cs b/Infrastructure/OptionsSetting.cs
index 160ced5..fc85ef3 100644
--- a/Infrastructure/OptionsSetting.cs
+++ b/Infrastructure/OptionsSetting.cs
@@ -1,4 +1,5 @@
+using System;
using System.Collections.Generic;
namespace Infrastructure
@@ -80,7 +81,11 @@ namespace Infrastructure
public class Gen
{
- public string Database { get; set; }
+ public bool AutoPre { get; set; }
+ public string VuePath { get; set; }
+ public string Author { get; set; }
+ public DbConfigs GenDbConfig { get; set; }
+ public CsharpTypeArr CsharpTypeArr { get; set; }
}
public class DbConfigs
@@ -95,4 +100,15 @@ namespace Infrastructure
public bool IsGenerateDb { get; set; }
public string DbName { get; set; }
}
+
+ public class CsharpTypeArr
+ {
+ public string[] String { get; set; }
+ public string[] Int { get; set; }
+ public string[] Long { get; set; }
+ public string[] DateTime { get; set; }
+ public string[] Float { get; set; }
+ public string[] Decimal { get; set; }
+ public string[] Bool { get; set; }
+ }
}
diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json
index 762a634..8b45059 100644
--- a/ZR.Admin.WebApi/appsettings.json
+++ b/ZR.Admin.WebApi/appsettings.json
@@ -62,7 +62,17 @@
"autoPre": true, //自动去除表前缀
"author": "admin",
"tablePrefix": "sys_", //"表前缀(生成类名不会包含表前缀,多个用逗号分隔)",
- "vuePath": "" //前端代码存储路径eg:D:\Work\ZRAdmin-Vue3
+ "vuePath": "", //前端代码存储路径eg:D:\Work\ZRAdmin-Vue3
+ "csharpTypeArr": {
+ "string": [ "varchar", "nvarchar", "text", "longtext" ],
+ "int": [ "int", "integer", "smallint", "int4", "int8", "int2" ],
+ "long": [ "bigint", "number" ],
+ "float": [ "numeric", "real", "float" ],
+ "decimal": [ "money", "decimal", "smallmoney" ],
+ "dateTime": [ "date", "datetime", "datetime2", "smalldatetime", "timestamp" ],
+ "byte": [ "tinyint" ],
+ "bool": [ "bit" ]
+ }
},
//邮箱配置信息
"MailOptions": {
@@ -71,7 +81,7 @@
//发送人邮箱
"FromEmail": "", //eg:xxxx@qq.com
//发送人邮箱密码
- "Password": "123456",
+ "Password": "",
//协议
"Smtp": "smtp.qq.com",
"Port": 587,
diff --git a/ZR.CodeGenerator/CodeGeneratorTool.cs b/ZR.CodeGenerator/CodeGeneratorTool.cs
index 26790b6..41a6509 100644
--- a/ZR.CodeGenerator/CodeGeneratorTool.cs
+++ b/ZR.CodeGenerator/CodeGeneratorTool.cs
@@ -334,23 +334,36 @@ namespace ZR.CodeGenerator
/// 获取C# 类型
///
///
+ ///
///
- public static string GetCSharpDatatype(string sDatatype)
+ public static CSharpDataType GetCSharpDatatype(string sDatatype, CsharpTypeArr csharpType)
{
sDatatype = sDatatype.ToLower();
- string sTempDatatype = sDatatype switch
+ if (csharpType.Int.Contains(sDatatype))
{
- "int" or "integer" or "smallint" or "int4" or "int8" or "int2" => "int",
- "bigint" or "number" => "long",
- "tinyint" => "byte",
- "numeric" or "real" or "float" => "float",
- "decimal" or "numer(8,2)" or "numeric" => "decimal",
- "bit" => "bool",
- "date" or "datetime" or "datetime2" or "smalldatetime" or "timestamp" => "DateTime",
- "money" or "smallmoney" => "decimal",
- _ => "string",
- };
- return sTempDatatype;
+ return CSharpDataType.@int;
+ }
+ else if (csharpType.Long.Contains(sDatatype))
+ {
+ return CSharpDataType.@long;
+ }
+ else if (csharpType.Float.Contains(sDatatype))
+ {
+ return CSharpDataType.@float;
+ }
+ else if (csharpType.Decimal.Contains(sDatatype))
+ {
+ return CSharpDataType.@decimal;
+ }
+ else if (csharpType.DateTime.Contains(sDatatype))
+ {
+ return CSharpDataType.DateTime;
+ }
+ else if (csharpType.Bool.Contains(sDatatype))
+ {
+ return CSharpDataType.@bool;
+ }
+ return CSharpDataType.@string;
}
#endregion
@@ -398,10 +411,18 @@ namespace ZR.CodeGenerator
///
public static List InitGenTableColumn(GenTable genTable, List dbColumnInfos, List seqs = null)
{
+ OptionsSetting optionsSetting = new();
+
+ var gen = AppSettings.Get("gen");
+ var dbConfigs = AppSettings.Get>("dbConfigs");
+ var dbConfig = dbConfigs.FirstOrDefault(f => f.IsGenerateDb);
+ optionsSetting.DbConfigs = dbConfigs;
+ optionsSetting.Gen = gen ?? throw new CustomException("代码生成节点配置异常");
+ optionsSetting.Gen.GenDbConfig = dbConfig ?? throw new CustomException("代码生成节点数据配置异常");
List genTableColumns = new();
foreach (var column in dbColumnInfos)
{
- genTableColumns.Add(InitColumnField(genTable, column, seqs));
+ genTableColumns.Add(InitColumnField(genTable, column, seqs, optionsSetting));
}
return genTableColumns;
}
@@ -411,12 +432,13 @@ namespace ZR.CodeGenerator
///
///
///
+ ///
+ /// oracle 序列
///
- private static GenTableColumn InitColumnField(GenTable genTable, DbColumnInfo column, List seqs)
+ private static GenTableColumn InitColumnField(GenTable genTable, DbColumnInfo column, List seqs, OptionsSetting optionsSetting)
{
- var dbConfig = AppSettings.Get>("dbConfigs").FirstOrDefault(f => f.IsGenerateDb);
var dataType = column.DataType;
- if (dbConfig.DbType == 3)
+ if (optionsSetting.Gen.GenDbConfig.DbType == 3)
{
dataType = column.OracleDataType;
var seqName = $"SEQ_{genTable.TableName}_{column.DbColumnName}";
@@ -431,7 +453,7 @@ namespace ZR.CodeGenerator
ColumnType = dataType,
TableId = genTable.TableId,
TableName = genTable.TableName,
- CsharpType = GetCSharpDatatype(dataType),
+ CsharpType = GetCSharpDatatype(dataType, optionsSetting.Gen.CsharpTypeArr).ToString(),
CsharpField = column.DbColumnName.ConvertToPascal("_"),
IsRequired = !column.IsNullable,
IsIncrement = column.IsIdentity,
diff --git a/ZR.CodeGenerator/GenConstants.cs b/ZR.CodeGenerator/GenConstants.cs
index fe5a8fc..772cb2c 100644
--- a/ZR.CodeGenerator/GenConstants.cs
+++ b/ZR.CodeGenerator/GenConstants.cs
@@ -28,7 +28,6 @@ namespace ZR.CodeGenerator
///
public static readonly string[] radioFiled = new string[] { "status", "state", "is" };
-
///
/// 单表(增删改查)
///
@@ -69,38 +68,60 @@ namespace ZR.CodeGenerator
///
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[] COLUMNNAME_NOT_EDIT = { "id", "create_by", "create_time", "delFlag" };
- /** 页面不需要显示的列表字段 */
+ ///
+ /// 页面不需要显示的列表字段
+ ///
public static string[] COLUMNNAME_NOT_LIST = { "create_by", "create_time", "delFlag", "update_by",
"update_time" , "password"};
- /** 页面不需要查询字段 */
+ ///
+ /// 页面不需要查询字段
+ ///
public static string[] COLUMNNAME_NOT_QUERY = { "id", "create_by", "create_time", "delFlag", "update_by",
"update_time", "remark" };
- /** Entity基类字段 */
+ ///
+ /// Entity基类字段
+ ///
public static string[] BASE_ENTITY = { "createBy", "createTime", "updateBy", "updateTime", "remark" };
- /** Tree基类字段 */
+ ///
+ /// Tree基类字段
+ ///
public static string[] TREE_ENTITY = { "parentName", "parentId", "orderNum", "ancestors", "children" };
- /** 文本框 */
+ ///
+ /// 文本框
+ ///
public static string HTML_INPUT = "input";
- /** 数字框 */
+ ///
+ /// 数字框
+ ///
public static string HTML_INPUT_NUMBER = "inputNumber";
- /** 文本域 */
+ ///
+ /// 文本域
+ ///
public static string HTML_TEXTAREA = "textarea";
/** 下拉框 */
@@ -110,53 +131,56 @@ namespace ZR.CodeGenerator
///
public static string HTML_SELECT_MULTI = "selectMulti";
- /** 单选框 */
+ ///
+ /// 单选框
+ ///
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 HTML_SORT = "sort";
///
/// 自定义输入框
///
public static string HTML_CUSTOM_INPUT = "customInput";
- //颜色选择器
+ ///
+ /// 颜色选择器
+ ///
public static string HTML_COLORPICKER = "colorPicker";
- //switch开关
- public static string HTML_SWITCH { get; set; }
- /** 字符串类型 */
- public static string TYPE_STRING = "string";
-
- /** 整型 */
- public static string TYPE_INT = "int";
-
- /** 长整型 */
- public static string TYPE_LONG = "long";
-
- /** 浮点型 */
- public static string TYPE_DOUBLE = "Double";
-
- /** 时间类型 */
- public static string TYPE_DATE = "DateTime";
-
- /** 模糊查询 */
+ ///
+ /// 模糊查询
+ ///
public static string QUERY_LIKE = "LIKE";
- /** 需要 */
+ ///
+ /// 需要
+ ///
public static string REQUIRE = "1";
+ ///
+ /// 时间类型
+ ///
+ public static string TYPE_DATE = "DateTime";
}
}
\ No newline at end of file