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):
    """
    资料清单
    """
    name = models.CharField('名称', max_length=100)
    desc = models.TextField('详情', null=True, blank=True)
    type = models.ForeignKey(Dict, verbose_name='材料类型', on_delete= models.DO_NOTHING)
    can_doself = models.BooleanField('可随时主动报送', default=False)
    sortnum = models.IntegerField('排序号', 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)
    up_rate = models.IntegerField('上报率', default=0)
    confirm_rate = models.IntegerField('确认率', default=0)
    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)
    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="关联文件")
    class Meta:
        verbose_name = '上报记录'
        verbose_name_plural = verbose_name
    