瀏覽代碼

优化服务数据查询

peng 3 年之前
父節點
當前提交
f840b046af
共有 1 個文件被更改,包括 122 次插入85 次删除
  1. 122 85
      AdminController/dataSystemManagement/ServiceDataController.py

+ 122 - 85
AdminController/dataSystemManagement/ServiceDataController.py

@@ -92,24 +92,6 @@ class ServiceDataView(View):
                 }
                 }
                 order_list.append(order_dict)
                 order_list.append(order_dict)
 
 
-            # 区域订单统计
-            region_list = []
-            region_qs = order_qs.values('userID__region_country').annotate(count=Count('UID')).order_by('-count')
-            for item in region_qs:
-                country_id = item['userID__region_country']
-                country_qs = CountryModel.objects.filter(id=country_id).values('country_name')
-                country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
-                if count:
-                    rate = round(item['count'] / count * 100, 2)
-                else:
-                    rate = 0
-                region_dict = {
-                    'countryName': country_name,
-                    'count': item['count'],
-                    'rate': rate
-                }
-                region_list.append(region_dict)
-
             # 设备类型订单统计
             # 设备类型订单统计
             device_type_list = []
             device_type_list = []
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
@@ -122,8 +104,6 @@ class ServiceDataView(View):
                     if device_temp_qs[0]['Type'] not in uid_type_dict:
                     if device_temp_qs[0]['Type'] not in uid_type_dict:
                         uid_type_dict[device_temp_qs[0]['Type']] = []
                         uid_type_dict[device_temp_qs[0]['Type']] = []
                     uid_type_dict[device_temp_qs[0]['Type']].append(item['UID'])
                     uid_type_dict[device_temp_qs[0]['Type']].append(item['UID'])
-            # device_qs = Device_Info.objects.filter(UID__in=uid_list).values('Type').annotate(
-            #     count=Count('Type', distinct=True)).order_by('-count')
             for k, v in uid_type_dict.items():
             for k, v in uid_type_dict.items():
                 type_qs = DeviceTypeModel.objects.filter(type=k).values('name')
                 type_qs = DeviceTypeModel.objects.filter(type=k).values('name')
                 type_name = type_qs[0]['name'] if type_qs.exists() else '未知类型'
                 type_name = type_qs[0]['name'] if type_qs.exists() else '未知类型'
@@ -133,10 +113,7 @@ class ServiceDataView(View):
                 else:
                 else:
                     type_rate = 0
                     type_rate = 0
                 temp_total = order_qs.filter(UID__in=v).aggregate(total=Sum('price'))['total']
                 temp_total = order_qs.filter(UID__in=v).aggregate(total=Sum('price'))['total']
-                if total:
-                    total_rate = round(temp_total / total * 100, 2)
-                else:
-                    total_rate = 0
+                total_rate = round(temp_total / total * 100, 2)
                 device_temp_qs = {
                 device_temp_qs = {
                     'typeName': type_name,
                     'typeName': type_name,
                     'count': device_count,
                     'count': device_count,
@@ -146,6 +123,32 @@ class ServiceDataView(View):
                 }
                 }
                 device_type_list.append(device_temp_qs)
                 device_type_list.append(device_temp_qs)
 
 
+            # 区域订单统计
+            region_list = []
+            region_dict = {}
+            for item in uid_list:
+                uidset_qs = UidSetModel.objects.filter(uid=item).values('tb_country')
+                if uidset_qs.exists():
+                    country_qs = CountryModel.objects.filter(id=uidset_qs[0]['tb_country']).values('country_name')
+                    country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
+                else:
+                    country_name = '未知区域'
+                if country_name not in region_dict:
+                    region_dict[country_name] = []
+                region_dict[country_name].append(item)
+            for k, v in region_dict.items():
+                region_order_count = order_qs.filter(UID__in=v).count()
+                if count:
+                    rate = round(region_order_count / count * 100, 2)
+                else:
+                    rate = 0
+                region_temp_dict = {
+                    'countryName': k,
+                    'count': region_order_count,
+                    'rate': rate
+                }
+                region_list.append(region_temp_dict)
+
             # 套餐订单统计
             # 套餐订单统计
             store_meal_list = []
             store_meal_list = []
             if store_meal_type == 0:
             if store_meal_type == 0:
