Exemple #1
0
def create():
    res = {'code': 1, 'msg': '成功', 'data': {}}
    try:
        member = g.member
        if not member:
            res['code'] = -1
            res['msg'] = '用户不存在'
            return jsonify(res)

        ids = request.form.get('ids')  # 商品的ids
        address_id = request.form.get('address_id')
        note = request.form.get('note')

        ids = json.loads(ids)


        pay_price = 0
        yun_price = 0
        # 根据ids去查购物车
        for id in ids:
            membercart = MemberCart.query.filter_by(member_id=member.id, food_id=id).first()

            if not membercart:  #是否合适
                continue

            food = Food.query.get(id)  #查食品表
            if not food or food.status != 1:
                continue

            pay_price+=food.price*membercart.quantity

        memberaddress = MemberAddress.query.get(address_id)#查地址

        if not memberaddress:
            res['code'] = -1
            res['msg'] = '地址不存在'
            return jsonify(res)


        # 生成订单
        payorder = PayOrder()
        payorder.order_sn = geneOrderSn()
        payorder.total_price = yun_price+pay_price
        payorder.yun_price = yun_price
        payorder.pay_price = pay_price
        payorder.note = note
        payorder.status = -8  #待支付
        payorder.express_status = -1  #待发货
        payorder.express_address_id = address_id
        payorder.express_info = memberaddress.showAddress()
        payorder.comment_status = -1 #待评论
        payorder.member_id = member.id

        db.session.add(payorder)

        #扣库存
        foods = db.session.query(Food).filter(Food.id.in_(ids)).with_for_update().all()
        temp_stock = {}  #临时的库存

        for food in foods:
            temp_stock[food.id] = food.stock
        # time.sleep(15)


        for id in ids:
            membercart = MemberCart.query.filter_by(member_id=member.id, food_id=id).first()

            if not membercart:
                res['code'] = -1
                res['msg'] = '购物车不存在'
                return jsonify(res)

            if membercart.quantity > temp_stock[id]:
                res['code'] = -1
                res['msg'] = '库存不足'
                return jsonify(res)
            food = db.session.query(Food).filter(Food.id==id).update({
                'stock':temp_stock[id]-membercart.quantity
            })



            if not food:
                raise Exception('更新失败')
            food = Food.query.get(id)  #查表

            # 生成订单的商品从表
            payorderitem = PayOrderItem()

            payorderitem.quantity = membercart.quantity
            payorderitem.price = food.price
            payorderitem.note = note
            payorderitem.status = 1
            payorderitem.pay_order_id = payorder.id
            payorderitem.member_id = member.id
            payorderitem.food_id = id

            db.session.add(payorderitem)

            #清空购物车已经下单的商品

            db.session.delete(membercart)  #删除已经下单的购物车

        db.session.commit()
    except Exception as e:
        db.session.rollback()
        res['code'] = -1
        res['msg'] = '出现异常'
        return jsonify(res)


    return jsonify(res)
Exemple #2
0
def create():
    res = {'code': 1, 'msg': '成功', 'data': {}}
    # try:
    member = g.member
    if not member:
        res['code'] = -1
        res['msg'] = '该用户不存在'
        return jsonify(res)
    ids = request.form.get('ids')
    address_id = request.form.get('address_id')
    note = request.form.get('note')
    ids = json.loads(ids)
    pay_price = 0
    yun_price = 0
    for id in ids:
        membercart = MemberCart.query.filter_by(food_id=id,
                                                member_id=member.id).first()
        if not membercart:
            continue
        food = Food.query.get(id)

        if not food or food.status != 1:
            continue

        pay_price += food.price * membercart.quantity
    memberaddress = MemberAddress.query.get(address_id)
    if not memberaddress:
        res['code'] = -1
        res['msg'] = '该地址不存在'
        return jsonify(res)
    payorder = PayOrder()
    payorder.order_sn = geneOrderSn()
    payorder.total_price = yun_price + pay_price
    payorder.yun_price = yun_price
    payorder.pay_price = pay_price
    payorder.note = note
    payorder.status = -8
    payorder.express_status = -1
    payorder.express_address_id = address_id
    payorder.express_info = memberaddress.showAddress()
    payorder.comment_status = -1
    payorder.member_id = member.id

    db.session.add(payorder)

    foods = db.session.query(Food).filter(
        Food.id.in_(ids)).with_for_update().all()
    temp_stock = {}
    for food in foods:
        temp_stock[food.id] = food.stock
    # time.sleep(50)
    for id in ids:
        membercart = MemberCart.query.filter_by(food_id=id,
                                                member_id=member.id).first()
        if membercart.quantity > temp_stock[id]:
            res['code'] = -1
            res['msg'] = '库存不足'
            return jsonify(res)
        food = db.session.query(Food).filter(Food.id == id).update(
            {'stock': temp_stock[id] - membercart.quantity})
        if not food:
            raise Exception('更新失败')
        food = Food.query.get(id)

        payorderitem = PayOrderItem()
        payorderitem.quantity = membercart.quantity
        payorderitem.price = food.price
        payorderitem.note = note
        payorderitem.status = 1
        payorderitem.pay_order_id = payorder.id
        payorderitem.member_id = member.id
        payorderitem.food_id = id
        db.session.add(payorderitem)
        db.session.delete(membercart)
    db.session.commit()
    return jsonify(res)
