from django.db import models
from apps.system.models import CommonAModel, CommonADModel, User
from apps.utils.models import CommonBDModel, BaseModel
from apps.mtm.models import Material, Mgroup, Team
from apps.wpm.models import SfLog


class TestItem(CommonAModel):
    """
    检验项目
    """
    name = models.CharField('名称', max_length=100)
    number = models.CharField('编号', max_length=20, default='', blank=True)
    tags = models.JSONField('标签', default=list, blank=True)
    sort = models.PositiveSmallIntegerField('排序', default=1)
    description = models.TextField('描述', default='')

    class Meta:
        ordering = ['sort', '-create_time']

# Create your models here.


class QuaStat(CommonBDModel):
    """
    质量数据表
    """
    material = models.ForeignKey(
        Material, verbose_name='关联产物', on_delete=models.CASCADE)
    sflog = models.ForeignKey(
        SfLog, verbose_name='关联值班记录', on_delete=models.CASCADE, null=True, blank=True)
    testitem = models.ForeignKey(
        TestItem, verbose_name='质检项目', on_delete=models.CASCADE)
    val_avg = models.FloatField('平均值', null=True, blank=True)
    num_test = models.PositiveSmallIntegerField('检测次数', null=True, blank=True)
    num_ok = models.PositiveSmallIntegerField('合格次数', null=True, blank=True)
    rate_pass = models.FloatField('合格率', null=True, blank=True)


class FtestWork(CommonBDModel):
    """
    检验工作
    """
    test_date = models.DateField('检验日期')
    material = models.ForeignKey(
        Material, verbose_name='产品', on_delete=models.CASCADE)
    batch = models.CharField('生产批次', max_length=20)
    count = models.IntegerField('总数量')
    count_sampling = models.IntegerField('抽检数量')


class Ftest(CommonBDModel):
    """
    检验记录
    """
    FTEST_TYPE_CHOICES = (
        ('first', '首件检验'),
        ('prod', '成品检验')
    )
    type = models.CharField('检验类型', max_length=20, choices=FTEST_TYPE_CHOICES)
    test_date = models.DateField('检验日期')
    test_numer = models.CharField('检测编号', max_length=20, default='')
    test_group = models.CharField('检验工序集', max_length=20, default='')
    test_user = models.ForeignKey(
        User, verbose_name='操作人', on_delete=models.CASCADE, related_name='ftest_test_user')
    check_user = models.ForeignKey(
        User, verbose_name='专检人', on_delete=models.CASCADE, related_name='ftest_check_user', null=True, blank=True)
    is_ok = models.BooleanField('是否合格', default=False)
    note = models.TextField('备注', default='', blank=True)
    ftest_work = models.ForeignKey(
        FtestWork, verbose_name='关联检验工作', on_delete=models.CASCADE, null=True, blank=True)

    @property
    def ftestitems(self):
        return FtestItem.objects.filter(ftest=self)


class FtestItem(BaseModel):
    """
    检测明细
    """
    ftest = models.ForeignKey(
        Ftest, verbose_name='关联检验', on_delete=models.CASCADE)
    testitem = models.ForeignKey(
        TestItem, verbose_name='质检项目', on_delete=models.CASCADE)
    test_val = models.FloatField('测量值', default=0, blank=True)
    check_val = models.FloatField('专检测量值', null=True, blank=True)


class Ptest(CommonAModel):
    """
    性能测试记录
    """
    PTEST_EQUIP_STATES = (
        ('R', '正常'),
        ('T', '异常')
    )
    PTEST_XJ_VALS = (
        ('S', '析晶'),
        ('R', '不析晶'),
        ('θ', '未化')
    )
    test_date = models.DateField('实验日期')
    test_user = models.ForeignKey(
        User, verbose_name='检验员', on_delete=models.CASCADE)
    testitem = models.ForeignKey(
        TestItem, verbose_name='测试项目', on_delete=models.CASCADE)
    sample_number = models.CharField('样品编号', max_length=20)
    sample_count = models.PositiveIntegerField('样品数量', null=True, blank=True)
    sample_density = models.FloatField('样品密度', null=True, blank=True)
    specification = models.CharField(
        '型号规格', max_length=20, null=True, blank=True)
    equip_state = models.CharField(
        '仪器状态', max_length=10, default='R', choices=PTEST_EQUIP_STATES, help_text=list(PTEST_EQUIP_STATES))
    note = models.TextField('备注', default='', blank=True)

    val_hd = models.FloatField('厚度', help_text='mm', null=True, blank=True)
    val_zsl = models.FloatField(
        '折射率', help_text='589nm', null=True, blank=True)
    val_tgl = models.FloatField(
        '透过率', help_text='550nm', null=True, blank=True)
    val_ts = models.FloatField("Ts", help_text='℃', null=True, blank=True)
    val_tg = models.FloatField("Tg", help_text='℃', null=True, blank=True)
    val_tf = models.FloatField("Tf", help_text='℃', null=True, blank=True)
    val_xj = models.CharField(
        '析晶', max_length=10, default='S', choices=PTEST_XJ_VALS, help_text=list(PTEST_XJ_VALS))
    val_pzxs = models.FloatField(
        '膨胀系数', help_text='30-300℃', null=True, blank=True)
    val_zgwd = models.FloatField('升至最高温度', null=True, blank=True)
