monthly_odd.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import datetime
  2. from django import http
  3. from django.db import connection, transaction
  4. def monthly_odd(sql):
  5. """
  6. :param odd: 传过来的旧单号
  7. odd_old_right:旧单号右边三位数
  8. odd_old_left:旧单号左边8位数
  9. odd_new_right:新单号右边三位数
  10. odd_new:新单号
  11. :return:
  12. """
  13. # print(sql)
  14. t_today = datetime.datetime.now().strftime('%Y%m%d') # 获取年月日
  15. with connection.cursor() as cursor:
  16. try:
  17. odd = cursor.execute(sql, []).fetchall()[0][0]
  18. except Exception:
  19. return http.HttpResponseForbidden("获取单号sql语句有问题")
  20. if odd is None:
  21. # print('是空')
  22. odd = t_today + '000'
  23. odd_old = odd
  24. odd_old_right = int(odd_old[8::])+1 #右边三位
  25. odd_old_left = odd_old[0:8] #左边八位
  26. if len(str(odd_old_right))== 1:
  27. odd_new_right = '0'+'0'+str(odd_old_right) #新的右边三位
  28. odd_new = odd_old_left+odd_new_right
  29. return odd_new
  30. if len(str(odd_old_right))== 2:
  31. odd_new_right = '0'+str(odd_old_right)
  32. odd_new = odd_old_left+odd_new_right
  33. return odd_new
  34. if len(str(odd_old_right))== 3:
  35. odd_new_right = str(odd_old_right)
  36. odd_new = odd_old_left+odd_new_right
  37. return odd_new