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.
168 lines
5.9 KiB
C#
168 lines
5.9 KiB
C#
using SqlSugar;
|
|
using System.Data;
|
|
using System.Text;
|
|
|
|
namespace DS.Module.Core;
|
|
|
|
/// <summary>
|
|
/// 编码帮助类
|
|
/// </summary>
|
|
public class NumUtil
|
|
{
|
|
#region 自动生成日期编号
|
|
|
|
/// <summary>
|
|
/// 自动生成编号 201008251145409865
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string CreateNo()
|
|
{
|
|
Random random = new Random();
|
|
string strRandom = random.Next(1000, 10000).ToString(); //生成编号
|
|
string code = DateTime.Now.ToString("yyyyMMddHHmmss") + strRandom; //形如
|
|
return code;
|
|
}
|
|
|
|
#endregion 自动生成日期编号
|
|
|
|
#region 生成随机推荐码
|
|
|
|
/// <summary>
|
|
/// 生成随机数的种子
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private static int getNewSeed()
|
|
{
|
|
byte[] rndBytes = new byte[4];
|
|
System.Security.Cryptography.RNGCryptoServiceProvider rng =
|
|
new System.Security.Cryptography.RNGCryptoServiceProvider();
|
|
rng.GetBytes(rndBytes);
|
|
return BitConverter.ToInt32(rndBytes, 0);
|
|
}
|
|
|
|
#endregion 生成随机推荐码
|
|
|
|
/// <summary>
|
|
/// 生成随机数
|
|
/// </summary>
|
|
/// <param name="length">位数</param>
|
|
/// <returns></returns>
|
|
public static string GetRandomString(int len)
|
|
{
|
|
string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ";
|
|
string reValue = string.Empty;
|
|
Random rnd = new Random(getNewSeed());
|
|
while (reValue.Length < len)
|
|
{
|
|
string s1 = s[rnd.Next(0, s.Length)].ToString();
|
|
if (reValue.IndexOf(s1) == -1) reValue += s1;
|
|
}
|
|
|
|
return reValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 生成单据编号
|
|
/// </summary>
|
|
/// <param name="db">数据库db</param>
|
|
/// <param name="prefix">前缀</param>
|
|
/// <returns></returns>
|
|
public static string GetBillNo(ISqlSugarClient db, string prefix)
|
|
{
|
|
var ps_BillType = new SugarParameter("@ps_BillType", prefix);
|
|
var ps_OrgCode = new SugarParameter("@ps_OrgCode", "Comcab2d43f60454327af30a131fc1d3abd");
|
|
var ps_EmpCode = new SugarParameter("@ps_EmpCode", "1BEC90E1-9780-472F-90C2-0C6390C044A4");
|
|
var ps_Date = new SugarParameter("@ps_Date", DateTime.Now.AddDays(0).ToString("yyyy-MM-dd"));
|
|
var ps_bshead = new SugarParameter("@ps_bshead", "");
|
|
var ps_BillNo = new SugarParameter("@ps_BillNo", "");
|
|
var ps_RefBillNo = new SugarParameter("@ps_RefBillNo", "");
|
|
var dt = db.Ado.UseStoredProcedure().GetDataTable("sSysGetBillNo_new", ps_BillType, ps_OrgCode, ps_EmpCode,
|
|
ps_Date, ps_bshead, ps_BillNo, ps_RefBillNo) as DataTable; //返回dt
|
|
//Console.WriteLine(dt.Rows[0].ItemArray[0]);
|
|
// return dt?[0]["BillNo"].ToString();
|
|
return dt.Rows[0].ItemArray[0].ToString();
|
|
}
|
|
|
|
public static string GetNo()
|
|
{
|
|
string No = ""; //生成编号
|
|
DateTime now = DateTime.Now;
|
|
string Year = now.Year.ToString().Substring(2, 2);
|
|
string Month = now.Month < 10 ? "0" + now.Month : now.Month.ToString();
|
|
string Day = now.Day < 10 ? "0" + now.Day : now.Day.ToString();
|
|
string Hour = now.Hour < 10 ? "0" + now.Hour : now.Hour.ToString();
|
|
string Minute = now.Minute < 10 ? "0" + now.Minute : now.Minute.ToString();
|
|
string Second = now.Second < 10 ? "0" + now.Second : now.Second.ToString();
|
|
string Millisecond = now.Millisecond < 10 ? "0" + now.Millisecond : now.Millisecond.ToString();
|
|
Random rd = new Random();
|
|
int a = rd.Next(0, 9);
|
|
int b = rd.Next(0, 9);
|
|
int c = rd.Next(0, 9);
|
|
No = Year + Month + Day + Hour + Minute + Second + Millisecond + a + b + c;
|
|
return No;
|
|
}
|
|
|
|
public static string GetShortNo()
|
|
{
|
|
string No = ""; //生成编号
|
|
DateTime now = DateTime.Now;
|
|
string Year = now.Year.ToString().Substring(2, 2);
|
|
string Month = now.Month < 10 ? "0" + now.Month : now.Month.ToString();
|
|
string Day = now.Day < 10 ? "0" + now.Day : now.Day.ToString();
|
|
string Hour = now.Hour < 10 ? "0" + now.Hour : now.Hour.ToString();
|
|
string Minute = now.Minute < 10 ? "0" + now.Minute : now.Minute.ToString();
|
|
string Second = now.Second < 10 ? "0" + now.Second : now.Second.ToString();
|
|
string Millisecond = now.Millisecond < 10 ? "0" + now.Millisecond : now.Millisecond.ToString();
|
|
Random rd = new Random();
|
|
int a = rd.Next(0, 9);
|
|
int b = rd.Next(0, 9);
|
|
int c = rd.Next(0, 9);
|
|
No = Year + Month + Day + Hour + Minute + a + b + c;
|
|
return No;
|
|
}
|
|
|
|
public static string GenerateRandomCode(int length)
|
|
{
|
|
var result = new StringBuilder();
|
|
for (var i = 0; i < length; i++)
|
|
{
|
|
var r = new Random(Guid.NewGuid().GetHashCode());
|
|
result.Append(r.Next(0, 10));
|
|
}
|
|
|
|
return result.ToString();
|
|
}
|
|
|
|
public static Guid GenerateGuid()
|
|
{
|
|
byte[] guidArray = Guid.NewGuid().ToByteArray();
|
|
|
|
var baseDate = new DateTime(1900, 1, 1);
|
|
DateTime now = DateTime.Now;
|
|
var days = new TimeSpan(now.Ticks - baseDate.Ticks);
|
|
TimeSpan msecs = now.TimeOfDay;
|
|
|
|
byte[] daysArray = BitConverter.GetBytes(days.Days);
|
|
byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));
|
|
|
|
Array.Reverse(daysArray);
|
|
Array.Reverse(msecsArray);
|
|
|
|
Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
|
|
Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);
|
|
|
|
return new Guid(guidArray);
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
} |