redis 定义序列号
规则
# 举例 AA20201102NN1234567X
# 规则 [产品号2位]+[生成日期8位]+[内部品类号2位]+[序列号7位数字]+[校验码]
# 校验码生成规则
# a = 产品号
# b = 生成日期
# c = 产品批次/内部品类
# d = 生成序列号
# m = ( hash( a+b+c+d ) + d ) / 26
# 校验码等于 m 转成字符,值域 A~Z 0~9
链接redis使用
class SerialNumber(object): _key = None # 自增ID key def __init__(self, product_code, batch): self.product_code = product_code self.batch = batch self.times = time.strftime("%Y%m%d", time.localtime(time.time())) self._key = f"{self.product_code}__{self.batch}__{self.times}" def get_serial_number(self): # 首先获取redis 自增ID redis_con = RedisCrease(self._key) incr_id = redis_con.get_crease_id() # 自增ID # 序列号 serial_num = self.product_code + self.times + self.batch + incr_id.zfill(7) # 加校验码 m = hash(serial_num) + int(incr_id) # 计算校验码 checks = divmod(m, 26)[1] if checks <= 9: check_str = str(checks) else: check_str = chr(checks + 55) return serial_num + check_str def check_serial_number(self, nums): # 校验序列号 m = hash(nums[:-1]) + int(nums[12:-1]) check = divmod(m, 26)[1] if check <= 9: checks = str(check) else: checks = str(check) return checks == nums[-1] # if "__main__" == __name__: # sno = get_serial_number("RG", "SK") # print(sno) # is_check = check_serial_number(sno) # print(is_check) # if "__main__" == __name__: # serial_obj = SerialNumber("RG", "SK") # sno = serial_obj.get_serial_number() # print(sno) # is_check = serial_obj.check_serial_number(sno) # print(is_check)