|
@@ -48,8 +48,6 @@ class AgentDeviceView(View):
|
|
return self.validation(put, request, operation)
|
|
return self.validation(put, request, operation)
|
|
|
|
|
|
def validation(self, request_dict, request, operation):
|
|
def validation(self, request_dict, request, operation):
|
|
-
|
|
|
|
- pass
|
|
|
|
language = request_dict.get('language', 'en')
|
|
language = request_dict.get('language', 'en')
|
|
response = ResponseObject(language, 'pc')
|
|
response = ResponseObject(language, 'pc')
|
|
# 订单结算界面
|
|
# 订单结算界面
|
|
@@ -65,6 +63,8 @@ class AgentDeviceView(View):
|
|
userID = tko.userID
|
|
userID = tko.userID
|
|
if operation == 'getAgentDevice':
|
|
if operation == 'getAgentDevice':
|
|
return self.get_agent_device(userID, request_dict, response)
|
|
return self.get_agent_device(userID, request_dict, response)
|
|
|
|
+ elif operation == 'getAgentDeviceOrder':
|
|
|
|
+ return self.get_agent_device_order(userID, request_dict, response)
|
|
else:
|
|
else:
|
|
return response.json(444, 'operation')
|
|
return response.json(444, 'operation')
|
|
|
|
|
|
@@ -122,3 +122,97 @@ class AgentDeviceView(View):
|
|
except Exception as e:
|
|
except Exception as e:
|
|
print(e)
|
|
print(e)
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
|
+
|
|
|
|
+ def calculate_profit_or_revenue(self, agent_device_orders, package_details, time_unit, metric_type, start_time, end_time):
|
|
|
|
+ """
|
|
|
|
+ 计算利润或者营业额
|
|
|
|
+ @param agent_device_orders: 代理设备订单
|
|
|
|
+ @param package_details: 代理套餐详情
|
|
|
|
+ @param time_unit: 时间单位
|
|
|
|
+ @param metric_type: 利润或者营业额
|
|
|
|
+ @param start_time: 开始时间
|
|
|
|
+ @param end_time: 结束时间
|
|
|
|
+ @return:
|
|
|
|
+ """
|
|
|
|
+ summary = defaultdict(lambda: {"云存": Decimal('0.00'), "4G": Decimal('0.00'), "all": Decimal('0.00')})
|
|
|
|
+ time_format = {
|
|
|
|
+ "month": "%Y-%m",
|
|
|
|
+ "year": "%Y",
|
|
|
|
+ "quarter": lambda x: f"{x.year}年{(x.month - 1) // 3 + 1}季度"
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for order in agent_device_orders:
|
|
|
|
+ package = package_details.get(order.csp_id)
|
|
|
|
+ if not package:
|
|
|
|
+ continue
|
|
|
|
+
|
|
|
|
+ # 根据利润类型计算利润或者直接使用营业额
|
|
|
|
+ if metric_type == 1: # 利润
|
|
|
|
+ profit = package.profit if package.profit_type == 1 else order.profit_amount * package.profit / Decimal(
|
|
|
|
+ '100.00')
|
|
|
|
+ else: # 营业额
|
|
|
|
+ profit = order.profit_amount
|
|
|
|
+
|
|
|
|
+ # 区分云服务 + 4G套餐并加入 summary
|
|
|
|
+ service_type = "icloud" if package.type == 1 else "unicom"
|
|
|
|
+ time_key = datetime.fromtimestamp(order.created_time).strftime(
|
|
|
|
+ time_format[time_unit]) if time_unit != "quarter" else time_format[time_unit](
|
|
|
|
+ datetime.fromtimestamp(order.created_time))
|
|
|
|
+
|
|
|
|
+ summary[time_key][service_type] += profit
|
|
|
|
+ summary[time_key]["all"] += profit
|
|
|
|
+
|
|
|
|
+ # 补全时间段内所有可能的时间单位
|
|
|
|
+ current_time = start_time
|
|
|
|
+ end_time += relativedelta(days=1) # 包括结束日期
|
|
|
|
+
|
|
|
|
+ while current_time < end_time:
|
|
|
|
+ time_key = current_time.strftime(time_format[time_unit]) if time_unit != "quarter" else time_format[
|
|
|
|
+ time_unit](current_time)
|
|
|
|
+ if time_key not in summary:
|
|
|
|
+ summary[time_key] = {"云存": Decimal('0.00'), "4G": Decimal('0.00'), "all": Decimal('0.00')}
|
|
|
|
+ current_time += relativedelta(months=1) if time_unit == "month" else relativedelta(
|
|
|
|
+ years=1) if time_unit == "year" else relativedelta(months=3)
|
|
|
|
+
|
|
|
|
+ return [{"time": time, **data} for time, data in sorted(summary.items())]
|
|
|
|
+
|
|
|
|
+ def get_agent_device_order(self, userID, request_dict, response):
|
|
|
|
+ """
|
|
|
|
+ 查询设备订单明细
|
|
|
|
+ @param userID: userID
|
|
|
|
+ @param request_dict: 请求参数
|
|
|
|
+ @param request_dict startTime: 开始时间
|
|
|
|
+ @param request_dict endTime: 结束时间
|
|
|
|
+ @param request_dict timeUnit: 时间单位
|
|
|
|
+ @param request_dict metric_type: 利润或者营业额
|
|
|
|
+ @param response: 响应对象
|
|
|
|
+ @return:
|
|
|
|
+ """
|
|
|
|
+ try:
|
|
|
|
+ startTime = int(request_dict.get('startTime'))
|
|
|
|
+ endTime = int(request_dict.get('endTime'))
|
|
|
|
+ timeUnit = request_dict.get('timeUnit')
|
|
|
|
+ metric_type = int(request_dict.get('metric_type'))
|
|
|
|
+
|
|
|
|
+ agent_customer_info = AgentCustomerInfo.objects.filter(user_id=userID).first()
|
|
|
|
+ if not agent_customer_info:
|
|
|
|
+ return response.json(104, 'Agent customer not found')
|
|
|
|
+
|
|
|
|
+ agent_device_orders = AgentDeviceOrder.objects.filter(
|
|
|
|
+ ac_id=agent_customer_info.id, created_time__gte=startTime, created_time__lte=endTime, status__in=[1, 2]
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+ # 获取代理套餐包id
|
|
|
|
+ package_ids = agent_device_orders.values_list('csp_id', flat=True).distinct()
|
|
|
|
+ package_details = {pkg.id: pkg for pkg in AgentCloudServicePackage.objects.filter(id__in=package_ids)}
|
|
|
|
+
|
|
|
|
+ start_time = datetime.fromtimestamp(startTime)
|
|
|
|
+ end_time = datetime.fromtimestamp(endTime)
|
|
|
|
+
|
|
|
|
+ result = self.calculate_profit_or_revenue(agent_device_orders, package_details, timeUnit, metric_type,
|
|
|
|
+ start_time, end_time)
|
|
|
|
+ return response.json(0, {'list': result})
|
|
|
|
+
|
|
|
|
+ except Exception as e:
|
|
|
|
+ error_msg = f"error_line:{traceback.format_exc()}, error_msg:{str(e)}"
|
|
|
|
+ return response.json(500, error_msg)
|