from django.shortcuts import render from apps.em.models import Equipment, EcheckRecord, EInspect, Ecate from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet from apps.em.serializers import EquipmentSerializer, EcheckRecordSerializer, EInspectSerializer, EcateSerializer from apps.em.filters import EquipFilterSet from rest_framework.exceptions import ParseError from rest_framework.mixins import ListModelMixin, CreateModelMixin, DestroyModelMixin from dateutil.relativedelta import relativedelta from rest_framework.decorators import action from rest_framework.serializers import Serializer from django.db import transaction from apps.em.services import daoru_equipment from rest_framework.response import Response from django.conf import settings from django.db.models import Count, Case, When, IntegerField # Create your views here. class EcateViewSet(CustomModelViewSet): """ list:设备分类 设备分类 """ queryset = Ecate.objects.all() serializer_class = EcateSerializer ordering = ['id', 'type', 'code', 'create_time'] filterset_fields = { "type": ['exact', 'in'], "code": ['exact', 'in', 'contains'], "is_for_safe": ['exact'], "is_for_enp": ['exact'], "is_car": ['exact'] } class EquipmentViewSet(CustomModelViewSet): """ list:设备列表 设备列表 """ queryset = Equipment.objects.all() serializer_class = EquipmentSerializer select_related_fields = ['create_by', 'belong_dept', 'keeper', 'mgroup'] search_fields = ['number', 'name'] filterset_class = EquipFilterSet # def filter_queryset(self, queryset): # if not self.detail and not self.request.query_params.get('type', None): # raise ParseError('请指定设备类型') # return super().filter_queryset(queryset) @action(methods=['post'], detail=False, perms_map={'post': 'equipment.create'}, serializer_class=Serializer) @transaction.atomic def daoru(self, request, *args, **kwargs): """导入 导入 """ daoru_equipment(settings.BASE_DIR + request.data.get('path', '')) return Response() @action(methods=['get'], detail=False, perms_map={'get': '*'}) def count_running_state(self, request, *args, **kwargs): """当前运行状态统计 当前运行状态统计 """ queryset = self.filter_queryset(self.get_queryset()) result = queryset.aggregate( count=Count('id'), count_online=Count( Case(When(is_online=1, then=1), output_field=IntegerField())), count_offline=Count( Case(When(is_online=0, then=1), output_field=IntegerField())), count_running=Count( Case(When(running_state=10, then=1), output_field=IntegerField())), count_standby=Count( Case(When(running_state=20, then=1), output_field=IntegerField())), count_stop=Count( Case(When(running_state=30, then=1), output_field=IntegerField())), count_fail=Count( Case(When(running_state=40, then=1), output_field=IntegerField())), count_unknown=Count( Case(When(running_state=50, then=1), output_field=IntegerField())) ) json_result = { 'count': result['count'], 'count_online': result['count_online'], 'count_offline': result['count_offline'], 'count_running': result['count_running'], 'count_standby': result['count_standby'], 'count_stop': result['count_stop'], 'count_fail': result['count_fail'], 'count_unknown': result['count_unknown'] } return Response(json_result) class EcheckRecordViewSet(ListModelMixin, CreateModelMixin, DestroyModelMixin, CustomGenericViewSet): """ list:校准/检定记录 校准/检定记录 """ perms_map = {'get': '*', 'post': 'echeckrecord.create', 'put': 'echeckrecord.update', 'delete': 'echeckrecord.delete'} queryset = EcheckRecord.objects.all() serializer_class = EcheckRecordSerializer select_related_fields = ['equipment', 'create_by'] filterset_fields = ['equipment', 'result'] def perform_create(self, serializer): instance = serializer.save() equipment = instance.equipment if equipment.cycle: equipment.check_date = instance.check_date equipment.next_check_date = instance.check_date + \ relativedelta(months=equipment.cycle) equipment.save() def perform_destroy(self, instance): instance.delete() equipment = instance.equipment er = EcheckRecord.objects.filter( equipment=equipment).order_by('check_date').last() if er: equipment.check_date = instance.check_date equipment.next_check_date = instance.check_date + \ relativedelta(months=equipment.cycle) equipment.save() else: equipment.check_date = None equipment.next_check_date = None equipment.save() class EInspectViewSet(CustomModelViewSet): """ list:巡检记录 巡检记录 """ queryset = EInspect.objects.all() serializer_class = EInspectSerializer select_related_fields = ['equipment', 'inspect_user'] filterset_fields = ['equipment']