def cart_info_query(self, user_id): """ 查询购物车内的书目信息 """ cart = {} if user_id: rows = db.session.query(ShoppingCart).filter_by( user_id=user_id).all() if rows: for row in rows: book = {} book['id'] = row.id book['user_id'] = row.user_id book['book_id'] = row.book_id book['book_name'] = row.book_name book['isbn'] = row.isbn book['supplier_id'] = row.supplier_id book['supplier'] = row.supplier book['origin_price'] = float(row.origin_price) book['actual_price'] = float(row.actual_price) book['total_price'] = float(row.total_price) book['discount'] = float(row.discount) book['order_quantity'] = row.order_quantity # 计算库存数 book_service = BookService() book_info = book_service.book_query_by_id(row.book_id) book['quantity'] = book_info['quantity'] cart[row.id] = book return cart return None
def cart_add(self, user_id, book_id, quantity): """ 购物车新增书目 """ if not user_id or not book_id or not quantity: return False # 购物车中书目不重复 rv = self.cart_query_by_id(user_id, book_id) if rv: return False book_service = BookService() book = book_service.book_query_by_id(book_id) price = book['price'] cart = ShoppingCart() cart.user_id = user_id cart.book_id = book_id cart.book_name = book['name'] cart.isbn = book['isbn'] cart.supplier_id = book['supplier_id'] cart.supplier = book['supplier'] cart.origin_price = price cart.order_quantity = quantity cart.discount = book['discount'] cart.actual_price = price * book['discount'] cart.total_price = price * quantity * book['discount'] db.session.add(cart) db.session.flush() db.session.commit() return False