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, CreateModelMixin, UpdateModelMixin, DestroyModelMixin
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet, ModelViewSet
from utils.pagination import PageOrNot
from openpyxl import load_workbook
import datetime

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 django.db import transaction

from .filters import RecordFilter, PgoalDeptFilter
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()


class PogalViewSet(CreateUpdateCustomMixin, ModelViewSet):
    perms_map = {'get': '*', 'post': 'pgoal',
                 'put': 'pgoal', 'delete': 'pgoal'}
    queryset = Pgoal.objects.all()
    serializer_class = PgoalSerializer
    filterset_fields = ['year']
    ordering = ['-year']

    def get_serializer_class(self):
        if self.action in ['create', 'update']:
            return PgoalCreateUpdateSerializer
        return super().get_serializer_class()

    
class Task2ViewSet(CreateUpdateCustomMixin, ModelViewSet):
    perms_map = {'get': '*', 'post': 'task2',
                 'put': 'task2', 'delete': 'task2'}
    queryset = Task2.objects.all()
    serializer_class = Task2Serializer
    filterset_fields = ['year', 'cycle', 'type', 'state']
    ordering = ['-create_time']

    @action(methods=['put'], detail=True, perms_map = {'put':'task2'})
    def close(self, request, *args, **kwargs):
        """关闭任务

        关闭任务
        """
        obj = self.get_object()
        if obj.state == Task2.TASK2_DOING:
            for i in Task2Do.objects.filter(task2=obj):
                if i.count_up < i.count_all:
                    raise ParseError('存在公司未完成报送')
            obj.state = Task2.TASK2_CLOSE
            obj.save()
        return Response()

    @transaction.atomic
    @action(methods=['put'], detail=True, perms_map = {'put':'task2'}, serializer_class=serializers.Serializer)
    def start(self, request, *args, **kwargs):
        """
        开始执行
        """
        obj = self.get_object()
        tds = Task2Do.objects.filter(task2=obj)
        if not tds.exists():
            raise ParseError('未添加执行公司')
        if obj.type == Task2.TASK2_F:
            try:
                pg = Pgoal.objects.get(year=obj.year)
            except:
                raise ParseError('未配置年度基础目标')
            for i in tds:
                for key in Pgoal.basedict:
                    pds = PgoalDept.objects.filter(task2do=i, goal_key=key)
                    if pds.exists():
                        pds.update(**{'goal_name': Pgoal.basedict[key], 'goal_value_a': getattr(pg, key)})
                    else:
                        PgoalDept.objects.create(**{'year': i.task2.year, 'task2do': i, 'belong_dept': i.belong_dept, 
                                    'goal_name': Pgoal.basedict[key], 'goal_key': key, 'goal_value_a': getattr(pg, key)})
        if obj.state == Task2.TASK2_CREATE:
            obj.state = Task2.TASK2_DOING
            obj.save()
        return Response()

    @action(methods=['post'], detail=True, perms_map = {'post':'task2'}, serializer_class=Task2DeptsSerializer)
    def add_depts(self, request, *args, **kwargs):
        """添加执行部门
        """
        task2 = self.get_object()
        if task2.state == Task2.TASK2_CLOSE:
            raise ParseError('任务已关闭,不可操作')
        sr = Task2DeptsSerializer(data=request.data)
        sr.is_valid(raise_exception=True)
        vdata = sr.validated_data
        count_all = 9
        if task2.type == Task2.TASK2_F:
            count_all = 5
        for i in vdata['ids']:
            Task2Do.objects.get_or_create(task2=task2, belong_dept=i, defaults={'task2': task2, 'belong_dept': i, 'count_all': count_all, 'create_by': self.request.user})
        return Response()
    

