feat: migrate snowflake ids and refresh migrations

This commit is contained in:
2025-12-02 09:04:37 +08:00
parent 462e15abbb
commit 148475fa43
174 changed files with 8020 additions and 34278 deletions

View File

@@ -23,15 +23,15 @@ public abstract class AuditableEntityBase : EntityBase, IAuditableEntity
/// <summary>
/// 创建人用户标识,匿名或系统操作时为 null。
/// </summary>
public Guid? CreatedBy { get; set; }
public long? CreatedBy { get; set; }
/// <summary>
/// 最后更新人用户标识,匿名或系统操作时为 null。
/// </summary>
public Guid? UpdatedBy { get; set; }
public long? UpdatedBy { get; set; }
/// <summary>
/// 删除人用户标识(软删除),未删除时为 null。
/// </summary>
public Guid? DeletedBy { get; set; }
public long? DeletedBy { get; set; }
}

View File

@@ -8,5 +8,5 @@ public abstract class EntityBase
/// <summary>
/// 实体唯一标识。
/// </summary>
public Guid Id { get; set; }
public long Id { get; set; }
}

View File

@@ -23,15 +23,15 @@ public interface IAuditableEntity : ISoftDeleteEntity
/// <summary>
/// 创建人用户标识,匿名或系统操作时为 null。
/// </summary>
Guid? CreatedBy { get; set; }
long? CreatedBy { get; set; }
/// <summary>
/// 最后更新人用户标识,匿名或系统操作时为 null。
/// </summary>
Guid? UpdatedBy { get; set; }
long? UpdatedBy { get; set; }
/// <summary>
/// 删除人用户标识(软删除),未删除时为 null。
/// </summary>
Guid? DeletedBy { get; set; }
long? DeletedBy { get; set; }
}

View File

@@ -8,5 +8,5 @@ public interface IMultiTenantEntity
/// <summary>
/// 所属租户 ID。
/// </summary>
Guid TenantId { get; set; }
long TenantId { get; set; }
}

View File

@@ -8,5 +8,5 @@ public abstract class MultiTenantEntityBase : AuditableEntityBase, IMultiTenantE
/// <summary>
/// 所属租户 ID。
/// </summary>
public Guid TenantId { get; set; }
public long TenantId { get; set; }
}

View File

@@ -0,0 +1,13 @@
namespace TakeoutSaaS.Shared.Abstractions.Ids;
/// <summary>
/// 雪花 ID 生成器接口。
/// </summary>
public interface IIdGenerator
{
/// <summary>
/// 生成下一个唯一长整型 ID。
/// </summary>
/// <returns>雪花 ID。</returns>
long NextId();
}

View File

@@ -0,0 +1,26 @@
using System.ComponentModel.DataAnnotations;
namespace TakeoutSaaS.Shared.Abstractions.Ids;
/// <summary>
/// 雪花 ID 生成器配置。
/// </summary>
public sealed class IdGeneratorOptions
{
/// <summary>
/// 配置节名称。
/// </summary>
public const string SectionName = "IdGenerator";
/// <summary>
/// 工作节点标识0-31。
/// </summary>
[Range(0, 31)]
public int WorkerId { get; set; }
/// <summary>
/// 机房标识0-31。
/// </summary>
[Range(0, 31)]
public int DatacenterId { get; set; }
}

View File

