from django.db import models
from django.db.models.enums import Choices
from apps.mtm.models import RecordForm, RecordFormField
from apps.system.models import CommonADModel, CommonAModel, File
from utils.model import BaseModel
# Create your models here.
class Standard(CommonAModel):
    """
    标准
    """
    number = models.CharField('标准编号', max_length=100)
    name = models.CharField('标准名称', max_length=100)
    enabled = models.BooleanField('是否启用', default=True)
    file = models.ForeignKey(File, verbose_name='文件', null=True, blank=True, on_delete=models.CASCADE)

    class Meta:
        verbose_name = '标准库'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.number + '-' + self.name

class TestItem(CommonAModel):
    """
    检验项目
    """
    name = models.CharField('名称', max_length=100)
    standard = models.ForeignKey(Standard, verbose_name='关联标准', on_delete=models.CASCADE)
    term_number = models.CharField('条款号', max_length=100)

    class Meta:
        verbose_name = '检验项目'
        verbose_name_plural = verbose_name

class AnalysisItem(CommonAModel):
    """
    检验分析项
    """
    name = models.CharField('名称', max_length=100)
    testitem = models.ForeignKey(TestItem, on_delete=models.CASCADE, verbose_name='关联检验项目')
    rules =models.JSONField('判定规则', default=list)

    class Meta:
        verbose_name = '检验分析项'

class TestRecord(CommonADModel):
    """
    检验记录
    """
    TEST_STEP = 10
    TEST_PROCESS = 20
    TEST_PROCESS_RE = 30
    TEST_COMB = 36
    TEST_FINAL = 40
    type_choice = (
        (TEST_STEP, '子工序检验'),
        (TEST_PROCESS, '工序检验'),
        (TEST_PROCESS_RE, '工序复检'),
        (TEST_COMB, '夹层检验'),
        (TEST_FINAL, '成品检验')
    )
    form = models.ForeignKey('mtm.recordform', verbose_name='所用表格', on_delete=models.CASCADE)
    type = models.PositiveSmallIntegerField(choices=type_choice, default=TEST_PROCESS)
    is_testok = models.BooleanField('是否合格', default=True)
    number = models.CharField('产品编号', null=True, blank=True, max_length=50)
    wproduct = models.ForeignKey('wpm.wproduct', verbose_name='关联的动态产品', on_delete=models.CASCADE, null=True, blank=True, related_name='test_wproduct')
    material = models.ForeignKey('mtm.material', verbose_name='关联的物料状态', on_delete=models.CASCADE, null=True, blank=True)
    step = models.ForeignKey('mtm.step', verbose_name='关联的工序步骤', on_delete=models.CASCADE, null=True, blank=True)
    subproduction_plan = models.ForeignKey('pm.subproductionplan', verbose_name='关联的生产子计划', on_delete=models.CASCADE, null=True, blank=True)
    fifo_item = models.ForeignKey('inm.fifoitem', verbose_name='关联的出入库批次', on_delete=models.CASCADE, null=True, blank=True)
    origin_test = models.ForeignKey('self', verbose_name='原检验记录', on_delete=models.CASCADE, null=True, blank=True)
    is_submited = models.BooleanField('是否提交', default=False)
    remark = models.TextField('备注', default='')


class TestRecordItem(BaseModel):
    """
    记录表格字段值
    """
    form_field = models.ForeignKey(RecordFormField, verbose_name='关联自定义表格字段', on_delete=models.CASCADE)
    field_value = models.JSONField('录入值', null=True, blank=True)
    is_hidden = models.BooleanField('是否隐藏', default=False)
    is_testok = models.BooleanField('是否合格', null=True, blank=True)
    test_record = models.ForeignKey(TestRecord, verbose_name='关联的检验记录', on_delete=models.CASCADE, related_name='item_test_record')