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

免费订单、首次付费订单、复购订单

peng 2 жил өмнө
parent
commit
6ef94fa65a

+ 164 - 31
AdminController/dataSystemManagement/ServiceDataController.py

@@ -336,11 +336,18 @@ class ServiceDataView(View):
             return response.json(444, {'error param': 'startTime or endTime or timeUnit or storeMealType'})
         try:
             order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=2,
-                                                    service_type=store_meal_type).values('count', 'country',
+                                                    service_type=store_meal_type).values('count', 'country', 'total',
                                                                                          'device_type')
             all_order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time).filter(
-                Q(query_type=0) | Q(query_type=1)).aggregate(count=Sum('count'))
-            all_order_count = all_order_qs['count'] if all_order_qs['count'] else 0  # 所有订单数量
+                Q(query_type=0) | Q(query_type=1)).values('total', 'count')
+            all_order_count = 0
+            all_order_cny_total = 0
+            all_order_usd_total = 0
+            for item in all_order_qs:
+                all_order_count += item['count']
+                temp_total = eval(item['total'])
+                all_order_cny_total += temp_total.get('CNY', 0)
+                all_order_usd_total += temp_total.get('USD', 0)
             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)
@@ -351,8 +358,16 @@ class ServiceDataView(View):
                 order_temp_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
                 temp_count = order_temp_qs.aggregate(count=Sum('count'))['count']
                 temp_count = temp_count if temp_count else 0
+                temp_cny_total = 0
+                temp_usd_total = 0
+                for each in order_temp_qs:
+                    temp_total = eval(each['total'])
+                    temp_cny_total += temp_total.get('CNY', 0)
+                    temp_usd_total += temp_total.get('USD', 0)
                 order_dict = {
                     'count': temp_count,
+                    'cnyTotal': temp_cny_total,
+                    'usdTotal': temp_usd_total,
                     'startTime': item[0],
                     'endTime': item[1]
                 }
@@ -365,34 +380,54 @@ class ServiceDataView(View):
                 device_type_temp_dict = eval(each['device_type'])
                 for k, v in country_temp_dict.items():
                     if k in country_dict:
-                        country_dict[k] += v
+                        for a, b in v.items():
+                            if a not in country_dict[k]:
+                                country_dict[k][a] = b
+                            else:
+                                country_dict[k][a] += b
                     else:
                         country_dict[k] = v
                 for k, v in device_type_temp_dict.items():
                     if k in device_type_dict:
-                        device_type_dict[k] += v
+                        for a, b in v.items():
+                            if a not in device_type_dict[k]:
+                                device_type_dict[k][a] = b
+                            else:
+                                device_type_dict[k][a] += b
                     else:
                         device_type_dict[k] = v
 
             # 设备类型订单统计
             device_type_list = []
             for k, v in device_type_dict.items():
-                type_rate = round(v / all_order_count * 100, 2) if all_order_count else 0
+                type_rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
+                cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
+                usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
                 device_temp_qs = {
                     'typeName': k,
-                    'count': v,
+                    'count': v['数量'],
+                    'cnyTotalRate': cny_total_rate,
+                    'usdTotalRate': usd_total_rate,
                     'typeRate': type_rate,
+                    'cnyTotalMoney': v.get('CNY', 0),
+                    'usdTotalMoney': v.get('USD', 0),
                 }
                 device_type_list.append(device_temp_qs)
 
             # 区域订单统计
             region_list = []
             for k, v in country_dict.items():
-                rate = round(v / all_order_count * 100, 2) if all_order_count else 0
+                rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
+                cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
+                usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
                 region_temp_dict = {
                     'countryName': k,
-                    'count': v,
-                    'rate': rate
+                    'count': v['数量'],
+                    'rate': rate,
+                    'cnyTotalMoney': v.get('CNY', 0),
+                    'usdTotalMoney': v.get('USD', 0),
+                    'cnyTotalRate': cny_total_rate,
+                    'usdTotalRate': usd_total_rate,
                 }
                 region_list.append(region_temp_dict)
 
@@ -400,7 +435,9 @@ class ServiceDataView(View):
                 'orders': order_list,
                 'regions': region_list,
                 'deviceType': device_type_list,
