SysManage.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD018
  6. @NAME: Ansjer
  7. @software: PyCharm
  8. @DATE: 2018/6/8 9:10
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: SysManage.py
  12. @Contact: chanjunkai@163.com
  13. """
  14. import os
  15. import threading
  16. import time
  17. from django.http import HttpResponse
  18. from django.views.decorators.csrf import csrf_exempt
  19. from django.views.generic.base import View
  20. from Ansjer.config import BASE_DIR
  21. from Controller.CouponController import CouponView
  22. from Model.models import SysMsgModel, Device_Info, Ai_Push_Info, UidSetModel, ExperienceContextModel, Order_Model, \
  23. UID_Bucket
  24. from Object.Enums.RedisKeyConstant import RedisKeyConstant
  25. from Object.RedisObject import RedisObject
  26. from Object.ResponseObject import ResponseObject
  27. from Object.TokenObject import TokenObject
  28. from Service.CommonService import CommonService
  29. from Service.ModelService import ModelService
  30. @csrf_exempt
  31. def updateLog(request):
  32. response = ResponseObject()
  33. request.encoding = 'utf-8'
  34. if request.method == 'GET':
  35. request_dict = request.GET
  36. if request.method == 'POST':
  37. request_dict = request.POST
  38. else:
  39. response.json(404)
  40. token = request_dict.get('token', None)
  41. content = request_dict.get('content', None)
  42. if not content:
  43. return response.json(444, 'content')
  44. tko = TokenObject(token)
  45. response.lang = tko.lang
  46. if tko.code != 0:
  47. return response.json(tko.code)
  48. userID = tko.userID
  49. own_permission = ModelService.check_perm(userID=userID, permID=50)
  50. if own_permission is not True:
  51. return response.json(404)
  52. file_path = os.path.join(BASE_DIR, 'static/Help/version.txt')
  53. try:
  54. with open(file_path, "w", encoding="utf-8") as f:
  55. f.write(content)
  56. except Exception as e:
  57. pass
  58. return response.json(0)
  59. class getStatView(View):
  60. def post(self, request, *args, **kwargs):
  61. request.encoding = 'utf-8'
  62. filePath = kwargs.get('filePath', None)
  63. filePath.encode(encoding='utf-8', errors='strict')
  64. response = ResponseObject()
  65. return self.getFile(filePath, response)
  66. def get(self, request, *args, **kwargs):
  67. request.encoding = 'gb2312'
  68. filePath = kwargs.get('filePath', None)
  69. response = ResponseObject()
  70. filePath.encode(encoding='gb2312', errors='strict')
  71. return self.getFile(filePath, response)
  72. def getFile(self, filePath, response):
  73. if filePath:
  74. pass
  75. else:
  76. return response.json(800)
  77. fullPath = os.path.join(BASE_DIR, filePath).replace('\\', '/')
  78. from var_dump import var_dump
  79. var_dump(fullPath)
  80. if os.path.isfile(fullPath):
  81. try:
  82. Imagedata = open(fullPath, 'rb').read()
  83. except Exception as e:
  84. return response.json(906, repr(e))
  85. else:
  86. return HttpResponse(Imagedata, content_type="image/jpeg")
  87. else:
  88. return response.json(907)
  89. from Service.EquipmentInfoService import EquipmentInfoService
  90. def initMsgFunc(request):
  91. response = ResponseObject()
  92. request.encoding = 'utf-8'
  93. if request.method == 'GET':
  94. request_dict = request.GET
  95. if request.method == 'POST':
  96. request_dict = request.POST
  97. else:
  98. response.json(404)
  99. token = request_dict.get('token', None)
  100. tko = TokenObject(token)
  101. response.lang = tko.lang
  102. if tko.code != 0:
  103. return response.json(tko.code)
  104. userID = tko.userID
  105. seven_days_ago = int(time.time()) - 3600 * 24 * 7 # 过滤七天前数据
  106. sm_count = SysMsgModel.objects.filter(userID_id=userID, status=0).count()
  107. kwargs = {
  108. 'device_user_id': userID,
  109. 'event_time__gt': seven_days_ago,
  110. 'status': 0
  111. }
  112. eq_count = EquipmentInfoService.count_equipment_info(**kwargs)
  113. # kwargs['event_type'] = 57
  114. # rq_count = EquipmentInfoService.count_equipment_info(**kwargs)
  115. rq_count = 0 # 影响查询性能APP未调用该属性,统一返回0
  116. ai_count = Ai_Push_Info.objects.filter(userID_id=userID, eventTime__gt=seven_days_ago, status=False).count()
  117. uid_reset_count = Device_Info.objects.filter(userID_id=userID, isExist=2).count()
  118. res = {
  119. 'sm_count': sm_count, # 系统消息未读数量
  120. 'eq_count': eq_count, # 未读消息总数
  121. 'rq_count': rq_count, # 人形检测总数
  122. 'ai_count': ai_count, # AI消息总数
  123. 'uid_reset_count': uid_reset_count, # 复位的设备数量
  124. }
  125. # 如果没有发放过云存优惠券,异步发放
  126. now_time = int(time.time())
  127. # 活动时间: 东部时间2024-12-23 00:00:00 - 2024-12-29 23:59:59
  128. start_time, end_time = 1734930000, 1735534799
  129. # 缓存过期时间
  130. expire = end_time - now_time
  131. if expire > 0:
  132. redis_obj = RedisObject()
  133. distribute_key = RedisKeyConstant.CLOUD_STORAGE_COUPONS.value + userID
  134. is_distributed = redis_obj.get_data(distribute_key)
  135. if not is_distributed:
  136. thread_kwargs = {
  137. 'user_id': userID,
  138. 'redis_obj': redis_obj,
  139. 'distribute_key': distribute_key,
  140. 'expire': expire,
  141. 'end_time': end_time
  142. }
  143. del_push_info_thread = threading.Thread(
  144. target=distribute_cloud_storage_coupons,
  145. kwargs=thread_kwargs)
  146. del_push_info_thread.start()
  147. return response.json(0, res)
  148. def distribute_cloud_storage_coupons(user_id, redis_obj, distribute_key, expire, end_time):
  149. """
  150. 发放云存优惠券
  151. 满足条件,用户存在以下三种类型的设备:
  152. 1. 未体验云存设备
  153. 2. 体验过未购买过
  154. 3. 购买过且已过期
  155. @param user_id: 用户id
  156. @param redis_obj: redis对象
  157. @param distribute_key: 发放优惠券缓存key
  158. @param expire: distribute_key过期时间
  159. @param end_time: 活动结束时间
  160. @return:
  161. """
  162. try:
  163. generate_success = False
  164. uid_list = Device_Info.objects.filter(userID_id=user_id).values_list('uid', flat=True)
  165. if uid_list:
  166. uid_set_qs = UidSetModel.objects.filter(uid__in=uid_list).values('uid', 'ucode', 'device_type')
  167. if uid_set_qs.exists():
  168. for uid_set in uid_set_qs:
  169. uid = uid_set['uid']
  170. # 判断设备是否支持云存
  171. is_cloud_vod = CommonService.is_cloud_device(uid_set['ucode'], uid_set['device_type'])
  172. if is_cloud_vod:
  173. # 未体验云存
  174. experience_qs = ExperienceContextModel.objects.filter(uid=uid, experience_type=0)
  175. if not experience_qs.exists():
  176. # 发放
  177. generate_success = CouponView.generate_coupon_by_user(user_id)
  178. else:
  179. # 体验过未购买过
  180. orders_qs = Order_Model.objects.filter(UID=uid, status=1, payType__in=[1, 4, 5])
  181. if not orders_qs.exists():
  182. # 发放
  183. generate_success = CouponView.generate_coupon_by_user(user_id)
  184. else:
  185. # 购买过且已过期
  186. # 没有未使用套餐,且过期时间在2024-12-29前
  187. vod_uid_bucket_qs = UID_Bucket.objects.filter(uid=uid, has_unused=0, endTime__lte=end_time)
  188. if vod_uid_bucket_qs.exists():
  189. # 发放
  190. generate_success = CouponView.generate_coupon_by_user(user_id)
  191. if generate_success:
  192. redis_obj.set_data(distribute_key, '1', expire)
  193. else:
  194. redis_obj.set_data(distribute_key, '0', expire)
  195. except Exception as e:
  196. return