博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
qhfl-7 结算中心
阅读量:6202 次
发布时间:2019-06-21

本文共 12591 字,大约阅读时间需要 41 分钟。

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

 

"""前端传过来数据 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 jsonimport redisfrom rest_framework.views import APIViewfrom rest_framework.response import Responsefrom utils.base_response import BaseResponsefrom utils.redis_pool import POOLfrom django.utils.timezone import nowfrom utils.my_auth import LoginAuthfrom .views import SHOPPINGCAR_KEYfrom .models import CouponRecordCONN = redis.Redis(connection_pool=POOL)SETTLEMENT_KEY = "SETTLEMENT_%s_%s"GLOBAL_COUPON_KEY = "GLOBAL_COUPON_%s""""前端传过来数据 course_listredis 中存的数据 {    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

转载于:https://www.cnblogs.com/wenyule/p/10456881.html

你可能感兴趣的文章
virtualbox 安装debian后安装增强功能
查看>>
MySQL优化方向&思路
查看>>
大一统的Netfilter-一种Linux防火墙优化方法
查看>>
Linux用户,组及权限管理
查看>>
Cisco asa5510 防火墙限制局域网内每个IP的速度
查看>>
HibernateException: No Session found for current thread
查看>>
OpenStack云平台的网络模式及其工作机制
查看>>
突破LVS瓶颈,LVS Cluster部署(OSPF + LVS)
查看>>
Java技术驿站
查看>>
为什么SQL Server数据文件和日志文件最后更新日期不准?
查看>>
dell服务器虚拟化平台应用【我身边的戴尔企业级解决方案】
查看>>
Freemarker 的 Eclipse 插件 安装
查看>>
84天平美女征婚【非诚勿扰】
查看>>
深入浅出之-route命令实战使用指南
查看>>
安装OpenStack ValueError: Tables "migrate_version" have non utf8 co
查看>>
后台登陆框post注入实战
查看>>
python安装pip (windows64)
查看>>
OEL6.1下oracle 11gr2 ASM安装
查看>>
Firewalld防火墙
查看>>
linux防火墙iptables详细教程
查看>>