-                'allOrderCount': all_order_count
+                'allOrderCount': all_order_count,
+                'allOrderUsdTotal': all_order_usd_total,
+                'allOrderCnyTotal': all_order_cny_total,
             }
             return response.json(0, res)
         except Exception as e:
@@ -426,14 +463,20 @@ class ServiceDataView(View):
             return response.json(444, {'error param': 'startTime or endTime or timeUnit or storeMealType'})
         try:
             order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time, query_type=3,
-                                                    service_type=store_meal_type).values('count', 'country',
+                                                    service_type=store_meal_type).values('count', 'country', 'total',
                                                                                          'device_type')
             repeat_pay_order_count = order_qs.aggregate(count=Sum('count'))['count']
             repeat_pay_order_count = repeat_pay_order_count if repeat_pay_order_count else 0
             all_order_qs = OrdersSummary.objects.filter(time__gte=start_time, time__lt=end_time).filter(
-                Q(query_type=0) | Q(query_type=1)).aggregate(count=Sum('count'))
-            all_order_count = all_order_qs['count'] if all_order_qs['count'] else 0  # 所有订单数量
-
+                Q(query_type=0) | Q(query_type=1)).values('total', 'count')
+            all_order_count = 0  # 所有订单数量
+            all_order_cny_total = 0
+            all_order_usd_total = 0
+            for item in all_order_qs:
+                all_order_count += item['count']
+                temp_total = eval(item['total'])
+                all_order_cny_total += temp_total.get('CNY', 0)
+                all_order_usd_total += temp_total.get('USD', 0)
             # 订单复购率
             repeat_rate = round(repeat_pay_order_count / all_order_count * 100, 2) if all_order_count else 0
             start_time = datetime.datetime.fromtimestamp(int(start_time))
@@ -445,8 +488,16 @@ class ServiceDataView(View):
                 order_temp_qs = order_qs.filter(time__gte=item[0], time__lt=item[1])
                 temp_count = order_temp_qs.aggregate(count=Sum('count'))['count']
                 temp_count = temp_count if temp_count else 0
+                temp_cny_total = 0
+                temp_usd_total = 0
+                for each in order_temp_qs:
+                    temp_total = eval(each['total'])
+                    temp_cny_total += temp_total.get('CNY', 0)
+                    temp_usd_total += temp_total.get('USD', 0)
                 order_dict = {
                     'count': temp_count,
+                    'tempCnyTotal': temp_cny_total,
+                    'tempUsdTotal': temp_usd_total,
                     'startTime': item[0],
                     'endTime': item[1]
                 }
@@ -458,34 +509,54 @@ class ServiceDataView(View):
                 device_type_temp_dict = eval(each['device_type'])
                 for k, v in country_temp_dict.items():
                     if k in country_dict:
-                        country_dict[k] += v
+                        for a, b in v.items():
+                            if a not in country_dict[k]:
+                                country_dict[k][a] = b
+                            else:
+                                country_dict[k][a] += b
                     else:
                         country_dict[k] = v
                 for k, v in device_type_temp_dict.items():
                     if k in device_type_dict:
-                        device_type_dict[k] += v
+                        for a, b in v.items():
+                            if a not in device_type_dict[k]:
+                                device_type_dict[k][a] = b
+                            else:
+                                device_type_dict[k][a] += b
                     else:
                         device_type_dict[k] = v
 
             # 设备类型订单统计
             device_type_list = []
             for k, v in device_type_dict.items():
-                type_rate = round(v / all_order_count * 100, 2) if all_order_count else 0
+                type_rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
+                cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
+                usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
                 device_temp_qs = {
                     'typeName': k,
-                    'count': v,
+                    'count': v['数量'],
                     'typeRate': type_rate,
+                    'cnyTotalMoney': v.get('CNY', 0),
+                    'usdTotalMoney': v.get('USD', 0),
+                    'cnyTotalRate': cny_total_rate,
+                    'usdTotalRate': usd_total_rate,
                 }
                 device_type_list.append(device_temp_qs)
 
             # 区域订单统计
             region_list = []
             for k, v in country_dict.items():
