| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 | 
							- # -*- encoding: utf-8 -*-
 
- """
 
- @File    : GatewayFamilyRoomController.py
 
- @Time    : 2022/5/24 19:43
 
- @Author  : stephen
 
- @Email   : zhangdongming@asj6.wecom.work
 
- @Software: PyCharm
 
- """
 
- from django.db import transaction
 
- from django.db.models import Q, Count
 
- from django.views.generic.base import View
 
- from Controller.SensorGateway.EquipmentFamilyController import EquipmentFamilyView
 
- from Model.models import FamilyRoomDevice, FamilyRoom
 
- from Object.ResponseObject import ResponseObject
 
- from Object.TokenObject import TokenObject
 
- # 家庭房间管理
 
- class GatewayFamilyRoomView(View):
 
-     def get(self, request, *args, **kwargs):
 
-         request.encoding = 'utf-8'
 
-         operation = kwargs.get('operation')
 
-         return self.validation(request.GET, request, operation)
 
-     def post(self, request, *args, **kwargs):
 
-         request.encoding = 'utf-8'
 
-         operation = kwargs.get('operation')
 
-         return self.validation(request.POST, request, operation)
 
-     def validation(self, request_dict, request, operation):
 
-         token = request.META.get('HTTP_AUTHORIZATION')
 
-         token = TokenObject(token)
 
-         lang = request_dict.get('lang', token.lang)
 
-         response = ResponseObject(lang)
 
-         if token.code != 0:
 
-             return response.json(token.code)
 
-         app_user_id = token.userID
 
-         # 添加设备关联房间
 
-         if operation == 'device-changes':
 
-             return self.room_device_save(app_user_id, request_dict, response)
 
-         # 房间排序
 
-         elif operation == 'sort':
 
-             return self.room_sort_save(request_dict, response)
 
-         # 房间删除
 
-         elif operation == 'del':
 
-             return self.room_del(app_user_id, request_dict, response)
 
-         # 房间详情
 
-         elif operation == 'details':
 
-             return self.get_room_details(request_dict, response)
 
-     @classmethod
 
-     def room_device_save(cls, app_user_id, request_dict, response):
 
-         """
 
-         房间加入设备or移除设备
 
-         @param app_user_id:
 
-         @param request_dict:
 
-         @param response:
 
-         @return:
 
-         """
 
-         family_id = request_dict.get('familyId', None)
 
-         device_ids = request_dict.get('deviceIds', None)
 
-         room_id = request_dict.get('roomId', None)
 
-         if not all([family_id, room_id]):
 
-             return response.json(444)
 
-         family_id = int(family_id)
 
-         room_id = int(room_id)
 
-         is_owner = EquipmentFamilyView.get_family_owner(app_user_id, family_id)
 
-         if not is_owner:
 
-             return response.json(404)
 
-         try:
 
-             with transaction.atomic():
 
-                 room_qs = FamilyRoom.objects.filter(family_id=family_id, id=room_id)
 
-                 if not room_qs.exists():
 
-                     return response.json(173)
 
-                 qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id)
 
-                 if qs.exists():
 
-                     qs.update(room_id=0, sort=0)
 
-                 if device_ids:
 
-                     device_ids = device_ids.split(',')
 
-                     for i, item in enumerate(device_ids):
 
-                         device_id = int(item)
 
-                         device_qs = FamilyRoomDevice.objects.filter(device_id=device_id)
 
-                         if device_qs.exists():
 
-                             device_qs.update(room_id=room_id, sort=i)
 
-                 return response.json(0)
 
-         except Exception as e:
 
-             print(e)
 
-             return response.json(177, repr(e))
 
-     @classmethod
 
-     def room_del(cls, user_id, request_dict, response):
 
-         """
 
-         房间多选删除
 
-         @param user_id: 当前登录用户id
 
-         @param request_dict: 请求参数
 
-         @param response: 响应参数
 
-         @return:
 
-         """
 