@@ -156,6 +159,7 @@ class ServiceDataView(View):
                 store_meal_qs = order_qs.values('unify_combo_id').annotate(count=Count('unify_combo_id')).order_by(
                 store_meal_qs = order_qs.values('unify_combo_id').annotate(count=Count('unify_combo_id')).order_by(
                     '-count')
                     '-count')
             for item in store_meal_qs:
             for item in store_meal_qs:
+                store_meal_total = store_meal_qs.aggregate(total=Sum('price'))['total']
                 if store_meal_type == 0:
                 if store_meal_type == 0:
                     store_meal_id = item['rank']
                     store_meal_id = item['rank']
                     store_meal_content_qs = store_meal_qs.filter(rank=store_meal_id, rank__lang__lang='cn').values(
                     store_meal_content_qs = store_meal_qs.filter(rank=store_meal_id, rank__lang__lang='cn').values(
@@ -179,10 +183,13 @@ class ServiceDataView(View):
                     rate = round(item['count'] / count * 100, 2)
                     rate = round(item['count'] / count * 100, 2)
                 else:
                 else:
                     rate = 0
                     rate = 0
+                total_rate = round(store_meal_total / total * 100, 2)
                 store_meal_dict = {
                 store_meal_dict = {
                     'storeMealId': store_meal_id,
                     'storeMealId': store_meal_id,
                     'count': item['count'],
                     'count': item['count'],
                     'storeMealName': store_meal_name,
                     'storeMealName': store_meal_name,
+                    'storeMealTotal': store_meal_total,
+                    'totalRate': total_rate,
                     'rate': rate
                     'rate': rate
                 }
                 }
                 store_meal_list.append(store_meal_dict)
                 store_meal_list.append(store_meal_dict)
@@ -247,29 +254,13 @@ class ServiceDataView(View):
                 }
                 }
                 order_list.append(order_dict)
                 order_list.append(order_dict)
 
 
-            # 区域订单统计
-            region_list = []
-            region_qs = order_qs.values('userID__region_country').annotate(count=Count('UID')).order_by('-count')
-            for item in region_qs:
-                country_id = item['userID__region_country']
-                country_qs = CountryModel.objects.filter(id=country_id).values('country_name')
-                country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
-                if count:
-                    rate = round(item['count'] / count * 100, 2)
-                else:
-                    rate = 0
-                region_dict = {
-                    'countryName': country_name,
-                    'count': item['count'],
-                    'rate': rate
-                }
-                region_list.append(region_dict)
-
             # 设备类型订单统计
             # 设备类型订单统计
             device_type_list = []
             device_type_list = []
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             uid_dict = {}
             uid_dict = {}
+            uid_list = []
             for item in device_type_qs:
             for item in device_type_qs:
+                uid_list.append(item['UID'])
                 device_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
                 device_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
                 device_type = device_qs[0]['Type'] if device_qs.exists() else 0
                 device_type = device_qs[0]['Type'] if device_qs.exists() else 0
                 if device_type not in uid_dict:
                 if device_type not in uid_dict:
@@ -291,6 +282,32 @@ class ServiceDataView(View):
                 }
                 }
                 device_type_list.append(device_temp_qs)
                 device_type_list.append(device_temp_qs)
 
 
+            # 区域订单统计
+            region_list = []
+            region_dict = {}
+            for item in uid_list:
+                uidset_qs = UidSetModel.objects.filter(uid=item).values('tb_country')
+                if uidset_qs.exists():
+                    country_qs = CountryModel.objects.filter(id=uidset_qs[0]['tb_country']).values('country_name')
+                    country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
+                else:
+                    country_name = '未知区域'
+                if country_name not in region_dict:
+                    region_dict[country_name] = []
+                region_dict[country_name].append(item)
+            for k, v in region_dict.items():
+                region_order_count = order_qs.filter(UID__in=v).count()
+                if count:
+                    rate = round(region_order_count / count * 100, 2)
+                else:
+                    rate = 0
+                region_temp_dict = {
+                    'countryName': k,
+                    'count': region_order_count,
+                    'rate': rate
+                }
+                region_list.append(region_temp_dict)
+
             res = {
             res = {
                 'orders': order_list,
                 'orders': order_list,
                 'regions': region_list,
                 'regions': region_list,
@@ -348,29 +365,13 @@ class ServiceDataView(View):
                 }
                 }
                 order_list.append(order_dict)
                 order_list.append(order_dict)
 
 
