SysManage.py 8.0 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.ConstantEnum import ConstantEnum
  25. from Object.Enums.RedisKeyConstant import RedisKeyConstant
  26. from Object.RedisObject import RedisObject
  27. from Object.ResponseObject import ResponseObject
  28. from Object.TokenObject import TokenObject
  29. from Service.CommonService import CommonService
  30. from Service.ModelService import ModelService
  31. @csrf_exempt
  32. def updateLog(request):
  33. response = ResponseObject()
  34. request.encoding = 'utf-8'
  35. if request.method == 'GET':
  36. request_dict = request.GET
  37. if request.method == 'POST':
  38. request_dict = request.POST
  39. else:
  40. response.json(404)
  41. token = request_dict.get('token', None)
  42. content = request_dict.get('content', None)
  43. if not content:
  44. return response.json(444, 'content')
  45. tko = TokenObject(token)
  46. response.lang = tko.lang
  47. if tko.code != 0:
  48. return response.json(tko.code)
  49. userID = tko.userID
  50. own_permission = ModelService.check_perm(userID=userID, permID=50)
  51. if own_permission is not True:
  52. return response.json(404)
  53. file_path = os.path.join(BASE_DIR, 'static/Help/version.txt')
  54. try:
  55. with open(file_path, "w", encoding="utf-8") as f:
  56. f.write(content)
  57. except Exception as e:
  58. pass
  59. return response.json(0)
  60. class getStatView(View):
  61. def post(self, request, *args, **kwargs):
  62. request.encoding = 'utf-8'
  63. filePath = kwargs.get('filePath', None)
  64. filePath.encode(encoding='utf-8', errors='strict')
  65. response = ResponseObject()
  66. return self.getFile(filePath, response)
  67. def get(self, request, *args, **kwargs):
  68. request.encoding = 'gb2312'
  69. filePath = kwargs.get('filePath', None)
  70. response = ResponseObject()
  71. filePath.encode(encoding='gb2312', errors='strict')
  72. return self.getFile(filePath, response)
  73. def getFile(self, filePath, response):
  74. if filePath:
  75. pass
  76. else:
  77. return response.json(800)
  78. fullPath = os.path.join(BASE_DIR, filePath).replace('\\', '/')
  79. from var_dump import var_dump
  80. var_dump(fullPath)
  81. if os.path.isfile(fullPath):
  82. try:
  83. Imagedata = open(fullPath, 'rb').read()
  84. except Exception as e:
  85. return response.json(906, repr(e))
  86. else:
  87. return HttpResponse(Imagedata, content_type="image/jpeg")
  88. else:
  89. return response.json(907)
  90. from Service.EquipmentInfoService import EquipmentInfoService
  91. def initMsgFunc(request):
  92. response = ResponseObject()
  93. request.encoding = 'utf-8'
  94. if request.method == 'GET':
  95. request_dict = request.GET
  96. if request.method == 'POST':
  97. request_dict = request.POST
  98. else:
  99. response.json(404)
  100. token = request_dict.get('token', None)
  101. tko = TokenObject(token)
  102. response.lang = tko.lang
  103. if tko.code != 0:
  104. return response.json(tko.code)
  105. userID = tko.userID
  106. seven_days_ago = int(time.time()) - 3600 * 24 * 7 # 过滤七天前数据
  107. sm_count = SysMsgModel.objects.filter(userID_id=userID, status=0).count()
  108. kwargs = {
  109. 'device_user_id': userID,
  110. 'event_time__gt': seven_days_ago,
  111. 'status': 0
  112. }
  113. eq_count = EquipmentInfoService.count_equipment_info(**kwargs)
  114. # kwargs['event_type'] = 57
  115. # rq_count = EquipmentInfoService.count_equipment_info(**kwargs)
  116. rq_count = 0 # 影响查询性能APP未调用该属性,统一返回0
  117. ai_count = Ai_Push_Info.objects.filter(userID_id=userID, eventTime__gt=seven_days_ago, status=False).count()
  118. uid_reset_count = Device_Info.objects.filter(userID_id=userID, isExist=2).count()
  119. res = {
  120. 'sm_count': sm_count, # 系统消息未读数量
  121. 'eq_count': eq_count, # 未读消息总数
  122. 'rq_count': rq_count, # 人形检测总数
  123. 'ai_count': ai_count, # AI消息总数
  124. 'uid_reset_count': uid_reset_count, # 复位的设备数量
  125. }
  126. # 如果没有发放过云存优惠券,异步发放
  127. now_time = int(time.time())
  128. start_time, end_time = ConstantEnum.PROMOTION_START_TIME.value, ConstantEnum.PROMOTION_END_TIME.value
  129. # 缓存过期时间
  130. expire = end_time - now_time
  131. if (now_time > start_time and expire > 0) or userID in ConstantEnum.TEST_ACCOUNT_LIST.value:
  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