from apps.dpm.serializers import (RiskPointCreateUpdateSerializer, RiskPointSerializer, RiskCreateUpdateSerializer, RiskSerializer, 
                                  CheckTaskSetCreateSerializer, CheckTaskSetUpdateSerializer, 
                                  CheckTaskSetSerializer, CheckWork_10_CreateUpdateSerializer, 
                                  CheckWorkSerializer, HazardSerializer)
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from apps.utils.mixins import CustomListModelMixin, BulkDestroyModelMixin, BulkCreateModelMixin
from apps.dpm.models import CheckTaskSet, CheckWork, RiskPoint, Risk, Hazard
from rest_framework.decorators import action
from rest_framework.response import Response
from django.db import transaction


# Create your views here.
class RiskPointViewSet(CustomModelViewSet):
    queryset = RiskPoint.objects.all()
    create_serializer_class = RiskPointCreateUpdateSerializer
    update_serializer_class = RiskPointCreateUpdateSerializer
    serializer_class = RiskPointSerializer
    select_related_fields = ['area', 'dept_res', 'user_res']
    search_fields = ['name']
    filterset_fields = ['area', 'type', 'level', 'manage_level']


class RiskViewSet(CustomModelViewSet):
    perms_map = {"get": "*", "post": "riskpoint.update", "put": "riskpoint.update", "delete": "riskpoint.update"}
    queryset = Risk.objects.all()
    create_serializer_class = RiskCreateUpdateSerializer
    update_serializer_class = RiskCreateUpdateSerializer
    serializer_class = RiskSerializer
    select_related_fields = ['riskpoint']
    prefetch_related_fields = ['results']
    search_fields = ['name']
    filterset_fields = ['results']


class CheckTaskSetViewSet(CustomListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
    queryset = CheckTaskSet.objects.all()
    create_serializer_class = CheckTaskSetCreateSerializer
    update_serializer_class = CheckTaskSetUpdateSerializer
    serializer_class = CheckTaskSetSerializer
    select_related_fields = ['periodictask', "myschedule", "user_duty"]
    search_fields = ['note']
    filterset_fields = ['periodictask__enabled']

    @action(methods=['put'], detail=True, perms_map={'put': 'checktaskset.update'})
    def toggle(self, request, pk=None):
        """修改启用禁用状态

        修改启用禁用状态
        """
        obj = self.get_object()
        periodictask = obj.periodictask
        periodictask.enabled = False if periodictask.enabled else True
        periodictask.save()
        return Response()

    def perform_destroy(self, instance):
        with transaction.atomic():
            periodictask = instance.periodictask
            instance.delete()
            periodictask.delete()


class CheckWorkViewSet(CustomModelViewSet):
    queryset = CheckWork.objects.all()
    create_serializer_class = CheckWork_10_CreateUpdateSerializer
    update_serializer_class = CheckWork_10_CreateUpdateSerializer
    serializer_class = CheckWorkSerializer
    search_fields = ['name', 'description']
    select_related_fields = ['category', 'user_duty', 'riskpoint']
    filterset_fields = ['type', 'category', 'riskpoint']


class HazardViewSet(CustomModelViewSet):
    queryset = Hazard.objects.all()
    serializer_class = HazardSerializer
    select_related_fields = ["checkwork", "area", "dept_res", "user_discover"]
