def post(self, request):
    '保存订单信息和订单商品信息'
    json_dict = json.loads(request.body)
    address_id = json_dict.get('address_id')
    pay_method = json_dict.get('pay_method')
    if (not all([address_id, pay_method])):
        return HttpResponseForbidden('缺少必传参数')
    try:
        address = Address.objects.get(id=address_id)
    except Exception:
        return HttpResponseForbidden('参数address_id错误')
    if (pay_method not in [OrderInfo.PAY_METHODS_ENUM['CASH'], OrderInfo.PAY_METHODS_ENUM['ALIPAY']]):
        return HttpResponseForbidden('参数pay_method错误')
    user = request.user
    order_id = (timezone.localtime().strftime('%Y%m%d%H%M%S') + ('%09d' % user.id))
    with transaction.atomic():
        save_id = transaction.savepoint()
        try:
            order = OrderInfo.objects.create(order_id=order_id, user=user, address=address, total_count=0, total_amount=Decimal('0'), freight=Decimal('10.00'), pay_method=pay_method, status=(OrderInfo.ORDER_STATUS_ENUM['UNPAID'] if (pay_method == OrderInfo.PAY_METHODS_ENUM['ALIPAY']) else OrderInfo.ORDER_STATUS_ENUM['UNSEND']))
            redis_conn = get_redis_connection('carts')
            item_dict = redis_conn.hgetall(('carts_%s' % user.id))
            cart_selected = redis_conn.smembers(('selected_%s' % user.id))
            carts = {}
            for sku_id in cart_selected:
                carts[int(sku_id)] = int(item_dict[sku_id])
            sku_ids = carts.keys()
            for sku_id in sku_ids:
                while True:
                    sku = SKU.objects.get(id=sku_id)
                    origin_stock = sku.stock
                    origin_sales = sku.sales
                    sku_count = carts[sku_id]
                    if (sku_count > origin_stock):
                        transaction.savepoint_rollback(save_id)
                        return JsonResponse({'code': RETCODE.STOCKERR, 'errmsg': '库存不足'})
                    new_stock = (origin_stock - sku_count)
                    new_sales = (origin_sales + sku_count)
                    result = SKU.objects.filter(id=sku_id, stock=origin_stock).update(stock=new_stock, sales=new_sales)
                    if (result == 0):
                        continue
                    sku.goods.sales += sku_count
                    sku.goods.save()
                    OrderGoods.objects.create(order=order, sku=sku, count=sku_count, price=sku.price)
                    order.total_count += sku_count
                    order.total_amount += (sku_count * sku.price)
                    break
            order.total_amount += order.freight
            order.save()
        except Exception as e:
            logger.error(e)
            transaction.savepoint_rollback(save_id)
            return JsonResponse({'code': RETCODE.DBERR, 'errmsg': '下单失败'})
        transaction.savepoint_commit(save_id)
    pl = redis_conn.pipeline()
    pl.hdel(('carts_%s' % user.id), *cart_selected)
    pl.srem(('selected_%s' % user.id), *cart_selected)
    pl.execute()
    return JsonResponse({'code': RETCODE.OK, 'errmsg': '下单成功', 'order_id': order.order_id})
保存订单信息和订单商品信息
def post(self, request):
    
    json_dict = json.loads(request.body)
    address_id = json_dict.get('address_id')
    pay_method = json_dict.get('pay_method')
    if (not all([address_id, pay_method])):
        return HttpResponseForbidden('缺少必传参数')
    try:
        address = Address.objects.get(id=address_id)
    except Exception:
        return HttpResponseForbidden('参数address_id错误')
    if (pay_method not in [OrderInfo.PAY_METHODS_ENUM['CASH'], OrderInfo.PAY_METHODS_ENUM['ALIPAY']]):
        return HttpResponseForbidden('参数pay_method错误')
    user = request.user
    order_id = (timezone.localtime().strftime('%Y%m%d%H%M%S') + ('%09d' % user.id))
    with transaction.atomic():
        save_id = transaction.savepoint()
        try:
            order = OrderInfo.objects.create(order_id=order_id, user=user, address=address, total_count=0, total_amount=Decimal('0'), freight=Decimal('10.00'), pay_method=pay_method, status=(OrderInfo.ORDER_STATUS_ENUM['UNPAID'] if (pay_method == OrderInfo.PAY_METHODS_ENUM['ALIPAY']) else OrderInfo.ORDER_STATUS_ENUM['UNSEND']))
            redis_conn = get_redis_connection('carts')
            item_dict = redis_conn.hgetall(('carts_%s' % user.id))
            cart_selected = redis_conn.smembers(('selected_%s' % user.id))
            carts = {}
            for sku_id in cart_selected:
                carts[int(sku_id)] = int(item_dict[sku_id])
            sku_ids = carts.keys()
            for sku_id in sku_ids:
                while True:
                    sku = SKU.objects.get(id=sku_id)
                    origin_stock = sku.stock
                    origin_sales = sku.sales
                    sku_count = carts[sku_id]
                    if (sku_count > origin_stock):
                        transaction.savepoint_rollback(save_id)
                        return JsonResponse({'code': RETCODE.STOCKERR, 'errmsg': '库存不足'})
                    new_stock = (origin_stock - sku_count)
                    new_sales = (origin_sales + sku_count)
                    result = SKU.objects.filter(id=sku_id, stock=origin_stock).update(stock=new_stock, sales=new_sales)
                    if (result == 0):
                        continue
                    sku.goods.sales += sku_count
                    sku.goods.save()
                    OrderGoods.objects.create(order=order, sku=sku, count=sku_count, price=sku.price)
                    order.total_count += sku_count
                    order.total_amount += (sku_count * sku.price)
                    break
            order.total_amount += order.freight
            order.save()
        except Exception as e:
            logger.error(e)
            transaction.savepoint_rollback(save_id)
            return JsonResponse({'code': RETCODE.DBERR, 'errmsg': '下单失败'})
        transaction.savepoint_commit(save_id)
    pl = redis_conn.pipeline()
    pl.hdel(('carts_%s' % user.id), *cart_selected)
    pl.srem(('selected_%s' % user.id), *cart_selected)
    pl.execute()
    return JsonResponse({'code': RETCODE.OK, 'errmsg': '下单成功', 'order_id': order.order_id})