Exemple #3
0
def create():
    res = {'code': 1, 'msg': '成功', 'data': {}}
    # try:
    member = g.member
    if not member:
        res['code'] = -1
        res['msg'] = "用户不存在"
        return jsonify(res)
    ids = request.form.get('ids')  # 商品的ids
    address_id = request.form.get('address_id')
    print(address_id, 'iddddddddddddddddddddddddddddddddddddddd')
    note = request.form.get('note')
    ids = json.loads(ids)
    # 0根据ids查看购物车
    yun_price = 0
    pay_price = 0
    for id in ids:
        membercart = MemberCart.query.filter_by(member_id=member.id,
                                                food_id=id).first()
        if not membercart:
            continue

        food = Food.query.get(id)  # 差食品表
        if not food or food.status != 1:
            continue
        pay_price += food.price * membercart.quantity
    memberaddress = MemberAddress.query.get(address_id)
    if not memberaddress:
        res['code'] = -1
        res['msg'] = '地址不存在'
        return jsonify(res)

    # 1生成订单
    payorder = PayOrder()

    payorder.order_sn = geneOrderSn()
    payorder.total_price = pay_price + yun_price
    payorder.yun_price = yun_price
    payorder.pay_price = pay_price
    payorder.note = note
    payorder.status = -8  # 待支付
    payorder.express_status = -1  # 代发货
    payorder.express_info = memberaddress.showAddress()
    payorder.comment_status = -1  # 带评论
    payorder.member_id = member.id  # 带评论

    db.session.add(payorder)

    # 2扣库存--悲、乐 锁
    foods = db.session.query(Food).filter(
        Food.id.in_(ids)).with_for_update().all()
    temp_stock = {}  # 临时库存
    for food in foods:
        temp_stock[food.id] = food.stock

    for id in ids:
        membercart = MemberCart.query.filter_by(member_id=member.id,
                                                food_id=id).first()

        # if not membercart:
        #     res['code'] = -1
        #     res['msg'] = '购物车不存在'
        #     return jsonify(res)
        aa = int(membercart.quantity)
        if aa > int(temp_stock[id]):
            res['code'] = -1
            res['msg'] = '库存不足'
            return jsonify(res)

        food = db.session.query(Food).filter(Food.id == id).update(
            {'stock': temp_stock[id] - membercart.quantity})
        if not food:
            raise Exception('更新失败')
        food = Food.query.get(id)
        # 3生成订单的商品从表
        payorderitem = PayOrderItem()
        payorderitem.quantity = membercart.quantity
        payorderitem.price = food.price
        payorderitem.note = note
        payorderitem.status = 1
        payorderitem.pay_order_id = payorder.id
        payorderitem.member_id = member.id
        payorderitem.food_id = id

        db.session.add(payorderitem)

        # 4清空下单的购物车商品
        db.session.delete(membercart)
    # 提交
    db.session.commit()
    # except Exception as e:
    #     print(e, 'wwwwwwwwwwwwwwwwwyichagn   wwwwwwwwwwwwwwwwwwwwwww')
    #     db.session.rollback()
    #     res['code'] = -1
    #     res['msg'] = '出现异常'
    #     return jsonify(res)

    return jsonify(res)
