from django.conf import settings
from django.core.cache import cache
from django.db.models import Count, Q
from django.db.models import query
from django.db.models.query import QuerySet
from django.shortcuts import render
from django.utils import timezone
from rest_framework import status
from rest_framework.decorators import action, permission_classes
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet
from utils.pagination import PageOrNot

from apps.system.mixins import CreateUpdateCustomMixin
from apps.system.models import Organization
from apps.system.permission import get_permission_list, has_permission
from apps.system.permission_data import RbacFilterSet

from .filters import RecordFilter
from .models import *
from .serializers import *

# Create your views here.

class ContentViewSet(CreateUpdateCustomMixin, ModelViewSet):
    """
    资料清单：增删改查
    """
    perms_map = {'get': '*', 'post': 'content',
                 'put': 'content', 'delete': 'content'}
    queryset = Content.objects.filter(cate=1)
    serializer_class = ContentSerializer
    pagination_class = None
    search_fields = ['name', 'desc']
    filterset_fields = ['type','can_doself']
    ordering = ['sortnum', 'type__sort', 'create_time']

class TaskViewSet(CreateUpdateCustomMixin, ModelViewSet):
    perms_map = {'get': '*', 'post': 'task_create',
                 'put': 'task_update', 'delete': 'task_delete'}
    queryset = Task.objects.all()
    search_fields = ['name']
    ordering = ['-create_time']

    def get_serializer_class(self):
        if self.action in ['create', 'update']:
            return TaskCreateUpdateSerializer
        elif self.action == 'retrieve':
            return TaksDetailSerializer
        return TaskListSerializer

    # def destroy(self, request, *args, **kwargs):
    #     instance = self.get_object()
    #     instance.delete()
    #     TaskDept.objects.filter(task=instance).delete()
    #     Record.objects.filter(task=instance).delete()
    #     return Response(status=status.HTTP_204_NO_CONTENT)

    @action(methods=['post'], detail=True, perms_map = {'post':'task_init'})
    def init(self, request, *args, **kwargs):
        """
        初始化任务,生成记录
        """
        obj = self.get_object()
        depts = request.data['depts']
        contents = request.data['contents']
        if depts and contents:
            record_list = []
            taskdept_list = []
            for m in depts:
                xdata = {}
                xdata['task'] = obj
                xdata['dept'] = Organization.objects.get(pk=m)
                taskdept_list.append(TaskDept(**xdata))
                for n in contents:
                    data={}
                    content = Content.objects.get(pk=n)
                    data['content'] = content
                    data['content_name'] = content.name
                    data['content_desc'] = content.desc
                    data['belong_dept'] = Organization.objects.get(pk=m)
                    data['task'] = obj
                    data['end_date'] = obj.end_date
                    data['create_by'] = request.user
                    data['state'] = '待发布'
                    data['dept_yes'] = False
                    record_list.append(Record(**data))
            TaskDept.objects.bulk_create(taskdept_list)
            Record.objects.bulk_create(record_list)
            obj.state = '待发布'
            obj.save()
            return Response(status=status.HTTP_200_OK)
        return Response('单位或清单不能为空', status=status.HTTP_400_BAD_REQUEST)
    
    @action(methods=['post'], detail=True, perms_map = {'post':'task_append'})
    def append(self, request, *args, **kwargs):
        """
        追加任务,生成记录
        """
        obj = self.get_object()
        depts = request.data['depts']
        contents = request.data['contents']
        if obj.state in ['待发布', '执行中']:          
            if depts and contents:
                for m in depts:
                    m = Organization.objects.get(pk=m)
                    TaskDept.objects.get_or_create(task=obj, dept=m, defaults={'task':obj, 'dept':m})
                    for n in contents:
                        n = Content.objects.get(pk=n)
                        if not Record.objects.filter(belong_dept=m, content=n, task=obj).exists():
                            r = Record()
                            r.content = n
                            r.content_name = n.name
                            r.content_desc = n.desc
                            r.belong_dept = m
                            r.task = obj
                            r.end_date = obj.end_date
                            r.create_by = request.user
                            r.state = '待报送'
                            r.dept_yes = False
                            r.save()
                obj.save()
                return Response(status=status.HTTP_200_OK)
            return Response('单位或清单不能为空', status=status.HTTP_400_BAD_REQUEST)
        return Response('任务状态错误', status=status.HTTP_400_BAD_REQUEST)


    @action(methods=['get'], detail=True, perms_map = {'get':'*'})
    def contents(self, request, *args, **kwargs):
        """
        聚合
        """
        obj = self.get_object()
        serializer = ContentListSerializer(obj.contents.distinct(), many=True)
        return Response(serializer.data)

    @action(methods=['get'], detail=True, perms_map = {'get':'*'})
    def depts(self, request, *args, **kwargs):
        """
        聚合
        """
        obj = self.get_object()
        serializer = OrganizationSimpleSerializer(obj.depts.distinct(), many=True)
        return Response(serializer.data)

    @action(methods=['put'], detail=True, perms_map = {'put':'task_start'})
    def start(self, request, *args, **kwargs):
        """
        发布任务
        """
        obj = self.get_object()
        if obj.state == '待发布':
            obj.state = '执行中'
            obj.save()
            Record.objects.filter(task=obj).update(state='待报送')
            return Response(status=status.HTTP_200_OK)
        return Response('任务状态错误', status=status.HTTP_400_BAD_REQUEST)
    
    @action(methods=['get'], detail=True, perms_map = {'get':'*'})
    def deptall(self, request, *args, **kwargs):
        obj = self.get_object()
        serializer = TaskDeptSerializer(TaskDept.objects.filter(task=obj).order_by('dept__sort'), many=True)
        return Response(serializer.data)

    @action(methods=['get'], detail=False, perms_map = {'get':'*'})
    def self(self, request, *args, **kwargs):
        """
        我的报送任务
        """
        queryset = TaskDept.objects.exclude(task__state='待发布').order_by('-create_time')
        mydept = request.user.dept
        belong_depts = get_child_queryset2(mydept)
        queryset = queryset.filter(dept__in = belong_depts)
        dept = self.request.query_params.get('dept', mydept.id)
        queryset = queryset.filter(dept=dept)
        serializer = TaskDeptSerializer(queryset, many=True)
        return Response(serializer.data)
    
    
    @action(methods=['put'], detail=True, perms_map = {'put':'task_close'})
    def close(self, request, *args, **kwargs):
        """
        关闭任务
        """
        obj = self.get_object()
        if obj.state == '执行中':
            obj.state = '已关闭'
            obj.save()
            return Response(status=status.HTTP_200_OK)
        return Response('任务状态错误', status=status.HTTP_400_BAD_REQUEST)


