diff --git a/apps/devices/services.py b/apps/devices/services.py new file mode 100644 index 0000000..4fcaed4 --- /dev/null +++ b/apps/devices/services.py @@ -0,0 +1,26 @@ +""" +设备模块服务层 +""" +from .models import Device, UserDevice + + +class DeviceStatsService: + """设备统计服务""" + + @staticmethod + def get_bindrate(user): + """获取用户设备绑定率""" + total_count = Device.objects.count() + if total_count == 0: + return { + 'total_count': 0, + 'bound_count': 0, + 'bind_rate': 0, + } + bound_count = Device.objects.filter(status='bound').count() + bind_rate = bound_count / total_count * 100 + return { + 'total_count': total_count, + 'bound_count': bound_count, + 'bind_rate': round(bind_rate, 2), + } diff --git a/apps/devices/views.py b/apps/devices/views.py index eaf9777..0056b8f 100644 --- a/apps/devices/views.py +++ b/apps/devices/views.py @@ -119,8 +119,11 @@ class DeviceViewSet(viewsets.ViewSet): 'is_active': True } ) - - # 更新设备状态 + + # 更新设备状态和可选的设备名称 + device_name = request.data.get('device_name') + if device_name: + device.name = device_name device.status = 'bound' device.save() diff --git a/apps/spirits/models.py b/apps/spirits/models.py index ad2f4d2..9899883 100644 --- a/apps/spirits/models.py +++ b/apps/spirits/models.py @@ -26,4 +26,6 @@ class Spirit(models.Model): ordering = ['-created_at'] def __str__(self): + if self.user_id is None: + return self.name return f"{self.name} - {self.user.phone}"