-         ids = request_dict.get('roomIds', None)
 
-         if not ids:
 
-             return response.json(444)
 
-         ids = ids.split(',')
 
-         room_id = ids[0]
 
-         room_info = FamilyRoom.objects.filter(id=room_id)
 
-         if not room_info.exists():
 
-             return response.json(173)
 
-         is_owner = EquipmentFamilyView.get_family_owner(user_id, room_info.first().family_id)
 
-         if not is_owner:
 
-             return response.json(404)
 
-         try:
 
-             with transaction.atomic():
 
-                 for item in ids:
 
-                     room_id = int(item)
 
-                     room_device = FamilyRoomDevice.objects.filter(room_id=room_id)
 
-                     if room_device.exists():
 
-                         room_device.update(room_id=0)
 
-                     FamilyRoom.objects.filter(id=room_id).delete()
 
-                 return response.json(0)
 
-         except Exception as e:
 
-             print(e)
 
-             return response.json(177, repr(e))
 
-     @classmethod
 
-     def room_sort_save(cls, request_dict, response):
 
-         """
 
-         房间排序
 
-         @param request_dict: 请求参数
 
-         @param response: 响应参数
 
-         @return:
 
-         """
 
-         ids = request_dict.get('ids', None)
 
-         if not ids:
 
-             return response.json(444)
 
-         items = ids.split(',')
 
-         for item in items:
 
-             vals = item.split('-')
 
-             room_id = vals[0]
 
-             sort = vals[1]
 
-             family_room = FamilyRoom.objects.filter(id=int(room_id))
 
-             if family_room.exists():
 
-                 family_room.update(sort=int(sort))
 
-         return response.json(0)
 
-     @classmethod
 
-     def get_room_details(cls, request_dict, response):
 
-         """
 
-         房间设备详情(所在当前房间下,和所在家庭不在当前房间下的主设备)
 
-         @param request_dict:
 
-         @param response:
 
-         @return:
 
-         """
 
-         family_id = request_dict.get('familyId', None)
 
-         room_id = request_dict.get('roomId', None)
 
-         if not all([family_id, room_id]):
 
-             return response.json(444)
 
-         family_id = int(family_id)
 
-         room_id = int(room_id)
 
-         room_device_qs = FamilyRoomDevice.objects.filter(family_id=family_id, room_id=room_id).order_by('sort').values(
 
-             'device_id').annotate(count=Count('device_id')).values('device_id', 'device__Type', 'device__NickName')
 
-         device_room = []
 
-         if room_device_qs.exists():
 
-             room_name = FamilyRoom.objects.filter(id=room_id)
 
-             for item in room_device_qs:
 
-                 device_room.append({
 
-                     'deviceId': item['device_id'],
 
-                     'deviceType': item['device__Type'],
 
-                     'nickName': item['device__NickName'],
 
-                     'roomName': room_name.first().name if room_name.exists() else '',
 
-                 })
 
-         device_not_room = []
 
-         device_not_room_qs = FamilyRoomDevice.objects.filter(family_id=family_id)
 
-         device_not_room_qs = device_not_room_qs.filter(~Q(room_id=room_id)).values('device_id').annotate(
 
-             count=Count('device_id')).values('room_id', 'device_id', 'device__Type', 'device__NickName')
 
-         if device_not_room_qs.exists():
 
-             for item in device_not_room_qs:
 
-                 name = ''
 
-                 if room_device_qs.exists():
 
-                     family_room_qs = FamilyRoom.objects.filter(id=item['room_id'])
 
-                     if family_room_qs.exists():
 
-                         name = family_room_qs.first().name
 
-                 device_not_room.append({
 
-                     'deviceId': item['device_id'],
 
-                     'deviceType': item['device__Type'],
 
-                     'nickName': item['device__NickName'],
 
-                     'roomName': name
 
-                 })
 
-         return response.json(0, {'deviceRooms': device_room, 'deviceNotRooms': device_not_room})
 
 
  |