-                rate = round(v / all_order_count * 100, 2) if all_order_count else 0
+                rate = round(v['数量'] / all_order_count * 100, 2) if all_order_count else 0
+                cny_total_rate = round(v.get('CNY', 0) / all_order_cny_total * 100, 2) if all_order_cny_total else 0
+                usd_total_rate = round(v.get('USD', 0) / all_order_usd_total * 100, 2) if all_order_usd_total else 0
                 region_temp_dict = {
                     'countryName': k,
-                    'count': v,
-                    'rate': rate
+                    'count': v['数量'],
+                    'rate': rate,
+                    'cnyTotalMoney': v.get('CNY', 0),
+                    'usdTotalMoney': v.get('USD', 0),
+                    'cnyTotalRate': cny_total_rate,
+                    'usdTotalRate': usd_total_rate,
                 }
                 region_list.append(region_temp_dict)
             res = {
@@ -494,7 +565,10 @@ class ServiceDataView(View):
                 'deviceType': device_type_list,
                 'repeatRate': repeat_rate,
                 'repeatOrderCount': repeat_pay_order_count,
-                'allOrderCount': all_order_count
+                'allOrderCount': all_order_count,
+                'allOrderCnyTotal': all_order_cny_total,
+                'allOrderUsdTotal': all_order_usd_total,
+
             }
             return response.json(0, res)
         except Exception as e:
@@ -532,8 +606,8 @@ class ServiceDataView(View):
                         for each in order_list:
                             if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
                                 each['count'] += int(item['count'])
-                                each['cnyTotal'] = round(each['cnyTotal'] + item['cnyTotal'], 2)
-                                each['usdTotal'] = round(each['usdTotal'] + item['usdTotal'], 2)
+                                each['cnyTotal'] += item['cnyTotal']
+                                each['usdTotal'] += item['usdTotal']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -544,8 +618,8 @@ class ServiceDataView(View):
                         for each in region_list:
                             if each['countryName'] == item['countryName']:
                                 each['count'] += int(item['count'])
-                                each['cnyTotalMoney'] = round(each['cnyTotalMoney'] + item['cnyTotalMoney'], 2)
-                                each['usdTotalMoney'] = round(each['usdTotalMoney'] + item['usdTotalMoney'], 2)
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -557,8 +631,8 @@ class ServiceDataView(View):
                         for each in device_type_list:
                             if each['typeName'] == item['typeName']:
                                 each['count'] += int(item['count'])
-                                each['cnyTotalMoney'] = round(each['cnyTotalMoney'] + item['cnyTotalMoney'], 2)
-                                each['usdTotalMoney'] = round(each['usdTotalMoney'] + item['usdTotalMoney'], 2)
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -570,8 +644,8 @@ class ServiceDataView(View):
                         for each in store_meal_list:
                             if each['storeMealName'] == item['storeMealName']:
                                 each['count'] += int(item['count'])
-                                each['cnyTotalMoney'] = round(each['cnyTotalMoney'] + item['cnyTotalMoney'], 2)
-                                each['usdTotalMoney'] = round(each['usdTotalMoney'] + item['usdTotalMoney'], 2)
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -582,14 +656,21 @@ class ServiceDataView(View):
                     all_order_count += result['result']['allOrderCount']
                 else:
                     return response.json(result['result_code'], result['result'])
+            for item in order_list:
+                item['cnyTotal'] = round(item['cnyTotal'], 2)
+                item['usdTotal'] = round(item['usdTotal'], 2)
             for item in region_list:
                 item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
                 item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
                                              2) if all_cny_order_total else 0
                 item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
                                              2) if all_usd_order_total else 0
             for item in device_type_list:
                 item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
                 item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
                                              2) if all_cny_order_total else 0
                 item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
