53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
namespace WmsMobileServe.Utils;
|
||
|
||
/// <summary>
|
||
/// 唯一识别号工具
|
||
/// </summary>
|
||
public class UUIDUtils
|
||
{
|
||
|
||
const string sysId = "009"; // 系统的编号
|
||
|
||
#region 返回一个 UUID,以时间戳形式
|
||
|
||
private static readonly object getNewUUIDLock2 = new();
|
||
private static string lastUUID2 = string.Empty;
|
||
private static string lasTimeTick2 = DateTime.Now.ToString("yyyyMMddHHmmssfff");
|
||
private static ushort sortUUID2 = 0;
|
||
/// <summary>
|
||
/// 返回一个唯一识别号 以时间戳为基础
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
/// <remarks>
|
||
/// 这方法产生的ID会短一点,但是单位时间内产生的数量较少
|
||
/// </remarks>
|
||
public static string GetNewUUID2()
|
||
{
|
||
lock (getNewUUIDLock2)
|
||
{
|
||
while (true)
|
||
{
|
||
string timeTick = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
|
||
if (timeTick != lasTimeTick2)
|
||
{
|
||
lasTimeTick2 = timeTick;
|
||
sortUUID2 = 0;
|
||
}
|
||
string sort = sortUUID2.ToString().PadLeft(3, '0');
|
||
string newUUID = $"{timeTick}{sysId}{sort}";
|
||
sortUUID2++;
|
||
if (sortUUID2 > 900)
|
||
sortUUID2 = 0;
|
||
if (newUUID != lastUUID2)
|
||
{
|
||
lastUUID2 = newUUID;
|
||
return newUUID;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
}
|