BillNoHelper.GenerateCouponKey();
public class BillNoHelper
{
/// <summary>
/// 防止创建类的实例
/// </summary>
private BillNoHelper() { }
private static readonly object locker = new object();
private static int _sn = 0;
/// <summary>
/// 生成单据编号
/// </summary>
/// <returns></returns>
public static string GenerateId()
{
lock (locker) //lock 关键字可确保当一个线程位于代码的临界区时,另一个线程不会进入该临界区。
{
if (_sn == 9999)
{
_sn = 1;
}
else
{
_sn++;
}
return DateTime.Now.ToString("yyMMddHHmmss") + _sn.ToString().PadLeft(4, '0');
}
}
/// <summary>
/// 唯一值
/// </summary>
/// <returns></returns>
public static string guid()
{
return Guid.NewGuid().ToString().Replace("-", "");
}
/// <summary>
/// GuidExtension
/// </summary>
/// <param name="guid"></param>
/// <returns></returns>
public static string IsNum(Guid guid)
{
var s = guid.ToString();
var sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
{
if (Char.IsNumber(s, i))
{
sb.Append(s.Substring(i, 1));
}
}
return sb.ToString();
}
#region 生成 Coupon Key
private static readonly object locker2 = new object();
private static int _sn2 = 0;
/// <summary>
/// 生成key 12位 字母+数字
/// </summary>
/// <returns></returns>
public static string GenerateCouponKey()//
{
string s;
lock (locker2)
{
if (_sn2 == 9999999)
{
_sn2 = 1;
}
else
{
_sn2++;
}
s = $"Qq@,.*745u787#$@,.><;'{DateTime.Now.Ticks}{_sn2}";
}
return MD5_16(s);
}
private static string MD5_16(string p_str)
{
System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] data = System.Text.Encoding.Default.GetBytes(p_str);
byte[] md5Data = md5.ComputeHash(data);
string strResult = System.BitConverter.ToString(md5Data, 4, 8);
md5.Clear();
strResult = strResult.Replace("-", "").ToUpper();
return strResult;
}
#endregion
}
|