287 lines
12 KiB
C#
287 lines
12 KiB
C#
using APT.BaseData.Domain.ApiModel.Platform;
|
|
using APT.BaseData.Domain.Entities.FM;
|
|
using APT.BaseData.Domain.Entities.OP;
|
|
using APT.BaseData.Domain.Enums.OP;
|
|
using APT.BaseData.Domain.IServices.OP;
|
|
using APT.Infrastructure.Api;
|
|
using APT.Infrastructure.Api.Redis;
|
|
using APT.Infrastructure.Core;
|
|
using APT.Migrations;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using System.Net;
|
|
|
|
namespace APT.BaseData.Services.Services.OP
|
|
{
|
|
public class OPTenantDBConnService : CommonService, IOPTenantDBConnService
|
|
{
|
|
public OPTenantDBConnService(IRepository repository) : base(repository)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 除 ORG_IDBeside 之外对应的数据库链接字典 ORG_ID DB_CONN
|
|
/// </summary>
|
|
/// <param name="ORG_IDBeside">过滤值 没有就不过滤</param>
|
|
/// <returns></returns>
|
|
public Dictionary<Guid, string> GetConnDictionary(Guid? ORG_IDBeside)
|
|
{
|
|
Dictionary<Guid, string> dicORGCONN = new Dictionary<Guid, string>();
|
|
//从Redis中获取所有数据库链接值
|
|
|
|
bool isRedisConfig = true;
|
|
var redisConfig = APT.Infrastructure.Api.ConfigurationManager.AppSettings["RedisFormConfig"];
|
|
if (!string.IsNullOrEmpty(redisConfig))
|
|
isRedisConfig = bool.Parse(redisConfig);
|
|
if (isRedisConfig)
|
|
{
|
|
if (CsRedisManager.KeyExists(RedisCacheKey.ConnAll))
|
|
dicORGCONN = CsRedisManager.StringGet<Dictionary<Guid, string>>(RedisCacheKey.ConnAll);
|
|
}
|
|
#region Redis没找到 去数据库查找
|
|
|
|
if (dicORGCONN == null || dicORGCONN.Count < 1)
|
|
{
|
|
string conn = string.Empty;
|
|
IEnumerable<T_OP_TENANT> listTent = null;
|
|
using (var context = new MigrationContext())
|
|
{
|
|
Expression<Func<T_OP_TENANT, bool>> expression = e => e.DB_CONN_ID.HasValue;
|
|
listTent = context.GetEntities(expression, null, null);
|
|
List<Guid> listConnID = listTent.Select(e => e.DB_CONN_ID.Value).ToList();
|
|
var listDbConn = context.GetEntities<T_OP_TENANT_DB_CONN>(e => listConnID.Contains(e.ID), null, null);
|
|
if (listDbConn != null && listDbConn.Count() > 0)
|
|
{
|
|
var env = ConfigurationManager.AppSettings["Env"];
|
|
foreach (var item in listDbConn)
|
|
{
|
|
conn = item.DB_CONN;
|
|
if (env == ((int)EnvType.外网).ToString())
|
|
{
|
|
conn = item.DB_CONN_WAN;
|
|
}
|
|
if (!dicORGCONN.ContainsKey(item.ID))
|
|
{
|
|
dicORGCONN.Add(listTent.First(e => e.DB_CONN_ID == item.ID).ID, conn);
|
|
}
|
|
}
|
|
if (isRedisConfig && dicORGCONN.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
CsRedisManager.StringSet<Dictionary<Guid, string>>(RedisCacheKey.ConnAll, dicORGCONN);//所有数据库链接 存入 Redis
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (dicORGCONN != null && dicORGCONN.Count > 0)
|
|
{
|
|
if (ORG_IDBeside.HasValue)
|
|
{
|
|
Dictionary<Guid, string> dicORGCONN1 = new Dictionary<Guid, string>();
|
|
foreach (var item in dicORGCONN)
|
|
{
|
|
if (item.Key == ORG_IDBeside.Value)
|
|
continue;
|
|
|
|
dicORGCONN1.Add(item.Key, item.Value);
|
|
}
|
|
dicORGCONN = dicORGCONN1;
|
|
}
|
|
}
|
|
return dicORGCONN;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取 ORG_ID的数据库链接
|
|
/// </summary>
|
|
/// <param name="ORG_ID">过滤值</param>
|
|
/// <returns></returns>
|
|
public string GetConnByORGID(Guid ORG_ID)
|
|
{
|
|
Dictionary<Guid, string> dicORGCONN = new Dictionary<Guid, string>();
|
|
//从Redis中获取所有数据库链接值
|
|
|
|
bool isRedisConfig = true;
|
|
var redisConfig = APT.Infrastructure.Api.ConfigurationManager.AppSettings["RedisFormConfig"];
|
|
if (!string.IsNullOrEmpty(redisConfig))
|
|
isRedisConfig = bool.Parse(redisConfig);
|
|
if (isRedisConfig)
|
|
{
|
|
if (CsRedisManager.KeyExists(RedisCacheKey.ConnAll))
|
|
dicORGCONN = CsRedisManager.StringGet<Dictionary<Guid, string>>(RedisCacheKey.ConnAll);
|
|
}
|
|
#region Redis没找到 去数据库查找
|
|
|
|
if (dicORGCONN == null || dicORGCONN.Count < 1)
|
|
{
|
|
string conn = string.Empty;
|
|
IEnumerable<T_OP_TENANT> listTent = null;
|
|
using (var context = new MigrationContext())
|
|
{
|
|
Expression<Func<T_OP_TENANT, bool>> expression = e => e.DB_CONN_ID.HasValue;
|
|
listTent = context.GetEntities(expression, null, null);
|
|
List<Guid> listConnID = listTent.Select(e => e.DB_CONN_ID.Value).ToList();
|
|
var listDbConn = context.GetEntities<T_OP_TENANT_DB_CONN>(e => listConnID.Contains(e.ID), null, null);
|
|
if (listDbConn != null && listDbConn.Count() > 0)
|
|
{
|
|
var env = ConfigurationManager.AppSettings["Env"];
|
|
foreach (var item in listDbConn)
|
|
{
|
|
conn = item.DB_CONN;
|
|
if (env == ((int)EnvType.外网).ToString())
|
|
{
|
|
conn = item.DB_CONN_WAN;
|
|
}
|
|
if (!dicORGCONN.ContainsKey(item.ID))
|
|
{
|
|
dicORGCONN.Add(listTent.First(e => e.DB_CONN_ID == item.ID).ID, conn);
|
|
}
|
|
}
|
|
if (isRedisConfig && dicORGCONN.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
CsRedisManager.StringSet<Dictionary<Guid, string>>(RedisCacheKey.ConnAll, dicORGCONN);//所有数据库链接 存入 Redis
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (dicORGCONN != null && dicORGCONN.Count > 0 && dicORGCONN.ContainsKey(ORG_ID))
|
|
{
|
|
return dicORGCONN[ORG_ID];
|
|
}
|
|
return "";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据ORGID 获取 Tenant
|
|
/// </summary>
|
|
/// <param name="ORG_ID"></param>
|
|
/// <returns></returns>
|
|
public string GetTenantByORGID(Guid ORG_ID)
|
|
{
|
|
|
|
Dictionary<Guid, string> dicORGCONN = new Dictionary<Guid, string>();
|
|
//从Redis中获取所有数据库链接值
|
|
|
|
bool isRedisConfig = true;
|
|
var redisConfig = APT.Infrastructure.Api.ConfigurationManager.AppSettings["RedisFormConfig"];
|
|
if (!string.IsNullOrEmpty(redisConfig))
|
|
isRedisConfig = bool.Parse(redisConfig);
|
|
if (isRedisConfig)
|
|
{
|
|
if (CsRedisManager.KeyExists(RedisCacheKey.TenantAll))
|
|
dicORGCONN = CsRedisManager.StringGet<Dictionary<Guid, string>>(RedisCacheKey.TenantAll);
|
|
}
|
|
#region Redis没找到 去数据库查找
|
|
|
|
if (dicORGCONN == null || dicORGCONN.Count < 1)
|
|
{
|
|
string conn = string.Empty;
|
|
IEnumerable<T_OP_TENANT> listTent = null;
|
|
using (var context = new MigrationContext())
|
|
{
|
|
Expression<Func<T_OP_TENANT, bool>> expression = e => e.DB_CONN_ID.HasValue;
|
|
listTent = context.GetEntities(expression, null, null);
|
|
if (listTent != null && listTent.Count() > 0)
|
|
{
|
|
foreach (var item in listTent)
|
|
{
|
|
if (!dicORGCONN.ContainsKey(item.ID))
|
|
{
|
|
dicORGCONN.Add(item.ID, item.CODE);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (dicORGCONN != null && dicORGCONN.Count > 0 && dicORGCONN.ContainsKey(ORG_ID))
|
|
{
|
|
return dicORGCONN[ORG_ID];
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取 ORG_ID的数据库链接
|
|
/// </summary>
|
|
/// <param name="TenantCOde">过滤值</param>
|
|
/// <returns></returns>
|
|
public string GetHeadConn(string TenantCOde)
|
|
{
|
|
Dictionary<Guid, string> dicORGCONN = new Dictionary<Guid, string>();
|
|
//从Redis中获取所有数据库链接值
|
|
|
|
bool isRedisConfig = true;
|
|
var redisConfig = APT.Infrastructure.Api.ConfigurationManager.AppSettings["RedisFormConfig"];
|
|
if (!string.IsNullOrEmpty(redisConfig))
|
|
isRedisConfig = bool.Parse(redisConfig);
|
|
if (isRedisConfig)
|
|
{
|
|
if (CsRedisManager.KeyExists(RedisCacheKey.ConnAll))
|
|
dicORGCONN = CsRedisManager.StringGet<Dictionary<Guid, string>>(RedisCacheKey.ConnAll);
|
|
}
|
|
#region Redis没找到 去数据库查找
|
|
|
|
if (dicORGCONN == null || dicORGCONN.Count < 1)
|
|
{
|
|
string conn = string.Empty;
|
|
IEnumerable<T_OP_TENANT> listTent = null;
|
|
using (var context = new MigrationContext())
|
|
{
|
|
Expression<Func<T_OP_TENANT, bool>> expression = e => e.DB_CONN_ID.HasValue;
|
|
listTent = context.GetEntities(expression, null, null);
|
|
List<Guid> listConnID = listTent.Select(e => e.DB_CONN_ID.Value).ToList();
|
|
var listDbConn = context.GetEntities<T_OP_TENANT_DB_CONN>(e => listConnID.Contains(e.ID), null, null);
|
|
if (listDbConn != null && listDbConn.Count() > 0)
|
|
{
|
|
var env = ConfigurationManager.AppSettings["Env"];
|
|
foreach (var item in listDbConn)
|
|
{
|
|
conn = item.DB_CONN;
|
|
if (env == ((int)EnvType.外网).ToString())
|
|
{
|
|
conn = item.DB_CONN_WAN;
|
|
}
|
|
if (!dicORGCONN.ContainsKey(item.ID))
|
|
{
|
|
dicORGCONN.Add(listTent.First(e => e.DB_CONN_ID == item.ID).ID, conn);
|
|
}
|
|
}
|
|
if (isRedisConfig && dicORGCONN.Count > 0)
|
|
{
|
|
try
|
|
{
|
|
CsRedisManager.StringSet<Dictionary<Guid, string>>(RedisCacheKey.ConnAll, dicORGCONN);//所有数据库链接 存入 Redis
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
if (dicORGCONN != null && dicORGCONN.Count > 0 && dicORGCONN.ContainsKey(ORG_ID))
|
|
{
|
|
return dicORGCONN[ORG_ID];
|
|
}
|
|
return "";
|
|
}
|
|
}
|
|
}
|