1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading; 6 7 namespace Qxun.Framework.Utility 8 { 9 public class CreateOrderNo 10 { 11 /// <summary> 12 /// 订单格式:时间加随机数+线程ID 13 /// 因为像并发或者超快进入或者其他情况,导致订单号一致的时候,根据线程ID比较分得开一些 14 /// </summary> 15 /// <param name="count">随机数个数,默认3个</param> 16 /// <param name="timeFormat">默认时间精确到毫秒</param> 17 /// <returns>订单string</returns> 18 public static string DateTimeAndNumber(int count = 3, string timeFormat = "yyyyMMddHHmmssFFF") 19 { 20 string threadID= Thread.CurrentThread.ManagedThreadId.ToString().PadLeft(4,'0'); 21 string time = string.Format("{0:" + timeFormat + "}", DateTime.Now).PadRight(timeFormat.Length,'0'); 22 List<int> number = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; 23 string numberList = string.Join("", Collection.Random(number, count)); 24 return string.Format("{0}{1}{2}", time, numberList,threadID); 25 } 26 } 27 }
相关文章: