EquipmentInfo.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import time
  2. import traceback
  3. from django.utils.decorators import method_decorator
  4. from django.views.decorators.csrf import csrf_exempt
  5. from django.views.generic.base import View
  6. from Model.models import Device_Info, Equipment_Info, UidPushModel
  7. from Model.models import Device_User
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. from Service.EquipmentInfoService import EquipmentInfoService
  12. from Service.ModelService import ModelService
  13. class EquipmentInfo(View):
  14. @method_decorator(csrf_exempt)
  15. def dispatch(self, *args, **kwargs):
  16. return super(EquipmentInfo, self).dispatch(*args, **kwargs)
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. return self.validation(request_dict=request.GET)
  20. def post(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. return self.validation(request_dict=request.POST)
  23. def validation(self, request_dict, *args, **kwargs):
  24. token = request_dict.get('token', None)
  25. response = ResponseObject()
  26. tko = TokenObject(token)
  27. if tko.code != 0:
  28. return response.json(tko.code)
  29. userID = tko.userID
  30. operation = request_dict.get('operation', None)
  31. if not userID:
  32. return response.json(309)
  33. if operation == 'query':
  34. return self.query_info(request_dict, userID, response)
  35. elif operation == 'add':
  36. return self.add_info(request_dict, userID, response)
  37. elif operation == 'update':
  38. return self.update_info(request_dict, userID, response)
  39. elif operation == 'update-answer-status':
  40. return self.update_answer_status(request_dict, userID, response)
  41. elif operation == 'delete':
  42. return self.delete_info(request_dict, response)
  43. elif operation == 'findByTime':
  44. return self.findByTime_info(request_dict, userID, response)
  45. if operation == 'queryByAdmin':
  46. return self.query_all_info(request_dict, userID, response)
  47. if operation == 'deleteByAdmin':
  48. return self.delete_by_admin(request_dict, userID, response)
  49. else:
  50. return response.json(444, {"parm": "operation"})
  51. def add_info(self, request_dict, userID, response):
  52. devUid = request_dict.get('devUid', None)
  53. Channel = request_dict.get('Channel', None)
  54. eventType = request_dict.get('eventType', None)
  55. eventTime = request_dict.get('eventTime', None)
  56. receiveTime = request_dict.get('receiveTime', None)
  57. alarm = request_dict.get('alarm', None)
  58. if not devUid or not Channel or not eventType or not eventTime or not alarm or not receiveTime:
  59. return response.json(444, 'devUid, Channel, eventType, eventTime, alarm, receiveTime')
  60. own_device = ModelService.check_own_device(userID, UID=devUid)
  61. if own_device is not True:
  62. return response.json(14)
  63. device_info = Device_Info.objects.filter(UID=devUid, userID_id=userID)
  64. if not device_info.exists():
  65. return response.json(14)
  66. try:
  67. nowTime = int(time.time())
  68. equipment_info = Equipment_Info(
  69. userID_id=userID,
  70. devUid=devUid,
  71. Channel=Channel,
  72. eventType=eventType,
  73. receiveTime=receiveTime,
  74. eventTime=eventTime,
  75. devNickName=device_info[0].NickName,
  76. addTime=nowTime,
  77. alarm=alarm)
  78. equipment_info.save()
  79. uid_push_qs = UidPushModel.objects.filter(uid_set__uid=devUid, uid_set__detect_status=1). \
  80. values('token_val', 'app_type', 'appBundleId', 'm_code', 'push_type', 'userID_id', 'userID__NickName',
  81. 'lang', 'm_code', 'tz', 'uid_set__nickname', 'uid_set__detect_interval', 'uid_set__detect_group',
  82. 'uid_set__channel')
  83. tz = '0'
  84. if uid_push_qs.exists():
  85. tz = uid_push_qs.first().tz
  86. local_date_time = CommonService.get_now_time_str(n_time=nowTime, tz=tz, lang='cn')
  87. local_date_time = local_date_time[0:10]
  88. equipment_info_vo = EquipmentInfoService.get_equipment_info_obj(
  89. local_date_time,
  90. device_user_id=userID,
  91. event_time=nowTime,
  92. event_type=eventType,
  93. device_uid=devUid,
  94. device_nick_name=device_info[0].NickName,
  95. channel=Channel,
  96. alarm=alarm,
  97. receive_time=receiveTime,
  98. add_time=nowTime,
  99. )
  100. equipment_info_vo.save()
  101. except Exception:
  102. errorInfo = traceback.format_exc()
  103. print(errorInfo)
  104. return response.json(500, errorInfo)
  105. else:
  106. if equipment_info.id:
  107. return response.json(0, {'infoID': equipment_info.id,
  108. 'devUid': devUid,
  109. 'Channel': Channel,
  110. 'alarm': alarm,
  111. 'eventType': eventType,
  112. 'eventTime': eventTime,
  113. 'receiveTime': receiveTime,
  114. 'devNickName': equipment_info.devNickName})
  115. else:
  116. return response.json(500)
  117. def query_info(self, request_dict, userID, response):
  118. page = int(request_dict.get('page', None))
  119. line = int(request_dict.get('line', None))
  120. if not page or not line:
  121. return response.json(444, 'page,line')
  122. qs = Equipment_Info.objects.filter(userID_id=userID)
  123. uid = request_dict.get('uid', None)
  124. if uid:
  125. qs = qs.filter(devUid=uid)
  126. dvqs = Device_Info.objects.filter(UID=uid).values('Type', 'NickName')
  127. uid_type_dict = {uid: {'type': dvqs[0]['Type'], 'NickName': dvqs[0]['NickName']}}
  128. else:
  129. dvqs = Device_Info.objects.filter(userID_id=userID).values('UID', 'Type', 'NickName')
  130. uid_type_dict = {}
  131. for dv in dvqs:
  132. uid_type_dict[dv['UID']] = {'type': dv['Type'], 'NickName': dv['NickName']}
  133. if not qs.exists():
  134. return response.json(0, {'datas': [], 'count': 0})
  135. count = qs.count()
  136. res = qs[(page - 1) * line:page * line]
  137. send_json = CommonService.qs_to_dict(res)
  138. res = []
  139. for p in send_json['datas']:
  140. p['fields']['viewAccont'] = ''
  141. p['fields']['viewPwd'] = ''
  142. devUid = p['fields']['devUid']
  143. if devUid in uid_type_dict.keys():
  144. p['fields']['uid_type'] = uid_type_dict[devUid]['type']
  145. p['fields']['devNickName'] = uid_type_dict[devUid]['NickName']
  146. else:
  147. p['uid_type'] = ''
  148. res.append(p)
  149. return response.json(0, {'datas': res, 'count': count})
  150. @staticmethod
  151. def update_info(request_dict, userID, response):
  152. is_update_all = int(request_dict.get('is_update_all', 0))
  153. event_type = request_dict.get('eventType', None)
  154. if is_update_all == 1:
  155. kwargs = {
  156. 'device_user_id': userID
  157. }
  158. if event_type:
  159. kwargs['event_type'] = int(event_type)
  160. try:
  161. EquipmentInfoService.all_read_equipment_info(**kwargs)
  162. except Exception as e:
  163. print(repr(e))
  164. else:
  165. id_list = request_dict.getlist('id[]', None)
  166. if id_list is None or len(id_list) < 1:
  167. id_list = request_dict.getlist('id', None)
  168. if not id_list:
  169. return response.json(444)
  170. for full_id in id_list:
  171. try:
  172. ei_id = int(full_id[2:])
  173. equipment_info_model = EquipmentInfoService.get_equipment_info_model_with_full_id(full_id)
  174. equipment_info_model.objects.filter(id=ei_id).update(status=1)
  175. except Exception as e:
  176. print(repr(e))
  177. return response.json(0)
  178. @staticmethod
  179. def update_answer_status(request_dict, user_id, response):
  180. """
  181. 更新一键通话消息状态为已接听
  182. @param request_dict:
  183. @param user_id: 用户id
  184. @param response:
  185. @return:
  186. """
  187. uid = request_dict.get('uid', None)
  188. event_time = request_dict.get('event_time', None)
  189. if not all([uid, event_time]):
  190. return response.json(444)
  191. kwargs = {
  192. 'device_user_id': user_id,
  193. 'device_uid': uid,
  194. 'event_time': event_time,
  195. 'event_type__in': [606, 607]
  196. }
  197. try:
  198. EquipmentInfoService.update_equipment_info_answer_status(**kwargs)
  199. except Exception as e:
  200. print(repr(e))
  201. return response.json(0)
  202. @staticmethod
  203. def delete_info(request_dict, response):
  204. id_list = request_dict.getlist('id[]', None)
  205. if id_list is None or len(id_list) < 1:
  206. id_list = request_dict.getlist('id', None)
  207. if not id_list:
  208. return response.json(444)
  209. try:
  210. for full_id in id_list:
  211. ei_id = int(full_id[2:])
  212. equipment_info_model = EquipmentInfoService.get_equipment_info_model_with_full_id(full_id)
  213. equipment_info_model.objects.filter(id=ei_id).delete()
  214. except Exception as e:
  215. print(repr(e))
  216. return response.json(0)
  217. def findByTime_info(self, request_dict, userID, response):
  218. startTime = request_dict.get('startTime')
  219. endTime = request_dict.get('endTime')
  220. page = int(request_dict.get('page', None))
  221. line = int(request_dict.get('line', None))
  222. if not startTime or not endTime or not page or not line:
  223. return response.json(444, 'startTime, endTime, page, line')
  224. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  225. if not len(uid_list):
  226. return response.json(0, {'datas': [], 'count': 0})
  227. qs = Equipment_Info.objects.filter(userID_id=userID,
  228. eventTime__range=(startTime, endTime)).order_by('-id')
  229. if qs.exists():
  230. count = qs.count()
  231. res = qs[(page - 1) * line:page * line]
  232. send_json = CommonService.qs_to_dict(res)
  233. send_json['count'] = count
  234. return response.json(0, send_json)
  235. # 管理员查询指定用户的推送信息接口
  236. def query_all_info(self, request_dict, userID, response):
  237. page = int(request_dict.get('page', None))
  238. line = int(request_dict.get('line', None))
  239. username = request_dict.get('username', None)
  240. uid = request_dict.get('uid', None)
  241. devNickName = request_dict.get('devNickName', None)
  242. Channel = request_dict.get('Channel', None)
  243. if not page or not line:
  244. return response.json(444, 'page,line')
  245. check_perm = ModelService.check_perm(userID=userID, permID=30)
  246. if not check_perm:
  247. return response.json(404)
  248. if not username and not uid and not devNickName and not Channel:
  249. qs = Equipment_Info.objects.all().order_by('-id')
  250. elif username:
  251. user = Device_User.objects.filter(username=username)
  252. users = CommonService.qs_to_dict(user)
  253. # 从字典类型中找到对应的pk值,即是userID
  254. user_id = users.get('datas')[0].get('pk')
  255. if not user.exists():
  256. return response.json(104)
  257. uid_list = Device_Info.objects.filter(userID_id=user_id).values_list('UID', flat=True)
  258. if not len(uid_list):
  259. return response.json(0, {'datas': [], 'count': 0})
  260. qs = Equipment_Info.objects.filter(userID_id=user_id).order_by('-id')
  261. elif uid:
  262. qs = Equipment_Info.objects.filter(devUid=uid).order_by('-id')
  263. elif devNickName:
  264. qs = Equipment_Info.objects.filter(devNickName=devNickName).order_by('-id')
  265. elif Channel:
  266. qs = Equipment_Info.objects.filter(Channel=Channel).order_by('-id')
  267. if not qs.exists():
  268. return response.json(0, {'datas': [], 'count': 0})
  269. count = qs.count()
  270. res = qs[(page - 1) * line:page * line]
  271. send_json = CommonService.qs_to_dict(res)
  272. send_json['count'] = count
  273. return response.json(0, send_json)
  274. # 管理员的删除推送信息的接口
  275. def delete_by_admin(self, request_dict, userID, response):
  276. id_list = request_dict.getlist('id', None)
  277. if not len(id_list):
  278. return response.json(444, 'id is None or not list')
  279. check_perm = ModelService.check_perm(userID=userID, permID=10)
  280. if check_perm is True:
  281. try:
  282. is_delete = Equipment_Info.objects.filter(id__in=id_list).delete()
  283. except Exception as e:
  284. errorInfo = traceback.format_exc()
  285. print(errorInfo)
  286. return response.json(424, {'details': repr(e)})
  287. else:
  288. return response.json(0, {'delete_count': is_delete[0]})
  289. else:
  290. return response.json(404)
  291. '''
  292. use information_schema;
  293. select concat(round(sum(data_length/1024/1024),2),'MB') as data from tables where table_schema='Ansjer81';
  294. '''
  295. # 按季度删除访问日志
  296. def deleteExpireEquipmentInfoById(request):
  297. response = ResponseObject()
  298. id = int(request.GET.get('id', None))
  299. if id is None:
  300. return response.json(0)
  301. Equipment_Info.objects.filter(id__lte=id).delete()
  302. return response.json(0)