def add_book(self, user_id: str, store_id: str, book_id: str, book_json_str: str, stock_level: int): book_json = json.loads(book_json_str) # try: if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not store_id_exist(store_id): return error.error_non_exist_store_id(store_id) if book_id_exist(store_id, book_id): return error.error_exist_book_id(book_id) book_one = Book(book_id = book_id,title = book_json.get("title")) if not session.query(Book).filter(Book.book_id==book_id).first(): session.add(book_one) session.commit() store_detail_one=Store_detail( store_id = store_id, book_id = book_id, stock_level = stock_level, price = book_json.get("price") ) session.add(store_detail_one) session.commit() # except BaseException as e: # return 530, "{}".format(str(e)) return 200, "ok"
def create_store(self,user_id: str, store_id: str) -> (int, str): try: if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if store_id_exist(store_id): return error.error_exist_store_id(store_id) store_one = Store(user_id=user_id,store_id=store_id) session.add(store_one) session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def add_stock_level(self,user_id: str, store_id: str, book_id: str, add_stock_level: int): try: if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) if not store_id_exist(store_id): return error.error_non_exist_store_id(store_id) if not book_id_exist(store_id, book_id): return error.error_non_exist_book_id(book_id) cursor = session.query(Store_detail).filter( and_(Store_detail.store_id== store_id,Store_detail.book_id==book_id)).first() cursor.stock_level = cursor.stock_level+add_stock_level session.commit() except BaseException as e: return 530, "{}".format(str(e)) return 200, "ok"
def new_order(self, user_id: str, store_id: str, id_and_count: [(str, int)]) -> (int, str, str): order_id = "" if not user_id_exist(user_id): return error.error_non_exist_user_id(user_id) + (order_id, ) if not store_id_exist(store_id): return error.error_non_exist_store_id(store_id) + (order_id, ) uid = "{}_{}_{}".format(user_id, store_id, str(uuid.uuid1())) for book_id, count in id_and_count: store_detail = session.query(Store_detail).filter( Store_detail.store_id == store_id, Store_detail.book_id == book_id).first() if (store_detail is None): return error.error_non_exist_book_id(book_id) + (order_id, ) stock_level = store_detail.stock_level if stock_level < count: return error.error_stock_level_low(book_id) + (order_id, ) store_detail.stock_level -= count session.commit() session.add( Order_detail(order_id=uid, book_id=book_id, count=count)) session.commit() session.add( Order(order_id=uid, user_id=user_id, store_id=store_id, paytime=datetime.now(), status=3)) session.commit() order_id = uid return 200, "ok", order_id
def payment(self, user_id: str, password: str, order_id: str) -> (int, str): # Buyer_func.check_order(self, order_id) order = session.query(Order).filter(Order.order_id == order_id).first() if order is None: return error.error_invalid_order_id(order_id) if order.status == 4: return error.error_order_closed(order_id) elif order.status != 3: return error.error_order_has_been_paid(order_id) # order_id = order.order_id buyer_id = order.user_id store_id = order.store_id if buyer_id != user_id: return error.error_authorization_fail() buyer = session.query(User).filter(User.user_id == buyer_id).first() if buyer is None: return error.error_non_exist_user_id(buyer_id) balance = buyer.balance if (password != buyer.password): return error.error_authorization_fail() store = session.query(Store).filter(Store.store_id == store_id).first() if store is None: return error.error_non_exist_store_id(store_id) seller_id = store.user_id if not user_id_exist(seller_id): return error.error_non_exist_user_id(seller_id) order_detail = session.query(Order_detail).filter( Order_detail.order_id == order_id).all() total_price = 0 for i in range(0, len(order_detail)): book_id = order_detail[i].book_id book = session.query(Store_detail).filter( Store_detail.store_id == store_id, Store_detail.book_id == book_id).first() count = order_detail[i].count price = book.price total_price = total_price + price * count if balance < total_price: return error.error_not_sufficient_funds(order_id) buyer.balance -= total_price seller = session.query(User).filter(User.user_id == seller_id).first() seller.balance += total_price order.status = 0 # 设置付款状态:已支付 order.paytime = datetime.now() # ?????代码在干嘛? 删除订单? # cursor = conn.execute("DELETE FROM new_order WHERE order_id = ?", (order_id,)) # if cursor.rowcount == 0: # return error.error_invalid_order_id(order_id) # # cursor = conn.execute("DELETE FROM new_order_detail where order_id = ?", (order_id,)) # if cursor.rowcount == 0: # return error.error_invalid_order_id(order_id) session.commit() return 200, "ok"