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.

82 lines
2.0 KiB
C#

12 months ago
using Snowflake.Core;
namespace DS.Module.Core;
/// <summary>
/// Guid工具类
/// </summary>
public class GuidHelper
{
/// <summary>
/// 生成50位有序Guid
/// </summary>
/// <returns></returns>
public static string GetSequentialGuid()
{
string guid = Guid.NewGuid().ToString();
var timeStr = (DateTime.Now.Ticks / 10000).ToString("x8");
var newGuid = $"{timeStr.PadLeft(13, '0')}-{guid}";
return newGuid;
}
/// <summary>
/// 由连字符分隔的32位数字
/// </summary>
/// <returns></returns>
private static string GetGuid()
{
System.Guid guid = new Guid();
guid = Guid.NewGuid();
return guid.ToString();
}
/// <summary>
/// 根据GUID获取16位的唯一字符串
/// </summary>
/// <param name=\"guid\"></param>
/// <returns></returns>
public static string GuidTo16String()
{
long i = 1;
foreach (byte b in Guid.NewGuid().ToByteArray())
i *= ((int)b + 1);
return string.Format("{0:x}", i - DateTime.Now.Ticks);
}
/// <summary>
/// 根据GUID获取19位的唯一数字序列
/// </summary>
/// <returns></returns>
public static long GuidToLongID()
{
byte[] buffer = Guid.NewGuid().ToByteArray();
return BitConverter.ToInt64(buffer, 0);
}
public static string GetSnowflakeId()
{
var worker = new IdWorker(1, 1);
long id = worker.NextId();
return id.ToString();
}
/// <summary>
/// 获取一个大写的字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string upper(string str)
{
return str.ToUpper();
}
/// <summary>
/// 获取32位不包含“-”号的GUID字符串
/// </summary>
/// <returns></returns>
public static string NewGuidFormatN(bool isUpper = false)
{
var guid = Guid.NewGuid().ToString("N");
return isUpper == true ? upper(guid) : guid;
}
12 months ago
}