# -*- encoding: utf-8 -*-
"""
@File    : UserDataController.py
@Time    : 2022/8/16 10:44
@Author  : peng
@Email   : zhangdongming@asj6.wecom.work
@Software: PyCharm
"""

from django.db.models import Count, Sum
from django.views.generic.base import View
import datetime
import requests

from Model.models import DeviceUserSummary
from Service.CommonService import CommonService


# 用户数据
class UserDataView(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 == 'increase':  # 查询用户新增数据
            return self.user_increase(request_dict, response)
        elif operation == 'active':  # 查询用户活跃数据
            return self.user_active(request_dict, response)
        elif operation == 'region':  # 查询用户地区分布
            return self.user_region(response)
        elif operation == 'global/increase':  # 查询全球用户新增数据
            return self.global_user_increase(request, request_dict, response)
        elif operation == 'global/active':  # 查询全球用户活跃数据
            return self.global_user_active(request, request_dict, response)
        elif operation == 'global/region':  # 查询全球用户地区分布
            return self.global_user_region(request, response)
        else:
            return response.json(414)

    @classmethod
    def user_increase(cls, request_dict, response):
        """
        查询用户增长数据
        @param request_dict:请求参数
        @request_dict startTime:开始时间
        @request_dict endTime:结束时间
        @request_dict timeUnit:时间单位
        @param response:响应对象
        @return:
        """
        start_time = request_dict.get('startTime', None)
        end_time = request_dict.get('endTime', None)
        time_unit = request_dict.get('timeUnit', None)
        if not all([start_time, end_time, time_unit]):
            return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
        try:
            all_user_qs = DeviceUserSummary.objects.filter(time__gte=start_time, time__lt=end_time,
                                                           query_type=0).values('count', 'country')
            count = all_user_qs.aggregate(total=Sum('count'))['total']
            count = count if count else 0
            # 处理用户地区
            region_dict = {}
            region_list = []
            for item in all_user_qs:
                country_temp_dict = eval(item['country'])
                for k, v in country_temp_dict.items():
                    if k in region_dict:
                        region_dict[k] += v
                    else:
                        region_dict[k] = v
            for k, v in region_dict.items():
                rate = round(v / count * 100, 2) if count else 0
                region_list.append({
                    'countryName': k,
                    'count': v,
                    'rate': rate
                })
            # 处理时间段用户
            start_time = datetime.datetime.fromtimestamp(int(start_time))
            end_time = datetime.datetime.fromtimestamp(int(end_time))
            time_list = CommonService.cutting_time(start_time, end_time, time_unit)
            user_list = []
            for item in time_list:
                user_qs = all_user_qs.filter(time__gte=item[0], time__lt=item[1]).values('count')
                temp_count = user_qs.aggregate(total=Sum('count'))['total']
                temp_count = temp_count if temp_count else 0
                rate = round(temp_count / count * 100, 2) if count else 0
                user_dict = {
                    'count': temp_count,
                    'rate': rate,
                    'startTime': item[0],
                    'endTime': item[1]
                }
                user_list.append(user_dict)

            res = {
                'user': user_list,
                'region': region_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)))

    @classmethod
    def user_active(cls, request_dict, response):
        """
        查询用户活跃数据
        @param request_dict:请求参数
        @request_dict startTime:开始时间
        @request_dict endTime:结束时间
        @request_dict timeUnit:时间单位
        @param response:响应对象
        @return:
        """
        start_time = request_dict.get('startTime', None)
        end_time = request_dict.get('endTime', None)
        time_unit = request_dict.get('timeUnit', None)
        if not all([start_time, end_time, time_unit]):
            return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
        try:
            all_user_qs = DeviceUserSummary.objects.filter(time__gte=start_time, time__lt=end_time,
                                                           query_type=1).values('count', 'country')
            count = all_user_qs.aggregate(total=Sum('count'))['total']
            count = count if count else 0
            # 处理用户地区
            region_dict = {}
            region_list = []
            for item in all_user_qs:
                country_temp_dict = eval(item['country'])
                for k, v in country_temp_dict.items():
                    if k in region_dict:
                        region_dict[k] += v
                    else:
                        region_dict[k] = v
            for k, v in region_dict.items():
                rate = round(v / count * 100, 2) if count else 0
                region_list.append({
                    'countryName': k,
                    'count': v,
                    'rate': rate
                })
            # 处理时间段用户
            start_time = datetime.datetime.fromtimestamp(int(start_time))
            end_time = datetime.datetime.fromtimestamp(int(end_time))
            time_list = CommonService.cutting_time(start_time, end_time, time_unit)
            user_list = []
            for item in time_list:
                user_qs = all_user_qs.filter(time__gte=item[0], time__lt=item[1]).values('count')
                temp_count = user_qs.aggregate(total=Sum('count'))['total']
                temp_count = temp_count if temp_count else 0
                rate = round(temp_count / count * 100, 2) if count else 0
                user_dict = {
                    'count': temp_count,
                    'rate': rate,
                    'startTime': item[0],
                    'endTime': item[1]
                }
                user_list.append(user_dict)
            res = {
                'user': user_list,
                'region': region_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)))

    @classmethod
    def user_region(cls, response):
        """
        查询用户地区分布
        @param response:响应对象
        @return:
        """

        try:
            user_qs = DeviceUserSummary.objects.filter(query_type=0).values('count', 'country', 'continent')
            count = user_qs.aggregate(total=Sum('count'))['total']
            count = count if count else 0
            continent_dict = {}
            country_dict = {}
            continent_list = []
            country_list = []
            for item in user_qs:
                country_temp_dict = eval(item['country'])
                continent_temp_dict = eval(item['continent'])
                for k, v in country_temp_dict.items():
                    if k in country_dict:
                        country_dict[k] += v
                    else:
                        country_dict[k] = v
                for k, v in continent_temp_dict.items():
                    if k in continent_dict:
                        continent_dict[k] += v
                    else:
                        continent_dict[k] = v
            # 处理用户国家
            for k, v in country_dict.items():
                rate = round(v / count * 100, 2) if count else 0
                country_list.append({
                    'countryName': k,
                    'count': v,
                    'rate': rate
                })
            # 处理用户大洲
            for k, v in continent_dict.items():
                rate = round(v / count * 100, 2) if count else 0
                continent_list.append({
                    'continentName': k,
                    'count': v,
                    'rate': rate
                })
            res = {
                'countries': country_list,
                'continent': continent_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)))

    @classmethod
    def global_user_increase(cls, request, request_dict, response):
        """
        查询全球用户新增数据
        @param request:请求
        @param request_dict:请求参数
        @param response:响应对象
        @return:
        """
        url_list = CommonService.get_domain_name()
        try:
            headers = {
                'Authorization': request.META.get('HTTP_AUTHORIZATION')
            }
            user_list = []
            user_count = 0
            region_list = []
            for url in url_list:
                url = url + request.path.replace('global/', '')
                res = requests.get(url=url, params=request_dict, headers=headers)
                result = res.json()
                if result['result_code'] == 0:
                    for item in result['result']['user']:
                        flag = 0
                        for each in user_list:
                            if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
                                each['count'] += item['count']
                                user_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            user_list.append(item)
                            user_count += item['count']

                    for item in result['result']['region']:
                        flag = 0
                        for each in region_list:
                            if item['countryName'] == each['countryName']:
                                each['count'] += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            region_list.append(item)
                else:
                    return response.json(result['result_code'], result['result'])
            for item in user_list:
                item['rate'] = round(item['count'] / user_count * 100, 2) if user_count else 0
            for item in region_list:
                item['rate'] = round(item['count'] / user_count * 100, 2) if user_count else 0
            res = {
                'user': user_list,
                'region': CommonService.list_sort(region_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)))

    @classmethod
    def global_user_active(cls, request, request_dict, response):
        """
        查询全球用户活跃数据
        @param request:请求
        @param request_dict:请求参数
        @param response:响应对象
        @return:
        """
        url_list = CommonService.get_domain_name()
        try:
            headers = {
                'Authorization': request.META.get('HTTP_AUTHORIZATION')
            }
            user_list = []
            user_count = 0
            region_list = []
            for url in url_list:
                url = url + request.path.replace('global/', '')
                res = requests.get(url=url, params=request_dict, headers=headers)
                result = res.json()
                if result['result_code'] == 0:
                    for item in result['result']['user']:
                        flag = 0
                        for each in user_list:
                            if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
                                each['count'] += item['count']
                                user_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            user_list.append(item)
                            user_count += item['count']

                    for item in result['result']['region']:
                        flag = 0
                        for each in region_list:
                            if item['countryName'] == each['countryName']:
                                each['count'] += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            region_list.append(item)

                else:
                    return response.json(result['result_code'], result['result'])
            for item in user_list:
                item['rate'] = round(item['count'] / user_count * 100, 2) if user_count else 0
            for item in region_list:
                item['rate'] = round(item['count'] / user_count * 100, 2) if user_count else 0
            res = {
                'user': user_list,
                'region': CommonService.list_sort(region_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)))

    @classmethod
    def global_user_region(cls, request, response):
        """
        查询全球用户地区分布
        @param request:请求
        @param response:响应对象
        @return:
        """
        url_list = CommonService.get_domain_name()
        try:
            headers = {
                'Authorization': request.META.get('HTTP_AUTHORIZATION')
            }
            country_list = []
            continent_list = []
            user_count = 0
            for url in url_list:
                url = url + request.path.replace('global/', '')
                res = requests.get(url=url, headers=headers)
                result = res.json()
                if result['result_code'] == 0:
                    # 处理地区
                    for item in result['result']['countries']:
                        flag = 0
                        for each in country_list:
                            if each['countryName'] == item['countryName']:
                                each['count'] += int(item['count'])
                                user_count += int(item['count'])
                                flag = 1
                                break
                        if flag == 0:
                            country_list.append(item)
                            user_count += int(item['count'])
                    for item in result['result']['continent']:
                        flag = 0
                        for each in continent_list:
                            if each['continentName'] == item['continentName']:
                                each['count'] += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            continent_list.append(item)
                else:
                    return response.json(result['result_code'], result['result'])
            for item in country_list:
                item['rate'] = round(item['count'] / user_count * 100, 2) if user_count else 0
            for item in continent_list:
                item['rate'] = round(item['count'] / user_count * 100, 2) if user_count else 0
            res = {
                'countries': CommonService.list_sort(country_list)[:20],
                'continent': CommonService.list_sort(continent_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)))
