# -*- encoding: utf-8 -*- """ @File : AgentCustomerController.py @Time : 2024/3/7 16:56 @Author : stephen @Email : zhangdongming@asj6.wecom.work @Software: PyCharm """ from django.http import QueryDict from django.views import View from django.core.paginator import Paginator from datetime import datetime from AgentModel.models import AgentCustomerInfo, AgentCustomerCard, AgentCustomerPackage, AgentCloudServicePackage, \ AgentDeviceOrder from Model.models import UnicomCombo, Store_Meal, Device_User from Object.ResponseObject import ResponseObject from Object.TokenObject import TokenObject class AgentCustomerView(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 delete(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') delete = QueryDict(request.body) if not delete: delete = request.GET return self.validation(delete, request, operation) def put(self, request, *args, **kwargs): request.encoding = 'utf-8' operation = kwargs.get('operation') put = QueryDict(request.body) return self.validation(put, request, operation) def validation(self, request_dict, request, operation): AgentCustomerInfo.objects.filter() language = request_dict.get('language', 'en') response = ResponseObject(language, 'pc') if operation == 'getUnicomAndIcloud': return self.get_unicom_and_icloud(response) else: return response.json(444, 'operation') tko = TokenObject( request.META.get('HTTP_AUTHORIZATION'), returntpye='pc') if tko.code != 0: return response.json(tko.code) response.lang = tko.lang userID = tko.userID if operation == 'getAgentServicePackage': return self.get_agent_service_package(request_dict, response) else: return response.json(444, 'operation') def get_unicom_and_icloud(self, response): """ 查询云存储套餐和物联网卡套餐列表 @param response: 响应对象 @return: """ try: # 云存储套餐名称查询,只包括is_show=1的记录 store_meal_names = Store_Meal.objects.filter(is_show=1).values_list('bucket__bucket', flat=True).distinct() # 联通套餐名称查询,只包括is_show=1且未被删除的记录 unicom_combo_names = UnicomCombo.objects.filter(is_show=1, is_del=False).values_list('combo_name', flat=True).distinct() # 将查询结果转换为列表 store_meal_name_list = list(store_meal_names) unicom_combo_name_list = list(unicom_combo_names) # 合并结果并返回 return response.json(0, { 'storeMealNames': store_meal_name_list, 'unicomComboNames': unicom_combo_name_list, }) except Exception as e: print(e) return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e))) def get_agent_service_package(self, request_dict, response): """ 查询所有代理云服务套餐 @param request_dict: 请求参数 @request_dict page: 页码 @request_dict page_size: 查询分页数 @param userID: userID @param response: 响应对象 @return: """ page = request_dict.get('page', 1) page_size = request_dict.get('page_size', 10) try: # 查询所有有效的代理云服务套餐 all_packages = AgentCloudServicePackage.objects.filter(status=1).order_by('-created_time') # 创建分页对象 paginator = Paginator(all_packages, page_size) # 获取请求页的数据 packages_page = paginator.page(page) # 准备响应数据,转换查询集为列表形式 packages_list = list(packages_page.object_list.values( 'id', 'service_name', 'package_id', 'type', 'profit_type', 'cost', 'profit', 'status', 'created_time', 'updated_time' )) # 返回分页数据 return response.json(0,{ 'page': page, 'page_size': page_size, 'total': paginator.count, 'num_pages': paginator.num_pages, 'packages': packages_list }) except Exception as e: # 出错时返回错误信息 return response.json({'error': str(e)}, status=500)