from django.shortcuts import render
from django.conf import settings
from rest_framework.decorators import action
from rest_framework.mixins import ListModelMixin
from rest_framework.response import Response
from rest_framework.exceptions import ParseError

from apps.mtm.filters import GoalFilter, MaterialFilter, RouteFilter
from apps.mtm.models import Goal, Material, Mgroup, Shift, Team, Process, Route, TeamMember, RoutePack, Srule, RouteMat
from apps.mtm.serializers import (GoalSerializer, MaterialSerializer,
                                  MgroupGoalYearSerializer, MgroupSerializer, MgroupDaysSerializer,
                                  ShiftSerializer, TeamSerializer, ProcessSerializer, 
                                  RouteSerializer, TeamMemberSerializer, RoutePackSerializer,
                                  SruleSerializer, RouteMatSerializer)
from apps.mtm.services import get_mgroup_goals, daoru_material, get_mgroup_days
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
from apps.utils.mixins import BulkCreateModelMixin, BulkDestroyModelMixin, CustomListModelMixin
from rest_framework.serializers import Serializer
from django.db import transaction
from django.db.models import Q
from apps.wf.models import Ticket
from django.utils import timezone

# Create your views here.
class MaterialViewSet(CustomModelViewSet):
    """
    list:产品

    产品
    """
    queryset = Material.objects.all()
    serializer_class = MaterialSerializer
    filterset_class = MaterialFilter
    search_fields = ['name', 'code', 'number', 'specification', 'model']
    select_related_fields = ['process']
    ordering = ['name', 'model', 'specification',
                'type', 'process', 'process__sort', 'sort', 'id', 'number']
    ordering_fields = ['name', 'model', 'specification',
                       'type', 'process', 'process__sort', 'sort', 'id', 'number']

    @transaction.atomic
    def perform_destroy(self, instance):
        from apps.inm.models import MaterialBatch
        if MaterialBatch.objects.filter(material=instance).exists():
            raise ParseError('该物料有库存!')
        if Route.objects.filter(Q(material=instance) | Q(material_in=instance) | Q(material_out=instance)).exists():
            raise ParseError('该物料有工艺路线!')
        instance.delete()
        Material.objects.filter(parent=instance).update(is_deleted=True)

    @action(methods=['post'], detail=False, serializer_class=Serializer, perms_map={'post': 'material.create'})
    @transaction.atomic
    def daoru(self, request, *args, **kwargs):
        """导入物料

        导入物料
        """
        daoru_material(settings.BASE_DIR + request.data.get('path', ''))
        return Response()

    @action(methods=['put'], detail=True, serializer_class=Serializer, perms_map={'put': '*'})
    @transaction.atomic
    def set_week_esitimate_consume(self, request, *args, **kwargs):
        """设置周预估消耗量

        设置周预估消耗量
        """
        obj: Material = self.get_object()
        obj.week_esitimate_consume = request.data.get(
            'week_esitimate_consume', None)
        obj.save()
        return Response()
    
    @action(methods=['get'], detail=False, serializer_class=Serializer, perms_map={'get': '*'})
    def cates(self, request, *args, **kwargs):
        res = Material.objects.exclude(cate='').exclude(cate=None).values_list('cate', flat=True).distinct()
        return Response(set(res))

class ShiftViewSet(ListModelMixin, CustomGenericViewSet):
    """
    list:班次

    班次
    """
    perms_map = {'get': '*'}
    queryset = Shift.objects.all()
    serializer_class = ShiftSerializer
    search_fields = ['name']
    ordering = ['rule', 'sort', 'id']


class MgroupViewSet(CustomModelViewSet):
    """
    list:测点集

    测点集
    """
    queryset = Mgroup.objects.all()
    serializer_class = MgroupSerializer
    select_related_fields = ['create_by', 'belong_dept', 'process']
    filterset_fields = {
        "belong_dept": ["exact"],
        "process": ["exact"],
        "cate": ["exact"],
        "belong_dept__name": ["exact", "contains"],
        "name": ["exact", "contains"],
        "code": ["exact", "in", "isnull"]
    }
    search_fields = ['name']
    ordering = ['sort', 'create_time']

    def perform_destroy(self, instance):
        Route.objects.filter(
            mgroup=instance, material__need_route=False).delete()
        Route.objects.filter(mgroup=instance).update(mgroup=None)
        instance.delete()
    
    @action(methods=['post'], detail=False, perms_map={'post': '*'}, serializer_class=MgroupDaysSerializer)
    def get_days(self, request):
        """
        获取所属班年月日

        给定一个时间获取所属班年月日
        """
        data = request.data
        sr = MgroupDaysSerializer(data=data)
        sr.is_valid(raise_exception=True)
        vdata = sr.validated_data
        mgroup = Mgroup.objects.get(id=vdata["mgroup"])
        return Response(get_mgroup_days(mgroup, vdata.get("now", None)))




class TeamViewSet(CustomModelViewSet):
    """
    list:班组

    班组
    """
    queryset = Team.objects.all()
    serializer_class = TeamSerializer
    select_related_fields = ['belong_dept', 'leader']
    filterset_fields = ['belong_dept']
    search_fields = ['rule']


