def recharge(session: Session, account: AccountModel): """充值账户 :param session: db session :param account: 要充值的账户以及充值金额 :return: None """ old = (session.query(AccountModel).filter( AccountModel.mobile == account.mobile).first()) old.surplus = old.surplus + account.surplus session.commit()
def cancel(session: Session, order: OrderModel): """ 撤销订单 :param session: db session :param order: 订单 :return: """ exist_order = (session.query(OrderModel).filter( OrderModel.order_id == order.order_id).first()) exist_order.status = 2 # 已取消 session.commit()
def create(session: Session, order: OrderModel, order_details: []): """ 创建订单 :param session: db session :param order: 订单 :param order_details: 订单详情 :return: """ session.add(order) session.add_all(order_details) # 批量插入 session.commit()
def add(session: Session, cart: CartModel): """ 添加到购物车,如果有了的话只需要增加数量 :param session: db session :param cart: 要增加的用户和图书 :return: """ exist = ( session.query(CartModel) .filter(CartModel.mobile == cart.mobile) .filter(CartModel.book_id == cart.book_id) .first() ) if exist is not None: exist.count += cart.count else: exist = cart session.commit()
def pay(session: Session, order: OrderModel): """ 支付订单 :param session: db session :param order: 订单 :return: """ # 实际上这里需要大量的校验,校验逻辑应该在外层就完成, # 比如余额要大于订单金额、订单状态是否正常之类的, # 暂时先省略,以后再补上 exist_order = (session.query(OrderModel).filter( OrderModel.order_id == order.order_id).first()) exist_order.status = 1 # 已支付 account = (session.query(AccountModel).filter( AccountModel.mobile == exist_order.mobile).first()) account.surplus -= order.fee session.commit()