diff --git a/Src/Asp.NetCore2/SqliteTest/UserTestCases/Main.cs b/Src/Asp.NetCore2/SqliteTest/UserTestCases/Main.cs index e1d3d0902..e45f20e37 100644 --- a/Src/Asp.NetCore2/SqliteTest/UserTestCases/Main.cs +++ b/Src/Asp.NetCore2/SqliteTest/UserTestCases/Main.cs @@ -32,6 +32,7 @@ namespace OrmTest } public static void Init() { + Unitasdfays.Init(); Unitadfafafa.Init(); UnitSubGroupadfa.Init(); UnitAsyncToken.Init(); diff --git a/Src/Asp.NetCore2/SqliteTest/UserTestCases/Unitasdfays.cs b/Src/Asp.NetCore2/SqliteTest/UserTestCases/Unitasdfays.cs new file mode 100644 index 000000000..5c7b9b436 --- /dev/null +++ b/Src/Asp.NetCore2/SqliteTest/UserTestCases/Unitasdfays.cs @@ -0,0 +1,112 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace OrmTest +{ + public class Unitasdfays + { + public static void Init() + { + var db = NewUnitTest.Db; + db.CodeFirst.InitTables(typeof(SqlSugarTestEntity)); + db.DbMaintenance.TruncateTable(); + SqlSugarTestEntity entity = new SqlSugarTestEntity { Id=1,BarCode = "1111", PatientName = "小明" }; + int affrow = db.Storageable(entity).WhereColumns(it => it.BarCode).SplitTable().ExecuteCommand(); + entity.PatientName = "小明1"; + entity.Id = 0; + var x= db.Storageable(entity ).As("SQLSUGARTEST").WhereColumns(it=>it.BarCode).ExecuteCommand(); + var x2=db.Updateable(new List() { entity }).AS("SQLSUGARTEST").WhereColumns(it => it.BarCode).ExecuteCommand(); + affrow += db.Storageable(entity).WhereColumns(it => it.BarCode).SplitTable().ExecuteCommand(); + if (affrow != 2||x!=1||x2!=1) + { + throw new Exception("unit error"); + } + + } + } + [SqlSugar.SugarTable("SQLSUGARTEST")] + [SqlSugar.SplitTable(SplitType._Custom01, typeof(SplitTableService))] + public class SqlSugarTestEntity + { + [SqlSugar.SugarColumn(IsPrimaryKey = true)] + public int Id { get; set; } + public string BarCode { get; set; } + public string PatientName { get; set; } + + /// + /// 操作时间 分表字段 + /// + [SplitField] + public DateTime? ProTime { get; set; } = DateTime.Now; + } + /// + /// 自定义分表集合 + /// + public class SplitTableService : ISplitTableService + { + /// + /// 获取所有匹配的表 + /// + /// + /// + /// + /// + public List GetAllTables(ISqlSugarClient db, EntityInfo EntityInfo, List tableInfos) + { + List result = new List(); + foreach (var item in tableInfos) + { + if (item.Name.Contains(EntityInfo.DbTableName)) + //if (item.Name.Contains("_2021") || item.Name == EntityInfo.DbTableName ) //区分标识如果不用正则符复杂一些,防止找错表 + { + SplitTableInfo data = new SplitTableInfo() + { + TableName = item.Name //要用item.name不要写错了 + }; + result.Add(data); + } + } + return result.OrderBy(it => it.TableName).ToList();//打断点看一下有没有查出所有分表 + } + + public object GetFieldValue(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object entityValue) + { + var splitColumn = entityInfo.Columns.FirstOrDefault(it => it.PropertyInfo.GetCustomAttribute() != null); + var value = splitColumn.PropertyInfo.GetValue(entityValue, null); + return value; + } + + public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo) + { + return entityInfo.DbTableName; + //return entityInfo.DbTableName + "_FirstA"; + } + + public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType type) + { + return entityInfo.DbTableName;//目前模式少不需要分类(自带的有 日、周、月、季、年等进行区分) + } + /// + /// 根据分表字段进行,查找表名,如果当前年份 + /// + /// + /// + /// + /// + /// + public string GetTableName(ISqlSugarClient db, EntityInfo entityInfo, SplitType splitType, object fieldValue) + { + var dbLst = db.DbMaintenance.GetTableInfoList(); + var dt = (DateTime)fieldValue; + if (dbLst.Any(x => x.Name == $"{entityInfo.DbTableName}_{dt.Year}")) + return $"{entityInfo.DbTableName}_{dt.Year}";//根据值按首字母 + return entityInfo.DbTableName; + + } + } +}