From 44b37d4ce4ca530d3d37b88e418d6631703c2171 Mon Sep 17 00:00:00 2001 From: Phoenix <10v@10v.cn> Date: Tue, 30 Apr 2024 18:02:07 +0000 Subject: [PATCH 1/6] update ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs. Signed-off-by: Phoenix <10v@10v.cn> --- ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs index 5cc17832..c0cb5f1b 100644 --- a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs +++ b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs @@ -1,4 +1,4 @@ -using Infrastructure; +using Infrastructure; using Infrastructure.Model; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -69,6 +69,7 @@ namespace ZR.ServiceCore.SqlSugar private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache) { var config = db.GetConnectionScope(iocConfig.ConfigId).CurrentConnectionConfig; + DbConvertToUnderLine dbConvertToUnderLine = AppSettings.Get("DbConvertToUnderLine"); var showDbLog = AppSettings.Get("ShowDbLog"); string configId = config.ConfigId; db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) => @@ -203,7 +204,15 @@ namespace ZR.ServiceCore.SqlSugar } } #endregion - } + if (dbConvertToUnderLine.Fields && !p.IsIgnore && !p.DbColumnName.Contains('_')) + p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName); // 驼峰转下划线 + }, + // 处理表 + EntityNameService = (type, entity) => + { + if (dbConvertToUnderLine.Tables && !entity.DbTableName.Contains('_')) + entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName);// 驼峰转下划线 + }, }; db.GetConnectionScope(configId).Aop.OnLogExecuted = (sql, pars) => { -- Gitee From ecde44ec2f3ab26b2b2f9377e59a8ff0a1892709 Mon Sep 17 00:00:00 2001 From: Phoenix <10v@10v.cn> Date: Tue, 30 Apr 2024 18:05:14 +0000 Subject: [PATCH 2/6] =?UTF-8?q?update=20ZR.Admin.WebApi/appsettings.json.?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9EToUnderLine=E9=A9=BC=E5=B3=B0=E8=BD=AC?= =?UTF-8?q?=E4=B8=8B=E5=88=92=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Phoenix <10v@10v.cn> --- ZR.Admin.WebApi/appsettings.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 1718cc6e..5ae4edcd 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -36,6 +36,10 @@ "InjectClass": [ "ZR.Repository", "ZR.Service", "ZR.Tasks", "ZR.ServiceCore" ], //自动注入类 "ShowDbLog": true, //是否打印db日志 "InitDb": false, //是否初始化db + "DbConvertToUnderLine": { + "Fields": true, // 表示是否将数据库字段名转换为下划线格式 + "Tables": true // 表示是否将数据库表名转换为下划线格式 + }, "DemoMode": false, //是否演示模式 "SingleLogin": false, //是否允许多设备/浏览器登录 "workId": 1, //雪花id唯一数字 -- Gitee From 36d6f438acd859c12678bf0cf570d81c786d4f08 Mon Sep 17 00:00:00 2001 From: Phoenix <10v@10v.cn> Date: Tue, 30 Apr 2024 18:06:10 +0000 Subject: [PATCH 3/6] update Infrastructure/Model/OptionsSetting.cs. Signed-off-by: Phoenix <10v@10v.cn> --- Infrastructure/Model/OptionsSetting.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Infrastructure/Model/OptionsSetting.cs b/Infrastructure/Model/OptionsSetting.cs index 85969a0d..c76d5798 100644 --- a/Infrastructure/Model/OptionsSetting.cs +++ b/Infrastructure/Model/OptionsSetting.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; namespace Infrastructure.Model { @@ -126,4 +126,13 @@ namespace Infrastructure.Model public string[] Decimal { get; set; } public string[] Bool { get; set; } } + + public class DbConvertToUnderLine { + + public bool Fields { get; set; } + + public bool Tables { get; set; } + + + } } -- Gitee From f2f68c7115f71801dccf76ee8ed7f1bf282c6b13 Mon Sep 17 00:00:00 2001 From: Phoenix <10v@10v.cn> Date: Tue, 30 Apr 2024 18:12:27 +0000 Subject: [PATCH 4/6] update ZR.Admin.WebApi/appsettings.json. Signed-off-by: Phoenix <10v@10v.cn> --- ZR.Admin.WebApi/appsettings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index 5ae4edcd..610f8bd6 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -37,8 +37,8 @@ "ShowDbLog": true, //是否打印db日志 "InitDb": false, //是否初始化db "DbConvertToUnderLine": { - "Fields": true, // 表示是否将数据库字段名转换为下划线格式 - "Tables": true // 表示是否将数据库表名转换为下划线格式 + "Fields": false, // 表示是否将数据库字段名转换为下划线格式 + "Tables": false // 表示是否将数据库表名转换为下划线格式 }, "DemoMode": false, //是否演示模式 "SingleLogin": false, //是否允许多设备/浏览器登录 -- Gitee From b51b60bd1ecec8e9c47dcfe4c72930cdc894796a Mon Sep 17 00:00:00 2001 From: Phoenix <10v@10v.cn> Date: Sat, 4 May 2024 06:55:30 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ZR.S?= =?UTF-8?q?erviceCore/SqlSugar/SqlsugarSetup.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs | 244 ----------------------- 1 file changed, 244 deletions(-) delete mode 100644 ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs diff --git a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs b/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs deleted file mode 100644 index c0cb5f1b..00000000 --- a/ZR.ServiceCore/SqlSugar/SqlsugarSetup.cs +++ /dev/null @@ -1,244 +0,0 @@ -using Infrastructure; -using Infrastructure.Model; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using SqlSugar.IOC; -using ZR.Common; -using ZR.Model.System; - -namespace ZR.ServiceCore.SqlSugar -{ - public static class SqlsugarSetup - { - private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); - - /// - /// 初始化db - /// - /// - /// - public static void AddDb(this IServiceCollection services, IWebHostEnvironment environment) - { - var options = App.OptionsSetting; - List dbConfigs = options.DbConfigs; - - var iocList = new List(); - foreach (var item in dbConfigs) - { - iocList.Add(new IocConfig() - { - ConfigId = item.ConfigId, - ConnectionString = item.Conn, - DbType = (IocDbType)item.DbType, - IsAutoCloseConnection = item.IsAutoCloseConnection - }); - } - SugarIocServices.AddSqlSugar(iocList); - ICacheService cache = new SqlSugarCache(); - SugarIocServices.ConfigurationSugar(db => - { - var u = App.User; - if (u != null) - { - DataPermi.FilterData(0); - //ConfigId = 1的数据权限过滤 - //DataPermiSevice.FilterData(1); - } - - iocList.ForEach(iocConfig => - { - SetSugarAop(db, iocConfig, cache); - }); - }); - - if (environment.IsDevelopment()) - { - InitTable.InitDb(options.InitDb); - - InitTable.InitNewTb(); - } - } - - /// - /// 数据库Aop设置 - /// - /// - /// - /// - private static void SetSugarAop(SqlSugarClient db, IocConfig iocConfig, ICacheService cache) - { - var config = db.GetConnectionScope(iocConfig.ConfigId).CurrentConnectionConfig; - DbConvertToUnderLine dbConvertToUnderLine = AppSettings.Get("DbConvertToUnderLine"); - var showDbLog = AppSettings.Get("ShowDbLog"); - string configId = config.ConfigId; - db.GetConnectionScope(configId).Aop.OnLogExecuting = (sql, pars) => - { - if (showDbLog) - { - string log = $"【db{configId} SQL】{UtilMethods.GetSqlString(config.DbType, sql, pars)}\n"; - if (sql.TrimStart().StartsWith("SELECT", StringComparison.OrdinalIgnoreCase)) - { - logger.Info(log); - } - else if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase)) - { - logger.Warn(log); - } - else if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("TRUNCATE", StringComparison.OrdinalIgnoreCase)) - { - logger.Error(log); - } - else - { - log = $"【db{configId} SQL语句】dbo.{sql} {string.Join(", ", pars.Select(x => x.ParameterName + " = " + GetParsValue(x)))};\n"; - logger.Info(log); - } - } - }; - db.GetConnectionScope(configId).Aop.OnError = (ex) => - { - //var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value)); - - string sql = "【错误SQL】" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n"; - logger.Error(ex, $"{sql}\r\n{ex.Message}\r\n{ex.StackTrace}"); - }; - db.GetConnectionScope(configId).Aop.DataExecuting = (oldValue, entiyInfo) => - { - }; - //差异日志功能 - db.GetConnectionScope(configId).Aop.OnDiffLogEvent = it => - { - //操作前记录 包含: 字段描述 列名 值 表名 表描述 - var editBeforeData = it.BeforeData;//插入Before为null,之前还没进库 - //操作后记录 包含: 字段描述 列名 值 表名 表描述 - var editAfterData = it.AfterData; - var sql = it.Sql; - var parameter = it.Parameters; - var data = it.BusinessData;//这边会显示你传进来的对象 - var time = it.Time; - var diffType = it.DiffType;//enum insert 、update and delete - string name = App.UserName; - - foreach (var item in editBeforeData) - { - var pars = db.Utilities.SerializeObject(item.Columns.ToDictionary(it => it.ColumnName, it => it.Value)); - - SqlDiffLog log = new() - { - BeforeData = pars, - BusinessData = data?.ToString(), - DiffType = diffType.ToString(), - Sql = sql, - TableName = item.TableName, - UserName = name, - AddTime = DateTime.Now, - ConfigId = configId - }; - if (editAfterData != null) - { - var afterData = editAfterData?.First(x => x.TableName == item.TableName); - var afterPars = db.Utilities.SerializeObject(afterData?.Columns.ToDictionary(it => it.ColumnName, it => it.Value)); - log.AfterData = afterPars; - } - //logger.WithProperty("title", data).Info(pars); - db.GetConnectionScope(0) - .Insertable(log) - .ExecuteReturnSnowflakeId(); - } - }; - db.GetConnectionScope(configId).CurrentConnectionConfig.MoreSettings = new ConnMoreSettings() - { - IsAutoRemoveDataCache = true - }; - db.GetConnectionScope(configId).CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() - { - DataInfoCacheService = cache, - EntityService = (c, p) => - { - if (p.IsPrimarykey == true)//主键不能为null - { - p.IsNullable = false; - } - else if (p.ExtendedAttribute?.ToString() == ProteryConstant.NOTNULL.ToString()) - { - p.IsNullable = false; - } - else//则否默认为null - { - p.IsNullable = true; - } - - if (config.DbType == DbType.PostgreSQL) - { - if (c.Name == nameof(SysMenu.IsCache) || c.Name == nameof(SysMenu.IsFrame)) - { - p.DataType = "char(1)"; - } - } - #region 兼容Oracle - if (config.DbType == DbType.Oracle) - { - if (p.IsIdentity == true) - { - if (p.EntityName == nameof(SysUser)) - { - p.OracleSequenceName = "SEQ_SYS_USER_USERID"; - } - else if (p.EntityName == nameof(SysRole)) - { - p.OracleSequenceName = "SEQ_SYS_ROLE_ROLEID"; - } - else if (p.EntityName == nameof(SysDept)) - { - p.OracleSequenceName = "SEQ_SYS_DEPT_DEPTID"; - } - else if (p.EntityName == nameof(SysMenu)) - { - p.OracleSequenceName = "SEQ_SYS_MENU_MENUID"; - } - else - { - p.OracleSequenceName = "SEQ_ID"; - } - } - } - #endregion - if (dbConvertToUnderLine.Fields && !p.IsIgnore && !p.DbColumnName.Contains('_')) - p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName); // 驼峰转下划线 - }, - // 处理表 - EntityNameService = (type, entity) => - { - if (dbConvertToUnderLine.Tables && !entity.DbTableName.Contains('_')) - entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName);// 驼峰转下划线 - }, - }; - db.GetConnectionScope(configId).Aop.OnLogExecuted = (sql, pars) => - { - var sqlExecutionTime = AppSettings.Get("sqlExecutionTime"); - if (db.Ado.SqlExecutionTime.TotalSeconds > sqlExecutionTime) - { - //代码CS文件名 - var fileName = db.Ado.SqlStackTrace.FirstFileName; - //代码行数 - var fileLine = db.Ado.SqlStackTrace.FirstLine; - //方法名 - var FirstMethodName = db.Ado.SqlStackTrace.FirstMethodName; - var logInfo = $"Sql执行超时,用时{db.Ado.SqlExecutionTime.TotalSeconds}秒【{sql}】,fileName={fileName},line={fileLine},methodName={FirstMethodName}"; - WxNoticeHelper.SendMsg("Sql请求时间过长", logInfo); - logger.Warn(logInfo); - } - }; - } - - private static object GetParsValue(SugarParameter x) - { - if (x.DbType == System.Data.DbType.String || x.DbType == System.Data.DbType.DateTime || x.DbType == System.Data.DbType.String) - { - return "'" + x.Value + "'"; - } - return x.Value; - } - } -} -- Gitee From 549c4ca40fca76f7b364bab8a848204e86d39812 Mon Sep 17 00:00:00 2001 From: Phoenix <10v@10v.cn> Date: Sat, 4 May 2024 06:55:36 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20Infr?= =?UTF-8?q?astructure/Model/OptionsSetting.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Infrastructure/Model/OptionsSetting.cs | 138 ------------------------- 1 file changed, 138 deletions(-) delete mode 100644 Infrastructure/Model/OptionsSetting.cs diff --git a/Infrastructure/Model/OptionsSetting.cs b/Infrastructure/Model/OptionsSetting.cs deleted file mode 100644 index c76d5798..00000000 --- a/Infrastructure/Model/OptionsSetting.cs +++ /dev/null @@ -1,138 +0,0 @@ -using System.Collections.Generic; - -namespace Infrastructure.Model -{ - /// - /// 获取配置文件POCO实体类 - /// - public class OptionsSetting - { - /// - /// 是否单点登录 - /// - public bool SingleLogin { get; set; } - /// - /// 是否演示模式 - /// - public bool DemoMode { get; set; } - /// - /// 初始化db - /// - public bool InitDb { get; set; } - public string[] InitTables { get; set; } - public List MailOptions { get; set; } - public Upload Upload { get; set; } - public ALIYUN_OSS ALIYUN_OSS { get; set; } - public JwtSettings JwtSettings { get; set; } - public CodeGen CodeGen { get; set; } - public List DbConfigs { get; set; } - public DbConfigs CodeGenDbConfig { get; set; } - } - /// - /// 发送邮件数据配置 - /// - public class MailOptions - { - public string FromName { get; set; } - public string FromEmail { get; set; } - public string Password { get; set; } - public string Smtp { get; set; } - public int Port { get; set; } - public bool UseSsl { get; set; } - public string Signature { get; set; } - } - /// - /// 上传 - /// - public class Upload - { - public string UploadUrl { get; set; } - public string LocalSavePath { get; set; } - public int MaxSize { get; set; } - public string[] NotAllowedExt { get; set; } = new string[0]; - } - /// - /// 阿里云存储 - /// - public class ALIYUN_OSS - { - public string REGIONID { get; set; } - public string KEY { get; set; } - public string SECRET { get; set; } - public string BucketName { get; set; } - public string DomainUrl { get; set; } - public int MaxSize { get; set; } = 100; - } - - /// - /// Jwt - /// - public class JwtSettings - { - /// - /// token是谁颁发的 - /// - public string Issuer { get; set; } - /// - /// token可以给那些客户端使用 - /// - public string Audience { get; set; } - /// - /// 加密的key(SecretKey必须大于16个,是大于,不是大于等于) - /// - public string SecretKey { get; set; } - /// - /// token时间(分) - /// - public int Expire { get; set; } = 1440; - /// - /// 刷新token时长 - /// - public int RefreshTokenTime { get; set; } - /// - /// token类型 - /// - public string TokenType { get; set; } = "Bearer"; - } - - public class CodeGen - { - public bool ShowApp { get; set; } - public bool AutoPre { get; set; } - public string VuePath { get; set; } - public string Author { get; set; } - public string TablePrefix { get; set; } - public string ModuleName { get; set; } - public int FrontTpl { get; set; } - public CsharpTypeArr CsharpTypeArr { get; set; } - } - - public class DbConfigs - { - public string Conn { get; set; } - public int DbType { get; set; } - public string ConfigId { get; set; } - public bool IsAutoCloseConnection { 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; } - } - - public class DbConvertToUnderLine { - - public bool Fields { get; set; } - - public bool Tables { get; set; } - - - } -} -- Gitee