浏览代码

修复根据日期查询消息日志不全问题,优化单个已读,优化选择删除

zhangdongming 3 年之前
父节点
当前提交
49fe40ab66
共有 3 个文件被更改,包括 74 次插入30 次删除
  1. 34 8
      Controller/EquipmentInfo.py
  2. 23 18
      Object/utils/LocalDateTimeUtil.py
  3. 17 4
      Service/EquipmentInfoService.py

+ 34 - 8
Controller/EquipmentInfo.py

@@ -1,3 +1,4 @@
+import datetime
 import time
 import traceback
 
@@ -170,16 +171,28 @@ class EquipmentInfo(View):
                 count = 0
                 for ei_id in id_list:
                     try:
-                        event_date = '20{}'.format(ei_id[0:6])
-                        ei_id = ei_id[6:]
-                        val = LocalDateTimeUtil.format_date_to_week(event_date, '%Y%m%d')
+                        dt = int(ei_id[0:10])
+                        event_date = datetime.datetime.utcfromtimestamp(dt).date()
+                        ei_id = int(ei_id[10:])
+                        val = LocalDateTimeUtil.format_date_to_week(str(event_date), '%Y-%m-%d')
                         eq = EquipmentInfoService.get_equipment_info_model('', val)
-                        eq = eq.filter(id=int(ei_id))
+                        eq = eq.filter(id=ei_id, event_time=dt)
                         if eq.exists():
                             own_dev = ModelService.check_own_device(userID, eq[0].device_uid)
                             if own_dev is True:
                                 count += 1
                                 eq.update(status=1)
+                        if count == 0:
+                            for i in range(2):
+                                week = (7 if val == 1 else val - 1) if i == 0 else (1 if val == 7 else val + 1)
+                                result = EquipmentInfoService.get_equipment_info_model('', week)
+                                result = result.filter(id=ei_id, event_time=dt)
+                                if result.exists():
+                                    own_dev = ModelService.check_own_device(userID, result[0].device_uid)
+                                    if own_dev is True:
+                                        count += 1
+                                        result.update(status=1)
+
                     except Exception as e:
                         print(repr(e))
                 return response.json(0, {'update_success': count})
@@ -194,15 +207,28 @@ class EquipmentInfo(View):
         if param_flag is True:
             try:
                 for ei_id in id_list:
-                    event_date = '20{}'.format(ei_id[0:6])
-                    ei_id = ei_id[6:]
-                    val = LocalDateTimeUtil.format_date_to_week(event_date, '%Y%m%d')
+                    dt = int(ei_id[0:10])
+                    event_date = datetime.datetime.utcfromtimestamp(dt).date()
+                    res = 0
+                    ei_id = int(ei_id[10:])
+                    val = LocalDateTimeUtil.format_date_to_week(str(event_date), '%Y-%m-%d')
                     eq = EquipmentInfoService.get_equipment_info_model('', val)
-                    eq = eq.filter(id=ei_id)
+                    eq = eq.filter(id=ei_id, event_time=dt)
                     if eq.exists():
                         own_dev = ModelService.check_own_device(userID, eq[0].device_uid)
                         if own_dev is True:
+                            res += 1
                             eq.delete()
+                    if res == 0:
+                        for i in range(2):
+                            week = (7 if val == 1 else val - 1) if i == 0 else (1 if val == 7 else val + 1)
+                            result = EquipmentInfoService.get_equipment_info_model('', week)
+                            result = result.filter(id=ei_id, event_time=dt)
+                            if result.exists():
+                                own_dev = ModelService.check_own_device(userID, result[0].device_uid)
+                                if own_dev is True:
+                                    result.delete()
+
             except Exception as e:
                 errorInfo = traceback.format_exc()
                 print(errorInfo)

+ 23 - 18
Object/utils/LocalDateTimeUtil.py

