# -*- encoding: utf-8 -*- """ @File : UserDataController.py @Time : 2024年6月7日09:27:28 @Author : peng @Email : peng @Software: PyCharm """ from django.db.models import Q, Sum from django.views.generic.base import View import datetime import requests from Model.models import OperatingCosts, Order_Model, CountryModel, UID_Bucket from Service.CommonService import CommonService from Ansjer.config import CONFIG_EUR, CONFIG_INFO, CONFIG_CN, CONFIG_US from dateutil.relativedelta import relativedelta # 运营成本数据 class OperatingCostsDataView(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 == 'getOperatingCosts': # 查询订单成本利润 return self.get_operating_costs(request, request_dict, response) else: return response.json(414) @classmethod def get_operating_costs(cls, request, request_dict, response): """ 查询订单成本利润 @param request:请求参数 @param request_dict:请求参数 @request_dict startTime:开始时间 @request_dict endTime:结束时间 @param response:响应对象 @return: """ start_time = request_dict.get('startTime', None) end_time = request_dict.get('endTime', None) page = request_dict.get('page', 1) line = request_dict.get('line', 10) if not all([page, line]): return response.json(444, {'error param': 'page or line'}) page = int(page) line = int(line) # params = {'page': page, 'line': line} try: if start_time and end_time: operating_costs_qs = OperatingCosts.objects.filter(Q(time__gte=start_time), Q(time__lt=end_time)) # params['startTime'] = start_time # params['endTime'] = end_time 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( '-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 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 = order_qs[0]['fee'] 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']) - float(order_qs[0]['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'] }) # if CONFIG_INFO == CONFIG_US: # headers = { # 'Authorization': request.META.get('HTTP_AUTHORIZATION') # } # eur_response = requests.get('https://www.zositeche.com/dataManagement/costsData/getOperatingCosts', # params=params, headers=headers) # if eur_response.status_code == 200: # eur_result = eur_response.json() # if eur_result['result_code'] == 0: # res += eur_result['result']['res'] # count += eur_result['result']['count'] # res = CommonService.list_sort_v2(res, 'time') return response.json(0, {'count': count, 'res': res}) except Exception as e: return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))