from django.db import models
from apps.utils.models import CommonADModel, CommonBDModel
from apps.mtm.models import Material, Mgroup

# Create your models here.

TASK_TYPE = (
    ('mass', '量产'),
    ('pilot', '中试')
)


class Utask(CommonBDModel):
    """
    生产大任务
    """
    UTASK_CREATED = 10
    UTASK_DECOMPOSE = 14
    UTASK_ASSGINED = 20
    UTASK_WORKING = 30
    UTASK_STOP = 34
    UTASK_SUBMIT = 40
    UTASK_STATES = (
        (UTASK_CREATED, '创建中'),
        (UTASK_DECOMPOSE, '已分解'),
        (UTASK_ASSGINED, '已下达'),
        (UTASK_WORKING, '生产中'),
        (UTASK_STOP, '已停止'),
        (UTASK_SUBMIT, '已提交')
    )
    type = models.CharField('任务类型', max_length=10,
                            help_text=str(TASK_TYPE), default='mass')
    state = models.PositiveIntegerField(
        '状态', choices=UTASK_STATES, default=UTASK_CREATED, help_text=str(UTASK_STATES))
    number = models.CharField('编号', max_length=50, unique=True)
    material = models.ForeignKey(
        Material, verbose_name='产品', on_delete=models.CASCADE)
    mgroup = models.ForeignKey(
        Mgroup, verbose_name='工段', on_delete=models.CASCADE, null=True, blank=True)
    material_in = models.ForeignKey(
        Material, verbose_name='领用物', on_delete=models.CASCADE, related_name='utask_material_in', null=True, blank=True)
    count = models.PositiveIntegerField('任务数', default=1)
    count_real = models.PositiveIntegerField('实际生产数', default=0)
    count_day = models.PositiveIntegerField('日均任务数', default=0)
    count_ok = models.PositiveIntegerField('合格数', default=0)
    count_notok = models.PositiveIntegerField('不合格数', default=0)
    start_date = models.DateField('计划开工日期')
    end_date = models.DateField('计划完工日期')


class Mtask(CommonADModel):
    """
    生产任务
    """
    MTASK_CREATED = 10
    MTASK_ASSGINED = 20
    MTASK_STOP = 34
    MTASK_SUBMIT = 40
    MTASK_STATES = (
        (MTASK_CREATED, '创建中'),
        (MTASK_ASSGINED, '已下达'),
        (MTASK_STOP, '已停止'),
        (MTASK_SUBMIT, '已提交')
    )
    type = models.CharField('任务类型', max_length=10,
                            help_text=str(TASK_TYPE), default='mass')
    state = models.PositiveIntegerField(
        '状态', choices=MTASK_STATES, default=MTASK_CREATED, help_text=str(MTASK_STATES))
    number = models.CharField('编号', max_length=50, unique=True)
    mgroup = models.ForeignKey(
        Mgroup, verbose_name='工段', on_delete=models.CASCADE)
    material_in = models.ForeignKey(
        Material, verbose_name='领用物', on_delete=models.CASCADE, related_name='mtask_material_in', null=True, blank=True)
    material_out = models.ForeignKey(
        Material, verbose_name='产物', on_delete=models.CASCADE, related_name='mtask_material_out')
    is_count_utask = models.BooleanField('是否主任务统计', default=False)
    count = models.PositiveIntegerField('任务数', default=1)
    count_real = models.PositiveIntegerField('实际生产数', default=0)
    count_ok = models.PositiveIntegerField('合格数', default=0)
    count_notok = models.PositiveIntegerField('不合格数', default=0)
    start_date = models.DateField('计划开工日期')
    end_date = models.DateField('计划完工日期')
    utask = models.ForeignKey(
        Utask, verbose_name='关联大任务', on_delete=models.CASCADE, related_name='mtask_utask', null=True, blank=True)
    peifen_kg = models.FloatField('配粉料数', default=0)

    submit_time = models.DateTimeField('提交时间', null=True, blank=True)
    submit_user = models.ForeignKey(
        'system.user', verbose_name='提交人', on_delete=models.CASCADE, null=True, blank=True, related_name='mtask_submit_user')

    @property
    def related(self):
        """
        当天已下达的所有的兄弟小任务
        """
        if self.utask:
            return Mtask.objects.filter(
                utask=self.utask, mgroup__belong_dept=self.mgroup.belong_dept, start_date=self.start_date, end_date=self.start_date, state=self.MTASK_ASSGINED).order_by(
                    'utask', 'mgroup__process__sort', 'create_time'
            )
        return Mtask.objects.none()

    @property
    def mlogs(self):
        from apps.wpm.models import Mlog
        return Mlog.objects.filter(mtask=self)
