Jelajahi Sumber

修改通道保存异常

zhangdongming 2 tahun lalu
induk
melakukan
d6c885d5be
1 mengubah file dengan 22 tambahan dan 18 penghapusan
  1. 22 18
      Service/UserDeviceService.py

+ 22 - 18
Service/UserDeviceService.py

@@ -294,22 +294,26 @@ class UserDeviceService:
         @param uid: 设备UID
         @return: True | False
         """
-        uid_set_qs = UidSetModel.objects.filter(uid=uid).values('id', 'channel')
-        if not uid_set_qs.exists():
+        try:
+            uid_set_qs = UidSetModel.objects.filter(uid=uid).values('id', 'channel')
+            if not uid_set_qs.exists():
+                return False
+            uid_set_id = uid_set_qs[0]['id']
+            channel_index = uid_set_qs[0]['channel']
+            uid_channel_qs = UidChannelSetModel.objects.filter(uid_id=uid_set_id).values('channel')
+            if uid_channel_qs.exists() and uid_channel_qs.count() != channel_index:
+                uid_channel_list = []
+                channels = [val['channel'] for val in uid_channel_qs]
+                for i in range(1, channel_index + 1):
+                    if i in channels:
+                        continue
+                    channel_name = 'channel' + str(i)  # channel1,channel2...
+                    UidChannelSet = UidChannelSetModel(uid_id=uid_set_id, channel=i, channel_name=channel_name)
+                    uid_channel_list.append(UidChannelSet)
+                if not uid_channel_list:
+                    return True
+                UidChannelSetModel.objects.bulk_create(uid_channel_list)
+            return True
+        except Exception as e:
+            LOGGER.info('异常详情,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return False
-        uid_set_id = uid_set_qs[0]['id']
-        channel_index = uid_set_qs[0]['channel']
-        uid_channel_qs = UidChannelSetModel.objects.filter(uid_id=uid_set_id)
-        if uid_channel_qs.exists() and uid_channel_qs.count() != channel_index:
-            uid_channel_list = []
-            channels = [val['channel'] for val in uid_channel_qs]
-            for i in range(1, channel_index + 1):
-                if i in channels:
-                    continue
-                channel_name = 'channel' + str(i)  # channel1,channel2...
-                UidChannelSet = UidChannelSetModel(uid_id=uid_set_id, channel=i, channel_name=channel_name)
-                uid_channel_list.append(UidChannelSet)
-            if not uid_channel_list:
-                return True
-            UidChannelSetModel.objects.bulk_create(uid_channel_list)
-        return True