import datetime from django import http from django.db import connection, transaction def monthly_odd(sql): """ :param odd: 传过来的旧单号 odd_old_right:旧单号右边三位数 odd_old_left:旧单号左边8位数 odd_new_right:新单号右边三位数 odd_new:新单号 :return: """ # print(sql) t_today = datetime.datetime.now().strftime('%Y%m%d') # 获取年月日 with connection.cursor() as cursor: try: odd = cursor.execute(sql, []).fetchall()[0][0] except Exception: return http.HttpResponseForbidden("获取单号sql语句有问题") if odd is None: # print('是空') odd = t_today + '000' odd_old = odd odd_old_right = int(odd_old[8::])+1 #右边三位 odd_old_left = odd_old[0:8] #左边八位 if len(str(odd_old_right))== 1: odd_new_right = '0'+'0'+str(odd_old_right) #新的右边三位 odd_new = odd_old_left+odd_new_right return odd_new if len(str(odd_old_right))== 2: odd_new_right = '0'+str(odd_old_right) odd_new = odd_old_left+odd_new_right return odd_new if len(str(odd_old_right))== 3: odd_new_right = str(odd_old_right) odd_new = odd_old_left+odd_new_right return odd_new