def create_order(product_ids, address_id, user_coupon_id, remark, uid): order_no = Order.__make_order_no() o_products = [] for ids in product_ids: o_products.append(Product.get_order_product(ids)) with db.auto_check_empty(UserException(error_code=6001, msg='地址不存在')): user_address = UserAddress.query.filter_by(id=address_id).first() snap_address = user_address.name + ' ' + user_address.mobile + ' ' + user_address.detail if user_coupon_id: with db.auto_check_empty( UserException(error_code=7001, msg='优惠卷不存在')): coupon_price = UserCoupon.query.filter( and_(UserCoupon.id == user_coupon_id, UserCoupon.status == 1)).first().coupon.price else: coupon_price = 0 total_price = Order.__product_to_calculate(o_products)[0] postage = Order.__product_to_calculate(o_products)[1] with db.auto_commit(): order = Order() order.order_no = order_no order.user_id = uid order.snap_address = snap_address order.coupon_price = coupon_price order.remark = remark order.total_price = total_price order.postage = postage order.pay_price = total_price + postage - coupon_price OrderSnap.add_order_snap(o_products, order_no) Order.__minus_product_stock(o_products) Order.__revise_user_coupon(user_coupon_id) db.session.add(order) return order_no
def get_order_product(): form = ProductIdValidator().validate_for_api() o_product = [] for ids in form.product_ids.data: o_product.append(Product.get_order_product(ids)) return jsonify(o_product)