GatewayFamilyRoomController.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : GatewayFamilyRoomController.py
  4. @Time : 2022/5/24 19:43
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. from django.db import transaction
  10. from django.db.models import Q, Count
  11. from django.views.generic.base import View
  12. from Controller.SensorGateway.EquipmentFamilyController import EquipmentFamilyView
  13. from Model.models import FamilyRoomDevice, FamilyRoom
  14. from Object.ResponseObject import ResponseObject
  15. from Object.TokenObject import TokenObject
  16. # 家庭房间管理
  17. class GatewayFamilyRoomView(View):
  18. def get(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.GET, request, operation)
  22. def post(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation')
  25. return self.validation(request.POST, request, operation)
  26. def validation(self, request_dict, request, operation):
  27. token = request.META.get('HTTP_AUTHORIZATION')
  28. token = TokenObject(token)
  29. lang = request_dict.get('lang', token.lang)
  30. response = ResponseObject(lang)
  31. if token.code != 0:
  32. return response.json(token.code)
  33. app_user_id = token.userID
  34. # 添加设备关联房间
  35. if operation == 'device-changes':
  36. return self.room_device_save(app_user_id, request_dict, response)
  37. # 房间排序
  38. elif operation == 'sort':
  39. return self.room_sort_save(request_dict, response)
  40. # 房间删除
  41. elif operation == 'del':
  42. return self.room_del(app_user_id, request_dict, response)
  43. # 房间详情
  44. elif operation == 'details':
  45. return self.get_room_details(request_dict, response)
  46. @classmethod
  47. def room_device_save(cls, app_user_id, request_dict, response):
  48. """
  49. 房间加入设备or移除设备
  50. @param app_user_id:
  51. @param request_dict:
  52. @param response:
  53. @return:
  54. """
  55. family_id = request_dict.get('familyId', None)
  56. device_ids = request_dict.get('deviceIds', None)
  57. room_id = request_dict.get('roomId', None)
  58. if not all([family_id, room_id]):
  59. return response.json(444)
  60. family_id = int(family_id)
  61. room_id = int(room_id)
  62. is_owner = EquipmentFamilyView.get_family_owner(app_user_id, family_id)
  63. if not is_owner:
  64. return response.json(404)
  65. try:
  66. with transaction.atomic():
  67. room_qs = FamilyRoom.objects.filter(family_id=family_id, id=room_id)
  68. if not room_qs.exists():
  69. return response.json(173)
  70. qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id)
  71. if qs.exists():
  72. qs.update(room_id=0, sort=0)
  73. if device_ids:
  74. device_ids = device_ids.split(',')
  75. for i, item in enumerate(device_ids):
  76. device_id = int(item)
  77. device_qs = FamilyRoomDevice.objects.filter(device_id=device_id)
  78. if device_qs.exists():
  79. device_qs.update(room_id=room_id, sort=i)
  80. return response.json(0)
  81. except Exception as e:
  82. print(e)
  83. return response.json(177, repr(e))
  84. @classmethod
  85. def room_del(cls, user_id, request_dict, response):
  86. """
  87. 房间多选删除
  88. @param user_id: 当前登录用户id
  89. @param request_dict: 请求参数
  90. @param response: 响应参数
  91. @return:
  92. """
  93. ids = request_dict.get('roomIds', None)
  94. if not ids:
  95. return response.json(444)
  96. ids = ids.split(',')
  97. room_id = ids[0]
  98. room_info = FamilyRoom.objects.filter(id=room_id)
  99. if not room_info.exists():
  100. return response.json(173)
  101. is_owner = EquipmentFamilyView.get_family_owner(user_id, room_info.first().family_id)
  102. if not is_owner:
  103. return response.json(404)
  104. try:
  105. with transaction.atomic():
  106. for item in ids:
  107. room_id = int(item)
  108. room_device = FamilyRoomDevice.objects.filter(room_id=room_id)
  109. if room_device.exists():
  110. room_device.update(room_id=0)
  111. FamilyRoom.objects.filter(id=room_id).delete()
  112. return response.json(0)
  113. except Exception as e:
  114. print(e)
  115. return response.json(177, repr(e))
  116. @classmethod
  117. def room_sort_save(cls, request_dict, response):
  118. """
  119. 房间排序
  120. @param request_dict: 请求参数
  121. @param response: 响应参数
  122. @return:
  123. """
  124. ids = request_dict.get('ids', None)
  125. if not ids:
  126. return response.json(444)
  127. items = ids.split(',')
  128. for item in items:
  129. vals = item.split('-')
  130. room_id = vals[0]
  131. sort = vals[1]
  132. family_room = FamilyRoom.objects.filter(id=int(room_id))
  133. if family_room.exists():
  134. family_room.update(sort=int(sort))
  135. return response.json(0)
  136. @classmethod
  137. def get_room_details(cls, request_dict, response):
  138. """
  139. 房间设备详情(所在当前房间下,和所在家庭不在当前房间下的主设备)
  140. @param request_dict:
  141. @param response:
  142. @return:
  143. """
  144. family_id = request_dict.get('familyId', None)
  145. room_id = request_dict.get('roomId', None)
  146. if not all([family_id, room_id]):
  147. return response.json(444)
  148. family_id = int(family_id)
  149. room_id = int(room_id)
  150. room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id).order_by('sort').values(
  151. 'device_id').annotate(count=Count('device_id')).values('device_id', 'device__Type', 'device__NickName')
  152. device_room = []
  153. if room_device_qs.exists():
  154. room_name = FamilyRoom.objects.filter(id=room_id)
  155. for item in room_device_qs:
  156. device_room.append({
  157. 'deviceId': item['device_id'],
  158. 'deviceType': item['device__Type'],
  159. 'nickName': item['device__NickName'],
  160. 'roomName': room_name.first().name if room_name.exists() else '',
  161. })
  162. device_not_room = []
  163. device_not_room_qs = FamilyRoomDevice.objects.filter(family_id=family_id)
  164. device_not_room_qs = device_not_room_qs.filter(~Q(room_id=room_id)).values('device_id').annotate(
  165. count=Count('device_id')).values('room_id', 'device_id', 'device__Type', 'device__NickName')
  166. if device_not_room_qs.exists():
  167. for item in device_not_room_qs:
  168. name = ''
  169. if room_device_qs.exists():
  170. family_room_qs = FamilyRoom.objects.filter(id=item['room_id'])
  171. if family_room_qs.exists():
  172. name = family_room_qs.first().name
  173. device_not_room.append({
  174. 'deviceId': item['device_id'],
  175. 'deviceType': item['device__Type'],
  176. 'nickName': item['device__NickName'],
  177. 'roomName': name
  178. })
  179. return response.json(0, {'deviceRooms': device_room, 'deviceNotRooms': device_not_room})