当代数据库管理系统课程实验二
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

132 satır
6.8 KiB

  1. from sqlalchemy.exc import SQLAlchemyError
  2. from be.model import error
  3. from be.model import postgreSQLORM
  4. from be.model.postgreSQLORM import New_Order
  5. from be.model import db_conn
  6. from sqlalchemy import and_
  7. import json
  8. class Seller(db_conn.DBConn):
  9. def __init__(self):
  10. db_conn.DBConn.__init__(self)
  11. def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int):
  12. try:
  13. if not self.user_id_exist(user_id):
  14. return error.error_non_exist_user_id(user_id)
  15. if not self.store_id_exist(store_id):
  16. return error.error_non_exist_store_id(store_id)
  17. if self.book_id_exist(store_id, book_id):
  18. return error.error_exist_book_id(book_id)
  19. # self.session.query(postgreSQLORM.Store_Book).
  20. new_book = postgreSQLORM.Store_Book(store_id=store_id,book_id=book_id,book_info=book_json_str,stock_level=stock_level)
  21. self.session.add(new_book)
  22. book = json.loads(book_json_str)
  23. thelist = [] # 由于没有列表类型,故使用将列表转为text的办法
  24. for tag in book.get('tags'):
  25. if tag.strip() != "":
  26. # book.tags.append(tag)
  27. thelist.append(tag)
  28. book['tags'] = str(thelist) # 解析成list请使用eval(
  29. if book.get('picture') is not None:
  30. new_Book = postgreSQLORM.Book(book_id=book['id'], title=book['title'],
  31. author=book['author'],publisher=book['publisher'],
  32. original_title=book['original_title'],translator=book['translator'],
  33. pub_year=book['pub_year'],pages=book['pages'],
  34. original_price=book['price'],currency_unit=book['currency_unit'],
  35. binding=book['binding'],isbn=book['isbn'],
  36. author_intro=book['author_intro'],book_intro=book['book_intro'],
  37. content=book['content'],tags=book['tags'],
  38. picture=book['picture'])
  39. else:
  40. new_Book = postgreSQLORM.Book(book_id=book['id'], title=book['title'],
  41. author=book['author'],publisher=book['publisher'],
  42. original_title=book['original_title'],translator=book['translator'],
  43. pub_year=book['pub_year'],pages=book['pages'],
  44. original_price=book['price'],currency_unit=book['currency_unit'],
  45. binding=book['binding'],isbn=book['isbn'],
  46. author_intro=book['author_intro'],book_intro=book['book_intro'],
  47. content=book['content'],tags=book['tags'],
  48. picture=book['picture'])
  49. self.session.add(new_Book)
  50. # self.conn.execute("INSERT into store(store_id, book_id, book_info, stock_level)"
  51. # "VALUES (?, ?, ?, ?)", (store_id, book_id, book_json_str, stock_level))
  52. # self.conn.commit()
  53. self.session.query(postgreSQLORM.Store).filter_by(store_id=store_id).update({'stock_level':postgreSQLORM.Store.stock_level+1})
  54. self.session.commit()
  55. except SQLAlchemyError as e:
  56. return 528, "{}".format(str(e))
  57. except BaseException as e:
  58. return 530, "{}".format(str(e))
  59. return 200, "ok"
  60. def add_stock_level(self, user_id: str, store_id: str, book_id: str, add_stock_level: int):
  61. try:
  62. if not self.user_id_exist(user_id):
  63. return error.error_non_exist_user_id(user_id)
  64. if not self.store_id_exist(store_id):
  65. return error.error_non_exist_store_id(store_id)
  66. if not self.book_id_exist(store_id, book_id):
  67. return error.error_non_exist_book_id(book_id)
  68. self.session.query(postgreSQLORM.Store).filter_by(store_id=store_id).update({'stock_level':add_stock_level})
  69. self.session.query(postgreSQLORM.Store_Book).filter_by(store_id=store_id,book_id=book_id).update({'stock_level':add_stock_level})
  70. # self.conn.execute("UPDATE store SET stock_level = stock_level + ? "
  71. # "WHERE store_id = ? AND book_id = ?", (add_stock_level, store_id, book_id))
  72. # self.conn.commit()
  73. self.session.commit()
  74. except SQLAlchemyError as e:
  75. return 528, "{}".format(str(e))
  76. except BaseException as e:
  77. return 530, "{}".format(str(e))
  78. return 200, "ok"
  79. def create_store(self, user_id: str, store_id: str) -> (int, str):
  80. try:
  81. if not self.user_id_exist(user_id):
  82. return error.error_non_exist_user_id(user_id)
  83. if self.store_id_exist(store_id):
  84. return error.error_exist_store_id(store_id)
  85. # new_store = postgreSQLORM.Store(store_id=store_id,stock_level=0)
  86. new_user_store = postgreSQLORM.User_Store(user_id=user_id,store_id=store_id)
  87. # self.session.add(new_store)
  88. # self.session.commit()
  89. self.session.add(new_user_store)
  90. # self.conn.execute("INSERT into user_store(store_id, user_id)"
  91. # "VALUES (?, ?)", (store_id, user_id))
  92. # self.conn.commit()
  93. self.session.commit()
  94. # print('touch1')
  95. except SQLAlchemyError as e:
  96. # print('touch2')
  97. return 528, "{}".format(str(e))
  98. except BaseException as e:
  99. # print('touch3')
  100. return 530, "{}".format(str(e))
  101. return 200, "ok"
  102. def send_out(self, order_id:str, user_id:str):
  103. session = self.session
  104. try:
  105. if not self.user_id_exist(user_id):
  106. return error.error_non_exist_user_id(user_id)
  107. row = session.query(New_Order).filter(New_Order.order_id==order_id).first()
  108. if row is None:
  109. return error.error_invalid_order_id(order_id)
  110. if row.status != 1:
  111. return error.error_invalid_order_id(order_id)
  112. row = session.query(New_Order).filter(New_Order.order_id==order_id).update({'status':2})
  113. if row == 0:
  114. return error.error_invalid_order_id(order_id)
  115. session.commit()
  116. except SQLAlchemyError as e:
  117. return 528, "{}".format(str(e))
  118. except BaseException as e:
  119. # print('touch3')
  120. return 530, "{}".format(str(e))
  121. return 200, "ok"