from io import open_code
from apps.system.models import CommonAModel, Organization
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.db.models.base import Model
import django.utils.timezone as timezone
from django.db.models.query import QuerySet

from utils.model import SoftModel, BaseModel
from apps.mtm.models import Material, Process, SubProduction, SubprodctionMaterial
from apps.sam.models import Order

class ProductionPlan(CommonAModel):
    """
    生产计划
    """
    number = models.CharField('编号', max_length=50, unique=True)
    order = models.ForeignKey(Order, verbose_name='关联订单', null=True, blank=True, on_delete=models.SET_NULL)
    product = models.ForeignKey(Material, verbose_name='生产产品', on_delete=models.CASCADE)
    count = models.IntegerField('生产数量', default=1)
    count_real = models.IntegerField('实际产出数', default=0)
    count_ok = models.IntegerField('合格数', default=0)
    start_date = models.DateField('计划开工日期')
    end_date = models.DateField('计划完工日期')
    is_planed = models.BooleanField('是否已排产', default=False)
    class Meta:
        verbose_name = '生产计划'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number

class SubProductionPlan(CommonAModel):
    """
    子生产计划
    """
    SUBPLAN_STATE_PLANING = 0
    SUBPLAN_STATE_ASSGINED = 1
    SUBPLAN_STATE_ACCEPTED = 2
    SUBPLAN_STATE_WORKING = 3
    SUBPLAN_STATE_DONE = 4
    state_choices=(
        (SUBPLAN_STATE_PLANING, '制定中'),
        (SUBPLAN_STATE_ASSGINED, '已下达'),
        (SUBPLAN_STATE_ACCEPTED, '已接收'),
        (SUBPLAN_STATE_WORKING, '生产中'),
        (SUBPLAN_STATE_DONE, '已完成')
    )
    number = models.CharField('子计划编号', max_length=50, unique=True, null=True, blank=True)
    production_plan = models.ForeignKey(ProductionPlan, verbose_name='关联主生产计划', on_delete=models.CASCADE)
    subproduction = models.ForeignKey(SubProduction, verbose_name='关联生产分解', on_delete=models.CASCADE, related_name='subplan_subprod')
    start_date = models.DateField('计划开工日期')
    end_date = models.DateField('计划完工日期')

    workshop = models.ForeignKey(Organization, verbose_name='生产车间', on_delete=models.CASCADE)
    process = models.ForeignKey(Process, verbose_name='关联大工序', on_delete=models.CASCADE)

    main_product = models.ForeignKey(Material, verbose_name='主要产品', on_delete=models.CASCADE, null=True, blank=True)
    main_count = models.IntegerField('应产出数', default=0)
    main_count_real = models.IntegerField('实际产出数', default=0)
    main_count_ok = models.IntegerField('合格数', default=0)

    steps = models.JSONField('工艺步骤', default=list)

    state = models.IntegerField('状态', default=SUBPLAN_STATE_PLANING)
    start_date_real = models.DateField('实际开工日期', null=True, blank=True)
    end_date_real = models.DateField('实际完工日期', null=True, blank=True)
    is_picked = models.BooleanField('是否已领料', default=False)

    # wproducts = models.JSONField('半成品表', default=list, blank=True)
    class Meta:
        verbose_name = '子生产计划'
        verbose_name_plural = verbose_name

class SubProductionProgress(BaseModel):
    """
    子计划生产进度统计表/物料消耗
    """
    subproduction_plan = models.ForeignKey(SubProductionPlan, verbose_name='关联子生产计划', on_delete=models.CASCADE, related_name='progress_subplan')
    material = models.ForeignKey(Material, verbose_name='关联物料', on_delete=models.CASCADE)
    is_main = models.BooleanField('是否主产出', default=False)
    type = models.IntegerField('物料应用类型', default=SubprodctionMaterial.type_choices)
    count = models.IntegerField('应出入数')
    count_pick = models.IntegerField('实际领用数', default=0)
    count_real = models.IntegerField('实际消耗/产出数', default=0)
    count_ok = models.IntegerField('合格数量', default=0)
