
from apps.system.models import City, Dict, Province
from django.db import models
from utils.model import BaseModel
from apps.system.models import CommonAModel, CommonBModel, File, Organization, User
# Create your models here.
from django.contrib.postgres.fields import JSONField

class Ability(BaseModel):
    dlxh = models.TextField('大类序号', null=True, blank=True)
    dlmc = models.TextField('大类', null=True, blank=True)
    lbxh = models.TextField('类别序号', null=True,blank=True)
    lbmc = models.TextField('类别名称', null=True,blank=True)
    xmxh = models.TextField('项目序号', null=True,blank=True)
    xmmc = models.TextField('项目名称', null=True,blank=True)
    bzmc = models.TextField('标准名称', null=True,blank=True)
    bzbh = models.TextField('标准编号', null=True,blank=True)
    bztk = models.TextField('标准条款', null=True,blank=True)
    xzfw = models.TextField('限制范围',null=True,blank=True)
    bz = models.TextField('备注',null=True,blank=True)
    cma = models.TextField('CMA中心',null=True,blank=True)
    cma_ok = models.BooleanField(null=True, blank=True)
    cnas_ok = models.BooleanField(null=True, blank=True)
    cnas = models.TextField('CNAS中心',null=True,blank=True)
    cma_o = models.TextField('中心',null=True,blank=True)
    cma_oplace = models.TextField('地点',null=True,blank=True)
    class Meta:
        verbose_name='记录合并'



class QueryRecord(BaseModel):
    user = models.ForeignKey(User, related_name='record_user', on_delete=models.CASCADE)
    path = models.CharField('访问地址', max_length=200)
    ip = models.CharField('IP地址', max_length=200, null=True, blank=True)
    method = models.CharField('方法', max_length=100, default='GET')
    query = JSONField(null=True,blank=True)
    search = models.TextField('搜索字符', null=True, blank=True)

    class Meta:
        verbose_name = '查询记录'
        verbose_name_plural = verbose_name

class Correct(BaseModel):
    dlxh = models.TextField('大类序号', null=True,blank=True)
    dlmc = models.TextField('大类', null=True,blank=True)
    lbxh = models.TextField('类别序号', null=True,blank=True)
    lbmc = models.TextField('类别名称', null=True,blank=True)
    bclxh = models.TextField('被测量序号', null=True,blank=True)
    bclmc = models.TextField('被测量名称', null=True,blank=True)
    jzgc = models.TextField('校准规程', null=True, blank=True)
    clfw = models.TextField('测量范围', null=True, blank=True)
    zqddj = models.TextField('准确度等级', null=True, blank=True)
    note = models.TextField('说明', null=True, blank=True)
    ssgs = models.TextField('所属公司', null=True, blank=True)
    ssbm = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True, related_name='correct_ssbm')
    
    class Meta:
        verbose_name='校验能力'
        verbose_name_plural=verbose_name

class CMA(BaseModel):
    """
    CMA检测能力表
    """
    type_choices = (
        ('center', '总部'),
        ('sub', '分子公司')
    )
    dlxh = models.TextField('大类序号', null=True,blank=True)
    dlmc = models.TextField('大类', null=True,blank=True)
    lbxh = models.TextField('类别序号', null=True,blank=True)
    lbmc = models.TextField('类别名称', null=True,blank=True)
    xmxh = models.TextField('项目序号', null=True,blank=True)
    xmmc = models.TextField('项目名称', null=True,blank=True)
    bzmc = models.TextField('标准名称', null=True,blank=True)
    bzbh = models.TextField('标准编号', null=True,blank=True)
    xzfw = models.TextField('限制范围',null=True,blank=True)
    bz = models.TextField('备注',null=True,blank=True)
    sszx = models.TextField('所属中心',null=True,blank=True)
    type = models.CharField('所属类型', max_length=50,
                             choices=type_choices, default='center')
    glzz = models.TextField('关联资质', null=True, blank=True)

class Inspection(BaseModel):
    """
    检验能力表
    """
    
    dlxh = models.TextField('大类序号', null=True,blank=True)
    dlmc = models.TextField('大类名称', null=True,blank=True)
    dxxh = models.TextField('对象序号', null=True,blank=True)
    jydx = models.TextField('检验对象', null=True,blank=True)
    jyxmxh = models.TextField('检验项目序号', null=True,blank=True)
    jyxmmc = models.TextField('检验项目名称', null=True,blank=True)
    jybz = models.TextField('检验标准', null=True,blank=True)
    sm = models.TextField('说明', null=True,blank=True)
    sxrq = models.TextField('生效日期', null=True,blank=True)
    sszx = models.TextField('所属中心',null=True,blank=True)

class CNAS(BaseModel):
    """
    CNAS检测能力表
    """
    lbmc = models.TextField('类别名称', null=True,blank=True)
    xmmc = models.TextField('项目名称', null=True,blank=True)
    bzmc = models.TextField('标准名称', null=True,blank=True)
    bzbh = models.TextField('标准编号', null=True,blank=True)
    bztk = models.TextField('标准条款', null=True,blank=True)
    sszx = models.TextField('所属中心',null=True,blank=True)

class Qualification(BaseModel):
    sszx = models.TextField('所属中心', null=True, blank=True)
    ssbm = models.ForeignKey(Organization, on_delete=models.CASCADE, null=True, blank=True, related_name='qualification_ssbm')
    cma = models.TextField('cma资质', null=True, blank=True)
    cnas = models.TextField('cnas资质', null=True, blank=True)
    other = models.TextField('检验检测相关其它资质', null=True, blank=True)
    service = models.TextField('主要检验检测服务', null=True, blank=True)

