现在的位置: 首页 > 综合 > 正文

一个交易号的生成和还原过程

2012年07月17日 ⁄ 综合 ⁄ 共 3823字 ⁄ 字号 评论关闭

需求是一个同学给的,我帮他做完,记录下。

需求如下:

业务变更内容及需求确认:

1.              原交易序号——>算法——>打乱交易序号

2.              打乱的交易序号和原交易序号一一对应,确保顾客可以根据打乱的交易序号退货

3.              收银小票只显示打乱的交易序号

4.              POS屏幕上方依旧显示正确的交易序号,便于收银员日常操作

系统变更内容说明:

在以前的pos小票中添加一个打乱后的时自动生成的字段,在顾客小票显示打乱后的序号,在退货时从表中查询原来的正确的交易序号进行退货交易。 

算法的实现: 移位算法。比如:一笔正常的交易序号为:1 2 3 4 5 6  对应顺序(a,b,c,d,e,f,g)现在按(b,a,d,c,f,e)再排序, 那么打乱之后的序号就变为:(2 1 4 3 6 5)

 

1、  设置系统参数是否打乱交易序号ConfuseTranNumber =yes;

2、  如果需要打乱,用程序把原交易序号打乱存放到内存变量A中,如果不需要打乱则把正确的交易序号存放到内存变量A里;

3、  打印时读取交易序号A;

4、  退货时,输入打乱后的交易序号用算法计算出正常的交易序号,把正常的交易序号内容取到界面上进行退货;

5、  算法解析:

Ø  销售时正算交易序号:

把传过来的交易序号打乱,如果交易序号长度不足8位前面用"0"补足,各位上之和与10求余,余数则有可能是0、1、2、3、4、5、6、7、8、9,针对不同的余数各位上有不同的排列方式:56204173、20417365、41736520、73652041、65412073、20736541、65204173、20417365、41736520、73652041

例如:交易序号为12345678,各位之和为36,和10求余为6,此时选择第六种打乱方式65204173,打乱后的交易序号为76315284。

Ø  退货时倒算交易序号:

把输入打乱后的退货交易序号,如果交易序号长度不足8位前面用"0"补足,各位上之和与10求余,余数则有可能是0、1、2、3、4、5、6、7、8、9,针对不同的余数各位上有不同的打乱方式:35274016、13052764、71630542、57416320、53472106、17036542、35274106、13052764、71630542、57416320

例如:退货交易序号为76315284,各位之和为36,和10求余为6,此时选择第六种打乱方式35274106,正常的交易序号为12345678。

 

直接上代码,注释一切都有:

 

代码

1 # -*- coding: gbk -*-
2  
3  '''交易号编码解码'''
4  #销售号排列方式
5 saleOrder = ['56204173','20417365','41736520','73652041','65412073',\
6 '20736541','65204173','20417365','41736520','73652041']
7 #退货号排列方式
8 sendbackOrder = ['35274016','13052764','71630542','57416320','53472106',\
9 '17036542','35274106','13052764','71630542','57416320']
10 def addAll(tid):
11 '''计算号码数值总和'''
12 total = 0
13 for n in tid:
14 total += int(n)
15 return total
16
17 def selectOrder(total,flag):
18 '''选择排列方式
19 total : 数值总和
20 flag : sale 销售号打乱;sendback 退货号还原
21 '''
22 if flag == 'sale':
23 return saleOrder[total % 10]
24 else:
25 return sendbackOrder[total % 10]
26
27 def showResult(tid,flag):
28 '''返回解码or编码结果
29 tid : 销售号 or 退货号
30 flag : sale 销售号打乱;sendback 退货号还原
31 '''
32 order = selectOrder(addAll(tid),flag)
33 res = ''
34 for index in order:
35 res += tid[int(index)]
36 return res
37
38 def formatId(tid):
39 '''格式化去除空格换行制定8位长度'''
40 tid = tid.strip()
41 tid = tid[:8]
42 tid = tid.zfill(8)
43 #tid = '%08d' % tid
44 return tid
45
46
47 def main():
48 '''测试函数'''
49 while(True):
50 tid = raw_input('\n输入号码:\n')
51 flag = raw_input('输入号码类型:sale or sendback:\n')
52 tid = formatId(tid)
53 print showResult(tid,flag)
54 if __name__ == '__main__':
55 main()

 

 

另外用到几个函数:

strip( [chars])
Return a copy of the string with trailing characters removed.
返回一个去除多余字符的字符串的拷贝
The chars argument is a string specifying the set of characters to be removed.
chars参数是一个指定的要删除的字符集
If omitted or None, the chars argument defaults to removing whitespace.
如果政略或者是None,chars参数默认为删除空白字符
The chars argument is not a suffix; rather, all combinations of its values are stripped:
char参数不是一个后缀。实际上所有其值的组合,都会被删去。

 

lstrip( [chars])
Return a copy of the string with leading characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a prefix; rather, all combinations of its values are stripped:

rstrip( [chars])
Return a copy of the string with trailing characters removed. The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped:

ljust( width[, fillchar])
Return the string left justified in a string of length width. Padding is done using the specified fillchar (default is a space).
返回一个字符串长度为width的左边排版的字符串,填充使用指定的(具体的,明确的)参数fillchar(填充字符串),默认是空格
The original string is returned if width is less than len(s). Changed in version 2.4: Support for the fillchar argument.
如果wdith小于字符串长度,则返回原来的字符串。在版本2.4的改变:支持fillchar参数。

rjust( width[, fillchar])
Return the string right justified in a string of length width. Padding is done using the specified fillchar (default is a space).
The original string is returned if width is less than len(s). Changed in version 2.4: Support for the fillchar argument.

zfill( width)
Return the numeric string left filled with zeros in a string of length width.
返回一个左边用0填充的字符串长度是width的数字字符串
The original string is returned if width is less than len(s). New in version 2.2.2.
如果width小于字符串长度,则返回原字符串。版本2.2.2新增。

抱歉!评论已关闭.