在基础集成平台中加入编码生成方案,用户可以通过参数设置来决定该单据是否需要自动生成编码,编码流程如下:
编号生成算法如下:
/// <summary>
/// 生成下一级编码
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static String genNextCode(String code)
{
Encoding unicode = Encoding.GetEncoding("Unicode");
byte[] bytes = unicode.GetBytes(code);
int i = bytes.Length - 2;
bytes = caculate(bytes, i);
return unicode.GetString(bytes);
}
/// <summary>计算进位</summary>
/// <remarks>
/// 单纯进位:
/// 数字进位:1...9,10,11...99,100,101...999...
/// 字母进位:A...Z,AA,AB...AZ,BA,BB,....BZ,...CZ...DC...ZZ,AAA,AAB...ZZZ
/// 混合进位:
/// 基本原理按照以上数字进位
/// A1...A9,B0,B1...B9...Z9...AA0..AA1..
/// </remarks>
/// <param name="bytes"></param>
/// <param name="i"></param>
private static byte[] caculate(byte[] bytes, int i)
{
//0->48;9->57;a->97;z->122;A->65;Z->90
switch (bytes[i])
{
case 57:
if (i == 0)
{
//增一位
bytes = new byte[bytes.Length + 2];
//第一位为1
bytes[0] = 49;
//其余为0
for (int n = 2; n < bytes.Length; n += 2)
{
bytes[n] = 48;
}
return bytes;
}
else
{
//设为0,上移一位
bytes[i] = 48;
return caculate(bytes, i - 2);
};
case 122:
if (i == 0)
{
//增一位
bytes = new byte[bytes.Length + 2];
//全部起始为A
for (int n = 0; n < bytes.Length; n += 2)
{
bytes[n] = 97;
}
return bytes;
}
else
{
//设为A,上移一位
bytes[i] = 97;
return caculate(bytes, i - 2);
};
case 90:
if (i == 0)
{
//增一位
//增一位
bytes = new byte[bytes.Length + 2];
//全部起始为a
for (int n = 0; n < bytes.Length; n += 2)
{
bytes[n] = 65;
}
return bytes;
}
else
{
//设为A,上移一位
bytes[i] = 65;
return caculate(bytes, i - 2);
};
default:
bytes[i] += 1;
return bytes;
}
}