class Qualificationother(BaseModel):
    qualification = models.ForeignKey(Qualification, on_delete=models.CASCADE, related_name='other_qualification')
    name = models.TextField('其他资质', null=True, blank=True)
    description = models.TextField('资质范围', null=True, blank=True)

class QualiLib(BaseModel):
    """
    资质库
    """
    name = models.CharField('名称', max_length=20)
    levels = JSONField('等级', default=list, null=True, blank=True)
    scopes = JSONField('范围', default=list, null=True, blank=True)


class Quali(CommonAModel):
    """
    资质
    """
    QUALI_TYPE_CHOICES=(
        ('CMA', 'CMA'),
        ('CNAS', 'CNAS'),
        ('OTHER', 'OTHER')
    )
    QUALI_GRADE_CHOICES = (
        (10, '国家级'),
        (20, '省级'),
        (30, '市级'),
        (40, '实验室'),
        (50, '检验机构')
    )
    org = models.ForeignKey(Organization, on_delete=models.CASCADE, verbose_name='所属单位')
    name = models.CharField('资质名称', max_length=20, null=True, blank=True)
    type = models.CharField('资质类型', choices=QUALI_TYPE_CHOICES, max_length=10)
    grade = models.PositiveSmallIntegerField('等级1', null=True, blank=True, choices=QUALI_GRADE_CHOICES)
    scope = models.TextField('范围', null=True, blank=True)
    level = models.CharField('等级2', max_length=10, null=True, blank=True)
    province = models.ForeignKey(Province, on_delete=models.SET_NULL, null=True, blank=True)
    city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True, blank=True, related_name='quali_city')
    citys = models.ManyToManyField(City, verbose_name='备案城市', related_name='quali_citys', blank=True)
    description = models.TextField('描述', null=True, blank=True)
    qualilib = models.ForeignKey(QualiLib, verbose_name='关联资质库',
                                 null=True, blank=True, on_delete=models.SET_NULL)
    end_date = models.DateField('截至日期', null=True, blank=True)
    file = models.ForeignKey(File, null=True, blank=True, verbose_name='关联文件', on_delete=models.CASCADE)
    


class QTask(CommonBModel):
    """
    资质报送任务
    """
    state_choices = (
        ('待发布', '待发布'),
        ('进行中', '进行中'),
        ('已关闭', '已关闭'),
    )
    name = models.CharField('名称', max_length=100)
    end_date = models.DateField('截止时间', null=True, blank=True)
    state = models.CharField('任务状态', max_length=50, choices=state_choices, default='待发布')
    orgs = models.ManyToManyField(Organization, through='ability.qorg')
    class Meta:
        verbose_name = '资质报送任务'
        verbose_name_plural = verbose_name


class QOrg(BaseModel):
    qtask = models.ForeignKey(QTask, verbose_name='关联任务', on_delete=models.CASCADE, related_name='qorg_qtask')
    org = models.ForeignKey(Organization, verbose_name='关联公司', on_delete=models.CASCADE, related_name='qorg_org')
    count = models.PositiveIntegerField(default=0)
    count_confirmed = models.PositiveIntegerField(default=0)
    class Meta:
        verbose_name = '资质报送任务部门'
        verbose_name_plural = verbose_name


class QAction(CommonBModel):
    """
    任务操作
    """
    QACTION_CHOICE = (
        ('service:update', '更新服务'),
        ('quali:create', '新增资质'),
        ('quali:update', '更新资质'),
        ('ablity:create', '新增能力'),
        ('ablity:nochange', '能力无变化'),
        ('quali:nochange', '资质无变化')
    )
    action = models.CharField('操作类型', max_length=20, choices=QACTION_CHOICE)
    quali = models.ForeignKey(Quali, null=True, blank=True, verbose_name='操作资质', on_delete=models.CASCADE)
    file = models.ForeignKey(File, null=True, blank=True, verbose_name='能力文件', on_delete=models.CASCADE)
    atype = models.ForeignKey(Dict, null=True, blank=True, verbose_name='能力类型',
                              on_delete=models.CASCADE, related_name='quali_atype')
    afield = models.ForeignKey(Dict, null=True, blank=True, verbose_name='所属领域',
                               on_delete=models.CASCADE, related_name='quali_afield')
    num = models.PositiveIntegerField('新增对象数量', null=True, blank=True)
    num2 = models.PositiveIntegerField('新增参数数量', null=True, blank=True)
    num3 = models.PositiveIntegerField('新增方法标准数量', null=True, blank=True)
    num4 = models.PositiveIntegerField('新增产品标准数量', null=True, blank=True)
    qtask = models.ForeignKey(QTask, on_delete=models.CASCADE)
    value1 = JSONField('值1', null=True, blank=True, default=dict)
    value2 = JSONField('值2', null=True, blank=True, default=dict)
    confirmed = models.BooleanField('是否确认',default=False)


class QActionItem(BaseModel):
    QACTIONITEM_CHOICE = (
        ('update', '更新'),
        ('citys:add', '新增备案城市'),
        ('citys:remove', '移除备案城市'),
    )
    action = models.CharField('操作类型', max_length=20, choices=QACTIONITEM_CHOICE)
    field = models.CharField('变动字段', max_length=20)
    value1 = JSONField('原值', null=True, blank=True, default=dict)
    value2 = JSONField('新值', null=True, blank=True, default=dict)
    city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True, blank=True)
    qaction = models.ForeignKey(QAction, verbose_name='关联操作',
                                on_delete=models.CASCADE, related_name='items_qaction', null=True, blank=True)


