wangxinhui
2024-12-26 78b99e5348592a29ca1393a5e13db619cc4eba56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//using Furion;
//using Mapster;
//using Microsoft.Extensions.Hosting;
//using SqlSugar;
//using System;
//using System.Collections.Generic;
//using System.Linq;
 
//namespace WIDESEA.Common
//{
//    /// <summary>
//    /// 数据库上下文对象
//    /// </summary>
//    public static class SqlSugarDbContext
//    {
//        /// <summary>
//        /// 读取配置文件中的 ConnectionStrings:Sqlsugar 配置节点
//        /// </summary>
//        public static readonly List<SqlSugarConfig> DbConfigs = App.GetConfig<List<SqlSugarConfig>>("SqlSugarSettings:ConnectionStrings");
 
//        /// <summary>
//        /// SqlSugar 数据库实例
//        /// </summary>
//        public static readonly SqlSugarScope Db = new(
//            DbConfigs.Adapt<List<ConnectionConfig>>()
//            , db =>
//            {
//                //遍历配置的数据库
//                DbConfigs.ForEach(it =>
//                {
//                    var sqlsugarScope = db.GetConnectionScope(it.ConfigId);//获取当前库
//                    MoreSetting(sqlsugarScope);//更多设置
//                    ExternalServicesSetting(sqlsugarScope, it);//实体拓展配置
//                    AopSetting(sqlsugarScope);//aop配置
//                    FilterSetting(sqlsugarScope);//过滤器配置
//                });
//            });
 
//        /// <summary>
//        /// 实体拓展配置,自定义类型多库兼容
//        /// </summary>
//        /// <param name="db"></param>
//        /// <param name="config"></param>
//        private static void ExternalServicesSetting(SqlSugarScopeProvider db, SqlSugarConfig config)
//        {
//            db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices
//            {
//                // 处理表
//                EntityNameService = (type, entity) =>
//                {
//                    if (config.IsUnderLine && !entity.DbTableName.Contains('_'))
//                        entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName); // 驼峰转下划线
//                },
//                //自定义类型多库兼容
//                EntityService = (c, p) =>
//                {
//                    //如果是mysql并且是varchar(max) 已弃用
//                    //if (config.DbType == SqlSugar.DbType.MySql && (p.DataType == SqlsugarConst.NVarCharMax))
//                    //{
//                    //    p.DataType = SqlsugarConst.LongText;//转成mysql的longtext
//                    //}
//                    //else if (config.DbType == SqlSugar.DbType.Sqlite && (p.DataType == SqlsugarConst.NVarCharMax))
//                    //{
//                    //    p.DataType = SqlsugarConst.Text;//转成sqlite的text
//                    //}
//                    //默认不写IsNullable为非必填
//                    //if (new NullabilityInfoContext().Create(c).WriteState is NullabilityState.Nullable)
//                    //    p.IsNullable = true;
//                    if (config.IsUnderLine && !p.IsIgnore && !p.DbColumnName.Contains('_'))
//                        p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName); // 驼峰转下划线
//                }
//            };
//        }
 
//        /// <summary>
//        /// Aop设置
//        /// </summary>
//        /// <param name="db"></param>
//        public static void AopSetting(SqlSugarScopeProvider db)
//        {
//            var config = db.CurrentConnectionConfig;
 
//            // 设置超时时间
//            db.Ado.CommandTimeOut = 30;
 
//            // 打印SQL语句
//            db.Aop.OnLogExecuting = (sql, pars) =>
//            {
//                //如果不是开发环境就打印sql
//                if (App.HostEnvironment.IsDevelopment())
//                {
//                    if (sql.StartsWith("SELECT"))
//                    {
//                        Console.ForegroundColor = ConsoleColor.Green;
//                        WriteSqlLog($"查询{config.ConfigId}库操作");
//                    }
//                    if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT"))
//                    {
//                        Console.ForegroundColor = ConsoleColor.Blue;
//                        WriteSqlLog($"修改{config.ConfigId}库操作");
//                    }
//                    if (sql.StartsWith("DELETE"))
//                    {
//                        Console.ForegroundColor = ConsoleColor.Red;
//                        WriteSqlLog($"删除{config.ConfigId}库操作");
//                    }
//                    Console.WriteLine(UtilMethods.GetSqlString(config.DbType, sql, pars));
//                    WriteSqlLog($"{config.ConfigId}库操作结束");
//                    Console.ForegroundColor = ConsoleColor.White;
//                    Console.WriteLine();
//                }
//            };
//            //异常
//            db.Aop.OnError = (ex) =>
//            {
//                //如果不是开发环境就打印日志
//                if (App.WebHostEnvironment.IsDevelopment())
//                {
//                    if (ex.Parametres == null) return;
//                    Console.ForegroundColor = ConsoleColor.Red;
//                    var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
//                    WriteSqlLog($"{config.ConfigId}库操作异常");
//                    Console.WriteLine(UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
//                    Console.ForegroundColor = ConsoleColor.White;
//                }
//            };
//        }
 
//        /// <summary>
//        /// 实体更多配置
//        /// </summary>
//        /// <param name="db"></param>
//        private static void MoreSetting(SqlSugarScopeProvider db)
//        {
//            db.CurrentConnectionConfig.MoreSettings = new ConnMoreSettings
//            {
//                SqlServerCodeFirstNvarchar = true,//设置默认nvarchar
//            };
//        }
 
//        /// <summary>
//        /// 过滤器设置
//        /// </summary>
//        /// <param name="db"></param>
//        public static void FilterSetting(SqlSugarScopeProvider db)
//        {
//            // 假删除过滤器
//            //LogicDeletedEntityFilter(db);
//        }
 
//        /// <summary>
//        /// 假删除过滤器
//        /// </summary>
//        /// <param name="db"></param>
//        private static void LogicDeletedEntityFilter(SqlSugarScopeProvider db)
//        {
//        }
 
//        private static void WriteSqlLog(string msg)
//        {
//            Console.WriteLine($"=============={msg}==============");
//        }
//    }
//}