Exemple #4
0
def create1():
    res = {'code': 1, 'msg': '成功', 'data': {}}
    # try:
    member = g.member
    if not member:
        res['code'] = -1
        res['msg'] = '用户不存'
        return jsonify(res)
    ids = request.form.get('ids')  # 商品的ids
    address_id = request.form.get('address_id')
    note = request.form.get('note')
    num = int(request.form.get('num'))

    ids = json.loads(ids)
    pay_price = 0
    yun_price = 0
    # 0、根据ids去购物车
    for id in ids:
        food = Food.query.get(id)  # 查食品表

        if not food or food.status != 1:
            continue

        pay_price += food.price * num

    memberaddress = MemberAddress.query.get(address_id)  # 查地址

    if not memberaddress:
        res['code'] = -1
        res['msg'] = '地址不存在'
        return jsonify(res)

        # 1、生成订单
    payorder = PayOrder()
    payorder.order_sn = geneOrderSn()  # 唯一
    payorder.total_price = yun_price + pay_price
    payorder.yun_price = yun_price
    payorder.pay_price = pay_price
    payorder.note = note
    payorder.status = -8  # 待付款
    payorder.express_status = -1  # 待发货
    payorder.express_address_id = address_id
    payorder.express_info = memberaddress.showAddress()
    payorder.comment_status = -1  # 待评论
    payorder.member_id = member.id  # 待评论

    db.session.add(payorder)

    # 2、扣库存--并发问题 Food  悲观锁----乐观锁
    foods = db.session.query(Food).filter(
        Food.id.in_(ids)).with_for_update().all()
    temp_stock = {}  # 临时的库存

    # {1:99,2:100}
    for food in foods:
        temp_stock[food.id] = food.stock
    for id in ids:
        if num > temp_stock[int(id)]:
            res['code'] = -1
            res['msg'] = '库存不足'
            return jsonify(res)

        food = db.session.query(Food).filter(Food.id == id).update(
            {'stock': temp_stock[int(id)] - num})

        if not food:
            raise Exception('更新失败')

        food = Food.query.get(int(id))  # 查

        # 3、生成订单的商品从表
        payorderitem = PayOrderItem()
        payorderitem.quantity = num
        payorderitem.price = food.price
        payorderitem.note = note
        payorderitem.status = 1
        payorderitem.pay_order_id = payorder.id
        payorderitem.member_id = member.id
        payorderitem.food_id = int(id)

        db.session.add(payorderitem)

    db.session.commit()
    # except Exception as e:
    #     print(e,1111111111111111111)
    #     db.session.rollback()
    #     res['code'] = -1
    #     res['msg'] = '出现异常'
    #     return jsonify(res)

    return jsonify(res)
Exemple #5
0
def create():
    ctx = {'code': 1, 'msg': '成功', 'data': {}}

    token = request.headers.get('token')
    id_and_token = token.split('#')

    if len(id_and_token) != 2:
        ctx['code'] = -1
        ctx['msg'] = 'token错误'
        return jsonify(ctx)

    member_id = id_and_token[0]
    ids = request.form.get('ids')  #商品ids
    ids = json.loads(ids)
    address_id = request.form.get('address_id')  #地址id
    note = request.form.get('note')
    total_price = 0
    yun_price = 0
    pay_price = 0
    for id in ids:
        membercart = MemberCart.query.filter_by(member_id=member_id,
                                                food_id=id).first()
        food = Food.query.get(id)
        pay_price += food.price * membercart.quantity

    total_price = pay_price + yun_price
    memaddress = MemberAddress.query.get(address_id)
    payorder = PayOrder()
    payorder.order_sn = geneOrderSn()
    payorder.total_price = total_price
    payorder.yun_price = yun_price
    payorder.pay_price = pay_price
    payorder.note = note
    payorder.status = -8
    payorder.express_status = -8
    payorder.express_address_id = address_id
    payorder.express_info = memaddress.showAddress
    payorder.comment_status = -8
    payorder.member_id = member_id
    db.session.add(payorder)

    # 2、扣库存--并发问题 Food  悲观锁----乐观锁
    foods = db.session.query(Food).filter(
        Food.id.in_(ids)).with_for_update().all()
    temp_stock = {}  # 临时的库存

    # {1:99,2:100}
    for food in foods:
        temp_stock[food.id] = food.stock
    for id in ids:
        membercart = MemberCart.query.filter_by(member_id=member.id,
                                                food_id=id).first()

        if not membercart:
            ctx['code'] = -1
            ctx['msg'] = '购物车不存在'
            return jsonify(ctx)

        if membercart.quantity > temp_stock[id]:
            ctx['code'] = -1
            ctx['msg'] = '库存不足'
            return jsonify(ctx)

        food = db.session.query(Food).filter(Food.id == id).update(
            {'stock': temp_stock[id] - membercart.quantity})

        if not food:
            raise Exception('更新失败')

        food = Food.query.get(id)  # 查

        # 3、生成订单的商品从表
        payorderitem = PayOrderItem()
        payorderitem.quantity = membercart.quantity
        payorderitem.price = food.price
        payorderitem.note = note
        payorderitem.status = 1
        payorderitem.pay_order_id = payorder.id
        payorderitem.member_id = member.id
        payorderitem.food_id = id

        db.session.add(payorderitem)

        db.session.delete(membercart)  # 删除已经下单的购物车

        db.session.commit()

        return jsonify(res)
        return jsonify(ctx)