Эх сурвалжийг харах

首页数据、销售额数据查询
优化全球数据查询,封装获取域名函数

peng 3 жил өмнө
parent
commit
9a123b366d

+ 8 - 30
AdminController/dataSystemManagement/BusinessDataController.py

@@ -11,7 +11,7 @@ import requests
 from django.db.models import Q, Count, Sum
 from django.views.generic.base import View
 
-from Model.models import VodHlsModel, VideoPlaybackTimeModel, Device_Info
+from Model.models import VodHlsModel, VideoPlaybackTimeModel
 from Service.CommonService import CommonService
 
 
@@ -36,8 +36,6 @@ class BusinessDataView(View):
             return self.query_device_vod_business(request_dict, response)
         elif operation == 'global/vodData':  # 查询全球设备云存储数据
             return self.query_global_device_vod_business(request, request_dict, response)
-        elif operation == 'test':  # 查询全球设备云存储数据
-            return self.test(request_dict, response)
         else:
             return response.json(414)
 
@@ -91,12 +89,7 @@ class BusinessDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/businessData/vodData'
-        as_url = 'https://www.zositecha.com/dataManagement/businessData/vodData'
-        na_url = 'https://www.dvema.com/dataManagement/businessData/vodData'
-        eu_url = 'https://www.zositeche.com/dataManagement/businessData/vodData'
-        local_url = 'http://127.0.0.1:8000/dataManagement/businessData/vodData'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -104,10 +97,14 @@ class BusinessDataView(View):
             device_count = 0
             vod_list = []
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
-                device_count += int(result['result']['deviceCount'])
-                vod_list += result['result']['vodData']
+                if result['result_code'] == 0:
+                    device_count += int(result['result']['deviceCount'])
+                    vod_list += result['result']['vodData']
+                else:
+                    return response.json(result['result_code'])
             res = {
                 'deviceCount': device_count,
                 'vodData': vod_list
@@ -115,22 +112,3 @@ class BusinessDataView(View):
             return response.json(0, res)
         except Exception as e:
             return response.json(500, repr(e))
-
-    @classmethod
-    def test(cls, request_dict, response):
-        """
-        查询全球设备云存储数据
-        @param request_dict:请求参数
-        @param response:响应对象
-        @return:
-        """
-
-        try:
-            a = Device_Info.objects.all().values('Type').annotate(count=Count('Type')).order_by('-count')
-            b = Device_Info.objects.all().values('Type').annotate(count=Count('Type', distinct=True)).order_by('-count')
-            for item in a:
-                c = Device_Info.objects.filter(Type=item['Type']).values('UID').annotate(
-                    count=Count('UID', distinct=True)).order_by('-count').count()
-            return response.json(0)
-        except Exception as e:
-            return response.json(500, repr(e))

+ 228 - 0
AdminController/dataSystemManagement/HomeDataController.py

@@ -0,0 +1,228 @@
+# -*- 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 Q, Count, Sum
+from django.views.generic.base import View
+
+from Model.models import VideoPlaybackTimeModel, Device_User, Device_Info, Order_Model
+from Service.CommonService import CommonService
+
+
+# 业务数据
+class HomeDataView(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 == 'allData':  # 查询首页数据
+            return self.query_all_data(response)
+        elif operation == 'salesVolume':  # 查询销售额数据
+            return self.query_sales_volume_data(request_dict, response)
+        elif operation == 'global/allData':  # 查询全球首页数据
+            return self.query_global_all_data(request, request_dict, response)
+        elif operation == 'global/salesVolume':  # 查询全球销售额数据
+            return self.query_global_sales_volume_data(request, request_dict, response)
+        else:
+            return response.json(414)
+
+    @classmethod
+    def query_all_data(cls, response):
+        """
+        查询首页数据
+        @param response:响应对象
+        @return:
+        """
+        end_time = datetime.datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
+        start_time = end_time + datetime.timedelta(days=-1)
+        end_time_stamp = CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S'))
+        start_time_stamp = CommonService.str_to_timestamp(start_time.strftime('%Y-%m-%d %H:%M:%S'))
+        try:
+            user_increase_count = Device_User.objects.filter(data_joined__range=(start_time, end_time)).count()
+            user_active_count = Device_User.objects.filter(last_login__range=(start_time, end_time)).count()
+            user_all_count = Device_User.objects.filter(data_joined__lte=end_time).count()
+            device_increase_count = Device_Info.objects.filter(data_joined__range=(start_time, end_time)).values(
+                'UID').distinct().count()
+            device_active_count = VideoPlaybackTimeModel.objects.filter(
+                startTime__range=(start_time_stamp, end_time_stamp)).values('uid').distinct().count()
+            device_all_count = Device_Info.objects.filter(data_joined__lte=end_time).values('UID').distinct().count()
+            order_qs = Order_Model.objects.filter(status=1, addTime__range=(start_time_stamp, end_time_stamp))
+            order_total = order_qs.aggregate(total=Sum('price'))['total']
+            vod_order_total = order_qs.filter(order_type=0).aggregate(total=Sum('price'))['total']
+            ai_order_total = order_qs.filter(order_type=1).aggregate(total=Sum('price'))['total']
+            unicom_order_total = order_qs.filter(order_type=2).aggregate(total=Sum('price'))['total']
+            order_all_qs = Order_Model.objects.filter(status=1, addTime__lte=end_time_stamp)
+            order_all_total = order_all_qs.aggregate(total=Sum('price'))['total']
+            vod_order_all_total = order_all_qs.filter(order_type=0).aggregate(total=Sum('price'))['total']
+            ai_order_all_total = order_all_qs.filter(order_type=1).aggregate(total=Sum('price'))['total']
+            unicom_order_all_total = order_all_qs.filter(order_type=2).aggregate(total=Sum('price'))['total']
+            res = {
+                'userIncreaseCount': user_increase_count,
+                'userActiveCount': user_active_count,
+                'userAllCount': user_all_count,
+                'deviceIncreaseCount': device_increase_count,
+                'deviceActiveCount': device_active_count,
+                'deviceAllCount': device_all_count,
+                'orderTotal': round(order_total, 2) if order_total else 0,
+                'vodOrderTotal': round(vod_order_total, 2) if vod_order_total else 0,
+                'aiOrderTotal': round(ai_order_total, 2) if ai_order_total else 0,
+                'unicomOrderTotal': round(unicom_order_total, 2) if unicom_order_total else 0,
+                'orderAllTotal': round(order_all_total, 2) if order_all_total else 0,
+                'vodOrderAllTotal': round(vod_order_all_total, 2) if vod_order_all_total else 0,
+                'aiOrderAllTotal': round(ai_order_all_total, 2) if ai_order_all_total else 0,
+                'unicomOrderAllTotal': round(unicom_order_all_total, 2) if unicom_order_all_total else 0,
+            }
+            return response.json(0, res)
+        except Exception as e:
+            return response.json(500, repr(e))
+
+    @classmethod
+    def query_sales_volume_data(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)
+        try:
+            order_qs = Order_Model.objects.filter(addTime__range=(start_time, end_time))
+            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)
+            order_list = []
+            for item in time_list:
+                total = order_qs.filter(addTime__range=item).aggregate(total=Sum('price'))['total']
+                res = {
+                    'total': round(total, 2) if total else 0,
+                    'startTime': item[0],
+                    'endTime': item[1]
+                }
+                order_list.append(res)
+            return response.json(0, order_list)
+        except Exception as e:
+            return response.json(500, repr(e))
+
+    @classmethod
+    def query_global_all_data(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_increase_count = 0
+            user_active_count = 0
+            user_all_count = 0
+            device_increase_count = 0
+            device_active_count = 0
+            device_all_count = 0
+            order_total = 0
+            vod_order_total = 0
+            ai_order_total = 0
+            unicom_order_total = 0
+            order_all_total = 0
+            vod_order_all_total = 0
+            ai_order_all_total = 0
+            unicom_order_all_total = 0
+            for url in url_list:
+                url = url + request.path
+                res = requests.get(url=url, params=request_dict, headers=headers)
+                result = res.json()
+                user_increase_count += result['result']['userIncreaseCount']
+                user_active_count += result['result']['userActiveCount']
+                user_all_count += result['result']['userAllCount']
+                device_increase_count += result['result']['deviceIncreaseCount']
+                device_active_count += result['result']['deviceActiveCount']
+                device_all_count += result['result']['deviceAllCount']
+                order_total += result['result']['orderTotal']
+                vod_order_total += result['result']['vodOrderTotal']
+                ai_order_total += result['result']['aiOrderTotal']
+                unicom_order_total += result['result']['unicomOrderTotal']
+                order_all_total += result['result']['orderAllTotal']
+                vod_order_all_total += result['result']['vodOrderAllTotal']
+                ai_order_all_total += result['result']['aiOrderAllTotal']
+                unicom_order_all_total += result['result']['unicomOrderAllTotal']
+            res = {
+                'userIncreaseCount': user_increase_count,
+                'userActiveCount': user_active_count,
+                'userAllCount': user_all_count,
+                'deviceIncreaseCount': device_increase_count,
+                'deviceActiveCount': device_active_count,
+                'deviceAllCount': device_all_count,
+                'orderTotal': order_total,
+                'vodOrderTotal': vod_order_total,
+                'aiOrderTotal': ai_order_total,
+                'unicomOrderTotal': unicom_order_total,
+                'orderAllTotal': order_all_total,
+                'vodOrderAllTotal': vod_order_all_total,
+                'aiOrderAllTotal': ai_order_all_total,
+                'unicomOrderAllTotal': unicom_order_all_total,
+            }
+            return response.json(0, res)
+        except Exception as e:
+            return response.json(500, repr(e))
+
+    @classmethod
+    def query_global_sales_volume_data(cls, request, request_dict, response):
+        """
+        查询全球销售额数据
+        @param request:请求
+        @param request_dict:请求参数
+        @param response:响应对象
+        @return:
+        """
+        ap_url = 'https://www.zositechc.cn/dataManagement/homeData/salesVolume'
+        as_url = 'https://www.zositecha.com/dataManagement/homeData/salesVolume'
+        na_url = 'https://www.dvema.com/dataManagement/homeData/salesVolume'
+        eu_url = 'https://www.zositeche.com/dataManagement/homeData/salesVolume'
+        local_url = 'http://127.0.0.1:8000/dataManagement/homeData/salesVolume'
+        url_list = [local_url]
+        try:
+            headers = {
+                'Authorization': request.META.get('HTTP_AUTHORIZATION')
+            }
+            order_list = []
+            for url in url_list:
+                res = requests.get(url=url, params=request_dict, headers=headers)
+                result = res.json()
+                for item in result['result']:
+                    flag = 0
+                    for each in order_list:
+                        if item['startTime'] == each['startTime'] and item['endTime'] == each['endTime']:
+                            each['total'] += item['total']
+                            break
+                    if flag == 0:
+                        order_list.append(item)
+            return response.json(0, order_list)
+        except Exception as e:
+            return response.json(500, repr(e))

+ 8 - 25
AdminController/dataSystemManagement/ServiceDataController.py

@@ -436,13 +436,7 @@ class ServiceDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/serviceData/payOrder'
-        as_url = 'https://www.zositecha.com/dataManagement/serviceData/payOrder'
-        na_url = 'https://www.dvema.com/dataManagement/serviceData/payOrder'
-        eu_url = 'https://www.zositeche.com/dataManagement/serviceData/payOrder'
-        local_url = 'http://127.0.0.1:8000/dataManagement/serviceData/payOrder'
-        url_list = [local_url]
-
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -456,6 +450,7 @@ class ServiceDataView(View):
             store_meal_list = []
             store_meal_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:
@@ -540,12 +535,7 @@ class ServiceDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/serviceData/freeOrder'
-        as_url = 'https://www.zositecha.com/dataManagement/serviceData/freeOrder'
-        na_url = 'https://www.dvema.com/dataManagement/serviceData/freeOrder'
-        eu_url = 'https://www.zositeche.com/dataManagement/serviceData/freeOrder'
-        local_url = 'http://127.0.0.1:8000/dataManagement/serviceData/freeOrder'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -556,6 +546,7 @@ class ServiceDataView(View):
             device_type_list = []
             device_type_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:
@@ -619,12 +610,7 @@ class ServiceDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/serviceData/firstPayOrder'
-        as_url = 'https://www.zositecha.com/dataManagement/serviceData/firstPayOrder'
-        na_url = 'https://www.dvema.com/dataManagement/serviceData/firstPayOrder'
-        eu_url = 'https://www.zositeche.com/dataManagement/serviceData/firstPayOrder'
-        local_url = 'http://127.0.0.1:8000/dataManagement/serviceData/firstPayOrder'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -635,6 +621,7 @@ class ServiceDataView(View):
             device_type_list = []
             device_type_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:
@@ -698,12 +685,7 @@ class ServiceDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/businessData/repeatPayOrder'
-        as_url = 'https://www.zositecha.com/dataManagement/businessData/repeatPayOrder'
-        na_url = 'https://www.dvema.com/dataManagement/businessData/repeatPayOrder'
-        eu_url = 'https://www.zositeche.com/dataManagement/businessData/repeatPayOrder'
-        local_url = 'http://127.0.0.1:8000/dataManagement/businessData/repeatPayOrder'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -716,6 +698,7 @@ class ServiceDataView(View):
             repeat_count = 0
             order_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:

+ 6 - 18
AdminController/dataSystemManagement/UserDataController.py

@@ -196,12 +196,7 @@ class UserDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/userData/increase'
-        as_url = 'https://www.zositecha.com/dataManagement/userData/increase'
-        na_url = 'https://www.dvema.com/dataManagement/userData/increase'
-        eu_url = 'https://www.zositeche.com/dataManagement/userData/increase'
-        local_url = 'http://127.0.0.1:8000/dataManagement/userData/increase'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -211,6 +206,7 @@ class UserDataView(View):
             region_list = []
             region_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:
@@ -259,12 +255,7 @@ class UserDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/userData/active'
-        as_url = 'https://www.zositecha.com/dataManagement/userData/active'
-        na_url = 'https://www.dvema.com/dataManagement/userData/active'
-        eu_url = 'https://www.zositeche.com/dataManagement/userData/active'
-        local_url = 'http://127.0.0.1:8000/dataManagement/userData/active'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -274,6 +265,7 @@ class UserDataView(View):
             region_list = []
             region_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, params=request_dict, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:
@@ -322,12 +314,7 @@ class UserDataView(View):
         @param response:响应对象
         @return:
         """
-        ap_url = 'https://www.zositechc.cn/dataManagement/userData/region'
-        as_url = 'https://www.zositecha.com/dataManagement/userData/region'
-        na_url = 'https://www.dvema.com/dataManagement/userData/region'
-        eu_url = 'https://www.zositeche.com/dataManagement/userData/region'
-        local_url = 'http://127.0.0.1:8000/dataManagement/userData/region'
-        url_list = [local_url]
+        url_list = CommonService.get_domain_name()
         try:
             headers = {
                 'Authorization': request.META.get('HTTP_AUTHORIZATION')
@@ -335,6 +322,7 @@ class UserDataView(View):
             user_list = []
             user_count = 0
             for url in url_list:
+                url = url + request.path
                 res = requests.get(url=url, headers=headers)
                 result = res.json()
                 if result['result_code'] == 0:

+ 2 - 1
Ansjer/server_urls/datasystem_url.py

@@ -9,11 +9,12 @@
 from django.urls import re_path
 
 from AdminController.dataSystemManagement import UserDataController, DeviceDataController, ServiceDataController, \
-    BusinessDataController
+    BusinessDataController, HomeDataController
 
 urlpatterns = [
     re_path(r'^userData/(?P<operation>.*)$', UserDataController.UserDataView.as_view()),
     re_path(r'^deviceData/(?P<operation>.*)$', DeviceDataController.DeviceDataView.as_view()),
     re_path(r'^serviceData/(?P<operation>.*)$', ServiceDataController.ServiceDataView.as_view()),
     re_path(r'^businessData/(?P<operation>.*)$', BusinessDataController.BusinessDataView.as_view()),
+    re_path(r'^homeData/(?P<operation>.*)$', HomeDataController.HomeDataView.as_view()),
 ]

+ 20 - 1
Service/CommonService.py

@@ -16,7 +16,8 @@ from django.core import serializers
 from django.utils import timezone
 from pyipip import IPIPDatabase
 
-from Ansjer.config import BASE_DIR, SERVER_DOMAIN_SSL, CONFIG_INFO, CONFIG_TEST, CONFIG_CN
+from Ansjer.config import BASE_DIR, SERVER_DOMAIN_SSL, CONFIG_INFO, CONFIG_TEST, CONFIG_CN, SERVER_DOMAIN_TEST, \
+    SERVER_DOMAIN_CN, SERVER_DOMAIN_US, CONFIG_US
 from Controller.CheckUserData import RandomStr
 from Model.models import iotdeviceInfoModel, Device_Info, CountryModel, RegionModel, UIDModel
 from Object.ResponseObject import ResponseObject
@@ -701,3 +702,21 @@ GCqvlyw5dfxNA+EtxNE2wCW/LW7ENJlACgcfgPlBZtpLheWoZB/maw4=
                           CommonService.str_to_timestamp(end_time.strftime('%Y-%m-%d %H:%M:%S')))
             time_list = [time_tuple]
         return time_list
+
+    @staticmethod
+    def get_domain_name():
+        """
+        按时间单位切割时间段
+        @param start_time: 开始时间
+        @param end_time: 结束时间
+        @param time_unit: 时间单位
+        @return: time_list 切割后的时间列表
+        """
+
+        if CONFIG_INFO == CONFIG_TEST:
+            domain_name_list = [SERVER_DOMAIN_TEST[:-1]]
+        elif CONFIG_INFO == CONFIG_CN or CONFIG_INFO == CONFIG_US:
+            domain_name_list = [SERVER_DOMAIN_US[:-1], SERVER_DOMAIN_CN[:-1]]
+        else:
+            domain_name_list = []
+        return domain_name_list