class Task2DoViewSet(ListModelMixin, DestroyModelMixin, GenericViewSet):
    perms_map = {'get': '*', 'delete': 'task2'}
    queryset = Task2Do.objects.select_related('belong_dept', 'task2').all()
    serializer_class = Task2DoSerializer
    ordering = ['belong_dept__sort']
    filterset_fields = ['task2', 'belong_dept', 'task2__year', 'task2__cycle']
    cfields = ['num_issue', 'num_expect', 'num_error', 'num_overdue', 'num_complaint', 'num_pt', 'num_risk', 'num_oinspect', 'num_acc']

    @classmethod
    def countup_task2do(cls, instance):
        """计算并更新某部门日常监督任务完成数量
        """
        count_up = 0
        for i in cls.cfields:
            if getattr(instance, i) is not None:
                count_up = count_up + 1
        instance.count_up = count_up
        instance.save()

    @action(methods=['put'], detail=True, perms_map = {'put':'task2do'}, serializer_class=UpdateFieldSerializer)
    def save_num(self, request, *args, **kwargs):
        """保存数值

        保存数值
        """
        obj = self.get_object()
        obj.num_issue = request.data.get('num_issue', 0)
        obj.num_expect = request.data.get('num_expect', 0)
        obj.num_acc = request.data.get('num_acc', 0)
        obj.update_by = request.user
        obj.save()
        self.countup_task2do(obj)
        return Response()
    
    @action(methods=['put'], detail=True, perms_map = {'put':'task2do'}, serializer_class=NoRecordSerializer)
    def no_record(self, request, *args, **kwargs):
        """没有记录

        没有记录
        """
        obj = self.get_object()
        field = request.data.get('key')
        theattr = getattr(obj, field, None)
        if theattr == 0:
            return Response()
        if theattr is None:
            setattr(obj, field, 0)
            if field == 'num_complaint':
                obj.num_complaint_10 = 0
            elif field == 'num_pt':
                obj.num_pt_10 = 0
        else:
            raise ParseError('已有记录')
        obj.save()
        self.countup_task2do(obj)
        return Response()
    
    @action(methods=['get'], detail=False, perms_map = {'get':'task2do'})
    def my(self, request, *args, **kwargs):
        """我的任务

        我的任务
        """
        mydept = request.user.dept
        belong_depts = get_child_queryset2(mydept)
        queryset = Task2Do.objects.filter(belong_dept__in = belong_depts).exclude(task2__state=10).order_by('-create_time')
        queryset = self.filter_queryset(queryset)
        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = Task2DoSerializer(page, many=True)
            return self.get_paginated_response(serializer.data)
        serializer = Task2DoSerializer(queryset, many=True)
        return Response(serializer.data)


class Domixin:
    perms_map = {'get': '*', 'put': 'task2do', 'post': 'task2do', 'delete': 'task2do'}
    ordering = ['create_time']
    ordering_fields = ['create_time', 'belong_dept__sort']
    filterset_fields = ['task2do', 'task2do__task2', 'task2do__belong_dept']

    def countnum_task2do_field(self, task2do):
        for k, v in self.count_dict.items():
            setattr(task2do, k,  self.queryset.model.objects.filter(**{**{'task2do': task2do}, **v}).count())
        task2do.save()
        # 更新count_up字段
        Task2DoViewSet.countup_task2do(task2do)

    def countup_task2do(self, task2do):
        if task2do.task2.type == Task2.TASK2_F:
            task2do.count_up = PgoalDept.objects.filter(task2do=task2do).exclude(goal_value_b=None).count()
            task2do.save()
        elif task2do.task2.type == Task2.TASK2_S:
            self.countnum_task2do_field(task2do)

    @transaction.atomic
    def perform_create(self, serializer):
        instance = serializer.save()
        self.countup_task2do(instance.task2do)

    @transaction.atomic
    def perform_destroy(self, instance):
        task2do = instance.task2do
        instance.delete()
        self.countup_task2do(task2do)
        

class PgoalDeptViewSet(Domixin, CreateUpdateCustomMixin, ModelViewSet):
    queryset = PgoalDept.objects.all()
    serializer_class = PgoalDeptSerializer
    filterset_class = PgoalDeptFilter

    def perform_update(self, serializer):
        instance = serializer.save()
        goal_file = instance.goal_file
        if goal_file:
            instance.goal_files.add(goal_file)
        self.countup_task2do(instance.task2do)

