# -*- encoding: utf-8 -*-
"""
@File    : CloudServiceController.py
@Time    : 2022/12/8 10:23
@Author  : stephen
@Email   : zhangdongming@asj6.wecom.work
@Software: PyCharm
"""
import time

from django.views import View

from Model.models import CouponCombo, CouponModel
from Object.ResponseObject import ResponseObject
from Object.TokenObject import TokenObject


class CloudServiceController(View):

    def get(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        return self.validation(request.GET, request, operation)

    def post(self, request, *args, **kwargs):
        request.encoding = 'utf-8'
        operation = kwargs.get('operation')
        return self.validation(request.POST, request, operation)

    def validation(self, request_dict, request, operation):
        token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
        lang = request_dict.get('lang', token.lang)
        response = ResponseObject(lang)
        if token.code != 0:
            return response.json(token.code)
        user_id = token.userID
        if operation == 'save-photo':
            return self.get_combo_list_by_coupon(user_id, response)
        else:
            return response.json(404)

    @classmethod
    def get_combo_list_by_coupon(cls, request_dict, response):
        """
        根据优惠券获取套餐列表
        @param request_dict: couponId 优惠券ID
        @param request_dict: couponType 优惠券类型
        @param response: 响应结果
        """
        coupon_id = int(request_dict.get('couponId', 0))
        coupon_type = int(request_dict.get('couponType', 0))
        if coupon_id == 0:
            return response.json(444)
        coupon_combo_qs = CouponCombo.objects.filter(coupon_type=coupon_type, coupon_id=coupon_id).values('combo_id')
        combo_list = []
        if not coupon_combo_qs.exists():
            return combo_list
        for item in coupon_combo_qs:
            combo_list.append(item['combo_id'])
        return combo_list

    @classmethod
    def get_user_coupon_list(cls, user_id):
        """
        获取用户未使用优惠券配置ID列表
        @param user_id:用户id
        @return: coupon_qs
        """
        now_time = int(time.time())
        coupon_qs = CouponModel.objects.filter(
            userID=user_id,
            use_status=0,
            distribute_time__lte=now_time,
            valid_time__gt=now_time
        ).values('coupon_config__id')
        return coupon_qs

    @classmethod
    def get_combo_list(cls, coupon_type, coupon_id):
        """
        根据优惠券类型与优惠券ID，获取关联套餐列表
        @param coupon_type: 优惠券类型
        @param coupon_id: 套餐id
        @return: coupon_list
        """
        combo_qs = CouponCombo.objects.filter(coupon_type=coupon_type, coupon_id=coupon_id) \
            .values('combo_id')
        combo_list = []
        if not combo_qs.exists():
            return combo_list
        for item in combo_qs:
            combo_list.append(item['combo_id'])
        return combo_list

    @classmethod
    def get_coupon_list(cls, coupon_type, combo_id):
        """
        根据优惠券类型与套餐ID，获取关联优惠券列表
        @param coupon_type: 优惠券类型
        @param combo_id: 套餐id
        @return: coupon_list
        """
        coupon_combo_qs = CouponCombo.objects.filter(coupon_type=coupon_type, combo_id=combo_id) \
            .values('coupon_id')
        coupon_list = []
        if not coupon_combo_qs.exists():
            return coupon_list
        for item in coupon_combo_qs:
            coupon_list.append(item['coupon_id'])
        return coupon_list
