def addOrder(request): orderids = request.session.get('orderids') order = OrderInfo() oOder = OrderInfo.objects.all().order_by('-oid')[0:1] if len(oOder) == 0: order.oid = 1 else: order.oid = int(oOder[0].oid) + 1 order.odate = datetime.now() order.oIsPay = 0 order.ototal = request.POST.get('total') order.oaddress = request.POST.get('address') order.user_id = request.session.get('user_id') order.zhifu = request.POST.get('zhifu') order.save() for oid in orderids: cartInfo = CartInfo.objects.get(id=oid) good = GoodsInfo.objects.get(id=cartInfo.goods_id) if cartInfo.count > good.gkucun: return JsonResponse({'status': 2}) detail = OrderDetailInfo() detail.price = good.gprice detail.count = cartInfo.count detail.goods_id = good.id detail.order_id = order.oid detail.save() return JsonResponse({'status': 1})
def order_handle(request): #保存一个事物点 tran_id = transaction.savepoint() #接收购物车编号 # 根据POST和session获取信息 # cart_ids=post.get('cart_ids') try: post = request.POST orderlist = post.getlist('id[]') total = post.get('total') address = post.get('address') order=OrderInfo() now=datetime.now() uid = request.session.get('user_id') order.oid='%s%d'%(now.strftime('%Y%m%d%H%M%S'),uid) order.user_id=uid order.odate=now order.ototal=Decimal(total) order.oaddress = address order.save() # 遍历购物车中提交信息,创建订单详情表 for orderid in orderlist: cartinfo = CartInfo.objects.get(id=orderid) # good = GoodsInfo.objects.get(cartinfo__id=cartinfo.id) good = GoodsInfo.objects.get(pk=cartinfo.goods_id) # print '*'*10 # print cartinfo.goods_id # 判断库存是否够 if int(good.gkucun) >= int(cartinfo.count): # 库存够,移除购买数量并保存 good.gkucun -= int(cartinfo.count) good.save() goodinfo = GoodsInfo.objects.get(cartinfo__id=orderid) # 创建订单详情表 detailinfo = OrderDetailInfo() detailinfo.goods_id = int(goodinfo.id) detailinfo.order_id = int(order.oid) detailinfo.price = Decimal(int(goodinfo.gprice)) detailinfo.count = int(cartinfo.count) detailinfo.save() # 循环删除购物车对象 cartinfo.delete() else: # 库存不够出发事务回滚 transaction.savepoint_rollback(tran_id) # 返回json供前台提示失败 return JsonResponse({'status': 2}) except Exception as e: # print '==================%s'%e transaction.savepoint_rollback(tran_id) # 返回json供前台提示成功 return JsonResponse({'status': 1})
def order_handle(request): #保存一个事物点 tran_id = transaction.savepoint() #接收购物车编号 # 根据POST和session获取信息 # cart_ids=post.get('cart_ids') try: post = request.POST orderlist = post.getlist('id[]') total = post.get('total') address = post.get('address') order=OrderInfo() now=datetime.now() uid = request.session.get('user_id') order.oid='%s%d'%(now.strftime('%Y%m%d%H%M%S'),uid) order.user_id=uid order.odate=now order.ototal=Decimal(total) order.oaddress = address order.save() # 遍历购物车中提交信息,创建订单详情表 for orderid in orderlist: cartinfo = CartInfo.objects.get(id=orderid) # good = GoodsInfo.objects.get(cartinfo__id=cartinfo.id) good = GoodsInfo.objects.get(pk=cartinfo.goods_id) # print '*'*10 # print cartinfo.goods_id # 判断库存是否够 if int(good.gkucun) >= int(cartinfo.count): # 库存够,移除购买数量并保存 good.gkucun -= int(cartinfo.count) good.save() goodinfo = GoodsInfo.objects.get(cartinfo__id=orderid) # 创建订单详情表 detailinfo = OrderDetailInfo() detailinfo.goods_id = int(goodinfo.id) detailinfo.order_id = int(order.oid) detailinfo.price = Decimal(int(goodinfo.gprice)) detailinfo.count = int(cartinfo.count) detailinfo.save() # 循环删除购物车对象 cartinfo.delete() else: # 库存不够出发事务回滚 transaction.savepoint_rollback(tran_id) # 返回json供前台提示失败 return JsonResponse({'status': 2}) except Exception as e: print('==================%s'%e) transaction.savepoint_rollback(tran_id) # 返回json供前台提示成功 return JsonResponse({'status': 1})
def order_handle(request): """提交订单""" # 保存一个点(django中的事务) tran_id = transaction.savepoint() cart_ids = str(request.POST.get('cart_ids')) total = request.POST.get('total') print('cart_ids:%s'%cart_ids) print('total:%s' % total) try: order = OrderInfo() now = datetime.now() uid = request.session['id'] # 采用订单创建时间+用户id来表示订单编号 order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid order.odate = now order.ototal = Decimal(request.POST.get('total')) order.save() cart_ids1 = [int(item) for item in cart_ids.split(' ')] for id1 in cart_ids1: detail = OrderDetailInfo() # 表示外键order是当前的订单对象 detail.order = order cart = CarInfo.objects.get(id=id1) goods = cart.goods # 判断库存 print goods.gkucun if goods.gkucun >= cart.count: goods.gkucun = cart.goods.gkucun - cart.count print cart.count print goods.gkucun goods.save() detail.goods_id = goods.id detail.price = goods.gprice detail.count = cart.count detail.save() cart.delete() else: transaction.savepoint_rollback(tran_id) return JsonResponse({'url':'/carts/'}) transaction.savepoint_commit(tran_id) except Exception as e: print '===========%s' % e transaction.savepoint_rollback(tran_id) return JsonResponse({'url':'/user/order_1/'})
def order_handle(request): tran_id = transaction.savepoint() # 接收购物车编号 cart_ids = request.POST.get('cart_ids') try: # 创建订单对象 order = OrderInfo() now = datetime.now() uid = request.session['user_id'] order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid order.odate = now order.oaddress = request.POST.get('address') order.ototal = 0 order.save() # 创建详单对象 cart_ids1 = [int(item) for item in cart_ids.split(',')] total = 0 for id1 in cart_ids1: detail = OrderDetailInfo() detail.order = order # 查询购物车信息 cart = CartInfo.objects.get(id=id1) # 判断商品库存 goods = cart.goods # 如果库存大于购买量 if goods.gkuncun >= cart.count: # 减少商品数量 goods.gkuncun = cart.goods.gkuncun - cart.count goods.save() # 完善订单信息 detail.goods_id = goods.id price = goods.gprice detail.price = price count = cart.count detail.count = count detail.save() total = total + price * count # 删除购物车数据 cart.delete() # 如果库存小于购买量 else: transaction.savepoint_rollback(tran_id) return redirect('/cart/') # 保存总价 order.ototal = total + 10 order.save() transaction.savepoint_commit(tran_id) except Exception as e: print "========================%s" % e transaction.savepoint_rollback(tran_id) return redirect('/user_center_order/')
def order_handle(request): print("进来了") #创建事物节点 status = 1 with transaction.atomic(): tran_id = transaction.savepoint() try: post = request.POST orderlist = post.getlist('ids[]') total = post.get('totalPay') address = post.get('address') # 1、创建订单 order = OrderInfo() now = datetime.now() uid = request.session['user_id'] #时间戳 order.id = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid order.odata = now order.ototal = total order.address = address order.save() # 2、遍历购物车 for orderid in orderlist: cart = CartInfo.objects.get(id=orderid) goods = cart.goods # 3、判断库存 print(int(goods.gstock), int(cart.num)) if int(goods.gstock) >= int(cart.num): goods.gstock -= int(cart.num) goods.save() #创建详单 detailinfo = OrderDetailInfo() detailinfo.goods_id = goods.id detailinfo.order_id = order.oid detailinfo.price = total detailinfo.num = cart.num detailinfo.save() #删除购物车 cart.delete() else: transaction.savepoint_rollback(tran_id) status = 2 except Exception as e: print "%s" % e status = 3 transaction.savepoint_rollback(tran_id) print(status) return JsonResponse({'status': status})
def order_handle(request): tran_id = transaction.savepoint() # 接收购物车编号 cart_ids = request.POST.get('cart_ids') # 5,6 try: # 创建订单对象 order = OrderInfo() now = datetime.now() uid = request.session['user_id'] order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid # print order.oid order.odate = now order.ototal = Decimal(request.POST.get('total')) order.oaddress = request.POST.get('address') order.save() # 创建详单对象 cart_ids1 = [int(item) for item in cart_ids.split(',')] for id1 in cart_ids1: detail = OrderDetailInfo() detail.order = order # 查询购物车信息 cart = CartInfo.objects.get(id=id1) # 判断商品库存 goods = cart.goods if goods.gkucun >= cart.count: # 如果库存大于购买数量 # 减少商品库存 goods.gkucun = cart.goods.gkucun - cart.count goods.save() # 完善详单信息 detail.goods_id = goods.id detail.price = goods.gprice detail.count = cart.count detail.save() # 删除购物车数据 cart.delete() else: # 如果库存小于购买数量 transaction.savepoint_rollback(tran_id) return redirect('/cart/') # return HttpResponse('no') transaction.savepoint_commit(tran_id) except Exception as e: print '================%s' % e transaction.savepoint_rollback(tran_id) return redirect('/order/pay/')
def addOrder(request):#先获取下单的所有购物车的id orderids = request.session.get('orderids') #构建一个订单对象 order = OrderInfo() #查询订单最大的id oOder = OrderInfo.objects.all().order_by('oid')[0:1] if len(oOder) == 0: order.oid = 1 else: print(int(oOder[0].oid)) order.oid = int(oOder[0].oid)+1 #增加订单时间 order.odate = datetime.now() #是否付款 order.oIsPay =0 order.ototal=request.POST.get('totle') order.oaddress= request.POST.get('address') order.user_id= request.session.get('user_id') order.zhifu= request.POST.get('zhifu') order.save() #增加订单明细的商品洗信息 for oid in orderids:#获取购物车(会有多条数据) cartInfo = CartInfo.objects.get(id=oid) good = GoodInfo.objects.get(id=cartInfo.goods_id) #如果返回2表示库存不够 if cartInfo.count>good.gkucun: return JsonResponse({'status':2}) #定义一个明细订单 detail = OrderDetailInfo() detail.price = good.gprice detail.count = cartInfo.count detail.goods_id = good.id detail.price = order.oid detail.save() #如果返回1表示增加成功 return JsonResponse({'status':1})
def order_handle(request): tran_id = transaction.savepoint() try: post = request.POST orderlist = post.getlist('id[]') total = post.get('total') address = post.get('address') order = OrderInfo() now = datetime.now() uid = request.session.get('user_id') order.oid = '%s%d' % (now.strftime('%Y%m%d%H%M%S'), uid) order.user_id = uid order.odate = now order.ototal = Decimal(total) order.oaddress = address order.save() for orderid in orderlist: cartinfo = CartInfo.objects.get(id=orderid) good = GoodsInfo.objects.get(pk=cartinfo.goods_id) if int(good.gkucun) >= int(cartinfo.count): good.gkucun -= int(cartinfo.count) good.save() goodinfo = GoodsInfo.objects.get(cartinfo__id=orderid) detailinfo = OrderDetailInfo() detailinfo.goods_id = int(goodinfo.id) detailinfo.order_id = int(order.oid) detailinfo.price = Decimal(int(goodinfo.gprice)) detailinfo.count = int(cartinfo.count) detailinfo.save() cartinfo.delete() else: transaction.savepoint_rollback(tran_id) return JsonResponse({'status': 2}) except Exception as e: print '==================%s' % e transaction.savepoint_rollback(tran_id) return JsonResponse({'status': 1})
def do_order(request): # {'cid': cids,'pay_style': pay_style}, pay_style = request.GET.get('pay_style') # cash zfb cids = request.GET.getlist('cid') # 购物车的id uid = request.session.get('user_id') # 用户id user = UserInfo.objects.get(id=uid) # 用户 # 开启事务 sid = transaction.savepoint() # # 创建订单 order = OrderInfo() order.oid = str(int(time.time() * 1000)) + str(uid) # 订单编号 order.user_id = uid order.ototal = 0 # 订单金额 order.oaddr = UserAddressInfo.objects.get(user=user).uaddress # 订单地址 #address = order.oaddr.objects.get("uaddress") order.save() cart_list = CartInfo.objects.filter(id__in=cids) # 查找到所有购物车对象 total_price = 0 total_count = 0 isOk = True for cart in cart_list: # if cart.count <= cart.goods.gkucun: # 库存充足 detail = OrderDetailInfo() detail.order = order detail.goods = cart.goods detail.price = cart.goods.gprice detail.count = cart.count detail.save() # 更改库存 cart.goods.gkucun -= cart.count cart.goods.save() # 计算总价 total_price += detail.count * detail.price total_count += detail.count # 删除购物车对象 cart.delete() else: # 库存不够 isOk = False break if isOk: # 最终提交订单 order.ototal = total_price # 订单成功 if pay_style == 'cash': order.oIsPay = 1 order.save() transaction.savepoint_commit(sid) # 提交事务 return JsonResponse({'res': 1}) else: order_id = order.oid # 订单编号 # total_pay = order.ototal total_pay = order.ototal order_string = alipay.api_alipay_trade_page_pay( out_trade_no=order_id, total_amount=str(total_pay), subject=f'粤嵌科技{order_id}', return_url='http://127.0.0.1:8000/order/check_pay/?order_id=' + order_id, notify_url='http://127.0.0.1:8000/order/check_pay/?order_id=' + order_id, ) alipay_url = settings.ALIPAY_URL + '?' + order_string return JsonResponse({'res': 3, 'pay_url': alipay_url}) else: # 订单失败 transaction.savepoint_rollback(sid) return JsonResponse({'res': 0})