结算中心,即从购物车前往支付前的确认页面,这里要开始选择优惠券了

"""
前端传过来数据 course_list 课程列表

redis 中将要存放的结算数据 {
settlement_userid_courseid: {
id, 课程id,
title,
course_img,
valid_period_display,
price,
course_coupon_dict: {  # 课程优惠券
coupon_id: {优惠券信息}
coupon_id2: {优惠券信息}
coupon_id3: {优惠券信息}
}
# 默认不给你选 这个字段只有更新的时候才添加
default_coupon_id: 1
} global_coupon_userid: { # 全局优惠券
coupon_id: {优惠券信息}
coupon_id2: {优惠券信息}
coupon_id3: {优惠券信息},
# 这个字段只有更新的时候才添加
default_global_coupon_id: 1
}
}
"""

加入结算中心接口

加入结算中心后,放到redis中,同时将redis中的购物车中课程清除。等待结算

class SettlementView(APIView):
authentication_classes = [LoginAuth, ] def post(self, request):
res = BaseResponse()
# 1 获取前端的数据以及user_id
course_list = request.data.get("course_list", "")
user_id = request.user.pk
# 2 校验数据的合法性
for course_id in course_list:
# 2.1 判断course_id 是否在购物车中
shopping_car_key = SHOPPINGCAR_KEY % (user_id, course_id)
if not CONN.exists(shopping_car_key):
res.code = 1050
res.error = "课程ID不合法"
return Response(res.dict)
# 3 构建数据结构
# 3.1 获取用户的所有合法优惠券
user_all_coupons = CouponRecord.objects.filter(
account_id=user_id,
status=0,
coupon__valid_begin_date__lte=now(),
coupon__valid_end_date__gte=now(),
).all()
print(user_all_coupons)
# 3.2 构建优惠券dict
course_coupon_dict = {}
global_coupon_dict = {}
for coupon_record in user_all_coupons:
coupon = coupon_record.coupon
if coupon.object_id == course_id: # 一门课程只能存在一张优惠券
course_coupon_dict[coupon.id] = {
"id": coupon.id,
"name": coupon.name,
"coupon_type": coupon.get_coupon_type_display(),
"object_id": coupon.object_id,
"money_equivalent_value": coupon.money_equivalent_value,
"off_percent": coupon.off_percent,
"minimum_consume": coupon.minimum_consume
}
elif coupon.object_id == "":
global_coupon_dict[coupon.id] = {
"id": coupon.id,
"name": coupon.name,
"coupon_type": coupon.get_coupon_type_display(),
"money_equivalent_value": coupon.money_equivalent_value,
"off_percent": coupon.off_percent,
"minimum_consume": coupon.minimum_consume
}
# 3.3 构建写入redis的数据结构
course_info = CONN.hgetall(shopping_car_key)
price_policy_dict = json.loads(course_info["price_policy_dict"])
default_policy_id = course_info["default_price_policy_id"]
valid_period = price_policy_dict[default_policy_id]["valid_period_display"]
price = price_policy_dict[default_policy_id]["price"] settlement_info = {
"id": course_info["id"],
"title": course_info["title"],
"course_img": course_info["course_img"],
"valid_period": valid_period,
"price": price,
"course_coupon_dict": json.dumps(course_coupon_dict, ensure_ascii=False)
}
# 4 写入redis
settlement_key = SETTLEMENT_KEY % (user_id, course_id)
global_coupon_key = GLOBAL_COUPON_KEY % user_id
CONN.hmset(settlement_key, settlement_info)
if global_coupon_dict:
CONN.hmset(global_coupon_key, global_coupon_dict) # 将全局优惠券也放到redis中
# 5 删除购物车中的数据
CONN.delete(shopping_car_key)
res.data = "加入结算中心成功"
return Response(res.dict)

post加入结算中心

模拟测试接口

查看结算中心

 def get(self, request):
res = BaseResponse()
# 1, 获取user_id
user_id = request.user.pk
# 2, 拼接所有key
# 3, 去redis取数据
settlement_key = SETTLEMENT_KEY % (user_id, "*")
global_coupon_key = GLOBAL_COUPON_KEY % user_id
all_keys = CONN.scan_iter(settlement_key)
ret = []
for key in all_keys:
ret.append(CONN.hgetall(key))
global_coupon_info = CONN.hgetall(global_coupon_key)
res.data = {
"settlement_info": ret,
"global_coupon_dict": global_coupon_info
}
return Response(res.dict)