@@ -85,7 +85,7 @@ def date_to_week(str_date):
 def format_date_to_week(str_date, str_format):
     """
     日期获取星期几
-    @param str_format:  %Y%m%d
+    @param str_format:  %Y-%m-%d
     @param str_date 日期 例:2022-03-03
     @return: int 1-7
     """
@@ -94,21 +94,26 @@ def format_date_to_week(str_date, str_format):
     return 0
 
 
+def get_start_and_end_time(date, str_format):
+    """
+    根据日期获取当日开始and结束时间戳
+    @param date: 日期 例:2022-03-07
+    @param str_format: 格式 例:%Y-%m-%d
+    @return: start_time,end_time
+    """
+    if date:
+        today = datetime.datetime.strptime(date, str_format)
+        day = today + datetime.timedelta(days=1)
+        start_time = int(time.mktime(time.strptime(str(today.date()), '%Y-%m-%d')))
+        end_time = int(time.mktime(time.strptime(str(day.date()), '%Y-%m-%d'))) - 1
+        return start_time, end_time
+    return 0
+
+
 if __name__ == "__main__":
-    val = format_date_to_week('20220417', '%Y%m%d')
-    pfe = str(22162688)
-    print(pfe[6:])
-    now_time = int(time.time())
-    date_time = datetime.datetime.fromtimestamp(now_time)
-    print(date_to_week(str(date_time.date())))
-    result = get_before_days_timestamp(now_time, 7)
-    print(result)
-    print(now_time - 3600 * 168)
-    dd = datetime.date.today()
-    print(type(dd))
-    dd = str(dd)
-    print(type(dd))
-    week = date_to_week('2022-03-03')
-    print(week)
-    start_time, end_time = get_today_date(True)
-    print('--- start_time = {} end_time = {}'.format(start_time, end_time))
+    dd = str(1650791368303)
+    print(dd[0:10])
+    print(dd[10:])
+    dateArray = datetime.datetime.utcfromtimestamp(1650791368)
+    print(dateArray.date())
+    print(get_start_and_end_time('20220317', '%Y%m%d'))

+ 17 - 4
Service/EquipmentInfoService.py

@@ -74,7 +74,22 @@ class EquipmentInfoService:
             # 调用查询方法
             qs = cls.query_equipment_info(qs, user_id, start_time, end_time, event_type,
                                           uid_list)
-            if qs.exists:
+            # 时区问题
+            week = LocalDateTimeUtil.date_to_week(str(start_date.date()))
+            if week > 0:
+                # 根据筛选日期 查找昨天数据
+                yesterday = 7 if week == 1 else week - 1
+                yesterday_info = EquipmentInfoService.get_equipment_info_model('', yesterday)
+                yesterday_info = cls.query_equipment_info(yesterday_info, user_id, start_time, end_time, event_type,
+                                                          uid_list)
+
+                # 根据筛选日期 查找明天数据
+                tomorrow = 1 if week == 7 else week + 1
+                tomorrow_info = EquipmentInfoService.get_equipment_info_model('', tomorrow)
+                tomorrow_info = cls.query_equipment_info(tomorrow_info, user_id, start_time, end_time, event_type,
+                                                         uid_list)
+                qs = qs.union(yesterday_info, tomorrow_info, all=True)
+            if qs.exists():
                 count = qs.count()
                 qs_page = cls.get_equipment_info_page(qs, page, size)
                 return qs_page, count
@@ -181,9 +196,7 @@ class EquipmentInfoService:
         if qs_page and len(qs_page) > 0:
             for item in qs_page:
                 event_time = item['event_time']
-                date_time = datetime.datetime.fromtimestamp(int(event_time)).strftime("%Y%m%d")
-                dt = date_time[2:8]
-                item['id'] = int(str(dt) + str(item['id']))
+                item['id'] = int(str(event_time) + str(item['id']))
                 item['devUid'] = item['device_uid']
                 item['devNickName'] = item['device_nick_name']
                 item['Channel'] = item['channel']