You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
6.3 KiB

2 years ago
  1. import sqlite3 as sqlite
  2. import uuid
  3. import json
  4. import logging
  5. from be.model import db_conn
  6. from be.model import error
  7. class Buyer(db_conn.DBConn):
  8. def __init__(self):
  9. db_conn.DBConn.__init__(self)
  10. def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str):
  11. order_id = ""
  12. try:
  13. if not self.user_id_exist(user_id):
  14. return error.error_non_exist_user_id(user_id) + (order_id, )
  15. if not self.store_id_exist(store_id):
  16. return error.error_non_exist_store_id(store_id) + (order_id, )
  17. uid = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1()))
  18. for book_id, count in id_and_count:
  19. cursor = self.conn.execute(
  20. "SELECT book_id, stock_level, book_info FROM store "
  21. "WHERE store_id = ? AND book_id = ?;",
  22. (store_id, book_id))
  23. row = cursor.fetchone()
  24. if row is None:
  25. return error.error_non_exist_book_id(book_id) + (order_id, )
  26. stock_level = row[1]
  27. book_info = row[2]
  28. book_info_json = json.loads(book_info)
  29. price = book_info_json.get("price")
  30. if stock_level < count:
  31. return error.error_stock_level_low(book_id) + (order_id,)
  32. cursor = self.conn.execute(
  33. "UPDATE store set stock_level = stock_level - ? "
  34. "WHERE store_id = ? and book_id = ? and stock_level >= ?; ",
  35. (count, store_id, book_id, count))
  36. if cursor.rowcount == 0:
  37. return error.error_stock_level_low(book_id) + (order_id, )
  38. self.conn.execute(
  39. "INSERT INTO new_order_detail(order_id, book_id, count, price) "
  40. "VALUES(?, ?, ?, ?);",
  41. (uid, book_id, count, price))
  42. self.conn.execute(
  43. "INSERT INTO new_order(order_id, store_id, user_id) "
  44. "VALUES(?, ?, ?);",
  45. (uid, store_id, user_id))
  46. self.conn.commit()
  47. order_id = uid
  48. except sqlite.Error as e:
  49. logging.info("528, {}".format(str(e)))
  50. return 528, "{}".format(str(e)), ""
  51. except BaseException as e:
  52. logging.info("530, {}".format(str(e)))
  53. return 530, "{}".format(str(e)), ""
  54. return 200, "ok", order_id
  55. def payment(self, user_id: str, password: str, order_id: str) -> (int, str):
  56. conn = self.conn
  57. try:
  58. cursor = conn.execute("SELECT order_id, user_id, store_id FROM new_order WHERE order_id = ?", (order_id,))
  59. row = cursor.fetchone()
  60. if row is None:
  61. return error.error_invalid_order_id(order_id)
  62. order_id = row[0]
  63. buyer_id = row[1]
  64. store_id = row[2]
  65. if buyer_id != user_id:
  66. return error.error_authorization_fail()
  67. cursor = conn.execute("SELECT balance, password FROM user WHERE user_id = ?;", (buyer_id,))
  68. row = cursor.fetchone()
  69. if row is None:
  70. return error.error_non_exist_user_id(buyer_id)
  71. balance = row[0]
  72. if password != row[1]:
  73. return error.error_authorization_fail()
  74. cursor = conn.execute("SELECT store_id, user_id FROM user_store WHERE store_id = ?;", (store_id,))
  75. row = cursor.fetchone()
  76. if row is None:
  77. return error.error_non_exist_store_id(store_id)
  78. seller_id = row[1]
  79. if not self.user_id_exist(seller_id):
  80. return error.error_non_exist_user_id(seller_id)
  81. cursor = conn.execute("SELECT book_id, count, price FROM new_order_detail WHERE order_id = ?;", (order_id,))
  82. total_price = 0
  83. for row in cursor:
  84. count = row[1]
  85. price = row[2]
  86. total_price = total_price + price * count
  87. if balance < total_price:
  88. return error.error_not_sufficient_funds(order_id)
  89. cursor = conn.execute("UPDATE user set balance = balance - ?"
  90. "WHERE user_id = ? AND balance >= ?",
  91. (total_price, buyer_id, total_price))
  92. if cursor.rowcount == 0:
  93. return error.error_not_sufficient_funds(order_id)
  94. cursor = conn.execute("UPDATE user set balance = balance + ?"
  95. "WHERE user_id = ?",
  96. (total_price, buyer_id))
  97. if cursor.rowcount == 0:
  98. return error.error_non_exist_user_id(buyer_id)
  99. cursor = conn.execute("DELETE FROM new_order WHERE order_id = ?", (order_id, ))
  100. if cursor.rowcount == 0:
  101. return error.error_invalid_order_id(order_id)
  102. cursor = conn.execute("DELETE FROM new_order_detail where order_id = ?", (order_id, ))
  103. if cursor.rowcount == 0:
  104. return error.error_invalid_order_id(order_id)
  105. conn.commit()
  106. except sqlite.Error as e:
  107. return 528, "{}".format(str(e))
  108. except BaseException as e:
  109. return 530, "{}".format(str(e))
  110. return 200, "ok"
  111. def add_funds(self, user_id, password, add_value) -> (int, str):
  112. try:
  113. cursor = self.conn.execute("SELECT password from user where user_id=?", (user_id,))
  114. row = cursor.fetchone()
  115. if row is None:
  116. return error.error_authorization_fail()
  117. if row[0] != password:
  118. return error.error_authorization_fail()
  119. cursor = self.conn.execute(
  120. "UPDATE user SET balance = balance + ? WHERE user_id = ?",
  121. (add_value, user_id))
  122. if cursor.rowcount == 0:
  123. return error.error_non_exist_user_id(user_id)
  124. self.conn.commit()
  125. except sqlite.Error as e:
  126. return 528, "{}".format(str(e))
  127. except BaseException as e:
  128. return 530, "{}".format(str(e))
  129. return 200, "ok"