sharding/samples/Sample.SqlServer/Shardings/SysUserSalaryVirtualTableRo...

91 lines
3.3 KiB
C#
Raw Normal View History

2021-09-03 16:00:12 +08:00
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
2023-03-22 11:09:27 +08:00
using Microsoft.Extensions.DependencyInjection;
2021-09-03 16:00:12 +08:00
using Sample.SqlServer.Domain.Entities;
2021-10-28 17:08:51 +08:00
using ShardingCore.Core.EntityMetadatas;
2023-03-22 11:09:27 +08:00
using ShardingCore.Core.RuntimeContexts;
2021-09-03 16:00:12 +08:00
using ShardingCore.Core.VirtualRoutes;
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
using ShardingCore.Sharding.EntityQueryConfigurations;
2021-09-03 16:00:12 +08:00
using ShardingCore.Sharding.PaginationConfigurations;
namespace Sample.SqlServer.Shardings
{
/*
* @Author: xjm
* @Description:
* @Date: Monday, 01 February 2021 15:54:55
* @Email: 326308290@qq.com
*/
public class SysUserSalaryVirtualTableRoute:AbstractShardingOperatorVirtualTableRoute<SysUserSalary,int>
2021-09-03 16:00:12 +08:00
{
public override string ShardingKeyToTail(object shardingKey)
{
var time = Convert.ToInt32(shardingKey);
2021-09-03 16:00:12 +08:00
return TimeFormatToTail(time);
}
public override List<string> GetTails()
2021-09-03 16:00:12 +08:00
{
2023-03-22 11:09:27 +08:00
var shardingRuntimeContext = RouteShardingProvider.ApplicationServiceProvider.GetRequiredService<IShardingRuntimeContext>();
2021-09-03 16:00:12 +08:00
var beginTime = new DateTime(2020, 1, 1);
var endTime = new DateTime(2021, 12, 1);
var list = new List<string>(24);
var tempTime = beginTime;
while (tempTime <= endTime)
{
list.Add($"{tempTime:yyyyMM}");
tempTime = tempTime.AddMonths(1);
}
return list;
}
2021-11-26 23:11:09 +08:00
public override void Configure(EntityMetadataTableBuilder<SysUserSalary> builder)
{
builder.ShardingProperty(o => o.DateOfMonth);
2021-11-26 23:11:09 +08:00
}
protected string TimeFormatToTail(int time)
2021-09-03 16:00:12 +08:00
{
var dateOfMonth=DateTime.ParseExact($"{time}","yyyyMM",System.Globalization.CultureInfo.InvariantCulture,System.Globalization.DateTimeStyles.AdjustToUniversal);
return $"{dateOfMonth:yyyyMM}";
}
public override Func<string, bool> GetRouteToFilter(int shardingKey, ShardingOperatorEnum shardingOperator)
2021-09-03 16:00:12 +08:00
{
var t = TimeFormatToTail(shardingKey);
switch (shardingOperator)
{
case ShardingOperatorEnum.GreaterThan:
case ShardingOperatorEnum.GreaterThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) >= 0;
case ShardingOperatorEnum.LessThan:
return tail => String.Compare(tail, t, StringComparison.Ordinal) < 0;
case ShardingOperatorEnum.LessThanOrEqual:
return tail => String.Compare(tail, t, StringComparison.Ordinal) <= 0;
case ShardingOperatorEnum.Equal: return tail => tail == t;
default:
{
#if DEBUG
Console.WriteLine($"shardingOperator is not equal scan all table tail");
#endif
return tail => true;
}
}
}
public override IPaginationConfiguration<SysUserSalary> CreatePaginationConfiguration()
{
return new SysUserSalaryPaginationConfiguration();
}
2021-10-28 17:08:51 +08:00
public override IEntityQueryConfiguration<SysUserSalary> CreateEntityQueryConfiguration()
{
return new SysUserSalaryEntityQueryConfiguration();
}
2021-09-03 16:00:12 +08:00
}
}