| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | 
							- # -*- 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.views.generic.base import View
 
- from Model.models import FamilyRoomDevice, FamilyRoom
 
- from Object.ResponseObject import ResponseObject
 
- from Object.TokenObject import TokenObject
 
- from SensorGateway.EquipmentFamilyController import EquipmentFamilyView
 
- # 家庭房间管理
 
- 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', None)
 
-         response = ResponseObject(lang) if lang else ResponseObject(token.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(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.getlist('deviceIds', None)
 
-         room_id = request_dict.get('roomId', None)
 
-         operate = request_dict.get('operate', None)
 
-         if not all([family_id, device_ids, operate, room_id]):
 
-             return response.json(444)
 
-         operate = int(operate)
 
-         is_owner = EquipmentFamilyView.get_family_owner(app_user_id, family_id)
 
-         if not is_owner:
 
-             return response.json(404)
 
-         with transaction.atomic():
 
-             room_qs = FamilyRoom.objects.filter(family_id=family_id, id=room_id)
 
-             if not room_qs.exists():
 
-                 return response.json(173)
 
-             for item in device_ids:
 
-                 qs = FamilyRoomDevice.objects.filter(family_id=family_id, device_id=item)
 
-                 if qs.exists():
 
-                     qs.update(room_id=0) if operate == 1 else qs.update(room_id=int(room_id))
 
-             return response.json(0)
 
-     @classmethod
 
-     def room_del(cls, request_dict, response):
 
-         """
 
-         房间多选删除
 
-         @param request_dict: 请求参数
 
-         @param response: 响应参数
 
-         @return:
 
-         """
 
-         ids = request_dict.getlist('roomIds', None)
 
-         if not ids:
 
-             return response.json(444)
 
-         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.getlist('ids', None)
 
-         if not ids:
 
-             return response.json(444)
 
-         for i, item in ids:
 
-             id_sort = item[i]
 
-             print(id_sort)
 
-         return response.json(0)
 
 
  |