Cloudsum.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. from Model.models import Order_Model, UID_Bucket, UserExModel, App_Info, Device_User
  2. from django.http import JsonResponse
  3. from Object.ResponseObject import ResponseObject
  4. from Object.TokenObject import TokenObject
  5. from django.views import View
  6. from django.utils.decorators import method_decorator
  7. from django.views.generic import TemplateView
  8. from django.views.decorators.csrf import csrf_exempt
  9. from Service.ModelService import ModelService
  10. class Cloudsum(TemplateView):
  11. @method_decorator(csrf_exempt)
  12. def dispatch(self, *args, **kwargs):
  13. return super(Cloudsum, self).dispatch(*args, **kwargs)
  14. def get(self, request, *args, **kwargs):
  15. request.encoding = 'utf-8'
  16. return self.validation(request_dict=request.GET)
  17. def post(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. return self.validation(request_dict=request.POST)
  20. def validation(self, request_dict, *args, **kwargs):
  21. response = ResponseObject()
  22. token = request_dict.get('token', None)
  23. tko = TokenObject(token)
  24. response.lang = tko.lang
  25. if tko.code != 0:
  26. return response.json(tko.code)
  27. userID = tko.userID
  28. operation = request_dict.get('operation', None)
  29. print("userID",userID)
  30. print('operation:', operation)
  31. if userID is None or operation is None:
  32. return response.json(444, 'operation')
  33. if operation == 'cloudservicesum':
  34. return self.cloudservicesum(userID, response)
  35. if operation == 'userappversion':
  36. return self.userappversion(userID, response)
  37. if operation == 'usercount':
  38. return self.usercount(userID, response)
  39. # 类型:用户手机
  40. # 统计用户手机型号 已有
  41. # 统计某段时间内新用户的增长数
  42. # 统计一天用户打开zosi app的频率 已有
  43. # 统计用户下载zosi app数,卸载人数
  44. # 统计用户使用zosi app的版本√
  45. def userappversion(self, userID, response):
  46. # own_permission = ModelService.check_perm(userID=userID, permID=30)
  47. # if own_permission is not True:
  48. # return response.json(404)
  49. version = UserExModel.objects.values_list('appBundleId').order_by('appBundleId').distinct()
  50. res = App_Info.objects.filter(appBundleId__in=version).values_list('appBundleId','newAppversion')
  51. return response.json(0, dict(res))
  52. # 类型:云存服务统计
  53. # 统计开通云存的设备有多少台√
  54. # 统计未支付,支付成功的订单√
  55. # 统计开通云存的增长率
  56. # 统计已开通云存的各套餐总数√
  57. # 统计已支付的订单总金额√
  58. def cloudservicesum(self, userID, response):
  59. # own_permission = ModelService.check_perm(userID=userID, permID=30)
  60. # if own_permission is not True:
  61. # return response.json(404)
  62. from django.db.models import Sum
  63. uid_cloud = UID_Bucket.objects.values('uid').distinct().count()
  64. pay_0 = Order_Model.objects.filter(status='0').count()
  65. pay_1 = Order_Model.objects.filter(status='1').count()
  66. money = Order_Model.objects.filter(status='1').aggregate(Sum('price'))
  67. print(type(money))
  68. print(money)
  69. cloud_sum = UID_Bucket.objects.all().count()
  70. data_dict = {
  71. "开通云存的设备数": uid_cloud,
  72. "未支付的订单数": pay_0,
  73. "已支付的订单数": pay_1,
  74. "已支付的订单总金额": money['price__sum'],
  75. "已开通云存的各套餐总数": cloud_sum,
  76. }
  77. return response.json(0, data_dict)
  78. def usercount(self, userID, response):
  79. from django.db.models import Count
  80. # own_permission = ModelService.check_perm(userID=userID, permID=30)
  81. # if own_permission is not True:
  82. # return response.json(404)
  83. count1 = Device_User.objects.extra(select={'year': 'year(data_joined)', 'month': 'month(data_joined)'}).values('year','month').annotate(用户合计=Count('data_joined')).order_by()
  84. #print(count1)
  85. return response.json(0, list(count1))
  86. class Cloudsum_test(View):
  87. def dispatch(self, requset, *args, **kwargs):
  88. return super(Cloudsum_test, self).dispatch(requset, *args, **kwargs)
  89. def get(self, request, *args, **kwargs):
  90. operation = kwargs.get('operation')
  91. request.encoding = 'utf-8'
  92. return self.validation(request.GET, request, operation)
  93. def post(self, request, *args, **kwargs):
  94. operation = kwargs.get('operation')
  95. print('operation:',operation)
  96. request.encoding = 'utf-8'
  97. return self.validation(request.POST, request, operation)
  98. def validation(self, request_dict, request, operation):
  99. response = ResponseObject()
  100. token = request_dict.get('token', None)
  101. tko = TokenObject(token)
  102. response.lang = tko.lang
  103. userID = tko.userID
  104. print('userID:',userID)
  105. if tko.code != 0:
  106. return response.json(tko.code)
  107. if userID is None or operation is None:
  108. return response.json(444, 'operation')
  109. if operation == 'cloudservicesum':
  110. return self.cloudservicesum(userID, response)
  111. def cloudservicesum(self, userID, response):
  112. # own_permission = ModelService.check_perm(userID=userID, permID=30)
  113. # if own_permission is not True:
  114. # return response.json(404)
  115. from django.db.models import Sum
  116. uid_cloud = UID_Bucket.objects.values('uid').distinct().count()
  117. pay_0 = Order_Model.objects.filter(status='0').count()
  118. pay_1 = Order_Model.objects.filter(status='1').count()
  119. money = Order_Model.objects.filter(status='1').aggregate(Sum('price'))
  120. cloud_sum = UID_Bucket.objects.all().count()
  121. data_dict = {
  122. "开通云存的设备数": uid_cloud,
  123. "未支付的订单数": pay_0,
  124. "已支付的订单数": pay_1,
  125. "已支付的订单总金额": money['price__sum'],
  126. "已开通云存的各套餐总数": cloud_sum,
  127. }
  128. return response.json(0, data_dict)
  129. def test(request):
  130. from django.db.models import Sum
  131. pay_0 = Order_Model.objects.filter(status='0').count()
  132. pay_1 = Order_Model.objects.filter(status='1').count()
  133. money = Order_Model.objects.filter(status='1').aggregate(Sum('price'))
  134. print(pay_0)
  135. print(money['price__sum'])
  136. data_dict = {
  137. "未支付的订单数": pay_0,
  138. "已支付的订单数": pay_1,
  139. "已支付的订单总金额": money['price__sum'],
  140. }
  141. return JsonResponse(data_dict, json_dumps_params={'ensure_ascii':False},safe=False)