-            # 区域订单统计
-            region_list = []
-            region_qs = order_qs.values('userID__region_country').annotate(count=Count('UID')).order_by('-count')
-            for item in region_qs:
-                country_id = item['userID__region_country']
-                country_qs = CountryModel.objects.filter(id=country_id).values('country_name')
-                country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
-                if count:
-                    rate = round(item['count'] / count * 100, 2)
-                else:
-                    rate = 0
-                region_dict = {
-                    'countryName': country_name,
-                    'count': item['count'],
-                    'rate': rate
-                }
-                region_list.append(region_dict)
-
             # 设备类型订单统计
             # 设备类型订单统计
             device_type_list = []
             device_type_list = []
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             uid_dict = {}
             uid_dict = {}
+            uid_list = []
             for item in device_type_qs:
             for item in device_type_qs:
+                uid_list.append(item['UID'])
                 device_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
                 device_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
                 device_type = device_qs[0]['Type'] if device_qs.exists() else 0
                 device_type = device_qs[0]['Type'] if device_qs.exists() else 0
                 if device_type not in uid_dict:
                 if device_type not in uid_dict:
@@ -391,11 +392,38 @@ class ServiceDataView(View):
                 }
                 }
                 device_type_list.append(device_temp_qs)
                 device_type_list.append(device_temp_qs)
 
 
+            # 区域订单统计
+            region_list = []
+            region_dict = {}
+            for item in uid_list:
+                uidset_qs = UidSetModel.objects.filter(uid=item).values('tb_country')
+                if uidset_qs.exists():
+                    country_qs = CountryModel.objects.filter(id=uidset_qs[0]['tb_country']).values('country_name')
+                    country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
+                else:
+                    country_name = '未知区域'
+                if country_name not in region_dict:
+                    region_dict[country_name] = []
+                region_dict[country_name].append(item)
+            for k, v in region_dict.items():
+                region_order_count = order_qs.filter(UID__in=v).count()
+                if count:
+                    rate = round(region_order_count / count * 100, 2)
+                else:
+                    rate = 0
+                region_temp_dict = {
+                    'countryName': k,
+                    'count': region_order_count,
+                    'rate': rate
+                }
+                region_list.append(region_temp_dict)
+
             res = {
             res = {
                 'orders': order_list,
                 'orders': order_list,
                 'regions': region_list,
                 'regions': region_list,
                 'deviceType': device_type_list,
                 'deviceType': device_type_list,
             }
             }
+
             return response.json(0, res)
             return response.json(0, res)
         except Exception as e:
         except Exception as e:
             return response.json(500, repr(e))
             return response.json(500, repr(e))
@@ -451,29 +479,13 @@ class ServiceDataView(View):
                 }
                 }
                 order_list.append(order_dict)
                 order_list.append(order_dict)
 
 
-            # 区域订单统计
-            region_list = []
-            region_qs = order_qs.values('userID__region_country').annotate(count=Count('UID')).order_by('-count')
-            for item in region_qs:
-                country_id = item['userID__region_country']
-                country_qs = CountryModel.objects.filter(id=country_id).values('country_name')
-                country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
-                if count:
-                    rate = round(item['count'] / count * 100, 2)
-                else:
-                    rate = 0
-                region_dict = {
-                    'countryName': country_name,
-                    'count': item['count'],
-                    'rate': rate
-                }
-                region_list.append(region_dict)
-
             # 设备类型订单统计
             # 设备类型订单统计
             device_type_list = []
             device_type_list = []
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             device_type_qs = order_qs.values('UID').annotate(count=Count('UID')).order_by('count')
             uid_dict = {}
             uid_dict = {}
+            uid_list = []
             for item in device_type_qs:
             for item in device_type_qs:
+                uid_list.append(item['UID'])
                 device_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
                 device_qs = Device_Info.objects.filter(UID=item['UID']).values('Type')
                 device_type = device_qs[0]['Type'] if device_qs.exists() else 0
                 device_type = device_qs[0]['Type'] if device_qs.exists() else 0
                 if device_type not in uid_dict:
                 if device_type not in uid_dict:
@@ -497,6 +509,31 @@ class ServiceDataView(View):
                 }
                 }
                 device_type_list.append(device_temp_qs)
                 device_type_list.append(device_temp_qs)
 
 
