后端生成id,后两位自增,若增到99则从0A开始自增,通过ASCII码值自增
/// <summary>
/// 生成类型:通过ASCII码值,对字符串自增1
/// </summary>
/// <param name="lastId"></param>
/// <returns></returns>
public static string CreateId(string lastId)
{
string _newId = "";
//截取后两位字符串
string lastTwo = lastId.Substring(lastId.Length-2,2);
//转化为ASCII码值
int vIntChar = (int)lastId[lastId.Length - 1];
int vIntCharLast = (int)lastId[lastId.Length - 2];
//如果后两位最大99,新增id将99替换为0A,从字母开始排
if (lastTwo == "99")
{
lastId=lastId.Remove(lastId.Length-2, 2);
_newId=lastId.Insert(lastId.Length, "0A");
}
//如果最后一位是字母(A - Z)
else if (65 <= vIntChar && vIntChar <= 90)
{
//取当前和下一位的ASCII码值转化为字符串
string oldChar =((char)vIntChar).ToString();
string newChar=((char)(vIntChar+1)).ToString();
//进行替换(A替换为B)
_newId = lastId.Replace(oldChar, newChar);
vIntChar++;
//如果最后一位超过字母Z
if (vIntChar == 91)
{
//将最后一位变为A,倒数第二位自增:(0Z增为1A)
vIntChar = 65;
vIntCharLast++;
lastId = lastId.Remove(lastId.Length - 2, 2);
_newId = lastId.Insert(lastId.Length, (((char)vIntCharLast).ToString()+(char)vIntChar).ToString());
}
}
else
{
//正常字符自增
string _newlastTwo = (long.Parse(lastTwo) + 1).ToString("D2");
lastId = lastId.Remove(lastId.Length - 2, 2);
_newId = lastId.Insert(lastId.Length, _newlastTwo);
}
return _newId;
}