change- 添加自定义分表策略

main
Wen JY 4 months ago
parent ac6199ec7a
commit 2db7a44df2

@ -1 +1 @@
38cea589c6583306e028f7802e4cb060ec1ca7f6
93cff6037311b0410aec40b449d81c076811b5b7

@ -27,6 +27,7 @@ using System;
using System.Collections.Generic;
using Sln.Iot.Model.dao;
using Sln.Iot.Repository.service.@base;
using Sln.Iot.Repository.service.split;
namespace Sln.Iot.Repository.service.Impl
{
@ -54,7 +55,12 @@ namespace Sln.Iot.Repository.service.Impl
{
// _rep.AsTenant().BeginTran();
insertIds = _rep.Context.Insertable(list).SplitTable().ExecuteReturnSnowflakeIdList();
var sqlSugarClient = _rep.Context;
//加载指定分表策略
//sqlSugarClient.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService = new MonitorIdToSplitService();
insertIds = sqlSugarClient.Insertable(list).SplitTable().ExecuteReturnSnowflakeIdList();
// _rep.AsTenant().CommitTran();
return true;
}

@ -0,0 +1,97 @@
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* (c) 2025 WenJY
* CLR4.0.30319.42000
* Mr.Wen's MacBook Pro
* Sln.Iot.Repository.service.split
* 2D1DE17A-79B9-48D8-BB98-60E89BEC711A
*
* WenJY
*
* 2025-04-27 10:10:14
* V1.0.0
*
*
*--------------------------------------------------------------------
*
*
*
*
* V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using SqlSugar;
namespace Sln.Iot.Repository.service.split
{
/// <summary>
/// 自定义分表策略:根据传感器 ID 进行分表
/// </summary>
public class MonitorIdToSplitService:ISplitTableService
{
/// <summary>
/// 返回数据库中所有分表
/// </summary>
/// <param name="db"></param>
/// <param name="EntityInfo"></param>
/// <param name="tableInfos"></param>
/// <returns></returns>
public List<SplitTableInfo> GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo, List<DbTableInfo> tableInfos)
{
List<SplitTableInfo> result = new List<SplitTableInfo>();
foreach (var item in tableInfos)
{
if (item.Name.Contains("record_iotenv_instant")) //区分标识如果不用正则符复杂一些,防止找错表
{
SplitTableInfo data = new SplitTableInfo()
{
TableName = item.Name //要用item.name不要写错了
};
result.Add(data);
}
}
return result.OrderBy(it=>it.TableName).ToList();//打断点看一下有没有查出所有分表
}
/// <summary>
/// 获取分表字段的值
/// </summary>
/// <param name="db"></param>
/// <param name="entityInfo"></param>
/// <param name="splitType"></param>
/// <param name="entityValue"></param>
/// <returns></returns>
public object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue)
{
var splitColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute<SplitFieldAttribute>() != null);
var value = splitColumn.PropertyInfo.GetValue(entityValue, null);
return value;
}
/// <summary>
/// 默认表名
/// </summary>
/// <param name="db"></param>
/// <param name="EntityInfo"></param>
/// <returns></returns>
public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo)
{
return entityInfo.DbTableName;
}
public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType type)
{
return entityInfo.DbTableName;//目前模式少不需要分类(自带的有 日、周、月、季、年等进行区分)
}
public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue)
{
return entityInfo.DbTableName + "_"+fieldValue; //根据值按首字母
}
}
}

@ -5,5 +5,6 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ADbType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0ba64183daa748c3bb3b3edf21a6b8772c6400_003F4c_003Fa39a659e_003FDbType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASingleStreamDataHandlingAdapter_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Feb369188171d26dfbd70fc9217c2c77e185e88012e5ccc16852cb4f3f20c072_003FSingleStreamDataHandlingAdapter_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASocketClient_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fb0f3662e64b2b46775d44a81f4853543f8867641934463348fae80f3dc775b_003FSocketClient_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASplitType_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003F0ba64183daa748c3bb3b3edf21a6b8772c6400_003Fbd_003Fbd737833_003FSplitType_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATcpCore_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003F2278ccad6b9afb60d1ee3b95183fab3b1975ca876f55b5567ef2db23d79dd_003FTcpCore_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATcpService_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003F_002E_002E_003FLibrary_003FApplication_0020Support_003FJetBrains_003FRider2024_002E3_003Fresharper_002Dhost_003FSourcesCache_003Fd2819fc242e5be7ca9eccaff9efd8baffa7341c1b1778daa5393ca48d4693_003FTcpService_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
Loading…
Cancel
Save