from django.db import models
from apps.system.models import CommonAModel, Dict
# Create your models here.

class Standard(CommonAModel):
    status_choices = (
        ('现行', '现行'),
        ('即将实施', '即将实施'),
        ('作废', '作废'),
        ('废止', '废止')
    )
    name = models.CharField('标准名称', max_length=1000)
    code = models.CharField('编号', max_length=30, null=True, blank=True)
    publish_date = models.DateField('发布日期')
    implement_date = models.DateField('实施日期')
    status = models.CharField('状态', max_length=50,
                             choices=status_choices, default='状态')
    path = models.CharField('文件地址', max_length=1000, null=True, blank=True)

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

    def __str__(self):
        return self.name


class ImplementRule(CommonAModel):

    name = models.CharField('规则名称', max_length=1000)
    code = models.CharField('编号', max_length=30, unique=True)
    cert_field = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='所属认证领域', related_name= 'implementrule_cert_field')
    standards = models.ManyToManyField(Standard, blank=True, verbose_name='依据标准', related_name='implementrules_standards')
    ccc_list = models.ManyToManyField(Dict, blank=True, verbose_name='CCC产品认证目录', related_name= 'implementrule_ccc_list')
    pv_scope = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='自愿性产品认证范围', related_name= 'implementrule_pv_scope')
    pv_class = models.ForeignKey(Dict, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='自愿性产品认证产品分类', related_name= 'implementrule_pv_class')
    cnas_scope = models.ManyToManyField(Dict, blank=True, verbose_name='CNAS业务范围', related_name= 'implementrule_cnas_scope')
    economy_class = models.ManyToManyField(Dict, blank=True, verbose_name='国民经济分类', related_name= 'implementrule_economy_class')
    cert_pattern = models.ManyToManyField(Dict, blank=True, verbose_name='认证模式', related_name= 'implementrule_cert_pattern')
    path = models.CharField('文件地址', max_length=1000, null=True, blank=True)

    class Meta:
        verbose_name = '实施规则'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class UnitType(CommonAModel):
    name = models.CharField('单元类型名', max_length=1000, unique=True)
    standard = models.ForeignKey(Standard, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='采用标准',  related_name='unittype_standard')
    implementrule = models.ForeignKey(ImplementRule, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='所属规则',  related_name='unittype_implementrule')
    description = models.TextField('描述', null=True, blank=True)
    
    class Meta:
        verbose_name = '单元类型'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

