# -*- encoding: utf-8 -*-
"""
@File    : UserDataController.py
@Time    : 2023/6/29 14:56
@Author  : peng
@Email   : zhangdongming@asj6.wecom.work
@Software: PyCharm
"""

from django.db.models import Q, Sum
from django.views.generic.base import View

from Model.models import UnicomDeviceInfo, UnicomComboOrderInfo
from Service.CommonService import CommonService
from Object.RedisObject import RedisObject


class UnicomDataView(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_code, user_id, response = CommonService.verify_token_get_user_id(request_dict, request)
        if token_code != 0:
            return response.json(token_code)
        if operation == 'getFlowInfo':  # 查询流量使用情况
            return self.get_flow_info(request_dict, response)
        elif operation == 'getMonthlyFlowInfo':  # 查询流量使用情况
            return self.get_monthly_flow_info(request_dict, response)
        else:
            return response.json(414)

    @classmethod
    def get_flow_info(cls, request_dict, response):
        """
        查询流量使用情况
        @param request_dict: 请求数据
        @request_dict year: 年
        @request_dict month: 月
        @request_dict query_type: 查询流量类型
        @request_dict iccid: 卡号
        @request_dict user_id: 用户id
        @request_dict page: 查询分页数
        @request_dict line: 查询条数
        @param response:响应对象
        @return:
        """
        query_type = request_dict.get('query_type', None)
        iccid = request_dict.get('iccid', None)
        user_id = request_dict.get('user_id', None)
        year = request_dict.get('year', None)
        month = request_dict.get('month', None)
        page = request_dict.get('page', None)
        line = request_dict.get('line', None)
        if not all([query_type, page, line]):
            return response.json(444)
        page = int(page)
        line = int(line)
        redis_obj = RedisObject()
        iccid_list = []
        try:
            if query_type == '1':  # 查询联通测试流量信息
                iccid_qs = UnicomComboOrderInfo.objects.all()
                iccid_qs = iccid_qs.filter(combo__combo_type=1)
                if user_id:
                    user_qs = UnicomDeviceInfo.objects.filter(user_id=user_id).values('iccid')
                    if not user_qs.exists():
                        return response.json(173)
                    iccid_qs = iccid_qs.filter(iccid=user_qs[0]['iccid'])
                if iccid:
                    iccid_qs = iccid_qs.filter(iccid=iccid)
                iccid_qs = iccid_qs.values('iccid', 'flow_total_usage')
                total_flow = iccid_qs.aggregate(total=Sum('flow_total_usage'))['total']
                count = iccid_qs.count()
                iccid_qs = iccid_qs[(page - 1) * line:page * line]
                for item in iccid_qs:
                    user_qs = UnicomDeviceInfo.objects.filter(iccid=item['iccid']).values('user_id')
                    item['user_id'] = user_qs[0]['user_id'] if user_qs.exists() else ''
                    iccid_list.append(item)
            elif query_type == '2':  # 查询联通月度流量信息
                iccid_qs = UnicomDeviceInfo.objects.filter(card_type=0)
                if iccid:
                    iccid_qs = iccid_qs.filter(iccid=iccid)
                if user_id:
                    iccid_qs = iccid_qs.filter(user_id=user_id)
                iccid_qs = iccid_qs.values('iccid').distinct().order_by('iccid')
                count = iccid_qs.count()
                iccid_qs = iccid_qs[(page - 1) * line:page * line]
                total_flow = 0
                for item in iccid_qs:
                    item['flow_total_usage'] = 0
                    key = 'monthly_flow_' + item['iccid']
                    user_qs = UnicomDeviceInfo.objects.filter(iccid=item['iccid']).values('user_id')
                    item['user_id'] = user_qs[0]['user_id'] if user_qs.exists() else ''
                    if year and month:
                        file = year + '-' + month
                        flow = redis_obj.get_hash_data(key, file)
                        flow = float(flow) if flow else 0
                        item['flow_total_usage'] = flow
                    else:
                        flow_dict = redis_obj.get_all_hash_data(key)
                        for k, v in flow_dict.items():
                            item['flow_total_usage'] += float(v)
                    iccid_list.append(item)
                    item['flow_total_usage'] = round(item['flow_total_usage'], 2)
                    total_flow += float(item['flow_total_usage'])
            else:
                return response.json(444)
            res = {
                'iccidInfo': iccid_list,
                'totalFlow': round(total_flow, 2),
                'count': count
            }
            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def get_monthly_flow_info(cls, request_dict, response):
        """
        查询月度流量使用情况
        @param request_dict: 请求数据
        @request_dict year: 年
        @request_dict month: 月
        @request_dict iccid: 卡号
        @param response:响应对象
        @return:
        """
        iccid = request_dict.get('iccid', None)
        year = request_dict.get('year', None)
        month = request_dict.get('month', None)
        page = request_dict.get('page', None)
        line = request_dict.get('line', None)
        redis_obj = RedisObject()
        data_list = []
        if not all([iccid, page, line]):
            return response.json(444)
        page = int(page)
        line = int(line)
        try:
            key = 'monthly_flow_' + iccid
            if year and month:
                file = year + '-' + month
                flow = redis_obj.get_hash_data(key, file)
                flow = flow if flow else 0
                data_list.append({file: flow})
            else:
                flow_dict = redis_obj.get_all_hash_data(key)
                for k, v in flow_dict.items():
                    data_list.append({k.decode(): float(v)})
            res = {
                'list': data_list[(page - 1) * line:page * line],
                'count': len(data_list)
            }
            return response.json(0, res)
        except Exception as e:
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