class ImpMixin:
    def format_date(self, ind, val):
        new_val = val
        if isinstance(val, datetime.datetime):
            new_val = val.date()
        elif isinstance(val, datetime.date):
            new_val = val
        elif isinstance(val, str):
            try:
                new_val = datetime.datetime.strptime(val, '%Y-%m-%d').date()
            except ValueError:
                raise ParseError(f'第{ind}行, 日期时间格式错误')
        else:
            raise ParseError(f'第{ind}行, 日期时间格式错误')
        return new_val

    def get_enum(self, val, atuple, ind):
        for i in atuple:
            if i[1] == val:
                return i[0]
        raise ParseError('第{}: 请选择固定选项值'.format(ind))
    
    def make_data(self, data, sheet, i, etype):
        raise NotImplementedError()

    def gen_imp_view(self, request, start: int, mySerializer, etype=0):
        sr = ImpSerializer(data=request.data)
        sr.is_valid(raise_exception=True)
        vdata = sr.validated_data
        path = vdata['path']
        if not path.endswith('.xlsx'):
            raise ParseError('请提供xlsx格式文件')
        task2do = vdata['task2do']
        fullpath = settings.BASE_DIR + path
        wb = load_workbook(fullpath,data_only=True)
        sheet = wb.worksheets[0]
        datalist = []
        i = start
        while sheet['b'+str(i)].value:
            data = {}
            data['task2do'] = task2do.id
            data = self.make_data(data, sheet, i, etype)
            datalist.append(data)
            i = i + 1
        sr = mySerializer(data=datalist, many=True, context={'request': request})
        sr.is_valid()
        err_msg = ''
        for ind, val in enumerate(sr.errors):
            if val:
                for i in val:
                    err_msg = '第{}行: {}'.format(5+ind, i + str(val[i][0]))
        if err_msg != '':
            raise ParseError(err_msg)
        else:
            sr.save(create_by=request.user)
        self.countup_task2do(task2do)  # 统计执行情况
        return Response()

class RcViewSet(ImpMixin, Domixin, CreateUpdateCustomMixin, ModelViewSet):
    queryset = Rc.objects.all()
    serializer_class = RcSerializer
    count_dict = {
        "num_error": {'etype': 10},
        "num_overdue": {'etype': 20}
    }
    filterset_fields = ['etype', 'task2do', 'task2do__belong_dept', 'task2do__task2__year']


    def make_data(self, data, sheet, i, etype):
        data['etype'] = etype
        if etype == 10:
            data['number'] = sheet['b'+str(i)].value
            data['number_correct'] = sheet['c'+str(i)].value
            data['reason_error'] = sheet['d'+str(i)].value
            data['date_issue'] = self.format_date(i, sheet['e'+str(i)].value)
        else:
            data['number'] = sheet['b'+str(i)].value
            data['date_accept'] = self.format_date(i, sheet['c'+str(i)].value)
            data['date_expect'] = self.format_date(i, sheet['d'+str(i)].value)
            data['date_issue'] = self.format_date(i, sheet['e'+str(i)].value)
            data['remark'] = sheet['f'+str(i)].value
        return data
    
    @action(methods=['post'], detail=False, perms_map = {'post':'task2do'}, serializer_class=ImpSerializer)
    @transaction.atomic
    def imp_error(self, request, *args, **kwargs):
        """导入表格

        导入表格
        """
        return self.gen_imp_view(request, 5, RcSerializer, 10)
    
    
    @action(methods=['post'], detail=False, perms_map = {'post':'task2do'}, serializer_class=ImpSerializer)
    @transaction.atomic
    def imp_overdue(self, request, *args, **kwargs):
        """导入表格

        导入表格
        """
        return self.gen_imp_view(request, 5, RcSerializer, 20)

    

