热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

qhfl-7结算中心

结算中心,即从购物车前往支付前的确认页面,这里要开始选择优惠券了前端传过来数据course_list课程列表redis中将要存放的结算数据{set

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

 

"""
前端传过来数据 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加入结算中心

模拟测试接口

image

查看结算中心

 

 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)
获取结算信息

image

 

更新结算中心接口

 

在结算时,如果有更改课程的优惠券,或全局优惠券时,需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)
更新结算信息

 

image

更新后查看结算的结果

image

 

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

推荐阅读
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 第四章高阶函数(参数传递、高阶函数、lambda表达式)(python进阶)的讲解和应用
    本文主要讲解了第四章高阶函数(参数传递、高阶函数、lambda表达式)的相关知识,包括函数参数传递机制和赋值机制、引用传递的概念和应用、默认参数的定义和使用等内容。同时介绍了高阶函数和lambda表达式的概念,并给出了一些实例代码进行演示。对于想要进一步提升python编程能力的读者来说,本文将是一个不错的学习资料。 ... [详细]
  • Python SQLAlchemy库的使用方法详解
    本文详细介绍了Python中使用SQLAlchemy库的方法。首先对SQLAlchemy进行了简介,包括其定义、适用的数据库类型等。然后讨论了SQLAlchemy提供的两种主要使用模式,即SQL表达式语言和ORM。针对不同的需求,给出了选择哪种模式的建议。最后,介绍了连接数据库的方法,包括创建SQLAlchemy引擎和执行SQL语句的接口。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • RouterOS 5.16软路由安装图解教程
    本文介绍了如何安装RouterOS 5.16软路由系统,包括系统要求、安装步骤和登录方式。同时提供了详细的图解教程,方便读者进行操作。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文整理了315道Python基础题目及答案,帮助读者检验学习成果。文章介绍了学习Python的途径、Python与其他编程语言的对比、解释型和编译型编程语言的简述、Python解释器的种类和特点、位和字节的关系、以及至少5个PEP8规范。对于想要检验自己学习成果的读者,这些题目将是一个不错的选择。请注意,答案在视频中,本文不提供答案。 ... [详细]
author-avatar
化合价steuart_968
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有