def book_upload_by_excel(): """ 上传包含书目库存信息的excel @api {POST} /book/upload_by_excel 上传书目的 excel @apiGroup book @apiVersion 0.0.1 @apiDescription 上传书目的 excel @apiParam {int} isbn @apiSuccess(200) {String} msg 信息 """ user_id = current_user.id if user_id: book_file = request.files.get('file', None) if book_file is None or not allowed_file( secure_filename(book_file.filename)): return make_api_response(message='文件不存在或类型有误', statusCode=400) # TODO # 遍历excel 存入db book_service = BookService() rv = book_service.add_books_by_excel(user_id, book_file) if rv: return make_api_response() return make_api_response(message='操作失败', statusCode=400) return make_api_response(message='用户不存在', statusCode=400)
def book_add(): """ 更新书目信息 """ isbn = request.json['isbn'] name = request.json['name'] author = request.json['author'] press = request.json['press'] quantity = request.json['quantity'] price = request.json['price'] description = request.json['description'] book_info = { 'isbn': isbn, 'name': name, 'author': author, 'press': press, 'quantity': quantity, 'price': price, 'description': description, 'is_active': 1 } book_service = BookService() rv = book_service.book_add(book_info) if rv: return make_api_response() else: return make_api_response(message='更新失败', statusCode=400)
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
def book_remove(book_id): """ 根据 id 删除书目信息 """ if book_id: book_sevice = BookService() rs = book_sevice.book_remove(book_id) if rs: return make_api_response() else: return make_api_response('书目id不存在') return make_api_response(message='缺少书目id')
def query_book_by_name(name): """ 根据 name 查询书目信息 """ if name: book_service = BookService() rv = book_service.book_query_by_name(name) if rv: return make_api_response(payload=rv, message='ok', statusCode=200) return make_api_response(message='没有对应书目', statusCode=500) return make_api_response(message='缺少isbn')
def query_book_by_name(name): """ @api {GET} /book/query_book_by_name/<name> 根据 书名 查询书目信息 @apiGroup book @apiVersion 0.0.1 @apiDescription 根据 书名 查询书目信息 @apiParam {string} name 书名 @apiSuccess (200) {String} msg 信息 @apiSuccess (200) {int} code 0 代表无错误 1代表有错误 @apiSuccessExample {json} 返回样例: { "status": "ok", "payload": { "1": { "name": "论语", "press": "北京教育出版社", "id": 1, "description": null, "quantity": 100, "price": 0.0, "author": "周杰伦", "isbn": 9203204223, "supplier": "天人1", "discount": 0.5 } }, "message": "ok" } @apiError (400) {String} msg 信息 @apiErrorExample {json} 返回样例: {"status": "fail", "message": "缺少isbn"} """ if name: book_service = BookService() rv = book_service.book_query_by_name(name) # 去除自己的书目 if current_user.id: for k, v in rv.copy().items(): if str(current_user.id) == str(v.get('supplier_id')): rv.pop(k) return make_api_response(payload=rv, message='ok', statusCode=200) return make_api_response(message='缺少isbn')
def book_query_by_supplier_id(supplier_id): """ @api {GET} /book/query_by_supplier_id/<supplier_id> 根据用户id查询该书库中可提供的全部书目 @apiGroup book @apiVersion 0.0.1 @apiDescription 当前用户书库中可提供的全部书目 @apiParam {int} isbn @apiSuccess (200) {String} msg 信息 @apiSuccess (200) {int} code 0 代表无错误 1代表有错误 @apiSuccessExample {json} 返回样例: { "status": "ok", "payload": { "1": { "name": "论语", "press": "北京教育出版社", "id": 1, "description": null, "quantity": 100, "price": 0.0, "author": "周杰伦", "isbn": 9203204223, "supplier_id": 12, "supplier": "天人1", "discount": 0.4 } }, "message": "ok" } @apiError (400) {String} msg 信息 @apiErrorExample {json} 返回样例: {"status": "fail", "message": "缺少isbn"} """ user_id = current_user.id if user_id: book_service = BookService() rv = book_service.books_query_by_supplier(supplier_id) return make_api_response(payload=rv, message='ok', statusCode=200) return make_api_response(message='用户不存在', statusCode=400)
def order_create(user_id, address_id): """ 新建订单 """ account_info = AccountService.account_query(user_id) cart = CartService() total_info = cart.cart_total(user_id) if account_info['balance'] < float(total_info['actual_cost']): return False, "余额不足" # 查询购物车 books = cart.cart_info_query(user_id) if not books: return False, "购物车为空" # order 表 order = Order() order_id = OrderService.generate_order_id() order.order_id = order_id order.user_id = user_id order.quantity = total_info['total_quantity'] order.origin_cost = total_info['origin_cost'] order.actual_cost = total_info['actual_cost'] order.order_status = 1 order.delivery_status = 0 order.pay_status = 1 db.session.add(order) db.session.flush() for book in books.values(): # order_detail 表 order_detail = OrderDetail() order_detail.order_id = order_id order_detail.book_id = book['book_id'] order_detail.book_name = book['book_name'] order_detail.isbn = book['isbn'] order_detail.origin_price = book['origin_price'] * \ book['order_quantity'] order_detail.actual_price = book['actual_price'] * \ book['order_quantity'] order_detail.supplier_id = book['supplier_id'] order_detail.warehouse = book['supplier'] order_detail.discount = book['discount'] order_detail.order_quantity = book['order_quantity'] order_detail.deliveried_quantity = 0 db.session.add(order_detail) db.session.flush() # book 表修改库存书 book_service = BookService() rowcount = book_service.book_quantity_update( book['book_id'], -book['order_quantity']) if rowcount is False: db.session.rollback() return False, "book_id : %s 库存不足,下单失败" % book['book_id'] # order_info 表 if not address_id: return False, "缺少收货地址id" address_service = AddressInfoService() address = address_service.address_query_by_id(user_id, address_id) order_info = OrderInfo() order_info.order_id = order_id order_info.consignee = address.name order_info.address = address.address order_info.post_code = address.post_code order_info.phone = address.phone db.session.add(order_info) db.session.flush() # account 表 account_service = AccountService() change = -total_info['actual_cost'] account_service.account_change(user_id, change) # account_consume 表 balance = account_info['balance'] - float(total_info['actual_cost']) account_service.account_consume_add(user_id, total_info['actual_cost'], balance) # 清空购物车 cart.cart_remove_all(user_id) return True, '操作成功'