from django.shortcuts import render
from apps.enm.models import Mpoint, MpLog, MpointStat, EnStat, EnStat2
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from rest_framework.mixins import ListModelMixin
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin
from apps.enm.serializers import (MpointSerializer, MpLogSerializer, MpointStatSerializer, EnStatSerializer, EnStat2Serializer, ReCalSerializer)
from apps.enm.filters import MpointStatFilter, EnStatFilter, EnStat2Filter
from apps.enm.tasks import cal_mpointstat_manual
from rest_framework.response import Response
from rest_framework.decorators import action
from apps.enm.tasks import cal_mpointstats_duration

class MpointViewSet(CustomModelViewSet):
    """
    list:测点

    测点
    """
    queryset = Mpoint.objects.all()
    serializer_class = MpointSerializer
    select_related_fields = ['create_by', 'belong_dept', 'ep_monitored', 'ep_belong', 'mgroup']
    filterset_fields = ['belong_dept', 'ep_monitored', 'ep_belong', 'mgroup', 'is_auto', 'is_all', 'mgroup__name']
    search_fields = ['number', 'code']


class MpLogViewSet(ListModelMixin, CustomGenericViewSet):
    """
    list:测点原始记录

    测点原始记录
    """
    perms_map = {'get': '*'}
    queryset = MpLog.objects.all()
    serializer_class = MpLogSerializer
    select_related_fields = ['mpoint']
    filterset_fields = ['mpoint', 'mpoint__mgroup', 'mpoint__mgroup__belong_dept']


class MpointStatViewSet(BulkCreateModelMixin, BulkDestroyModelMixin, ListModelMixin, CustomGenericViewSet):
    """
    list:测点统计记录

    测点统计记录
    """
    perms_map = {'get': '*', 'post': 'mpointstat.create', 'delete': 'mpointstat.delete'}
    queryset = MpointStat.objects.all()
    serializer_class = MpointStatSerializer
    select_related_fields = ['mpoint', 'mpoint__ep_monitored', 'mpoint__ep_belong', 'mgroup', 'mgroup__belong_dept']
    filterset_class = MpointStatFilter

    def perform_create(self, serializer):
        ins = serializer.save()
        cal_mpointstat_manual.delay(ins.mpoint.id, ins.sflog.id, ins.mgroup.id, None, None, None, None, ins.year_s, ins.month_s, ins.day_s, nex_cal=1)

    def perform_destroy(self, instance):
        mpoint, sflog, mgroup, year_s, month_s, day_s = instance.mpoint, instance.sflog, instance.mgroup, instance.year_s, instance.month_s, instance.day_s
        instance.delete()
        cal_mpointstat_manual.delay(mpoint.id, sflog.id, mgroup.id, None, None, None, None, year_s, month_s, day_s)

    @action(methods=['post'], detail=False, perms_map={'post': 'mpointstat.create'}, serializer_class=ReCalSerializer)
    def recal(self, request, *args, **kwargs):
        """重新运行某段时间的enm计算

        重新运行某段时间的enm计算
        """
        data = request.data
        sr = ReCalSerializer(data=data)
        sr.is_valid(raise_exception=True)
        vdata = sr.validated_data
        task = cal_mpointstats_duration.delay(data['start_time'], data['end_time'])
        return Response({'task_id': task.task_id})


class EnStatViewSet(ListModelMixin, CustomGenericViewSet):
    """
    list:能耗统计记录

    能耗统计记录
    """
    perms_map = {'get': '*'}
    queryset = EnStat.objects.all()
    serializer_class = EnStatSerializer
    select_related_fields = ['mgroup', 'team', 'mgroup__belong_dept']
    filterset_class = EnStatFilter
    ordering = ['mgroup__sort', 'year_s', 'month_s', 'day_s', 'hour']


class EnStat2ViewSet(ListModelMixin, CustomGenericViewSet):
    """
    list:全厂统计记录

    全厂统计记录
    """
    perms_map = {'get': '*'}
    queryset = EnStat2.objects.all()
    serializer_class = EnStat2Serializer
    filterset_class = EnStat2Filter