欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 生成唯一订单号

程序员文章站 2023-03-28 10:00:33
根据GUID+DateTime.Now.Ticks生产唯一订单号 ......

根据guid+datetime.now.ticks生产唯一订单号

 1     /// <summary>
 2     /// 生成唯一数
 3     /// </summary>
 4     public class uniquedata
 5     {
 6         private static object obj = new object();
 7         private static int guidint { get { return guid.newguid().gethashcode(); } }
 8         private static string guidintstr { get { return math.abs(guidint).tostring(); } }
 9 
10         /// <summary>
11         /// 生成
12         /// </summary>
13         /// <param name="mark">前缀</param>
14         /// <param name="timetype">时间精确类型  1 日,2 时,3 分,4 秒(默认) </param>
15         /// <param name="id">id 小于或等于0则随机生成id</param>
16         /// <returns></returns>
17         public static string gener(string mark, int timetype = 4, int id = 0)
18         {
19             lock (obj)
20             {
21                 var number = mark;
22                 var ticks = (datetime.now.ticks - guidint).tostring();
23                 int fillcount = 0;//填充位数
24 
25                 number += gettimestr(timetype, out fillcount);
26                 if (id > 0)
27                 {
28                     number += ticks.substring(ticks.length - (fillcount + 3)) + id.tostring().padleft(10, '0');
29                 }
30                 else
31                 {
32                     number += ticks.substring(ticks.length - (fillcount + 3)) + guidintstr.padleft(10, '0');
33                 }
34                 return number;
35             }
36         }
37 
38         /// <summary>
39         /// 生成
40         /// </summary>
41         /// <param name="mark">前缀</param>
42         /// <param name="timetype">时间精确类型  1 日,2 时,3 分,4 秒(默认)</param>
43         /// <param name="id">id 小于或等于0则随机生成id</param>
44         /// <returns></returns>
45         public static string generlong(string mark, int timetype = 4, long id = 0)
46         {
47             lock (obj)
48             {
49                 var number = mark;
50                 var ticks = (datetime.now.ticks - guidint).tostring();
51                 int fillcount = 0;//填充位数
52 
53                 number += gettimestr(timetype, out fillcount);
54                 if (id > 0)
55                 {
56                     number += ticks.substring(ticks.length - fillcount) + id.tostring().padleft(19, '0');
57                 }
58                 else
59                 {
60                     number += guidintstr.padleft(10, '0') + ticks.substring(ticks.length - (9 + fillcount));
61                 }
62                 return number;
63             }
64         }
65 
66         /// <summary>
67         /// 获取时间字符串
68         /// </summary>
69         /// <param name="timetype">时间精确类型  1 日,2 时,3 分,4 秒(默认)</param>
70         /// <param name="fillcount">填充位数</param>
71         /// <returns></returns>
72         private static string gettimestr(int timetype, out int fillcount)
73         {
74             var time = datetime.now;
75             if (timetype == 1)
76             {
77                 fillcount = 6;
78                 return time.tostring("yyyymmdd");
79             }
80             else if (timetype == 2)
81             {
82                 fillcount = 4;
83                 return time.tostring("yyyymmddhh");
84             }
85             else if (timetype == 3)
86             {
87                 fillcount = 2;
88                 return time.tostring("yyyymmddhhmm");
89             }
90             else
91             {
92                 fillcount = 0;
93                 return time.tostring("yyyymmddhhmmss");
94             }
95         }
96     }