peng 1 рік тому
батько
коміт
ad0ca68d8e

+ 5 - 70
AdminController/dataSystemManagement/OperatingCostsDataController.py

@@ -65,80 +65,15 @@ class OperatingCostsDataView(View):
             else:
                 operating_costs_qs = OperatingCosts.objects.all()
             count = operating_costs_qs.count()
-            order_list = list(operating_costs_qs.values_list('order_id', flat=True).order_by('-time'))[:page * line]
             operating_qs = operating_costs_qs.values('order_id', 'uid', 'day_average_price', 'month_average_price',
                                                      'purchase_quantity', 'actual_storage', 'actual_api',
                                                      'monthly_income', 'settlement_days', 'remaining_usage_time',
-                                                     'end_time', 'created_time', 'start_time', 'time').order_by(
+                                                     'end_time', 'created_time', 'start_time', 'time', 'storage_cost',
+                                                     'api_cost', 'profit', 'profit_margin', 'fee', 'flow_cost',
+                                                     'real_income', 'price', 'region', 'country_name',
+                                                     'order_type', 'expire').order_by(
                 '-time')[(page - 1) * line:page * line]
-            all_order_qs = Order_Model.objects.filter(orderID__in=order_list)
-            country_qs = CountryModel.objects.values('id', 'country_name')
-            country_dict = {}
-            for item in country_qs:
-                country_dict[item['id']] = item['country_name']
-            res = []
-            storage_univalence = 0.023 / 30
-            api_univalence = 0.005 / 1000
-            for item in operating_qs:
-                order_qs = all_order_qs.filter(orderID=item['order_id']).values('price', 'order_type', 'fee',
-                                                                                'userID__region_country',
-                                                                                'rank__expire', 'payType')
-                if not order_qs.exists():
-                    continue
-                country_name = country_dict.get(order_qs[0]['userID__region_country'], '未知国家')
-                region = '国内' if CONFIG_INFO == CONFIG_CN else '国外'
-                if order_qs[0]['order_type'] in [0, 1]:
-                    order_type = '云存'
-                    storage_cost = round(
-                        float(item['actual_storage']) / 1024 * storage_univalence * item['settlement_days'], 2)
-                    api_cost = round(int(item['actual_api']) * api_univalence, 2)
-                    if CONFIG_INFO == CONFIG_CN:  # 国内要换算汇率
-                        storage_cost = storage_cost * 7
-                        api_cost = api_cost * 7
-                    if float(item['monthly_income']) == 0.0:
-                        profit = 0
-                        profit_margin = 0
-                    else:
-                        profit = round(float(item['monthly_income']) - storage_cost - api_cost, 2)  # 利润=月结算金额-月成本
-                        profit_margin = round(profit / float(item['month_average_price']), 2)  # 利润率=利润/每月收入分摊
-                    expire = str(order_qs[0]['rank__expire']) + '个月'
-                else:
-                    order_type = '4G流量'
-                    storage_cost = 0
-                    api_cost = 0
-                if order_qs[0]['payType'] in [2, 3]:
-                    fee = float(order_qs[0]['price']) * 0.0054
-                else:
-                    fee = float(order_qs[0]['fee']) if order_qs[0]['fee'] else 0
-                res.append({
-                    'order_id': item['order_id'],
-                    'uid': item['uid'],
-                    'region': region,
-                    'country_name': country_name,
-                    'order_type': order_type,
-                    'expire': expire,
-                    'price': order_qs[0]['price'],
-                    'fee': fee,
-                    'real_income': round(float(order_qs[0]['price']) - fee, 2),
-                    'day_average_price': item['day_average_price'],
-                    'month_average_price': item['month_average_price'],
-                    'purchase_quantity': item['purchase_quantity'],
-                    'start_time': item['start_time'],
-                    'end_time': item['end_time'],
-                    'settlement_time': item['created_time'],
-                    'settlement_days': item['settlement_days'],
-                    'monthly_income': item['monthly_income'],
-                    'remaining_usage_time': item['remaining_usage_time'],
-                    'actual_storage': item['actual_storage'],
-                    'actual_api': item['actual_api'],
-                    'storage_cost': storage_cost,
-                    'api_cost': api_cost,
-                    'profit': profit,
-                    'profit_margin': profit_margin,
-                    'time': item['time']
-                })
-            return response.json(0, {'count': count,
-                                     'res': res})
+            return response.json(0, {'count': count, 'res': list(operating_qs)})
         except Exception as e:
             print('error')
             return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))

+ 34 - 5
Controller/Cron/CronTaskController.py

@@ -23,7 +23,7 @@ from django.views import View
 
 from Ansjer.config import USED_SERIAL_REDIS_LIST, UNUSED_SERIAL_REDIS_LIST, CONFIG_INFO, CONFIG_US, \
     RESET_REGION_ID_SERIAL_REDIS_LIST, LOGGER, PAYPAL_CRD, CONFIG_EUR, DETECT_PUSH_DOMAINS, ACCESS_KEY_ID, \
