MoneyRecord.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from flask import jsonify
  2. import TokenUtils
  3. from data import MySQLTool
  4. from returnTemp import get_template, get_error_message
  5. import time
  6. class MoneyRecord:
  7. def __init__(self, action, data, logger):
  8. self.action = action
  9. self.data = data
  10. self.logger = logger
  11. def money_record_router(self):
  12. if self.action == "insert":
  13. return self.insert()
  14. elif self.action == "check":
  15. return self.check()
  16. elif self.action == "delete":
  17. return self.deleteSomeItem()
  18. else:
  19. return self.test()
  20. def insert(self):
  21. print("---insert---")
  22. return insert(self.data)
  23. def check(self):
  24. print("---check----")
  25. return check(self.data)
  26. def deleteSomeItem(self):
  27. print("---deleteSomeItem----")
  28. return deleteSomeItem(self.data)
  29. def insert(data):
  30. # 检查参数是否存在
  31. required_fields = ['cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid']
  32. missing_fields = [field for field in required_fields if field not in data]
  33. if missing_fields:
  34. return get_template(2000, get_error_message(2000), {})
  35. token = data["token"]
  36. uid = data["uid"]
  37. mount = data["mount"]
  38. if token == "" or uid == "" or mount == "":
  39. # 三个值必须存在
  40. template = get_template(2000, get_error_message(2000), {})
  41. return template
  42. if not TokenUtils.verify_jwt_token(uid, token):
  43. return get_template(2001, get_error_message(2001), {})
  44. # 获取支付信息
  45. kind = data["kind"]
  46. mount = data["mount"]
  47. des = data["des"]
  48. paytime = data["paytime"]
  49. cash_type = data["cash_type"]
  50. location = data["location"]
  51. noteid = get_order_code()
  52. # 插入数据
  53. mysql_tool.connect()
  54. columns = ['cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid', 'noteid']
  55. values = [cash_type, des, kind, location, mount, paytime, uid, noteid]
  56. insert_result = mysql_tool.insert(table='cash', columns=columns, values=values)
  57. print(insert_result)
  58. mysql_tool.disconnect()
  59. return get_template(0, "记录成功", {})
  60. def deleteSomeItem(data):
  61. required_fields = ['token', 'uid', 'noteid']
  62. missing_fields = [field for field in required_fields if field not in data]
  63. if missing_fields:
  64. return get_template(2000, get_error_message(2000), {})
  65. token = data["token"]
  66. uid = data["uid"]
  67. noteid = data["noteid"]
  68. # 检查参数
  69. if token == "" or uid == "" or noteid == "":
  70. # 三个值必须存在
  71. template = get_template(2000, get_error_message(2000), {})
  72. return template
  73. if not TokenUtils.verify_jwt_token(uid, token):
  74. return get_template(2001, get_error_message(2001), {})
  75. mysql_tool.connect()
  76. result = mysql_tool.select(table='cash', columns='*', conditions={"uid": uid, "noteid": noteid})
  77. if len(result):
  78. delete_result = mysql_tool.delete(table='cash', conditions={"uid": uid, "noteid": noteid})
  79. if delete_result == 1:
  80. return get_template(0, get_error_message(0), {})
  81. else:
  82. return get_template(2002, get_error_message(2002), {})
  83. else:
  84. return get_template(1006, get_error_message(1006), {})
  85. def check(data):
  86. # 检查参数是否存在
  87. required_fields = ['token', 'uid', 'start', 'end', 'type']
  88. missing_fields = [field for field in required_fields if field not in data]
  89. if missing_fields:
  90. return get_template(2000, get_error_message(2000), {})
  91. token = data["token"]
  92. uid = data["uid"]
  93. start = data["start"]
  94. end = data["end"]
  95. kindtype = data["type"]
  96. if token == "" or uid == "" or start == "" or end == "" or kindtype == "":
  97. # 三个值必须存在
  98. template = get_template(2000, get_error_message(2000), {})
  99. return template
  100. if not TokenUtils.verify_jwt_token(uid, token):
  101. return get_template(2001, get_error_message(2001), {})
  102. # 组装查询参数
  103. mysql_tool.connect()
  104. conditions = {}
  105. if kindtype != 0:
  106. conditions['kind'] = kindtype
  107. conditions['uid'] = uid
  108. result = mysql_tool.select(table='cash', columns='*', conditions=conditions)
  109. final_result = []
  110. i_start = int(start)
  111. i_end = int(end)
  112. required_fields = ['id', 'cash_type', 'des', 'kind', 'location', 'mount', 'paytime', 'uid', 'noteid']
  113. for r in result:
  114. if i_start <= r[6] <= i_end:
  115. temp = {}
  116. for inx, val in enumerate(r):
  117. temp[required_fields[inx]] = val
  118. final_result.append(temp)
  119. mysql_tool.disconnect()
  120. return get_template(0, get_error_message(0), {"record": final_result})
  121. mysql_tool = MySQLTool(host='localhost', user='root', password='wyt615115@', database='apiserver')
  122. # 生成订单号
  123. def get_order_code():
  124. # 年月日时分秒+time.time()的后7位
  125. order_no = str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time())) + str(time.time()).replace('.', '')[-7:])
  126. return order_no