最近遇到一个比较奇葩的事情,自己所负责的一个系统出现一个流水号用完的问题;正常情况下,流水号用完应该增加多位来解决这个问题。鉴于各种因素,最后决定:位数不变,增加字母进去,当数字用完后,会出现字母,而且规则还有点绕!!
看到这样的规则后,不由感觉不是一般的绕啊,又由于项目是用ASP来做的,所以用Asp 语言实现。(呵呵,估计现在的都很少来做得了)
为了害怕自己忘记,赶紧将这规则的大体的算法记录下来.
算法过程:
<%
'max_dimension_no=right(max_dimension_no,len(max_dimension_no)-1)
max_dimension_no="B099"
max_dimension_no="Z999"
max_dimension_no="ZZ99"
max_dimension_no="ZZZ9"
max_dimension_no="9999"
'max_dimension_no="AB096"
'max_dimension_no="AZ999"
length_subcode=4
response.Write("---max_dimension_no:"&max_dimension_no &"<br>")
ii=1
'从9999到ZZZZ ---ii:269461----gen code max_dimension_no:ZZZZ
'while not max_dimension_no="ZZZZ"
for ii=1 to 269463
'length_subcode=len(right(max_dimension_no,len(max_dimension_no)-1))'可以根据此选项改变subcode的位数
response.Write("------------------------------<br>")
'全数字
if IsNumeric(max_dimension_no) then
if clng(max_dimension_no)<clng(gensubcode_maxvalue(length_subcode,9)) then
max_dimension_no=max_dimension_no+1
elseif clng(max_dimension_no)=clng(gensubcode_maxvalue(length_subcode,9)) then '临界点
max_dimension_no="A"+ gensubcode_maxvalue(clng(length_subcode-1),0)
end if
else
'字母和数字混合
'判断字母和数字分界点
dim char_part_ASCVaule
char_part_ASCVaule=""
for i=1 to len(max_dimension_no)
'response.Write("i:"&mid(max_dimension_no,i,1)&"<br>")
if not IsNumeric(mid(max_dimension_no,i,1)) then
'写入数组
'response.Write("ascii_value:"&asc(mid(max_dimension_no,i,1))&"<br>")
char_part_ASCVaule=char_part_ASCVaule&asc(mid(max_dimension_no,i,1))&"$"
char_location=i
'response.Write("char_part_ASCVaule:"& char_part_ASCVaule&"<br>")
end if
next
' response.Write("char_part_ASCVaule:"& char_part_ASCVaule &"<br>")
' response.Write("char_location:"& char_location &"<br>")
if char_location=length_subcode then
IsStopCheck=false
for p=length_subcode to 1 step-1
if clng(asc(mid(max_dimension_no,p,1)))>=90 then
IsStopCheck=true
else
IsStopCheck=false
end if
next
if IsStopCheck then
response.Write("this running number is use out!")
response.End()
end if
end if
'比较生成
'拆分数字部分和文字部分
num_part=mid(max_dimension_no,clng(char_location)+1,len(max_dimension_no))
char_part=mid(max_dimension_no,1,char_location)
' response.Write("char_location:"& char_location &"<br>")
' response.Write("char_part_ASCVaule:"& char_part_ASCVaule&"<br>")
' response.Write("num_part:"&num_part&" lenght_num_part:"&len(num_part)&"<br>")
'正常生数
if num_part<> "" then '字母与数字混合
if clng(num_part)<clng(gensubcode_maxvalue(len(num_part),9)) then
'response.Write("clng(num_part)+1"&clng(num_part)+1&"<br>")
'response.Write("len(num_part)"&len(num_part)&"<br>")
num_part=gensubcode_numpart(clng(num_part)+1,len(num_part),0)
max_dimension_no=char_part&num_part
'数字和字母的临界点
elseif clng(num_part)=clng(gensubcode_maxvalue(len(num_part),9)) and num_part<>"" then
num_part=gensubcode_numpart(clng(num_part)+1,len(num_part),0)
char_part_len=length_subcode-last_num_part_len
char_part=""
Arr_char_part_ASCVaule=split(left(char_part_ASCVaule,len(char_part_ASCVaule)-1),"$")
char_arry=clng(UBound(Arr_char_part_ASCVaule))
if Arr_char_part_ASCVaule(char_arry)<90 then
Arr_char_part_ASCVaule(char_arry)=Arr_char_part_ASCVaule(char_arry)+1
for k=0 to char_arry
char_part=char_part&chr(Arr_char_part_ASCVaule(k))
next
num_part=gensubcode_maxvalue(length_subcode-char_arry-1,0)'ps
max_dimension_no=char_part&num_part
elseif Arr_char_part_ASCVaule(char_arry)>=90 then
Arr_char_part_ASCVaule(char_arry)=65
if char_arry=0 then
char_part="A"& chr(Arr_char_part_ASCVaule(char_arry))
'num_part=gensubcode_maxvalue(length_subcode-char_arry-1,0)
max_dimension_no=char_part&num_part
elseif char_arry>0 then
' response.Write("UBound(Arr_char_part_ASCVaule):"&UBound(Arr_char_part_ASCVaule)&"<br>")
' response.Write("LBound(Arr_char_part_ASCVaule):"&LBound(Arr_char_part_ASCVaule)&"<br>")
Isaddmore_dital=false
for j=char_arry-1 to LBound(Arr_char_part_ASCVaule) step -1
if Arr_char_part_ASCVaule(j)<90 then
Arr_char_part_ASCVaule(j)=Arr_char_part_ASCVaule(j)+1
exit for
elseif Arr_char_part_ASCVaule(j)>=90 AND j=LBound(Arr_char_part_ASCVaule) then
'进位
Arr_char_part_ASCVaule(j)=65
Isaddmore_dital=true
elseif Arr_char_part_ASCVaule(j)>=90 and j<>LBound(Arr_char_part_ASCVaule) then
'不进位
Arr_char_part_ASCVaule(j)=65
'response.Write(j&"<br>")
'Arr_char_part_ASCVaule(j+1)=Arr_char_part_ASCVaule(j+1)+1
end if
next
for l=0 to char_arry
char_part=char_part&chr(Arr_char_part_ASCVaule(l))
next
num_part=gensubcode_maxvalue(length_subcode-char_arry-1,0)'ps
if Isaddmore_dital then
char_part="A"&char_part
num_part=gensubcode_maxvalue(length_subcode-len(char_part),0)'ps
end if
max_dimension_no=char_part&num_part
end if
end if
end if
else'全字母
if char_location=length_subcode then
Arr_char_part_ASCVaule=split(left(char_part_ASCVaule,len(char_part_ASCVaule)-1),"$")
for n=ubound(Arr_char_part_ASCVaule) to lbound(Arr_char_part_ASCVaule) step-1
if Arr_char_part_ASCVaule(n)<90 then
Arr_char_part_ASCVaule(n)=Arr_char_part_ASCVaule(n)+1
exit for
end if
next
char_part=""
for o=0 to ubound(Arr_char_part_ASCVaule)
char_part=char_part&chr(Arr_char_part_ASCVaule(o))
next
max_dimension_no=char_part
end if
end if
end if
response.Write("---ii:"&ii&"----gen code max_dimension_no:"&max_dimension_no&"<br>")
response.Write("------------------------------<br>")
next
'ii=ii+1
'WEND
function gensubcode_numpart(detailvalue,length,fillaga_value)
dim tempstr
tempstr=""
If len(detailvalue)<= clng(length) then
for i=1 to clng(length)-len(detailvalue)
tempstr=tempstr&fillaga_value
next
gensubcode_numpart=tempstr&detailvalue
elseif len(detailvalue)> clng(length) then
for i=1 to clng(length)-1
tempstr=tempstr&fillaga_value
next
gensubcode_numpart=tempstr
end if
end function
function gensubcode_maxvalue(Length,RoundValue)
dim tempstr
tempstr=""
if Length>=1 then
for i=1 to Length
tempstr=cstr(tempstr)&cstr(RoundValue)
next
gensubcode_maxvalue=tempstr
end if
end function
%>
以上实现的过程,注释和循环要忽略掉,因为方便自己自己调试和看结果的。