最近有个韩国客户项目,要求真奇特.4位流水号规则:0001~9999,之后到A001~A999,B001~B999,以此类推一直到ZZZZ.不更改数据库内顺延流水号.
实现代码如下:
1.先生成A001~ZZZZ到数组集合:
/// /// 生成A~ZZZZ /// /// private static string[] Get_str() { string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; string[] BASE_CHAR = new string[475273]; int s01 = 0;//记录第一位字母 int s02 = 0;//记录第二位字母 int s03 = 0;//记录第三位字母 int s04 = 0;//记录第四位字母 try { for (int i = 0; i <= 475273; i++) { if (i < 36) { BASE_CHAR[i] = str[i].ToString(); } else if (i < 712) { if (s02 > 25) { s01++; s02 = 0; } BASE_CHAR[i] = str1[s01].ToString() + str1[s02].ToString(); s02++; if (i == 711) { s01 = 0; s02 = 0; } } else if (i < 18288) { if (s03 > 25) { s02++; s03 = 0; } if (s02 > 25) { s01++; s02 = 0; } BASE_CHAR[i] = str1[s01].ToString() + str1[s02].ToString() + str1[s03].ToString(); if (i == 18287) { s01 = 0; s02 = 0; s03 = 0; } s03++; } else { if (s04 > 25) { s03++; s04 = 0; } if (s03 > 25) { s02++; s03 = 0; } if (s02 > 25) { s01++; s02 = 0; } if (s01 > 25) { break; } BASE_CHAR[i] = str1[s01].ToString() + str1[s02].ToString() + str1[s03].ToString() + str1[s04].ToString(); s04++; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } return BASE_CHAR; }
/// /// 返回流水号 /// /// /// private string Get_Number(int _ls) { string _val = ""; string _d = ""; string _v = "";//后面流水号 if (_ls.ToString().Length == 5) { _d = _ls.ToString().Substring(0, 2); //取后3位流水号 _v = _ls.ToString().Substring((_ls.ToString().Length - 3), 3); } else if (_ls.ToString().Length == 6) { _d = _ls.ToString().Substring(0, 3); //取后2位流水号 _v = _ls.ToString().Substring((_ls.ToString().Length - 4), 2); } else { _d = _ls.ToString().Substring(0, (_ls.ToString().Length - 1)); //取后1位流水号 _v = _ls.ToString().Substring((_ls.ToString().Length - 1), 1); } //0 进 1 if (int.Parse(_v) == 0) { if (_v.Length == 3) _v = "001"; else if (_v.Length == 2) _v = "01"; else _v = "1"; LS_number++; LS_number++; } int z = int.Parse(_d); if (z > 475237) { MessageBox.Show("流水号已经超出 475237"); } else { _val = str_36[z] + _v; } return _val; }
/// /// 返回进制 /// /// 流水号 /// private string Get_jz(int _ls) { string _val = ""; try { if (_ls.ToString().Length <= 4) { _val = _ls.ToString().PadLeft(4, '0'); return _val; } _val = Get_Number(_ls); } catch (Exception ex) { MessageBox.Show("流水号错误:" + ex.Message); } return _val; }