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

import datetime

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

from Model.models import DeviceInfoSummary
from Service.CommonService import CommonService


# 设备数据
class DeviceDataView(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 == 'type':  # 统计设备类型
            return self.type_statistics(response)
        if operation == 'regional':  # 设备地区分布
            return self.regional_statistics(response)
        if operation == 'addDevice':  # 查询设备增长数据(数据有些许差异)
            return self.add_device(request_dict, response)
        if operation == 'active':  # 设备活跃数据
            return self.device_active(request_dict, response)
        if operation == 'global/regional':  # 全球设备分布
            return self.global_regional(request, request_dict, response)
        if operation == 'global/type':  # 全球设备类型
            return self.golbal_type(request, request_dict, response)
        if operation == 'global/active':  # 全球设备活跃分布
            return self.golbal_active(request, request_dict, response)
        if operation == 'global/addDevice':  # 全球新增设备数据
            return self.golbal_add_device(request, request_dict, response)
        else:
            return response.json(414)

    @classmethod
    def golbal_add_device(cls, request, request_dict, response):
        """
        全球新增设备数据
        @param request:请求
        @param request_dict:请求参数
        @param response: 响应对象
        """
        url_list = CommonService.get_domain_name()
        try:
            headers = {
                'Authorization': request.META.get('HTTP_AUTHORIZATION')
            }
            device_list = []
            device_count = 0
            type_list = []
            type_count = 0
            region_list = []
            region_count = 0
            order_list = []
            order_count = 0
            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']['addDevice']:
                        flag = 0
                        for each in device_list:
                            if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
                                each['count'] += item['count']
                                device_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            device_list.append(item)
                            device_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']
                                region_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            region_list.append(item)
                            region_count += item['count']
                    for item in result['result']['type']:
                        flag = 0
                        for each in type_list:
                            if item['type'] == each['type']:
                                each['count'] += item['count']
                                type_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            type_list.append(item)
                            type_count += item['count']
                    for item in result['result']['version']:
                        flag = 0
                        for each in order_list:
                            if item['type'] == each['type']:
                                each['count'] += item['count']
                                order_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            order_list.append(item)
                            order_count += item['count']

                        if order_count != 0:
                            item['rate'] = round(item['count'] / order_count * 100, 2)
                        else:
                            break
                else:
                    return response.json(result['result_code'], result['result'])
            for item in device_list:
                item['rate'] = round(item['count'] / device_count * 100, 2) if device_count else 0
            for item in region_list:
                item['rate'] = round(item['count'] / region_count * 100, 2) if region_count else 0
            for item in type_list:
                item['rate'] = round(item['count'] / type_count * 100, 2) if type_count else 0
            for item in order_list:
                item['rate'] = round(item['count'] / order_count * 100, 2) if order_count else 0
            res = {
                'device': device_list,
                'type': CommonService.list_sort(type_list),
                'region': CommonService.list_sort(region_list),
                'version': CommonService.list_sort(order_list)
            }
            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def golbal_active(cls, request, request_dict, response):
        """
        全球设备活跃分布
        @param request:请求
        @param request_dict:请求参数
        @param response: 响应对象
        """
        url_list = CommonService.get_domain_name()
        try:
            headers = {
                'Authorization': request.META.get('HTTP_AUTHORIZATION')
            }
            type_list = []
            type_count = 0
            region_list = []
            region_count = 0
            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']['vodHls']:
                        flag = 0
                        for each in type_list:
                            if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
                                each['count'] += item['count']
                                type_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            type_list.append(item)
                            type_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']
                                region_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            region_list.append(item)
                            region_count += item['count']
                else:
                    return response.json(result['result_code'], result['result'])
            for item in region_list:
                item['rate'] = round(item['count'] / region_count * 100, 2) if region_count else 0
            for item in type_list:
                item['rate'] = round(item['count'] / type_count * 100, 2) if type_count else 0
            res = {
                'device': type_list,
                'region': CommonService.list_sort(region_list)
            }
            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def golbal_type(cls, request, request_dict, response):
        """
        全球设备类型分布
        @param request:请求
        @param request_dict:请求参数
        @param response: 响应对象
        """
        url_list = CommonService.get_domain_name()
        try:
            headers = {
                'Authorization': request.META.get('HTTP_AUTHORIZATION')
            }
            type_list = []
            type_count = 0
            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']['type']:
                        flag = 0
                        for each in type_list:
                            if item['type'] == each['type']:
                                each['count'] += item['count']
                                type_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            type_list.append(item)
                            type_count += item['count']
                else:
                    return response.json(result['result_code'], result['result'])
            for item in type_list:
                item['rate'] = round(item['count'] / type_count * 100, 2) if type_count else 0
            res = {
                'type': CommonService.list_sort(type_list)
            }
            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500)

    @classmethod
    def global_regional(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')
            }
            device_list = []
            device_count = 0
            region_list = []
            region_count = 0
            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']['countries']:
                        flag = 0
                        for each in device_list:
                            if each['countryName'] == item['countryName']:
                                each['count'] += int(item['count'])
                                device_count += int(item['count'])
                                flag = 1
                                break
                        if flag == 0:
                            device_list.append(item)
                            device_count += int(item['count'])
                    for item in result['result']['continent']:
                        flag = 0
                        for each in region_list:
                            if each['continentName'] == item['continentName']:
                                each['count'] += item['count']
                                region_count += item['count']
                                flag = 1
                                break
                        if flag == 0:
                            region_list.append(item)
                            region_count += item['count']
                else:
                    return response.json(result['result_code'], result['result'])
            for item in device_list:
                item['rate'] = round(item['count'] / device_count * 100, 2) if device_count else 0
            for item in region_list:
                item['rate'] = round(item['count'] / region_count * 100, 2) if region_count else 0
            res = {
                'countries': CommonService.list_sort(device_list[:30]),
                'continent': 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 device_active(cls, request_dict, response):
        """
        设备活跃数据
        @param request_dict:请求参数
        @request_dict starTime:开始时间
        @request_dict endTime:结束时间
        @param response:响应对象
        """
        start_time = request_dict.get('startTime', None)  # 时间戳
        end_time = request_dict.get('endTime', None)
        unit_time = request_dict.get('timeUnit', None)
        if not all([start_time, end_time, unit_time]):
            return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
        s_time = datetime.datetime.fromtimestamp(int(start_time))
        e_time = datetime.datetime.fromtimestamp(int(end_time))
        time_list = CommonService.cutting_time(s_time, e_time, unit_time)
        try:
            device_info_summary_qs = DeviceInfoSummary.objects.filter(
                time__gte=start_time, time__lt=end_time, query_type=1).values('country', 'count')
            count_all = device_info_summary_qs.aggregate(total=Sum('count'))['total']
            count_all = count_all if count_all else 0
            video_list = []
            region_list = []
            region_dict = {}
            for item in device_info_summary_qs:
                region_temp_dict = eval(item['country'])
                for country, count in region_temp_dict.items():
                    if country in region_dict:
                        region_dict[country] += count
                    else:
                        region_dict[country] = count
            for item in time_list:
                deivce_type_qs = device_info_summary_qs.filter(time__gte=item[0], time__lt=item[1]).values('count')
                count = deivce_type_qs.aggregate(total=Sum('count'))['total']
                count = count if count else 0
                rate = round(count / count_all * 100, 2) if count_all else 0
                vod_dict = {
                    'count': count,
                    'rate': rate,
                    'startTime': item[0],
                    'endTime': item[1]
                }
                video_list.append(vod_dict)
            for country, count in region_dict.items():
                rate = round(count / count_all * 100, 2) if count_all else 0
                region_list.append({
                    'countryName': country,
                    'count': count,
                    'rate': rate
                })
            res = {
                'vodHls': video_list,
                'region': CommonService.list_sort(region_list)

            }
            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500)

    @classmethod
    def add_device(cls, request_dict, response):
        """
        查询设备增长数据
        @param request_dict:请求参数
        @request_dict starTime:开始时间
        @request_dict endTime:结束时间
        @param response:响应对象
        """
        start_time = request_dict.get('startTime', None)  # 时间戳
        end_time = request_dict.get('endTime', None)
        unit_time = request_dict.get('timeUnit', None)
        if not all([start_time, end_time, unit_time]):
            return response.json(444, {'error param': 'startTime or endTime or timeUnit'})
        try:
            device_info_qs = DeviceInfoSummary.objects.filter(time__gte=start_time, time__lt=end_time,
                                                              query_type=0).values(
                'count', 'country', 'device_type', 'vod_service')
            count_all = device_info_qs.aggregate(total=Sum('count'))['total']
            count_all = count_all if count_all else 0
            region_dict = {}
            type_dict = {}
            vod_dict = {}
            for item in device_info_qs:
                region_temp_dict = eval(item['country'])
                type_temp_dict = eval(item['device_type'])
                vod_temp_dict = eval(item['vod_service'])
                for k, v in region_temp_dict.items():
                    if k in region_dict:
                        region_dict[k] += v
                    else:
                        region_dict[k] = v
                for k, v in type_temp_dict.items():
                    if k in type_dict:
                        type_dict[k] += v
                    else:
                        type_dict[k] = v
                for k, v in vod_temp_dict.items():
                    if k in vod_dict:
                        vod_dict[k] += v
                    else:
                        vod_dict[k] = v
            # 统计该时间段的设备数量（已去重）
            info_list = []
            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, unit_time)
            for item in time_list:
                device_uid_qs = device_info_qs.filter(time__gte=item[0], time__lt=item[1])
                count = device_uid_qs.aggregate(total=Sum('count'))['total']
                count = count if count else 0
                rate = round(count / count_all * 100, 2) if count_all else 0
                info_dict = {
                    'startTime': item[0],
                    'endTime': item[1],
                    'count': count,
                    'rate': rate
                }
                info_list.append(info_dict)
            # 统计地区设备数量
            region_list = []
            for x, y in region_dict.items():
                rate = round(y / count_all * 100, 2) if count_all else 0
                region_list.append({
                    'countryName': x,
                    'count': y,
                    'rate': rate
                })
            # 统计设备类型数量
            type_list = []
            for x, y in type_dict.items():
                rate = round(y / count_all * 100, 2) if count_all else 0
                type_list.append({
                    'type': x,
                    'count': y,
                    'rate': rate
                })
            # 云存版本数量
            vod_list = []
            for x, y in vod_dict.items():
                rate = round(y / count_all * 100, 2) if count_all else 0
                vod_list.append({
                    'type': x,
                    'count': y,
                    'rate': rate
                })
            res = {
                'addDevice': info_list,
                'region': CommonService.list_sort(region_list),
                'type': CommonService.list_sort(type_list),
                'version': CommonService.list_sort(vod_list)
            }
            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

    @classmethod
    def regional_statistics(cls, response):
        """
        统计地区设备数量
        @param response:响应对象
        """
        all_device_qs = DeviceInfoSummary.objects.filter(query_type=0).values('continent', 'count', 'country')
        country_count = all_device_qs.aggregate(total=Sum('count'))['total']
        try:
            continent_list = []
            country_list = []
            continent_dict = {}
            country_dict = {}
            for item in all_device_qs:
                country_temp_dict = eval(item['country'])
                continent_temp_dict = eval(item['continent'])
                for x, y in country_temp_dict.items():
                    if x in country_dict:
                        country_dict[x] += y
                    else:
                        country_dict[x] = y
                for x, y in continent_temp_dict.items():
                    if x in continent_dict:
                        continent_dict[x] += y
                    else:
                        continent_dict[x] = y
            # 地区设备量前30
            for country, count in country_dict.items():
                rate = round(count / country_count * 100, 2) if country_count else 0
                country_list.append({
                    'countryName': country,
                    'count': count,
                    'rate': rate
                })
            for continent, count in continent_dict.items():
                rate = round(count / country_count * 100, 2) if country_count else 0
                continent_list.append({
                    'continentName': continent,
                    'count': count,
                    'rate': rate
                })
            res = {
                'countries': CommonService.list_sort(country_list),
                'continent': CommonService.list_sort(continent_list)
            }

            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500)

    @classmethod
    def type_statistics(cls, response):
        """
        统计设备类型
        @param response:响应对象
        @return:
        """
        all_device_qs = DeviceInfoSummary.objects.filter(query_type=0).values('device_type', 'count')
        if not all_device_qs.exists():
            return response.json(173)
        count_all = all_device_qs.aggregate(total=Sum('count'))['total']
        try:
            device_type_list = []
            device_type_dict = {}
            for item in all_device_qs:
                country_temp_dict = eval(item['device_type'])
                for k, v in country_temp_dict.items():
                    if k in device_type_dict:
                        device_type_dict[k] += v
                    else:
                        device_type_dict[k] = v
            for x, y in device_type_dict.items():
                rate = round(y / count_all * 100, 2) if count_all else 0
                device_type_list.append({
                    'type': x,
                    'count': y,
                    'rate': rate
                })
            res = {
                'type': CommonService.list_sort(device_type_list)
            }
            return response.json(0, res)
        except Exception as e:
            print(e)
            return response.json(500)