@@ -599,6 +680,8 @@ class ServiceDataView(View):
                                              2) if all_cny_order_total else 0
                 item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
                                              2) if all_usd_order_total else 0
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
                 item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
             res = {
                 'orders': order_list,
@@ -709,6 +792,8 @@ class ServiceDataView(View):
             region_list = []
             device_type_list = []
             all_order_count = 0
+            all_usd_order_total = 0
+            all_cny_order_total = 0
             for url in url_list:
                 url = url + request.path.replace('global/', '')
                 res = requests.get(url=url, params=request_dict, headers=headers)
@@ -720,6 +805,8 @@ class ServiceDataView(View):
                         for each in order_list:
                             if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
                                 each['count'] += int(item['count'])
+                                each['cnyTotal'] += item['cnyTotal']
+                                each['usdTotal'] += item['usdTotal']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -730,6 +817,8 @@ class ServiceDataView(View):
                         for each in region_list:
                             if each['countryName'] == item['countryName']:
                                 each['count'] += int(item['count'])
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -741,16 +830,35 @@ class ServiceDataView(View):
                         for each in device_type_list:
                             if each['typeName'] == item['typeName']:
                                 each['count'] += int(item['count'])
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
                             device_type_list.append(item)
                     all_order_count += result['result']['allOrderCount']
+                    all_cny_order_total += result['result']['allOrderCnyTotal']
+                    all_usd_order_total += result['result']['allOrderUsdTotal']
                 else:
                     return response.json(result['result_code'], result['result'])
+            for item in order_list:
+                item['cnyTotal'] = round(item['cnyTotal'], 2)
+                item['usdTotal'] = round(item['usdTotal'], 2)
             for item in region_list:
+                item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
+                                             2) if all_cny_order_total else 0
+                item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
+                                             2) if all_usd_order_total else 0
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
                 item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
             for item in device_type_list:
+                item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
+                                             2) if all_cny_order_total else 0
+                item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
+                                             2) if all_usd_order_total else 0
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
                 item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
             res = {
                 'orders': order_list,
@@ -780,6 +888,8 @@ class ServiceDataView(View):
             repeat_order_count = 0
             device_type_list = []
             all_order_count = 0
+            all_usd_order_total = 0
+            all_cny_order_total = 0
             for url in url_list:
                 url = url + request.path.replace('global/', '')
                 res = requests.get(url=url, params=request_dict, headers=headers)
@@ -791,6 +901,8 @@ class ServiceDataView(View):
                         for each in order_list:
                             if each['startTime'] == item['startTime'] and each['endTime'] == item['endTime']:
                                 each['count'] += int(item['count'])
+                                each['cnyTotal'] += item['cnyTotal']
+                                each['usdTotal'] += item['usdTotal']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -801,6 +913,8 @@ class ServiceDataView(View):
                         for each in region_list:
                             if each['countryName'] == item['countryName']:
                                 each['count'] += int(item['count'])
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -812,6 +926,8 @@ class ServiceDataView(View):
                         for each in device_type_list:
                             if each['typeName'] == item['typeName']:
                                 each['count'] += int(item['count'])
+                                each['cnyTotalMoney'] += item['cnyTotalMoney']
+                                each['usdTotalMoney'] += item['usdTotalMoney']
                                 flag = 1
                                 break
                         if flag == 0:
@@ -820,12 +936,29 @@ class ServiceDataView(View):
                     # 处理订单复购率
                     repeat_order_count += result['result']['repeatOrderCount']
                     all_order_count += result['result']['allOrderCount']
+                    all_usd_order_total += result['result']['allOrderUsdTotal']
+                    all_cny_order_total += result['result']['allOrderCnyTotal']
                 else:
                     return response.json(result['result_code'], result['result'])
             repeat_rate = round(repeat_order_count / all_order_count * 100, 2) if all_order_count else 0
+            for item in order_list:
+                item['cnyTotal'] = round(item['cnyTotal'], 2)
+                item['usdTotal'] = round(item['usdTotal'], 2)
             for item in device_type_list:
+                item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
+                                             2) if all_cny_order_total else 0
+                item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
+                                             2) if all_usd_order_total else 0
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
                 item['typeRate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
             for item in region_list:
+                item['cnyTotalMoney'] = round(item['cnyTotalMoney'], 2)
+                item['usdTotalMoney'] = round(item['usdTotalMoney'], 2)
+                item['cnyTotalRate'] = round(item['cnyTotalMoney'] / all_cny_order_total * 100,
+                                             2) if all_cny_order_total else 0
+                item['usdTotalRate'] = round(item['usdTotalMoney'] / all_usd_order_total * 100,
+                                             2) if all_usd_order_total else 0
                 item['rate'] = round(item['count'] / all_order_count * 100, 2) if all_order_count else 0
             res = {
                 'orders': order_list,