获取结算信息

更新结算中心接口

在结算时,如果有更改课程的优惠券,或全局优惠券时,需put请求更新结算中心的数据

   def put(self, request):
# course_id course_coupon_id global_coupon_id
res = BaseResponse()
# 1, 获取前端传过来数据
course_id = request.data.get("course_id", "")
course_coupon_id = request.data.get("course_coupon_id", "")
global_coupon_id = request.data.get("global_coupon_id", "")
user_id = request.user.pk
# 2, 校验数据合法性
# 2.1 校验course_id
key = SETTLEMENT_KEY % (user_id, course_id)
if course_id:
if not CONN.exists(key):
res.code = 1060
res.error = "课程ID不合法"
return Response(res.dict)
# 2.2 校验 course_coupon_id
if course_coupon_id:
course_coupon_dict = json.loads(CONN.hget(key, "course_coupon_dict"))
if str(course_coupon_id) not in course_coupon_dict:
res.code = 1061
res.error = "课程优惠券ID不合法"
return Response(res.dict)
# 2.3 校验global_coupon_id
if global_coupon_id:
global_coupon_key = GLOBAL_COUPON_KEY % user_id
if not CONN.exists(global_coupon_key):
res.code = 1062
res.error = "全局优惠券ID不合法"
return Response(res.dict)
CONN.hset(global_coupon_key, "default_global_coupon_id", global_coupon_id)
# 3,修改redis中数据
CONN.hset(key, "default_coupon_id", course_coupon_id)
res.data = "更新成功"
return Response(res.dict)

更新结算信息

更新后查看结算的结果