class TeamMemberViewSet(ListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
    """
    list: 班组成员

    班组成员
    """
    perms_map = {'get': '*', 'post': 'team.update', 'delete': 'team.delete'}
    queryset = TeamMember.objects.all()
    serializer_class = TeamMemberSerializer
    select_related_fields = ['mgroup', 'post', 'team', 'user']
    filterset_fields = ['mgroup', 'post', 'team']


class GoalViewSet(CustomModelViewSet):
    """
    list: 目标

    目标
    """
    queryset = Goal.objects.all()
    serializer_class = GoalSerializer
    select_related_fields = ['mgroup', 'goal_cate']
    filterset_class = GoalFilter
    search_fields = ['name']
    ordering = ['mgroup__sort', 'goal_cate__sort', 'goal_cate__create_time']

    @action(methods=['post'], detail=False, perms_map={'post': '*'}, serializer_class=MgroupGoalYearSerializer)
    def mgroup_goals_year(self, request, pk=None):
        """
        获取工段全年目标

        获取工段全年目标
        """
        sr = MgroupGoalYearSerializer(data=request.data)
        sr.is_valid(raise_exception=True)
        vdata = sr.validated_data
        res = get_mgroup_goals(vdata['mgroup'], vdata['year'], True)
        return Response(res)


class ProcessViewSet(CustomModelViewSet):
    queryset = Process.objects.all()
    serializer_class = ProcessSerializer
    select_related_fields = ['belong_dept']
    search_fields = ['name', 'cate']
    filterset_fields = ['cate']
    ordering = ['sort', 'create_time']

    def perform_destroy(self, instance):
        if Route.objects.filter(process=instance).exists():
            raise ParseError('存在使用的工艺路线!')
        return super().perform_destroy(instance)



class RoutePackViewSet(CustomModelViewSet):
    """
    list: 工艺路线包

    工艺路线包
    """
    queryset = RoutePack.objects.all()
    serializer_class = RoutePackSerializer
    search_fields = ['name']
    select_related_fields = ['material', 'create_by']
    filterset_fields = ['material', 'state']

    def update(self, request, *args, **kwargs):
        obj: RoutePack = self.get_object()
        if obj.state != RoutePack.RP_S_CREATE:
            raise ParseError('该状态下不可编辑')
        return super().update(request, *args, **kwargs)

    @transaction.atomic
    def destroy(self, request, *args, **kwargs):
        obj: RoutePack = self.get_object()
        if obj.state != RoutePack.RP_S_CREATE:
            raise ParseError('该状态下不可删除')
        obj.delete()
        Ticket.objects.filter(ticket_data__t_id=obj.id, ticket_data__t_model='routepack').delete()
        return Response(status=204)
    
    @transaction.atomic
    @action(methods=['post'], detail=True, perms_map={'post': 'routepack.create'}, serializer_class=Serializer)
    def copy(self, request, *args, **kwargs):
        """复制工艺路线

        复制工艺路线
        """
        obj = self.get_object()
        user = request.user
        now = timezone.now()
        obj_c = RoutePack()
        obj_c.name = f'{obj.name}_copy'
        obj_c.material = obj.material
        obj_c.create_by = user
        obj_c.create_time = now
        obj_c.save()
        for route in Route.objects.filter(routepack=obj):
            route_new = Route()
            for f in Route._meta.fields:
                if f.name not in ['id', 'create_by', 'update_by', 'create_time', 'update_time']:
                    setattr(route_new, f.name, getattr(route, f.name, None))
            route_new.routepack = obj_c
            route_new.create_by = user
            route_new.create_time = now
            route_new.save()
            for rm in RouteMat.objects.filter(route=route):
                rm_new = RouteMat()
                rm_new.route = route_new
                rm_new.material = rm.material
                rm_new.save()
        return Response()


class RouteViewSet(CustomModelViewSet):
    queryset = Route.objects.all()
    serializer_class = RouteSerializer
    filterset_class = RouteFilter
    ordering = ['sort', 'process__sort', 'create_time']
    select_related_fields = ['material',
                             'process', 'material_in', 'material_out', 'mgroup', 'routepack']

    def update(self, request, *args, **kwargs):
        obj:Route = self.get_object()
        routepack = obj.routepack
        if routepack and routepack.state != RoutePack.RP_S_CREATE:
            raise ParseError('该状态下不可编辑')
        return super().update(request, *args, **kwargs)


class SruleViewSet(CustomModelViewSet):
    """
    list: 排班规则
    """
    queryset = Srule.objects.all()
    serializer_class = SruleSerializer
    select_related_fields = ['belong_dept']
    filterset_fields = ['belong_dept']
    search_fields = ['belong_dept__name']
    
    def add_info_for_list(self, data):
        rule_list = [x['rule'] for x in data]
        teamIds = []
        for rule in rule_list:
            teamIds.extend(rule)
        teams = Team.objects.filter(id__in=teamIds).values_list('id', 'name')
        teams_dict = dict(teams)
        for item in data:
            rule = item["rule"]
            item["rule_display"] = "->".join([teams_dict.get(x, '未知') for x in rule])
        return data

class RouteMatViewSet(CustomListModelMixin, BulkCreateModelMixin, BulkDestroyModelMixin, CustomGenericViewSet):
    perms_map = {"get": "*", "post": "route.update", "delete": "route.update"}
    queryset = RouteMat.objects.all()
    serializer_class = RouteMatSerializer
    filterset_fields = ['route', 'material']