C#生成唯一订单号
今天系统出了一个问题,发现生成的订单号存在重复的情况了,这是要命的bug,不马上解决,就会有投诉了
经过改进后的代码我先简单的放一下,后面在慢慢的写清楚整个流程
string key = ""; string[] yCode = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" }; Random ran = new Random(); int randKey = ran.Next(0, 99); //获取微秒 ( 底层 api ) double l = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds; string unix = Convert.ToString((System.DateTime.Now.Ticks - TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks) / 1000); // key = yCode[DateTime.Now.Year - 2011] + Convert.ToString(DateTime.Now.Month, 16).ToUpper() + DateTime.Now.Day + (unix.Substring(unix.Length - 6)) + l.ToString().Substring(0, 5) + randKey.ToString().PadLeft(2, '0'); key = "ST" + Convert.ToString(DateTime.Now.Month, 16).ToUpper() + DateTime.Now.Day + (unix.Substring(unix.Length - 6)) + l.ToString().Substring(0, 5) + randKey.ToString().PadLeft(2, '0');