| 
									
										
										
										
											2016-01-05 17:14:10 +08:00
										 |  |  |  | // *********************************************************************** | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  | // Assembly         : FairUtility | 
					
						
							|  |  |  |  | // Author           : Yubao Li | 
					
						
							|  |  |  |  | // Created          : 10-13-2015 | 
					
						
							|  |  |  |  | // | 
					
						
							|  |  |  |  | // Last Modified By : Yubao Li | 
					
						
							|  |  |  |  | // Last Modified On : 10-13-2015 | 
					
						
							|  |  |  |  | // *********************************************************************** | 
					
						
							|  |  |  |  | // <copyright file="GenerateId.cs" company=""> | 
					
						
							|  |  |  |  | //     Copyright (c) . All rights reserved. | 
					
						
							|  |  |  |  | // </copyright> | 
					
						
							|  |  |  |  | // <summary>创建唯一ID</summary> | 
					
						
							|  |  |  |  | // *********************************************************************** | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | using System; | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  | using System.Security.Cryptography; | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Infrastructure | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public class GenerateId | 
					
						
							|  |  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |         public static string GetGuidHash() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             return Guid.NewGuid().ToString().GetHashCode().ToString("x"); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 生成一个长整型,可以转成19字节长的字符串 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <returns>System.Int64.</returns> | 
					
						
							|  |  |  |  |         public static long GenerateLong() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             byte[] buffer = Guid.NewGuid().ToByteArray(); | 
					
						
							|  |  |  |  |             return BitConverter.ToInt64(buffer, 0); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 生成16个字节长度的数据与英文组合串 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         public static string GenerateStr() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             long i = 1; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             foreach (byte b in Guid.NewGuid().ToByteArray()) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 i *= ((int)b + 1); | 
					
						
							|  |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |             return String.Format("{0:x}", i - DateTime.Now.Ticks); | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 创建11位的英文与数字组合 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <returns>System.String.</returns> | 
					
						
							|  |  |  |  |         public static string ShortStr() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             return Convert(GenerateLong()); | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 唯一订单号生成 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <returns></returns> | 
					
						
							|  |  |  |  |         public static string GenerateOrderNumber() | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             string strDateTimeNumber = DateTime.Now.ToString("yyyyMMddHHmmssffff"); | 
					
						
							|  |  |  |  |             string strRandomResult = NextRandom(1000, 1).ToString("0000"); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |             return strDateTimeNumber + strRandomResult; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |         #region private | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 参考:msdn上的RNGCryptoServiceProvider例子 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <param name="numSeeds"></param> | 
					
						
							|  |  |  |  |         /// <param name="length"></param> | 
					
						
							|  |  |  |  |         /// <returns></returns> | 
					
						
							|  |  |  |  |         private static int NextRandom(int numSeeds, int length) | 
					
						
							|  |  |  |  |         { | 
					
						
							|  |  |  |  |             // Create a byte array to hold the random value. | 
					
						
							|  |  |  |  |             byte[] randomNumber = new byte[length]; | 
					
						
							|  |  |  |  |             // Create a new instance of the RNGCryptoServiceProvider. | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |             RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |             // Fill the array with a random value. | 
					
						
							|  |  |  |  |             rng.GetBytes(randomNumber); | 
					
						
							|  |  |  |  |             // Convert the byte to an uint value to make the modulus operation easier. | 
					
						
							|  |  |  |  |             uint randomResult = 0x0; | 
					
						
							|  |  |  |  |             for (int i = 0; i < length; i++) | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |                 randomResult |= ((uint) randomNumber[i] << ((length - 1 - i)*8)); | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |             } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |             return (int) (randomResult%numSeeds) + 1; | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         static string Seq = "s9LFkgy5RovixI1aOf8UhdY3r4DMplQZJXPqebE0WSjBn7wVzmN2Gc6THCAKut"; | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |         /// <summary> | 
					
						
							|  |  |  |  |         /// 10进制转换为62进制 | 
					
						
							|  |  |  |  |         /// </summary> | 
					
						
							|  |  |  |  |         /// <param name="id"></param> | 
					
						
							|  |  |  |  |         /// <returns></returns> | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |         private static string Convert(long id) | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |         { | 
					
						
							|  |  |  |  |             if (id < 62) | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |                 return Seq[(int) id].ToString(); | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |             int y = (int) (id%62); | 
					
						
							|  |  |  |  |             long x = (long) (id/62); | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |             return Convert(x) + Seq[y]; | 
					
						
							|  |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 11:28:38 +08:00
										 |  |  |  |         #endregion | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |       | 
					
						
							| 
									
										
										
										
											2015-10-26 21:58:12 +08:00
										 |  |  |  |     } | 
					
						
							|  |  |  |  | } |