import json
import redis
from rest_framework.views import APIView
from rest_framework.response import Response
from utils.base_response import BaseResponse
from utils.redis_pool import POOL
from django.utils.timezone import now
from utils.my_auth import LoginAuth from .views import SHOPPINGCAR_KEY
from .models import CouponRecord CONN = redis.Redis(connection_pool=POOL)
SETTLEMENT_KEY = "SETTLEMENT_%s_%s"
GLOBAL_COUPON_KEY = "GLOBAL_COUPON_%s"
"""
前端传过来数据 course_list redis 中存的数据 {
settlement_userid_courseid: {
id, 课程id,
title,
course_img,
valid_period_display,
price,
course_coupon_dict: {
coupon_id: {优惠券信息}
coupon_id2: {优惠券信息}
coupon_id3: {优惠券信息}
}
# 默认不给你选 这个字段只有更新的时候才添加
default_coupon_id: 1
} global_coupon_userid: {
coupon_id: {优惠券信息}
coupon_id2: {优惠券信息}
coupon_id3: {优惠券信息},
# 这个字段只有更新的时候才添加
default_global_coupon_id: 1 } }
""" class SettlementView(APIView):
authentication_classes = [LoginAuth, ] def post(self, request):
res = BaseResponse()
# 1 获取前端的数据以及user_id
course_list = request.data.get("course_list", "")
user_id = request.user.pk
# 2 校验数据的合法性
for course_id in course_list:
# 2.1 判断course_id 是否在购物车中
shopping_car_key = SHOPPINGCAR_KEY % (user_id, course_id)
if not CONN.exists(shopping_car_key):
res.code = 1050
res.error = "课程ID不合法"
return Response(res.dict)
# 3 构建数据结构
# 3.1 获取用户的所有合法优惠券
user_all_coupons = CouponRecord.objects.filter(
account_id=user_id,
status=0,
coupon__valid_begin_date__lte=now(),
coupon__valid_end_date__gte=now(),
).all()
print(user_all_coupons)
# 3.2 构建优惠券dict
course_coupon_dict = {}
global_coupon_dict = {}
for coupon_record in user_all_coupons:
coupon = coupon_record.coupon
if coupon.object_id == course_id: # 一门课程只能存在一张优惠券
course_coupon_dict[coupon.id] = {
"id": coupon.id,
"name": coupon.name,
"coupon_type": coupon.get_coupon_type_display(),
"object_id": coupon.object_id,
"money_equivalent_value": coupon.money_equivalent_value,
"off_percent": coupon.off_percent,
"minimum_consume": coupon.minimum_consume
}
elif coupon.object_id == "":
global_coupon_dict[coupon.id] = {
"id": coupon.id,
"name": coupon.name,
"coupon_type": coupon.get_coupon_type_display(),
"money_equivalent_value": coupon.money_equivalent_value,
"off_percent": coupon.off_percent,
"minimum_consume": coupon.minimum_consume
}
# 3.3 构建写入redis的数据结构
course_info = CONN.hgetall(shopping_car_key)
price_policy_dict = json.loads(course_info["price_policy_dict"])
default_policy_id = course_info["default_price_policy_id"]
valid_period = price_policy_dict[default_policy_id]["valid_period_display"]
price = price_policy_dict[default_policy_id]["price"] settlement_info = {
"id": course_info["id"],
"title": course_info["title"],
"course_img": course_info["course_img"],
"valid_period": valid_period,
"price": price,
"course_coupon_dict": json.dumps(course_coupon_dict, ensure_ascii=False)
}
# 4 写入redis
settlement_key = SETTLEMENT_KEY % (user_id, course_id)
global_coupon_key = GLOBAL_COUPON_KEY % user_id
CONN.hmset(settlement_key, settlement_info)
if global_coupon_dict:
CONN.hmset(global_coupon_key, global_coupon_dict) # 将全局优惠券也放到redis中
# 5 删除购物车中的数据
CONN.delete(shopping_car_key)
res.data = "加入结算中心成功"
return Response(res.dict) def get(self, request):
res = BaseResponse()
# 1, 获取user_id
user_id = request.user.pk
# 2, 拼接所有key
# 3, 去redis取数据
settlement_key = SETTLEMENT_KEY % (user_id, "*")
global_coupon_key = GLOBAL_COUPON_KEY % user_id
all_keys = CONN.scan_iter(settlement_key)
ret = []
for key in all_keys:
ret.append(CONN.hgetall(key))
global_coupon_info = CONN.hgetall(global_coupon_key)
res.data = {
"settlement_info": ret,
"global_coupon_dict": global_coupon_info
}
return Response(res.dict) def put(self, request):
# course_id course_coupon_id global_coupon_id
res = BaseResponse()
# 1, 获取前端传过来数据
course_id = request.data.get("course_id", "")
course_coupon_id = request.data.get("course_coupon_id", "")
global_coupon_id = request.data.get("global_coupon_id", "")
user_id = request.user.pk
# 2, 校验数据合法性
# 2.1 校验course_id
key = SETTLEMENT_KEY % (user_id, course_id)
if course_id:
if not CONN.exists(key):
res.code = 1060
res.error = "课程ID不合法"
return Response(res.dict)
# 2.2 校验 course_coupon_id
if course_coupon_id:
course_coupon_dict = json.loads(CONN.hget(key, "course_coupon_dict"))
if str(course_coupon_id) not in course_coupon_dict:
res.code = 1061
res.error = "课程优惠券ID不合法"
return Response(res.dict)
# 2.3 校验global_coupon_id
if global_coupon_id:
global_coupon_key = GLOBAL_COUPON_KEY % user_id
if not CONN.exists(global_coupon_key):
res.code = 1062
res.error = "全局优惠券ID不合法"
return Response(res.dict)
CONN.hset(global_coupon_key, "default_global_coupon_id", global_coupon_id)
# 3,修改redis中数据
CONN.hset(key, "default_coupon_id", course_coupon_id)
res.data = "更新成功"
return Response(res.dict)

shopping/settlement_view.py

