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
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)
    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):
    """
    子生产计划
    """
    production_plan = models.ForeignKey(ProductionPlan, verbose_name='关联主生产计划', on_delete=models.CASCADE)
    subproduction = models.ForeignKey(SubProduction, verbose_name='关联生产分解', on_delete=models.CASCADE)
    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)
    # steps = models.JSONField('工艺步骤', default=list)
    class Meta:
        verbose_name = '子生产计划'
        verbose_name_plural = verbose_name

# class ProductionProgress(BaseModel):
#     """
#     子计划生产进度
#     """
#     subproduction_plan = models.ForeignKey(SubProductionPlan, verbose_name='关联子生产计划', on_delete=models.CASCADE)
#     material = models.