from django.db import models
from utils.model import BaseModel
from apps.system.models import CommonAModel, CommonBModel, Organization, User, Dict, File
# 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
    