You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.3 KiB
C#

using djy.Paas.Model;
using FreeRedis;
using FreeRedis.Internal;
using FreeSql;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Ys.Core.Common;
namespace djy.Paas.Service
{
/// <summary>
/// 数据库结构体
/// </summary>
public static class DbList
{
/// <summary>
///独立的大简云publice库
/// </summary>
public const string djypublicedb = "djydb";
/// <summary>
///大简云平台老库
/// </summary>
public const string djyolddb = "djyolddb";
/// <summary>
/// 账单中心库
/// </summary>
public const string BillCenter = "billcenter";
/// <summary>
/// 日志库
/// </summary>
public const string Logsdb = "logsdb";
}
/// <summary>
/// 数据库操作类
/// </summary>
public class DbContext
{
/// <summary>
/// 数据库连接池
/// </summary>
public static IdleBus<IFreeSql> DbBus=new IdleBus<IFreeSql>(TimeSpan.FromSeconds(20));
//public static IFreeSql DbBus.Get(DbList.djypublicedb);
/// <summary>
/// RedisDB
/// </summary>
public static RedisClient DbRedis = new RedisClient(string.Format("{0}", sysOptionConfig.YsWebconfig.Redis));
/// <summary>
/// 数据库连接池初始化
/// </summary>
/// <returns></returns>
public static bool DbBusInit() {
foreach (var item in sysOptionConfig.YsWebconfig.DataConnList)
{
if (item.SysKey == DbList.Logsdb)//日志库允许自动更新表结构
{
DbBus.Register(item.SysKey, () => new FreeSqlBuilder().UseConnectionString((DataType)item.DataType, item.ConnString)
.UseAutoSyncStructure(true)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd =>
{
Trace.WriteLine("\r\n" + Thread.CurrentThread.ManagedThreadId + ":" + cmd.CommandText);
})
.Build());
}
else
{
DbBus.Register(item.SysKey, () => new FreeSqlBuilder().UseConnectionString((DataType)item.DataType, item.ConnString)
// .UseAutoSyncStructure(sysOptionConfig.YsWebconfig.IsDev)
.UseAutoSyncStructure(false)
.UseNoneCommandParameter(true)
.UseMonitorCommand(cmd =>
{
Trace.WriteLine("\r\n" + Thread.CurrentThread.ManagedThreadId + ":" + cmd.CommandText);
})
.Build());
}
}
return true;
}
/// <summary>
/// 数据库初始化
/// </summary>
public static ReturnResult<string> DbInit()
{
var rs = new ReturnResult<string>();
DbBus.Get(DbList.djypublicedb).CodeFirst.SyncStructure<tb_BaoguanContainer>();
DbBus.Get(DbList.djypublicedb).CodeFirst.SyncStructure<tb_BaoguanDecList>();
rs.OK("生成成功!");
return rs;
}
}
}