Explorar o código

优化查询统计订单利润成本

peng hai 1 ano
pai
achega
f4b088dbb5

+ 9 - 10
AdminController/dataSystemManagement/OperatingCostsDataController.py

@@ -72,8 +72,8 @@ class OperatingCostsDataView(View):
             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', 'time').order_by('-time')[
-                           (page - 1) * line:page * line]
+                                                     'end_time', 'created_time', 'start_time', 'time').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 = {}
@@ -92,16 +92,14 @@ class OperatingCostsDataView(View):
                 region = '国内' if CONFIG_INFO == CONFIG_CN else '国外'
                 if order_qs[0]['order_type'] in [0, 1]:
                     order_type = '云存'
-                    order_start_time = int((datetime.datetime.fromtimestamp(item['end_time']) - relativedelta(
-                        months=order_qs[0]['rank__expire'])).timestamp())
-                    storage_cost = float(item['actual_storage']) / 1024 * storage_univalence * item['settlement_days']
-                    api_cost = int(item['actual_api']) * api_univalence
+                    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
-                        profit = round(float(item['monthly_income']) - storage_cost - api_cost, 2)
-                    else:
-                        profit = round(float(item['monthly_income']) - storage_cost - api_cost, 2)
+                    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流量'
@@ -124,7 +122,7 @@ class OperatingCostsDataView(View):
                     'day_average_price': item['day_average_price'],
                     'month_average_price': item['month_average_price'],
                     'purchase_quantity': item['purchase_quantity'],
-                    'start_time': order_start_time,
+                    'start_time': item['start_time'],
                     'end_time': item['end_time'],
                     'settlement_time': item['created_time'],
                     'settlement_days': item['settlement_days'],
@@ -135,6 +133,7 @@ class OperatingCostsDataView(View):
                     'storage_cost': storage_cost,
                     'api_cost': api_cost,
                     'profit': profit,
+                    'profit_margin': profit_margin,
                     'time': item['time']
                 })
             # if CONFIG_INFO == CONFIG_US:

+ 17 - 7
Controller/Cron/CronTaskController.py

@@ -1119,15 +1119,24 @@ class CronCollectDataView(View):
                         fee = 0.0054 * price
                     else:
                         fee = float(order['fee']) if order['fee'] else 0
-                    if item['end_time'] > end_time_stamp:  # 当月结算天数
-                        settlement_days = (end_time - start_time).days
-                        remaining_usage_time = (item['end_time'] - end_time_stamp) // 86400  # 剩余使用时间
-                    else:
-                        settlement_days = (item['end_time'] - start_time_stamp) // 86400
+                    order_start_time = int((datetime.datetime.fromtimestamp(item['end_time']) - relativedelta(
+                        months=order['rank__expire'])).timestamp())
+                    order_days = round((item['end_time'] - order_start_time) / 86400)
+                    if item['end_time'] > end_time_stamp:  # 订单到下个月结束
+                        if order_start_time <= start_time_stamp:  # 订单在上月开始服务
+                            settlement_days = (end_time - start_time).days  # 当月结算天数=月初-月底
+                        else:  # 订单当月开始服务
+                            settlement_days = round((end_time_stamp - order_start_time) / 86400)
+                        remaining_usage_time = round((item['end_time'] - end_time_stamp) / 86400)  # 剩余使用时间
+                    else:  # 订单当月结束
+                        if order_start_time <= start_time_stamp:
+                            settlement_days = round((item['end_time'] - start_time_stamp) / 86400)
+                        else:
+                            settlement_days = round((item['end_time'] - order_start_time) / 86400)
                         remaining_usage_time = 0
-                    day_average_price = round(price / (order['rank__expire'] * 30), 2)  # 收入分摊/天
+                    day_average_price = round(price / order_days, 2)  # 收入分摊/天
                     month_average_price = round(day_average_price * settlement_days, 2)  # 收入分摊/月
-                    monthly_income = round((price - fee) / (order['rank__expire'] * 30) * settlement_days, 2)  # 当月结算收入
+                    monthly_income = round((price - fee) / order_days * settlement_days, 2)  # 当月结算收入
                     if item['end_time'] < end_time_stamp:
                         uid_bucket_statistics = UidBucketStatistics.objects.filter(time__gte=start_time_stamp,
                                                                                    time__lt=item['end_time'],
@@ -1147,6 +1156,7 @@ class CronCollectDataView(View):
                                                                           settlement_days=settlement_days,
                                                                           actual_api=actual_api,
                                                                           created_time=create_time,
+                                                                          start_time=order_start_time,
                                                                           remaining_usage_time=remaining_usage_time)
             print('结束')
         except Exception as e: