Browse Source

优化首页获取未读消息

zhangdongming 1 year ago
parent
commit
7aaf667cf1
2 changed files with 18 additions and 81 deletions
  1. 1 1
      Controller/DetectControllerV2.py
  2. 17 80
      Service/EquipmentInfoService.py

+ 1 - 1
Controller/DetectControllerV2.py

@@ -339,7 +339,7 @@ class DetectControllerViewV2(View):
                 end_time = int(time.time())
                 start_time = LocalDateTimeUtil.get_before_days_timestamp(end_time, 7)
             equipment_info_qs, count = EquipmentInfoService. \
-                union_equipment_info_v2(userID, uid_list, event_type, start_time, end_time, page, line)
+                union_equipment_info(userID, uid_list, event_type, start_time, end_time, page, line)
 
             # 查询设备类型,昵称
             if uid_list:

+ 17 - 80
Service/EquipmentInfoService.py

@@ -454,7 +454,8 @@ class EquipmentInfoService:
                 equipment_info_21, equipment_info_22, equipment_info_23, equipment_info_24, equipment_info_25,
                 equipment_info_26, equipment_info_27, equipment_info_28, equipment_info_29, equipment_info_30,
                 equipment_info_31, equipment_info_32, equipment_info_33, equipment_info_34, equipment_info_35,
-                equipment_info_36, equipment_info_37, equipment_info_38, equipment_info_39, equipment_info_40
+                equipment_info_36, equipment_info_37, equipment_info_38, equipment_info_39, equipment_info_40,
+                all=True
             )
 
             count = equipment_info_union.count()
@@ -555,10 +556,21 @@ class EquipmentInfoService:
         @param kwargs:
         @return: count
         """
-        count = 0
-        for equipment_info_model in EQUIPMENT_INFO_MODEL_LIST:
-            count += equipment_info_model.objects.filter(**kwargs).count()
-        return count
+        try:
+            combined_queryset = None
+
+            for equipment_info_model in EQUIPMENT_INFO_MODEL_LIST:
+                queryset = equipment_info_model.objects.filter(**kwargs).values('id')
+
+                if combined_queryset is None:
+                    combined_queryset = queryset
+                else:
+                    combined_queryset = combined_queryset.union(queryset, all=True)
+
+            count = combined_queryset.count() if combined_queryset is not None else 0
+            return count
+        except Exception as e:
+            LOGGER.error(f'获取未读消息异常:{repr(e)}')
 
     @classmethod
     def get_equipment_info(cls, **kwargs):
@@ -577,78 +589,3 @@ class EquipmentInfoService:
         count = equipment_info_union.count()
         return equipment_info_union, count
 
-    @classmethod
-    def union_equipment_info_v2(cls, user_id, uid_list, event_type, start_time, end_time, page, line, event_time=None):
-        """
-        联表查询推送数据V2
-        @param user_id: 用户id
-        @param uid_list: uid列表
-        @param event_type: 事件类型
-        @param start_time:
-        @param end_time:
-        @param page: 页数
-        @param line: 条数
-        @param event_time: 设备触发时间
-        @return: 响应结果
-        """
-        try:
-            kwargs = {
-                'device_user_id': user_id,
-                'event_time__range': (start_time, end_time),
-            }
-            event_type_kwargs = {
-                'device_user_id': user_id,
-                'event_time__range': (start_time, end_time),
-            }
-            if uid_list:
-                kwargs['device_uid__in'] = uid_list
-                event_type_kwargs['device_uid__in'] = uid_list
-            event_type_list = []
-            if event_type:
-                event_type_list = cls.get_comb_event_type(event_type)
-                event_type_list = list(set(event_type_list))
-                tags = cls.get_event_tag(event_type)
-                if event_type_list:
-                    event_type_kwargs['event_type__in'] = event_type_list
-                    event_type_kwargs['event_tag'] = ''
-                    kwargs['event_tag__regex'] = tags
-                elif tags:
-                    kwargs['event_tag__regex'] = tags
-            if event_time:
-                kwargs['event_time'] = event_time
-                event_type_kwargs['event_time'] = event_time
-                kwargs.pop('event_tag__regex', None)
-                event_type_kwargs.pop('event_tag', None)
-
-            equipment_info_list = []
-            total_count = 0
-
-            for i in range(1, 41):
-                model_class = globals()[f'EquipmentInfo{i}']
-                if event_type_list:
-                    queryset = model_class.objects.filter(Q(**kwargs) | Q(**event_type_kwargs))
-                else:
-                    queryset = model_class.objects.filter(**kwargs)
-
-                count = queryset.count()  # 单独对每个表计算记录数
-                total_count += count
-
-                if count > 0:  # 只有在有数据的情况下才加入到 union 查询
-                    equipment_info_list.append(
-                        queryset.annotate(
-                            tab_val=Value(str(i + 10), output_field=CharField())
-                        )
-                    )
-
-            if total_count == 0:
-                return None, 0
-
-            equipment_info_union = equipment_info_list[0]
-            for equipment_info in equipment_info_list[1:]:
-                equipment_info_union = equipment_info_union.union(equipment_info, all=True)
-
-            equipment_info_union = cls.get_equipment_info_union_page(equipment_info_union, page, line)
-            return equipment_info_union, total_count
-
-        except Exception as e:
-            LOGGER.error(f'user:{user_id},error_line:{e.__traceback__.tb_lineno},error_msg:{repr(e)}')