+            # 区域订单统计
+            region_list = []
+            region_dict = {}
+            for item in uid_list:
+                uidset_qs = UidSetModel.objects.filter(uid=item).values('tb_country')
+                if uidset_qs.exists():
+                    country_qs = CountryModel.objects.filter(id=uidset_qs[0]['tb_country']).values('country_name')
+                    country_name = country_qs[0]['country_name'] if country_qs.exists() else '未知区域'
+                else:
+                    country_name = '未知区域'
+                if country_name not in region_dict:
+                    region_dict[country_name] = []
+                region_dict[country_name].append(item)
+            for k, v in region_dict.items():
+                region_order_count = order_qs.filter(UID__in=v).count()
+                if count:
+                    rate = round(region_order_count / count * 100, 2)
+                else:
+                    rate = 0
+                region_temp_dict = {
+                    'countryName': k,
+                    'count': region_order_count,
+                    'rate': rate
+                }
+                region_list.append(region_temp_dict)
             res = {
             res = {
                 'orders': order_list,
                 'orders': order_list,
                 'regions': region_list,
                 'regions': region_list,
@@ -528,7 +565,7 @@ class ServiceDataView(View):
             region_count = 0
             region_count = 0
             device_type_list = []
             device_type_list = []
             device_type_count = 0
             device_type_count = 0
-            device_type_total = 0
+            total = 0
             store_meal_list = []
             store_meal_list = []
             store_meal_count = 0
             store_meal_count = 0
             for url in url_list:
             for url in url_list:
@@ -572,22 +609,19 @@ class ServiceDataView(View):
                                 each['count'] += int(item['count'])
                                 each['count'] += int(item['count'])
                                 item['totalMoney'] += item['totalMoney']
                                 item['totalMoney'] += item['totalMoney']
                                 device_type_count += int(item['count'])
                                 device_type_count += int(item['count'])
-                                device_type_total += item['totalMoney']
+                                total += item['totalMoney']
                                 flag = 1
                                 flag = 1
                                 break
                                 break
                         if flag == 0:
                         if flag == 0:
                             device_type_list.append(item)
                             device_type_list.append(item)
                             device_type_count += int(item['count'])
                             device_type_count += int(item['count'])
-                            device_type_total += item['totalMoney']
+                            total += item['totalMoney']
                     for item in device_type_list:
                     for item in device_type_list:
                         if device_type_count:
                         if device_type_count:
                             type_rate = round(item['count'] / device_type_count * 100, 2)
                             type_rate = round(item['count'] / device_type_count * 100, 2)
                         else:
                         else:
                             type_rate = 0
                             type_rate = 0
-                        if device_type_total:
-                            total_rate = round(item['totalMoney'] / device_type_total * 100, 2)
-                        else:
-                            total_rate = 0
+                        total_rate = round(item['totalMoney'] / total * 100, 2)
                         item['typeRate'] = type_rate
                         item['typeRate'] = type_rate
                         item['totalRate'] = total_rate
                         item['totalRate'] = total_rate
                     # 处理套餐
                     # 处理套餐
@@ -596,6 +630,7 @@ class ServiceDataView(View):
                         for each in store_meal_list:
                         for each in store_meal_list:
                             if each['storeMealId'] == item['storeMealId']:
                             if each['storeMealId'] == item['storeMealId']:
                                 each['count'] += int(item['count'])
                                 each['count'] += int(item['count'])
+                                each['storeMealTotal'] += item['storeMealTotal']
                                 store_meal_count += int(item['count'])
                                 store_meal_count += int(item['count'])
                                 flag = 1
                                 flag = 1
                                 break
                                 break
@@ -603,11 +638,13 @@ class ServiceDataView(View):
                             store_meal_list.append(item)
                             store_meal_list.append(item)
                             store_meal_count += int(item['count'])
                             store_meal_count += int(item['count'])
                     for item in store_meal_list:
                     for item in store_meal_list:
+                        total_rate = round(item['storeMealTotal'] / total * 100, 2)
                         if store_meal_count:
                         if store_meal_count:
                             rate = round(item['count'] / store_meal_count * 100, 2)
                             rate = round(item['count'] / store_meal_count * 100, 2)
                         else:
                         else:
                             rate = 0
                             rate = 0
                         item['rate'] = rate
                         item['rate'] = rate
+                        item['totalRate'] = total_rate
                 else:
                 else:
                     return response.json(result['result_code'])
                     return response.json(result['result_code'])
             res = {
             res = {