From 46e98339480d853fc78a014c34d7ff9fcaf13890 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期四, 05 十二月 2024 14:09:02 +0800
Subject: [PATCH] 产线协议

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Tenants/TenantUtil.cs |  127 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 127 insertions(+), 0 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Tenants/TenantUtil.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Tenants/TenantUtil.cs"
new file mode 100644
index 0000000..a0a9755
--- /dev/null
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Tenants/TenantUtil.cs"
@@ -0,0 +1,127 @@
+锘縰sing SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using WIDESEA_Core.DB;
+
+namespace WIDESEA_Core.Tenants
+{
+    public static class TenantUtil
+    {
+        //public static SysTenant DefaultTenantConfig(this SysTenant tenant)
+        //{
+        //    tenant.DbType ??= DbType.Sqlite;
+
+        //    //濡傛灉娌℃湁閰嶇疆杩炴帴
+        //    if (tenant.Connection.IsNullOrEmpty())
+        //    {
+        //        //姝ゅ榛樿閰嶇疆 Sqlite 鍦板潃
+        //        //瀹為檯涓氬姟涓� 涔熶細鏈夎繍缁淬�佺郴缁熺鐞嗗憳绛夋潵缁存姢
+        //        switch (tenant.DbType.Value)
+        //        {
+        //            case DbType.Sqlite:
+        //                tenant.Connection = $"DataSource={Path.Combine(Environment.CurrentDirectory, tenant.ConfigId)}.db";
+        //                break;
+        //        }
+        //    }
+
+        //    return tenant;
+        //}
+
+        //public static ConnectionConfig GetConnectionConfig(this SysTenant tenant)
+        //{
+        //    if (tenant.DbType is null)
+        //    {
+        //        throw new ArgumentException("Tenant DbType Must");
+        //    }
+
+
+        //    return new ConnectionConfig()
+        //    {
+        //        ConfigId = tenant.ConfigId,
+        //        DbType = tenant.DbType.Value,
+        //        ConnectionString = tenant.Connection,
+        //        IsAutoCloseConnection = true,
+        //        MoreSettings = new ConnMoreSettings()
+        //        {
+        //            IsAutoRemoveDataCache = true
+        //        },
+        //    };
+        //}
+
+        public static List<Type> GetTenantEntityTypes(TenantTypeEnum? tenantType = null)
+        {
+            List<Type> types = RepositorySetting.Entitys.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass).ToList();
+
+            List<Type> returnTypes = types.Where(s => IsTenantEntity(s, tenantType)).ToList();
+            return returnTypes;
+        }
+
+        public static bool IsTenantEntity(this Type u, TenantTypeEnum? tenantType = null)
+        {
+            var mta = u.GetCustomAttribute<MultiTenantAttribute>();
+            if (mta is null)
+            {
+                return false;
+            }
+
+            if (tenantType != null)
+            {
+                if (mta.TenantType != tenantType)
+                {
+                    return false;
+                }
+            }
+
+            return true;
+        }
+
+        public static string GetTenantTableName(this Type type, ISqlSugarClient db, string id)
+        {
+            var entityInfo = db.EntityMaintenance.GetEntityInfo(type);
+            return $@"{entityInfo.DbTableName}_{id}";
+        }
+
+        //public static string GetTenantTableName(this Type type, ISqlSugarClient db, SysTenant tenant)
+        //{
+        //    return GetTenantTableName(type, db, tenant.Id.ToString());
+        //}
+
+        public static void SetTenantTable(this ISqlSugarClient db, string id)
+        {
+            var types = GetTenantEntityTypes(TenantTypeEnum.Tables);
+
+            foreach (var type in types)
+            {
+                db.MappingTables.Add(type.Name, type.GetTenantTableName(db, id));
+            }
+        }
+
+        public static List<SelectModel> GetTenantSelectModels()
+        {
+            List<SelectModel> selectModels = new List<SelectModel>()
+            {
+                new SelectModel
+                {
+                    FieldName = MainDb.TenantId
+                },
+                //new SelectModel
+                //{
+                //    FieldName = MainDb.TenantName
+                //},
+                new SelectModel
+                {
+                    FieldName = MainDb.ConnectionString,
+                },
+                new SelectModel
+                {
+                    FieldName = MainDb.TenantDbType
+                }
+            };
+            return selectModels;
+        }
+    }
+}

--
Gitblit v1.9.3