AgentCustomerController.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : AgentCustomerController.py
  4. @Time : 2024/3/7 16:56
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. from django.http import QueryDict
  10. from django.views import View
  11. from django.core.paginator import Paginator
  12. from datetime import datetime
  13. from AgentModel.models import AgentCustomerInfo, AgentCustomerCard, AgentCustomerPackage, AgentCloudServicePackage, \
  14. AgentDeviceOrder
  15. from Model.models import UnicomCombo, Store_Meal, Device_User
  16. from Object.ResponseObject import ResponseObject
  17. from Object.TokenObject import TokenObject
  18. class AgentCustomerView(View):
  19. def get(self, request, *args, **kwargs):
  20. request.encoding = 'utf-8'
  21. operation = kwargs.get('operation')
  22. return self.validation(request.GET, request, operation)
  23. def post(self, request, *args, **kwargs):
  24. request.encoding = 'utf-8'
  25. operation = kwargs.get('operation')
  26. return self.validation(request.POST, request, operation)
  27. def delete(self, request, *args, **kwargs):
  28. request.encoding = 'utf-8'
  29. operation = kwargs.get('operation')
  30. delete = QueryDict(request.body)
  31. if not delete:
  32. delete = request.GET
  33. return self.validation(delete, request, operation)
  34. def put(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. operation = kwargs.get('operation')
  37. put = QueryDict(request.body)
  38. return self.validation(put, request, operation)
  39. def validation(self, request_dict, request, operation):
  40. AgentCustomerInfo.objects.filter()
  41. language = request_dict.get('language', 'en')
  42. response = ResponseObject(language, 'pc')
  43. if operation == 'getUnicomAndIcloud':
  44. return self.get_unicom_and_icloud(response)
  45. else:
  46. return response.json(444, 'operation')
  47. tko = TokenObject(
  48. request.META.get('HTTP_AUTHORIZATION'),
  49. returntpye='pc')
  50. if tko.code != 0:
  51. return response.json(tko.code)
  52. response.lang = tko.lang
  53. userID = tko.userID
  54. if operation == 'getAgentServicePackage':
  55. return self.get_agent_service_package(request_dict, response)
  56. else:
  57. return response.json(444, 'operation')
  58. def get_unicom_and_icloud(self, response):
  59. """
  60. 查询云存储套餐和物联网卡套餐列表
  61. @param response: 响应对象
  62. @return:
  63. """
  64. try:
  65. # 云存储套餐名称查询,只包括is_show=1的记录
  66. store_meal_names = Store_Meal.objects.filter(is_show=1).values_list('bucket__bucket', flat=True).distinct()
  67. # 联通套餐名称查询,只包括is_show=1且未被删除的记录
  68. unicom_combo_names = UnicomCombo.objects.filter(is_show=1, is_del=False).values_list('combo_name',
  69. flat=True).distinct()
  70. # 将查询结果转换为列表
  71. store_meal_name_list = list(store_meal_names)
  72. unicom_combo_name_list = list(unicom_combo_names)
  73. # 合并结果并返回
  74. return response.json(0, {
  75. 'storeMealNames': store_meal_name_list,
  76. 'unicomComboNames': unicom_combo_name_list,
  77. })
  78. except Exception as e:
  79. print(e)
  80. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  81. def get_agent_service_package(self, request_dict, response):
  82. """
  83. 查询所有代理云服务套餐
  84. @param request_dict: 请求参数
  85. @request_dict page: 页码
  86. @request_dict page_size: 查询分页数
  87. @param userID: userID
  88. @param response: 响应对象
  89. @return:
  90. """
  91. page = request_dict.get('page', 1)
  92. page_size = request_dict.get('page_size', 10)
  93. try:
  94. # 查询所有有效的代理云服务套餐
  95. all_packages = AgentCloudServicePackage.objects.filter(status=1).order_by('-created_time')
  96. # 创建分页对象
  97. paginator = Paginator(all_packages, page_size)
  98. # 获取请求页的数据
  99. packages_page = paginator.page(page)
  100. # 准备响应数据,转换查询集为列表形式
  101. packages_list = list(packages_page.object_list.values(
  102. 'id', 'service_name', 'package_id', 'type',
  103. 'profit_type', 'cost', 'profit', 'status',
  104. 'created_time', 'updated_time'
  105. ))
  106. # 返回分页数据
  107. return response.json(0,{
  108. 'page': page,
  109. 'page_size': page_size,
  110. 'total': paginator.count,
  111. 'num_pages': paginator.num_pages,
  112. 'packages': packages_list
  113. })
  114. except Exception as e:
  115. # 出错时返回错误信息
  116. return response.json({'error': str(e)}, status=500)