@@ -99,6 +99,65 @@ public sealed record ApiResponse<T>
return TraceContext.TraceId;
}
return Activity.Current?.Id ?? Guid.NewGuid().ToString("N");
if (!string.IsNullOrWhiteSpace(TraceContext.TraceId))
{
return TraceContext.TraceId;
}
if (Activity.Current?.Id is { } id && !string.IsNullOrWhiteSpace(id))
{
return id;
}
return IdFallbackGenerator.Instance.NextId().ToString();
}
}
internal sealed class IdFallbackGenerator
{
private static readonly Lazy<IdFallbackGenerator> Lazy = new(() => new IdFallbackGenerator());
public static IdFallbackGenerator Instance => Lazy.Value;
private readonly object _sync = new();
private long _lastTimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
private long _sequence;
private IdFallbackGenerator()
{
}
public long NextId()
{
lock (_sync)
{
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
if (timestamp == _lastTimestamp)
{
_sequence = (_sequence + 1) & 4095;
if (_sequence == 0)
{
timestamp = WaitNextMillis(_lastTimestamp);
}
}
else
{
_sequence = 0;
}
_lastTimestamp = timestamp;
return ((timestamp - 1577836800000L) << 22) | _sequence;
}
}
private static long WaitNextMillis(long lastTimestamp)
{
var timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
while (timestamp <= lastTimestamp)
{
Thread.SpinWait(100);
timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
}
return timestamp;
}
}

View File

@@ -8,7 +8,7 @@ public interface ICurrentUserAccessor
/// <summary>
/// 当前用户 ID未登录时为 Guid.Empty。
/// </summary>
Guid UserId { get; }
long UserId { get; }
/// <summary>
/// 是否已登录。

View File

@@ -0,0 +1,52 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace TakeoutSaaS.Shared.Abstractions.Serialization;
/// <summary>
/// 将 long 类型的雪花 ID 以字符串形式序列化/反序列化,避免前端精度丢失。
/// </summary>
public sealed class SnowflakeIdJsonConverter : JsonConverter<long>
{
/// <inheritdoc />
public override long Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
JsonTokenType.Number => reader.GetInt64(),
JsonTokenType.String when long.TryParse(reader.GetString(), out var value) => value,
JsonTokenType.Null => 0,
_ => throw new JsonException("无法解析雪花 ID")
};
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, long value, JsonSerializerOptions options)
{
writer.WriteStringValue(value == 0 ? "0" : value.ToString());
}
}
/// <summary>
/// 可空雪花 ID 转换器。
/// </summary>
public sealed class NullableSnowflakeIdJsonConverter : JsonConverter<long?>
{
/// <inheritdoc />
public override long? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.TokenType switch
{
JsonTokenType.Number => reader.GetInt64(),
JsonTokenType.String when long.TryParse(reader.GetString(), out var value) => value,
JsonTokenType.Null => null,
_ => throw new JsonException("无法解析雪花 ID")
};
}
/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, long? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.HasValue ? value.Value.ToString() : null);
}
}

View File

@@ -8,5 +8,5 @@ public interface ITenantProvider
/// <summary>
/// 获取当前租户 ID未解析时返回 Guid.Empty。
/// </summary>
Guid GetCurrentTenantId();
long GetCurrentTenantId();
}

View File

@@ -8,7 +8,7 @@ public sealed class TenantContext
/// <summary>
/// 未解析到租户时的默认上下文。
/// </summary>
public static TenantContext Empty { get; } = new(Guid.Empty, null, "unresolved");
public static TenantContext Empty { get; } = new(0, null, "unresolved");
/// <summary>
/// 初始化租户上下文。
@@ -16,7 +16,7 @@ public sealed class TenantContext
/// <param name="tenantId">租户 ID</param>
/// <param name="tenantCode">租户编码(可选)</param>
/// <param name="source">解析来源</param>
public TenantContext(Guid tenantId, string? tenantCode, string source)
public TenantContext(long tenantId, string? tenantCode, string source)
{
TenantId = tenantId;
TenantCode = tenantCode;
@@ -26,7 +26,7 @@ public sealed class TenantContext
/// <summary>
/// 当前租户 ID未解析时为 Guid.Empty。
/// </summary>
public Guid TenantId { get; }
public long TenantId { get; }
/// <summary>
/// 当前租户编码(例如子域名或业务编码),可为空。
@@ -41,5 +41,5 @@ public sealed class TenantContext
/// <summary>
/// 是否已成功解析到租户。
/// </summary>
public bool IsResolved => TenantId != Guid.Empty;
public bool IsResolved => TenantId != 0;
}