qhfl-7 结算中心的更多相关文章

  1. AI学习吧-结算中心

    结算中心流程 在结算中心中,主要是对用户添加到购物车商品的结算,由于用户可能添加了多个课程,但是,结算时会选择性的进行支付.在结算时会选中课程id,和对应的价格策略.在后台,首先会对用户进行校验,验证 ...

  2. python 全栈开发,Day106(结算中心(详细),立即支付)

    昨日内容回顾 1. 为什么要开发路飞学城? 提供在线教育的学成率: 特色: 学,看视频,单独录制增加趣味性. 练,练习题 改,改学生代码 管,管理 测,阶段考核 线下:8次留级考试 2. 组织架构 - ...

  3. python 全栈开发,Day104(DRF用户认证,结算中心,django-redis)

    考试第二部分:MySQL数据库 6.  MySQL中char和varchar的区别(1分) char是定长,varchar是变长. char的查询速度比varchar要快. 7.   MySQL中va ...

  4. python 全栈开发,Day103(微信消息推送,结算中心业务流程)

    昨日内容回顾 第一部分:考试题(Python基础) 第二部分:路飞相关 1. 是否遇到bug?难解决的技术点?印象深刻的事? - orm操作费劲 - 最开始学习路由系统时候,匹配规则: 答案一: 有, ...

  5. Django day 38 结算中心,支付中心,计算价格方法

    一:结算中心 二:支付中心 三:计算价格方法

  6. day 109结算中心.

    from django.db import models from django.contrib.contenttypes.fields import GenericForeignKey,Generi ...

  7. PP.io的三个阶段,“强中心”——“弱中心”——“去中心”

    什么是PP.io? PP.io是我和Bill发起的存储项目,目的在于为开发者提供一个去中心化的存储和分发平台,能做到更便宜,更高速,更隐私. 当然做去中心化存储的项目也有好几个,FileCoin,Si ...

  8. python 全栈开发,Day105(路飞其他数据库表结构,立即结算需求)

    考试第三部分:Django 16.  列列举你熟悉的Http协议头以及作用.(1分) Accept-Charset: 用于告诉浏览器,客户机采用的编码 Host: 客户机通过这个头告诉服务器,想访问的 ...

  9. s11 day103 luffy项目结算部分+认证+django-redis

    1.增加认证用的表 class Account(models.Model): username =models.CharField(,unique=True) email= models.EmailF ...

随机推荐

  1. ARM920T的Cache

    转载自:http://www.eefocus.com/mcu-dsp/242034 ARM920T有16K的数据Cache和16K的指令Cache,这两个Cache是基本相同的,数据Cache多了一些 ...

  2. python常见循环练习

    第一题:求5的阶乘 # 方法1,递归 def jc(num): if num == 1: return 1 else: return num*jc(num-1) print(jc(5)) # 方法2, ...

  3. 成为JavaGC专家—深入浅出Java垃圾回收机制

    对于Java开发人员来说,了解垃圾回收机制(GC)有哪些好处呢? 首先可以满足作为一名软件工程师的求知欲, 其次,深入了解GC如何工作可以帮你写出更好的Java应用. 这仅仅代表我个人的意见,但我坚信 ...

  4. C#操作IIS程序池及站点的创建配置(转)

      原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...

  5. py库: arrow (时间)

    arrow是个时间日期库,简洁易用.支持python3.6 https://arrow.readthedocs.io/en/latest/ arrow官网api https://github.com/ ...

  6. 多进程模块 multiprocessing

    由于GIL的存在,python中的多线程其实并不是真正的多线程,如果想要充分地使用多核CPU的资源,在python中大部分情况需要使用多进程. multiprocessing包是Python中的多进程 ...

  7. Delphi中Chrome Chromium、Cef3学习笔记(一)

    原文   http://blog.csdn.net/xtfnpgy/article/details/46635225   官方下载地址:https://cefbuilds.com/ CEF简介: 嵌入 ...

  8. 在chrome console添加jQuery支持

    有时候想在chrome console使用jq,那么下面这段代码就可以完美解决问题了. var script = document.createElement('script');script.src ...

  9. 解决no module named ipykernel_launcher

    解决no module named ipykernel_launcher 最近开hydrogen的时候,提示no module named ipykernel_launcher. 记得以前解决过这个问 ...

  10. oracle 连接字符串的问题

    未指定的错误,发生了一个 Oracle 错误,但无法从 Oracle 中检索错误信息.数据类型不被支持. 原因是你用的ADO   for   ORACLE的驱动是微软的Microsoft OLE DB ...