def cal_task_rate(task, dept):
    """
    计算任务报送率, 确认率
    传入任务及公司
    """
    objs = Record.objects.filter(task=task).filter(belong_dept=dept)
    count = objs.count()
    up_rate = objs.filter(up_user__isnull=False).count()/count
    confirm_rate = objs.filter(state='已确认').count()/count
    instance = TaskDept.objects.get(task=task,dept=dept)
    instance.up_rate = int(up_rate*100)
    instance.confirm_rate = int(confirm_rate*100)
    instance.save()
    
from utils.queryset import get_child_queryset2

from .permission import RecordPermission

class RecordViewSet(RbacFilterSet, PageOrNot, CreateUpdateCustomMixin, ModelViewSet):
    perms_map = {'get': '*', 'post': 'record_create',
                 'put': 'record_update', 'delete': 'record_delete'}
    queryset = Record.objects.filter(content__cate=1)
    serializer_class = RecordSerializer
    search_fields = ['content__name','task__name']
    ordering_fields = ['content__sortnum', 'belong_dept__sort', 'task']
    ordering = ['-task', 'content__sortnum', '-create_time']
    filter_class = RecordFilter # 过滤类

    def filter_queryset(self, queryset):
        if not self.request.query_params.get('pageoff', None):
            queryset = queryset.exclude(state='待发布')
        return super().filter_queryset(queryset)

    def get_serializer_class(self):
        if self.action == 'create':
            return RecordCreateSerializer
        return super().get_serializer_class()

    @action(methods=['get'], detail=False, perms_map = {'get':'*'})
    def todos(self, request, *args, **kwargs):
        ret={}
        dept = request.user.dept
        objs = Record.objects.filter(is_deleted=False, belong_dept=dept)
        ret['toup'] = objs.filter(state='待报送').count()
        ret['tozg'] = objs.filter(state='待整改').count()
        if has_permission('record_confirm', request.user):
            ret['toconfirm'] = Record.objects.filter(is_deleted=False, state='已报送').count()
        return Response(ret)

    def update(self, request, *args, **kwargs):
        instance = Record.objects.get(pk=kwargs['pk'])
        instance.note = request.data.get('note', None)
        instance.update_by = request.user
        instance.save()
        return Response(status=status.HTTP_200_OK)

    @action(methods=['put'], detail=False, perms_map = {'put':'record_update'})
    def updates(self, request, *args, **kwargs):
        ids = request.data.get('ids')
        note = request.data.get('note', None)
        Record.objects.filter(pk__in=ids).update(note=note)
        return Response(status=status.HTTP_200_OK)

    @action(methods=['post'], detail=False, perms_map = {'post':'record_createself'})
    def createself(self, request, *args, **kwargs):
        """
        创建主动报送记录
        """
        contents = request.data['contents']
        rlist = []
        for i in contents:
            content = Content.objects.get(pk=i)
            data = {'content':content, 'belong_dept':request.user.dept, 'is_self':True,'dept_yes':False, 'content_name':content.name, 'content_desc':content.desc}
            rlist.append(Record(**data))
        Record.objects.bulk_create(rlist)
        return Response(status=status.HTTP_200_OK)

    def create(self, request, *args, **kwargs):
        """
        主动报送
        """
        data = request.data
        if data.get('files', None):
            serializer = RecordCreateSerializer(data=data)
            serializer.is_valid(raise_exception=True)
            content = Content.objects.get(pk=data['content'])
            if content.can_doself:
                sdata = {'belong_dept':request.user.dept, 
                'is_self':True, 
                'content_name':content.name, 
                'content_desc':content.desc, 
                'state':'已报送',
                'up_user':request.user,
                'up_date':timezone.now()}
                serializer.save(**sdata)
                return Response(status=status.HTTP_201_CREATED)
            return Response('该材料不在主动报送范围!', status=status.HTTP_400_BAD_REQUEST)
        return Response('未上传文件', status=status.HTTP_400_BAD_REQUEST)

    @action(methods=['put'], detail=True, perms_map = {'put':'record_up'})
    def up(self, request, *args, **kwargs):
        """
        报送
        """
        obj = self.get_object()
        if obj.task and obj.task.state == '已关闭':
            return Response('该任务已关闭,无法报送', status=status.HTTP_400_BAD_REQUEST) 
        if obj.state in ['待整改','待报送', '已报送']:
            if 'files' in request.data and (not request.data['files']):
                if request.data.get('noteb', None) and (not request.data.get('is_yes', True)):
                    pass
                else:
                    return Response('请上传文件或选择不适用并填写报送说明', status=status.HTTP_400_BAD_REQUEST)                   
            obj.is_yes = request.data.get('is_yes')
            obj.files.clear()
            obj.files.add(*request.data['files'])
            obj.noteb = request.data['noteb']
            obj.state = '已报送'
            if obj.belong_dept.type.name=='2级公司':
                obj.dept_yes = True
            obj.up_user = request.user
            obj.up_date = timezone.now()
            obj.save()
            if obj.task:
                cal_task_rate(obj.task, obj.belong_dept)
            return Response(status=status.HTTP_200_OK)
        return Response('记录状态错误', status=status.HTTP_400_BAD_REQUEST)
    
    @action(methods=['put'], detail=True, perms_map = {'put':'record_reject'}, permission_classes=[RecordPermission])
    def reject(self, request, *args, **kwargs):
        """
        驳回
        """
        obj = self.get_object()
        if obj.state == '已报送':
            if request.data.get('opinion', None):
                obj.opinion = request.data['opinion']
                obj.state = '待整改'
                obj.save()
                if obj.task:
                    cal_task_rate(obj.task, obj.belong_dept)
                return Response(status=status.HTTP_200_OK)
            else:
                return Response('请填写修改意见', status=status.HTTP_400_BAD_REQUEST)
        return Response('记录状态错误', status=status.HTTP_400_BAD_REQUEST)
    @action(methods=['put'], detail=True, perms_map = {'put':'record_detconfirm'})
    def deptconfirm(self, request, *args, **kwargs):
        """
        二级单位确认
        """
        obj = self.get_object()
        if obj.state in ['已报送']:
            obj.dept_yes = True
            obj.save()
            return Response(status=status.HTTP_200_OK)
        return Response('记录状态错误', status=status.HTTP_400_BAD_REQUEST)
    @action(methods=['put'], detail=True, perms_map = {'put':'record_confirm'})
    def confirm(self, request, *args, **kwargs):
        """
        确认
        """
        obj = self.get_object()
        if obj.state in ['已报送', '待报送']:
            obj.state = '已确认'
            obj.save()
            if obj.task:
                cal_task_rate(obj.task, obj.belong_dept)
            return Response(status=status.HTTP_200_OK)
        return Response('记录状态错误', status=status.HTTP_400_BAD_REQUEST)
    
    
class TaskDeptViewSet(ModelViewSet):
    """
    任务关联部门 操作
    """
    perms_map = {'get': '*', 'post': 'task_update',
                 'put': 'task_update', 'delete': 'task_update'}
    queryset = TaskDept.objects.all()
    serializer_class = TaskDeptSerializer
    filterset_fields = ['task','dept']
    ordering = ['dept__sort']

    @action(methods=['put'], detail=True, perms_map = {'put':'record_confirm'})
    def confirm(self, request, *args, **kwargs):
        """
        批量确认
        """
        obj = self.get_object()
        if obj.up_rate == 100:
            Record.objects.filter(task=obj.task, belong_dept=obj.dept).update(state='已确认')
            obj.confirm_rate = 100
            obj.save()
            return Response(status=status.HTTP_200_OK)
        return Response('报送未完成', status=status.HTTP_400_BAD_REQUEST)
    
    def destroy(self, request, *args, **kwargs):
        """
        删除
        """
        obj = self.get_object()
        Record.objects.filter(task=obj.task, belong_dept=obj.dept).delete() # 删除相应上报记录
        obj.delete()
        return Response()
   