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

class Content(CommonAModel):
    """
    资料清单
    """
    cate_choices = (
        (1, '监督'),
        (2, '资质')
    )
    name = models.CharField('名称', max_length=100)
    desc = models.TextField('详情', null=True, blank=True)
    type = models.ForeignKey(Dict, verbose_name='材料类型', on_delete= models.DO_NOTHING, null=True, blank=True)
    can_doself = models.BooleanField('可随时主动报送', default=False)
    sortnum = models.IntegerField('排序号', default=1)
    template = models.CharField('模板地址', max_length=200, null=True, blank=True)
    cate = models.IntegerField('材料归类', choices=cate_choices, default=1)
    class Meta:
        verbose_name = '报送清单'
        verbose_name_plural = verbose_name


class Task(CommonBModel):
    """
    报送任务
    """
    state_choices = (
        ('创建中', '创建中'),
        ('待发布', '待发布'),
        ('执行中', '执行中'),
        ('已关闭', '已关闭'),
    )
    name = models.CharField('名称', max_length=100)
    end_date = models.DateField('截止时间', null=True, blank=True)
    note = models.TextField('任务备注', null=True, blank=True)
    contents = models.ManyToManyField('supervision.content', through='supervision.record')
    depts = models.ManyToManyField('system.organization', through='supervision.record')
    state = models.CharField('任务状态', max_length=50, choices=state_choices, default='创建中')
    class Meta:
        verbose_name = '报送任务'
        verbose_name_plural = verbose_name


class TaskDept(BaseModel):
    task = models.ForeignKey(Task, verbose_name='关联任务', on_delete=models.CASCADE, related_name='taskdept_task')
    dept = models.ForeignKey(Organization, verbose_name='关联公司', on_delete=models.CASCADE, related_name='taskdept_dept')
    up_rate = models.IntegerField('报送率', default=0)
    confirm_rate = models.IntegerField('确认率', default=0)
  
    file = models.CharField('反馈文件', max_length=200, null=True, blank=True)
    is_yes = models.BooleanField('是否反馈', default=False)
    class Meta:
        verbose_name = '任务部门视图'
        verbose_name_plural = verbose_name


class Record(CommonBModel):
    """
    报送记录
    """
    state_choices = (
        ('待发布', '待发布'),
        ('待报送', '待报送'),
        ('已报送', '已报送'),
        ('已确认', '已确认'),
        ('待整改', '待整改')
    )
    content = models.ForeignKey(Content, verbose_name='材料内容ID', on_delete=models.DO_NOTHING, related_name='record_content')
    content_name = models.CharField('材料名称', max_length=100)
    content_desc = models.TextField('材料详情', null=True, blank=True)
    task = models.ForeignKey(Task, verbose_name='关联任务', null=True, blank=True, on_delete=models.SET_NULL, related_name='record_task')
    is_self = models.BooleanField('是否主动报送', default=False)
    up_date = models.DateField('报送时间', null=True, blank=True)
    up_user = models.ForeignKey(User, verbose_name='报送人', null=True, blank=True,on_delete=models.SET_NULL, related_name='record_up_user')
    end_date = models.DateField('截止时间', null=True, blank=True)
    state = models.CharField('记录状态', max_length=50, choices=state_choices, default='待报送')
    is_yes = models.BooleanField('是否适用', default=True)
    note = models.TextField('报送要求', null=True, blank=True)
    opinion = models.TextField('修改意见', null=True, blank=True)
    noteb = models.TextField('报送说明', null=True, blank=True)
    files = models.ManyToManyField(File, verbose_name="关联文件")
    dept_yes = models.BooleanField('二级单位是否确认', default=True)
    class Meta:
        verbose_name = '报送记录'
        verbose_name_plural = verbose_name


class TestReport(CommonBModel):
    """检测报告
    """
    accept_number = models.CharField('受理编号', max_length=20)
    report_number = models.CharField('报告编号', max_length=20, null=True, blank=True)
    description = models.TextField('业务描述', null=True, blank=True)
    expect_date = models.DateField('预计发放时间')
    issue_date = models.DateField('实际发放时间', null=True, blank=True)


class TAction(CommonAModel):
    """检测报告变更记录
    """
    TACTION_TYPE = (
        (10, '正常发放'),
        (20, '报告出错'),
        (30, '报告迟单')
    )
    testreport = models.ForeignKey(TestReport, on_delete=models.CASCADE, verbose_name='关联检测报告', null=True, blank=True)
    type = models.PositiveSmallIntegerField('变更类型', default=10)
    reason_1 = models.ForeignKey(Dict, verbose_name='变更主要原因', related_name='taction_r1', on_delete= models.SET_NULL, null=True, blank=True)
    reason_2 = models.ForeignKey(Dict, verbose_name='变更次要原因', related_name='taction_r2', on_delete= models.SET_NULL, null=True, blank=True)
    value_old = JSONField('原值', null=True, blank=True, default=dict)
    value_new = JSONField('新值', null=True, blank=True, default=dict)

