from django_filters import rest_framework as filters
from apps.wpm.models import (Handoverbw, Mlogbw, WMaterial)
from apps.inm.models import MIOItemw
from apps.wpmw.models import Wpr
from apps.mtm.models import Route, Material, Mgroup
from django.db.models import Q
from rest_framework.exceptions import ParseError


class WprFilter(filters.FilterSet):
    can_use = filters.CharFilter(method='filter_can_use')
    mgroupx = filters.CharFilter(label='MgroupId', method='filter_mgroupx')
    tag = filters.CharFilter(label="todo/done", method="filter_tag")
    class Meta:
        model = Wpr
        fields = {
            "mb": ["exact", "isnull"],
            "wm": ["exact", "isnull"],
            "material__process": ["exact"],
            "material__name": ["exact", "contains"],
            "wpr_from": ["exact", "isnull"],
            "state": ["exact"],
            "defects": ["exact"],
            "number": ["exact"]
        }
    
    def filter_mgroupx(self, queryset, name, value):
        return queryset.filter(wm__in=WMaterial.ava_qs(mgroup=Mgroup.objects.get(id=value)))
    
    def filter_tag(self, queryset, name, value):
        mgroupId = self.data.get("mgroupx", None)
        if mgroupId:
            process = Mgroup.objects.get(id=mgroupId).process
            
            queryset = queryset.filter(material__type__in=[Material.MA_TYPE_MAINSO, Material.MA_TYPE_HALFGOOD, Material.MA_TYPE_GOOD])
            if value == "todo":
                matIds = process.get_canin_mat_ids()
                qs = queryset.filter(material__id__in=matIds).exclude(state=WMaterial.WM_REPAIRED)|queryset.filter(state=WMaterial.WM_REPAIR, wm__mgroup__id=mgroupId)
                return qs
            elif value == "done":
                matoutIds = process.get_canout_mat_ids()
                qs = queryset.filter(material__id__in=matoutIds).exclude(state=WMaterial.WM_REPAIR)|queryset.filter(state=WMaterial.WM_REPAIRED, wm__mgroup__id=mgroupId)
                return qs
            elif value == "canfix":
                matoutIds = process.get_canout_mat_ids()
                qs = queryset.filter(state=WMaterial.WM_REPAIR, wm__mgroup__id=mgroupId)| queryset.filter(material__id__in=matoutIds, state=WMaterial.WM_NOTOK)
                return qs
        else:
            raise ParseError("请提供工段查询条件")

    def filter_can_use(self, queryset, name, value):
        if value == 'yes':
            # 交接记录
            handoverws = Handoverbw.objects.filter(handoverb__handover__submit_time__isnull=True)
            if self.data.get("wm"):
                handoverws = handoverws.filter(handoverb__wm=self.data.get("wm"))
            handoverws = handoverws.values_list('wpr__id', flat=True)
            # 生产日志
            mlogbws = Mlogbw.objects.filter(mlogb__mlog__submit_time__isnull=True)
            if self.data.get("wm"):
                mlogbws = mlogbws.filter(mlogb__wm_in=self.data.get("wm"))
            mlogbws = mlogbws.values_list('wpr__id', flat=True)
            # 出入库记录
            mioitemws = MIOItemw.objects.filter(mioitem__mio__submit_time__isnull=True)
            if self.data.get("wm"):
                mioitemws = mioitemws.filter(mioitem__wm=self.data.get("wm"))
            if self.data.get("mb"):
                mioitemws = mioitemws.filter(mioitem__mb=self.data.get("mb"))
            mioitemws = mioitemws.values_list('wpr__id', flat=True)
            return queryset.exclude(id__in=handoverws).exclude(id__in=mlogbws).exclude(id__in=mioitemws)
        return queryset