class ComplaintViewSet(ImpMixin, Domixin, CreateUpdateCustomMixin, ModelViewSet):
    queryset = Complaint.objects.all()
    serializer_class = ComplaintSerializer
    count_dict = {
        "num_complaint": {},
        "num_complaint_10": {'is_satisfied': True}
    }
    filterset_fields = ['task2do', 'task2do__belong_dept', 'task2do__task2__year']

    def make_data(self, data, sheet, i, etype):
        data['complainant'] = sheet['b'+str(i)].value
        data['date_occurrence'] = self.format_date(i, sheet['c'+str(i)].value)
        data['content'] = sheet['d'+str(i)].value
        data['date_feedback'] = self.format_date(i, sheet['e'+str(i)].value)
        data['is_satisfied'] = True if  sheet['f'+str(i)].value == '是' else False
        return data
    
    @action(methods=['post'], detail=False, perms_map = {'post':'task2do'}, serializer_class=ImpSerializer)
    @transaction.atomic
    def imp(self, request, *args, **kwargs):
        """导入表格

        导入表格
        """
        return self.gen_imp_view(request, 5, ComplaintSerializer)

        
class PtViewSet(ImpMixin, Domixin, CreateUpdateCustomMixin, ModelViewSet):
    queryset = Pt.objects.all()
    serializer_class = PtSerializer
    count_dict = {
        "num_pt": {},
        "num_pt_10": {'result': 10}
    }
    filterset_fields = ['task2do', 'task2do__belong_dept', 'task2do__task2__year']

    def make_data(self, data, sheet, i, etype):
        data['name'] = sheet['b'+str(i)].value
        data['number'] = sheet['c'+str(i)].value
        data['organizer'] = sheet['d'+str(i)].value
        data['params'] = sheet['e'+str(i)].value
        data['result'] = self.get_enum(sheet['f'+str(i)].value, Pt.pt_result_choices, i)
        data['handle_result'] = self.get_enum(sheet['g'+str(i)].value, Pt.pt_hresult_choices, i)
        return data
    
    @action(methods=['post'], detail=False, perms_map = {'post':'task2do'}, serializer_class=ImpSerializer)
    @transaction.atomic
    def imp(self, request, *args, **kwargs):
        """导入表格

        导入表格
        """
        return self.gen_imp_view(request, 5, PtSerializer)


class RiskViewSet(ImpMixin, Domixin, CreateUpdateCustomMixin, ModelViewSet):
    queryset = Risk.objects.all()
    serializer_class = RiskSerializer
    count_dict = {
        "num_risk": {}
    }
    filterset_fields = ['task2do', 'task2do__belong_dept', 'task2do__task2__year']

    def make_data(self, data, sheet, i, etype):
        data['content'] = sheet['b'+str(i)].value
        data['level'] = self.get_enum(sheet['c'+str(i)].value, Risk.risk_level_choices, i)
        data['code'] = sheet['d'+str(i)].value
        data['state'] = self.get_enum(sheet['e'+str(i)].value, Risk.risk_state_choices, i)
        return data
    
    @action(methods=['post'], detail=False, perms_map = {'post':'task2do'}, serializer_class=ImpSerializer)
    @transaction.atomic
    def imp(self, request, *args, **kwargs):
        """导入表格

        导入表格
        """
        return self.gen_imp_view(request, 5, RiskSerializer)


class OinspectViewSet(ImpMixin, Domixin, CreateUpdateCustomMixin, ModelViewSet):
    queryset = Oinspect.objects.all()
    serializer_class = OinspectSerializer
    count_dict = {
        "num_oinspect": {}
    }
    filterset_fields = ['task2do', 'task2do__belong_dept', 'task2do__task2__year']

    def make_data(self, data, sheet, i, etype):
        data['cate'] = self.get_enum(sheet['b'+str(i)].value, Oinspect.oinspect_cate_choices, i)
        data['checker'] = sheet['c'+str(i)].value
        data['date_inspect'] = self.format_date(i, sheet['d'+str(i)].value)
        data['result'] = sheet['e'+str(i)].value
        return data
    
    @action(methods=['post'], detail=False, perms_map = {'post':'task2do'}, serializer_class=ImpSerializer)
    @transaction.atomic
    def imp(self, request, *args, **kwargs):
        """导入表格

        导入表格
        """
        return self.gen_imp_view(request, 5, OinspectSerializer)