from django.db import models
from django.db.models.enums import Choices
from apps.mtm.models import RecordForm, RecordFormField
from apps.system.models import 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(CommonAModel):
    """
    检验记录
    """
    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)
    is_testok_robot = 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)
    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)
    remark = models.TextField('备注', default='')


class TestRecordItem(BaseModel):
    """
    记录表格字段值
    """
    form_field = models.ForeignKey(RecordFormField, verbose_name='关联字段', on_delete=models.CASCADE, db_constraint=False)
    field_name = models.CharField('字段名', max_length=50)
    field_key = models.CharField('字段标识', max_length=50)
    field_type = models.CharField('字段类型', choices=RecordForm.type_choices, max_length=50)
    field_value = models.JSONField('录入值', default=dict, blank=True)
    need_judge = models.BooleanField('是否需要判定', default=False)
    sort = models.IntegerField('排序号', default=1)
    is_testok = models.BooleanField('是否合格', null=True, blank=True)
    is_testok_robot = models.BooleanField('自动判定的是否合格', null=True, blank=True)
    test_record = models.ForeignKey(TestRecord, verbose_name='关联的检测记录', on_delete=models.CASCADE, related_name='item_test_record')