class Pgoal(CommonADModel):
    """
    基础目标
    """
    basedict = {
        "pgoal_1": "重大质量事故",
        "pgoal_2": "报告/证书合格率",
        "pgoal_3": "报告/证书及时率",
        "pgoal_4": "能力验证满意率",
        "pgoal_5": "客户投诉处理满意率"
    }
    year = models.PositiveSmallIntegerField('年份', unique=True)
    pgoal_1 = models.FloatField('重大质量事故-基础值')
    pgoal_2 = models.FloatField('报告/证书合格率-基础值')
    pgoal_3 = models.FloatField('报告/证书及时率-基础值')
    pgoal_4 = models.FloatField('能力验证满意率-基础值')
    pgoal_5 = models.FloatField('客户投诉处理满意率-基础值')
    pgoal_1_file = models.ForeignKey(File, related_name='pgoal_1_file', on_delete=models.SET_NULL, null=True, blank=True)
    pgoal_2_file = models.ForeignKey(File, related_name='pgoal_2_file', on_delete=models.SET_NULL, null=True, blank=True)
    pgoal_3_file = models.ForeignKey(File, related_name='pgoal_3_file', on_delete=models.SET_NULL, null=True, blank=True)
    pgoal_4_file = models.ForeignKey(File, related_name='pgoal_4_file', on_delete=models.SET_NULL, null=True, blank=True)
    pgoal_5_file = models.ForeignKey(File, related_name='pgoal_5_file', on_delete=models.SET_NULL, null=True, blank=True)

class Task2(CommonADModel):
    """
    报送任务2
    """
    TASK2_CREATE = 10
    TASK2_DOING = 20
    TASK2_CLOSE = 30
    TASK2_STATES = (
        (TASK2_CREATE, '创建中'),
        (TASK2_DOING, '进行中'),
        (TASK2_CLOSE, '已关闭'),
    )
    TASK2_F = 10
    TASK2_S = 20
    TASK2_TYPES = (
        (TASK2_F, '目标制定'),
        (TASK2_S, '日常监督')
    )

    year = models.PositiveSmallIntegerField('年份', default=2023)
    cycle = models.PositiveSmallIntegerField('所属周期', default=0, help_text='1-6个周期', blank=True)
    end_date = models.DateField('截至日期', null=True, blank=True)
    type = models.PositiveSmallIntegerField('任务类型', choices=TASK2_TYPES, default=20)
    state = models.PositiveSmallIntegerField('任务状态', choices=TASK2_STATES, default=10)
    depts = models.ManyToManyField(Organization, verbose_name='执行公司', through='supervision.task2do', blank=True)


class Task2Do(CommonBDModel):
    """任务执行情况
    """
    task2 = models.ForeignKey(Task2, verbose_name='关联任务', on_delete=models.CASCADE, related_name='task2dept_task2')
    count_up = models.PositiveSmallIntegerField('完成上报内容数', default=0)
    count_all = models.PositiveSmallIntegerField('需上报内容数', default=9)

    num_issue = models.PositiveIntegerField('已发放报告数量', null=True, blank=True)
    num_expect = models.PositiveIntegerField('应发报告数', null=True, blank=True)
    num_acc = models.PositiveSmallIntegerField('重大质量事故', null=True, blank=True)

    num_error = models.PositiveSmallIntegerField('不准确报告数', null=True, blank=True)
    num_overdue = models.PositiveSmallIntegerField('超期报告数', null=True, blank=True)
    num_complaint = models.PositiveSmallIntegerField('客户投诉数', null=True, blank=True)
    num_complaint_10 = models.PositiveSmallIntegerField('满意客户投诉数', null=True, blank=True)
    num_pt = models.PositiveSmallIntegerField('能力验证数', null=True, blank=True)
    num_pt_10 = models.PositiveSmallIntegerField('满意能力验证数', null=True, blank=True)
    num_risk = models.PositiveSmallIntegerField('风险识别数', null=True, blank=True)
    num_oinspect = models.PositiveSmallIntegerField('外部检查数', null=True, blank=True)


