WCS
dengjunjie
2024-10-14 901c64f294ee75784f3fec80b47ae6b77932fff3
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
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEAWCS_Core.Const;
using WIDESEAWCS_Core.Helper;
using WIDESEAWCS_Core.Tenants;
 
namespace WIDESEAWCS_Core.DB
{
    public class BaseDBConfig
    {
        /* 之前的单库操作已经删除,如果想要之前的代码,可以查看我的GitHub的历史记录
         * 目前是多库操作,默认加载的是appsettings.json设置为true的第一个db连接。
         */
        public static List<MutiDBOperate> MutiConnectionString => MutiInitConn();
 
        private static string DifDBConnOfSecurity(params string[] conn)
        {
            foreach (var item in conn)
            {
                try
                {
                    if (File.Exists(item))
                    {
                        return File.ReadAllText(item).Trim();
                    }
                }
                catch (System.Exception)
                {
                }
            }
 
            return conn[conn.Length - 1];
        }
 
        public static List<MutiDBOperate> MutiInitConn()
        {
            SqlSugarClient sqlSugarClient = new SqlSugarClient(new ConnectionConfig
            {
                ConfigId = MainDb.CurrentDbConnId,
                ConnectionString = AppSettings.app(MainDb.ConnectionString).DecryptDES(AppSecret.DB),
                IsAutoCloseConnection = true,
                DbType = MainDb.DbType,
                AopEvents = new AopEvents
                {
                    OnError = x =>
                    {
                        Console.WriteLine(x.Sql);
                    }
                }
            });
 
            List<ExpandoObject> list = sqlSugarClient.Queryable(MainDb.TenantTableName, "x").Where(MainDb.TenantStatus, "=", TenantStatus.Enable).Select(TenantUtil.GetTenantSelectModels()).ToList();
            List<MutiDBOperate> listdatabaseSlaveDB = new List<MutiDBOperate>();
            MutiDBOperate mainDb = new MutiDBOperate()
            {
                Connection = AppSettings.app(MainDb.ConnectionString).DecryptDES(AppSecret.DB),
                ConnId = MainDb.CurrentDbConnId,
                DbType = DataBaseType.SqlServer
            };
            listdatabaseSlaveDB.Add(mainDb);
            for (int i = 0; i < list.Count; i++)
            {
                dynamic data = list[i];
                MutiDBOperate mutiDBOperate = new MutiDBOperate()
                {
                    Connection = data.ConnectionString,
                    ConnId = data.TenantId + "",
                    DbType = (DataBaseType)data.DbType,
                };
                mutiDBOperate.Connection = mutiDBOperate.Connection.DecryptDES(AppSecret.DB);
                listdatabaseSlaveDB.Add(mutiDBOperate);
            }
 
            return listdatabaseSlaveDB;
        }
    }
 
    public enum DataBaseType
    {
        MySql = 0,
        SqlServer = 1,
        Sqlite = 2,
        Oracle = 3,
        PostgreSQL = 4,
        Dm = 5,
        Kdbndp = 6,
    }
 
    public class MutiDBOperate
    {
        /// <summary>
        /// 连接启用开关
        /// </summary>
        public bool Enabled { get; set; }
 
        /// <summary>
        /// 连接ID
        /// </summary>
        public string ConnId { get; set; }
 
        /// <summary>
        /// 从库执行级别,越大越先执行
        /// </summary>
        public int HitRate { get; set; }
 
        /// <summary>
        /// 连接字符串
        /// </summary>
        public string Connection { get; set; }
 
        /// <summary>
        /// 数据库类型
        /// </summary>
        public DataBaseType DbType { get; set; }
    }
}