from typing import List
from apps.pm.models import SubProductionPlan
from apps.mtm.models import Step, SubprodctionMaterial
from apps.wpm.models import WProduct
class WpmServies(object):

    @classmethod    
    def get_next_step(cls, subproduction_plan:SubProductionPlan, nowstep:Step):
        """
        获取下一步骤
        """
        stepIds = [i['id'] for i in subproduction_plan.steps]
        pindex = stepIds.index(nowstep.id)
        if pindex + 1 < len(stepIds):
            return Step.objects.get(pk=stepIds[pindex+1]), True
        else:
            return nowstep, False

    @classmethod
    def get_subplans_queryset_from_wproducts(cls, wproducts:List):
        """
        通过半成品列表获取所属子计划
        """
        splans = SubProductionPlan.objects.filter(is_deleted=False, wproduct_subplan__in=wproducts)
        return splans

    @classmethod
    def get_subplans_queyset_from_step(cls, step:Step):
        """
        通过当前操作获取所有正在进行的子计划
        """
        splans = SubProductionPlan.objects.filter(is_deleted=False, 
                subproduction__usedstep_subproduction__step=step, state=SubProductionPlan.SUBPLAN_STATE_WORKING)
        return splans