class PgoalDept(CommonBDModel):
    """企业年度目标值
    """
    year = models.PositiveSmallIntegerField('年份')
    goal_name = models.CharField('目标名称', max_length=100)
    goal_key = models.CharField('目标key值', max_length=100, default='other', blank=True)
    goal_value_a = models.FloatField('基础值', null=True, blank=True)
    goal_value_b = models.FloatField('设定值', null=True, blank=True)
    goal_value_c = models.FloatField('实际值', null=True, blank=True)
    goal_file = models.ForeignKey(File, related_name='goal_file', verbose_name='当前文件ID', on_delete=models.SET_NULL, null=True, blank=True)
    goal_files = models.ManyToManyField(File, related_name='goal_files', verbose_name='历史文件', blank=True)
    task2do = models.ForeignKey('supervision.task2do', verbose_name= '关联任务执行', on_delete=models.SET_NULL, null=True, blank=True)


class Rc(CommonBDModel):
    """报告或证书
    """
    etype_choices = (
        (10, '出错'),
        (20, '超期')
    )
    number = models.CharField('报告证书编号', max_length=100)
    etype = models.PositiveSmallIntegerField('异常情况', default=0, choices=etype_choices)
    number_correct = models.CharField('更改后报告证书编号', max_length=100, default='', blank=True)
    reason_error = models.CharField('出错原因', max_length=100, default='', blank=True)
    date_accept = models.DateField('业务受理日期', null=True, blank=True)
    date_expect = models.DateField('预计发放日期', null=True, blank=True)
    date_issue = models.DateField('实际发放日期', null=True, blank=True)
    task2do = models.ForeignKey('supervision.task2do', verbose_name= '关联任务执行', on_delete=models.SET_NULL, null=True, blank=True)
    remark = models.TextField('备注', null=True, blank=True)


class Complaint(CommonBDModel):
    """客户投诉
    """
    complainant = models.CharField('投诉人', max_length=100)
    content = models.TextField('投诉内容')
    date_occurrence = models.DateField('投诉日期')
    date_feedback = models.DateField('反馈日期')
    is_satisfied = models.BooleanField('是否满意', default=True)
    task2do = models.ForeignKey('supervision.task2do', verbose_name= '关联任务执行', on_delete=models.SET_NULL, null=True, blank=True)


class Pt(CommonBDModel):
    """能力验证
    """
    pt_result_choices = (
        (10, '满意'),
        (20, '有问题'),
        (30, '不满意')
    )
    pt_hresult_choices = (
        (0, '/'),
        (10, '已整改'),
        (20, '被暂停'),
        (30, '已被撤销认可资格')
    )
    name = models.CharField('名称', max_length=100)
    number = models.CharField('编号', max_length=100)
    organizer = models.CharField('组织方', max_length=100)
    params = models.TextField('参加参数名称')
    result = models.PositiveSmallIntegerField('结果', choices=pt_result_choices, default=10, help_text=str(pt_result_choices))
    handle_result = models.PositiveSmallIntegerField('处理结果', choices = pt_hresult_choices, default=0, help_text=str(pt_hresult_choices))
    task2do = models.ForeignKey('supervision.task2do', verbose_name= '关联任务执行', on_delete=models.SET_NULL, null=True, blank=True)


class Risk(CommonBDModel):
    """风险识别
    """
    risk_level_choices = (
        (10, '主要风险'),
        (20, '严重风险'),
        (30, '重大风险')
    )
    risk_state_choices = (
        (10, '未整改'),
        (20, '整改中'),
        (30, '整改完成')
    )
    content = models.TextField('内容')
    level = models.PositiveSmallIntegerField('风险等级', choices=risk_level_choices, default=10, help_text=str(risk_level_choices))
    code = models.CharField('风险代码', max_length=100)
    state = models.PositiveSmallIntegerField('整改状态', choices=risk_state_choices, default=10, help_text=str(risk_state_choices))
    task2do = models.ForeignKey('supervision.task2do', verbose_name= '关联任务执行', on_delete=models.SET_NULL, null=True, blank=True)


class Oinspect(CommonBDModel):
    """外部检查
    """
    oinspect_cate_choices = (
        (10, '市场监管部门检查'),
        (20, '行业主管部门检查'),
        (30, '其他检查')
    )
    cate = models.PositiveSmallIntegerField('检查分类', choices=oinspect_cate_choices, default=30, help_text=str(oinspect_cate_choices))
    checker = models.CharField('检查方', max_length=100)
    date_inspect = models.DateField('检查日期')
    result = models.TextField('检查结果')
    task2do = models.ForeignKey('supervision.task2do', verbose_name= '关联任务执行', on_delete=models.SET_NULL, null=True, blank=True)