-    SECRET_ACCESS_KEY, REGION_NAME
+    SECRET_ACCESS_KEY, REGION_NAME, CONFIG_CN
 from Model.models import Device_User, Device_Info, UidSetModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \
     VodHlsModel, ExperienceContextModel, AiService, VodHlsSummary, VideoPlaybackTimeModel, DeviceUserSummary, \
     CountryModel, DeviceTypeModel, OrdersSummary, DeviceInfoSummary, CompanySerialModel, \
@@ -1154,13 +1154,24 @@ class CronCollectDataView(View):
             operating_costs_qs = OperatingCosts.objects.filter(time=start_time_stamp).values('order_id', 'end_time',
                                                                                              'uid')
             create_time = int(time.time())
+            storage_univalence = 0.023 / 30
+            api_univalence = 0.005 / 1000
+            region = '国内' if CONFIG_INFO == CONFIG_CN else '国外'
+            country_qs = CountryModel.objects.values('id', 'country_name')
+            country_dict = {}
+            for item in country_qs:
+                country_dict[item['id']] = item['country_name']
             for item in operating_costs_qs:
                 order_qs = Order_Model.objects.filter(orderID=item['order_id']).values('price', 'payTime', 'order_type',
-                                                                                       'rank__expire', 'fee', 'payType')
+                                                                                       'rank__expire', 'fee', 'payType',
+                                                                                       'userID__region_country')
                 if order_qs.exists():
                     order = order_qs[0]
+                    country_name = country_dict.get(order['userID__region_country'], '未知国家')
                     if order['order_type'] not in [0, 1]:
                         continue
+                    order_type = '云存'
+                    expire = str(order_qs[0]['rank__expire']) + '个月'
                     price = float(order['price'])
                     if order['payType'] in [2, 3]:
                         fee = 0.0054 * price
@@ -1184,6 +1195,7 @@ class CronCollectDataView(View):
                     day_average_price = round(price / order_days, 2)  # 收入分摊/天
                     month_average_price = round(day_average_price * settlement_days, 2)  # 收入分摊/月
                     monthly_income = round((price - fee) / order_days * settlement_days, 2)  # 当月结算收入
+                    real_income = round(price - fee, 2)
                     if item['end_time'] < end_time_stamp:
                         uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=start_time_stamp,
                                                                                    time__lt=item['end_time'],
@@ -1195,16 +1207,32 @@ class CronCollectDataView(View):
                     result = uid_bucket_statistics.aggregate(size=Sum('storage_size'), api_count=Sum('api_count'))
                     actual_storage = round(result['size'], 2) if result['size'] else 0
                     actual_api = result['api_count'] if result['api_count'] else 0
+                    storage_cost = round(actual_storage / 1024 * storage_univalence * settlement_days, 2)
+                    api_cost = round(actual_api * api_univalence, 2)
+                    if CONFIG_INFO == CONFIG_CN:  # 国内要换算汇率
+                        storage_cost = storage_cost * 7
+                        api_cost = api_cost * 7
+                    if monthly_income == 0.0:
+                        profit = 0
+                        profit_margin = 0
+                    else:
+                        profit = round(monthly_income - storage_cost - api_cost, 2)  # 利润=月结算金额-月成本
+                        profit_margin = round(profit / month_average_price, 2)  # 利润率=利润/每月收入分摊
                     OperatingCosts.objects.filter(time=start_time_stamp, order_id=item['order_id'],
                                                   uid=item['uid']).update(day_average_price=day_average_price,
                                                                           month_average_price=month_average_price,
                                                                           monthly_income=monthly_income,
                                                                           actual_storage=actual_storage,
                                                                           settlement_days=settlement_days,
-                                                                          actual_api=actual_api,
-                                                                          created_time=create_time,
+                                                                          actual_api=actual_api, fee=fee,
+                                                                          created_time=create_time, region=region,
                                                                           start_time=order_start_time,
-                                                                          remaining_usage_time=remaining_usage_time)
+                                                                          remaining_usage_time=remaining_usage_time,
+                                                                          storage_cost=storage_cost, api_cost=api_cost,
+                                                                          profit=profit, profit_margin=profit_margin,
+                                                                          real_income=real_income, price=price,
+                                                                          country_name=country_name,
+                                                                          order_type=order_type, expire=expire)
             print('结束')
         except Exception as e:
             LOGGER.info(
@@ -1266,6 +1294,7 @@ class CronCollectDataView(View):
                 print(actual_storage, actual_api)
             print('结束')
         except Exception as e:
+            print('error')
             LOGGER.info('统计s3信息异常:errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
 
     @staticmethod