from django.db import models
from apps.utils.models import BaseModel, CommonAModel, CommonADModel
from django_celery_beat.models import PeriodicTask

# Create your models here.
class RiskPoint(CommonAModel):
    """
    TN: 风险点表
    """
    R_LEVEL = (
        (10, '低风险'),
        (20, '一般风险'),
        (30, '较大风险'),
        (40, '重大风险')
    )
    R_TYPE = (
        (10, '作业活动类'),
        (20, '设备设施类')
    )
    R_M_LEVEL = (
        (10, '岗位级'),
        (20, '班组级'),
        (30, '部门级'),
        (40, '公司级')
    )
    name = models.CharField('名称', max_length=40)
    area = models.ForeignKey('am.area', on_delete=models.CASCADE, verbose_name='所在区域', related_name='riskpoint_area', null=True, blank=True)
    place = models.CharField('具体地点', max_length=100, null=True, blank=True)
    level = models.PositiveSmallIntegerField('风险点等级', default=10, choices=R_LEVEL)
    type = models.PositiveSmallIntegerField('风险点类型', default=10, choices=R_TYPE)
    manage_level = models.PositiveSmallIntegerField('管控层级', default=10, choices=R_M_LEVEL)
    dept_res = models.ForeignKey('system.dept', on_delete=models.SET_NULL, verbose_name='责任部门', related_name='riskpoint_dept_res', null=True, blank=True)
    user_res = models.ForeignKey('system.user', on_delete=models.CASCADE, verbose_name='责任人', related_name='riskpoint_user_res', null=True, blank=True)

class Risk(CommonAModel):
    """
    TN: 风险表
    """
    name = models.TextField('项目/步骤')
    level = models.PositiveSmallIntegerField('风险等级', default=10, choices=RiskPoint.R_LEVEL)
    standard = models.TextField('检查标准', null=True, blank=True)
    hazard_factors = models.TextField('危害因素', null=True, blank=True)
    results = models.ManyToManyField('system.dictionary', verbose_name='可能后果', related_name='risk_results', blank=True)
    sort = models.PositiveSmallIntegerField('排序', default=1)
    measure_tech = models.TextField('工程技术措施', null=True, blank=True)
    measure_manage = models.TextField('管理措施', null=True, blank=True)
    measure_edu = models.TextField('培训教育措施', null=True, blank=True)
    measure_protect = models.TextField('个体防护措施', null=True, blank=True)
    measure_emer = models.TextField('应急处置措施', null=True, blank=True)
    riskl = models.DecimalField(max_digits=6, decimal_places=1, null=True, blank=True)
    riske = models.DecimalField(max_digits=6, decimal_places=1, null=True, blank=True)
    riskc = models.DecimalField(max_digits=6, decimal_places=1, null=True, blank=True)
    riskd = models.DecimalField(max_digits=6, decimal_places=1, null=True, blank=True)
    riskpoint = models.ForeignKey(RiskPoint, on_delete=models.CASCADE, verbose_name='所属风险点')


class CheckTaskSet(CommonADModel):
    """
    TN:检查任务派发设置
    """
    riskpoint = models.ForeignKey(RiskPoint, verbose_name='关联风险点', related_name='ctask_riskpoint', on_delete=models.SET_NULL, null=True, blank=True)
    note = models.TextField('派发备注', null=True, blank=True)
    expire = models.PositiveSmallIntegerField('执行有效期(h)', default=0, help_text='0代表不设限制直到下个任务派发')
    user_duty = models.ForeignKey('system.user', verbose_name='指派人', on_delete=models.SET_NULL, null=True, blank=True)
    myschedule = models.ForeignKey('system.myschedule', verbose_name='周期', on_delete=models.SET_NULL, null=True, blank=True)
    periodictask = models.ForeignKey(PeriodicTask, verbose_name='关联定时任务', on_delete=models.CASCADE, related_name='checktask_periodictask')


class CheckWork(CommonAModel):
    """
    TN: 检查工作
    """
    CW_TYPE = (
        (10, '手动'),
        (20, '派发')
    )
    name = models.CharField('名称', max_length=40)
    description = models.TextField('工作描述', null=True, blank=True)
    type = models.PositiveSmallIntegerField('任务类型', default=10, choices=CW_TYPE)
    category = models.ForeignKey('system.dictionary', on_delete=models.CASCADE, verbose_name='工作分类', null=True, blank=True)
    usable = models.BooleanField('是否可用', default=True)
    time_start = models.DateTimeField('开启时间')
    time_end = models.DateTimeField('关闭时间', null=True, blank=True)
    user_duty = models.ForeignKey('system.user', verbose_name='指派人', on_delete=models.SET_NULL, null=True, blank=True)
    riskpoint = models.ForeignKey(RiskPoint, verbose_name='关联风险点', related_name='cwork_riskpoint', on_delete=models.SET_NULL, null=True, blank=True)
    note = models.TextField('检查备注', null=True, blank=True)
    time_check = models.DateTimeField('开始检查时间', null=True, blank=True)
    checktaskset = models.ForeignKey(CheckTaskSet, verbose_name='关联派发策略', on_delete=models.CASCADE, null=True, blank=True)


class Hazard(CommonADModel):
    """
    TN: 事故隐患表
    """
    H_HARM = (
        (10, '无伤害'),
        (20, '轻微伤'),
        (30, '轻伤'),
        (40, '重伤'),
        (50, '死亡')
    )
    H_LEVEL = (
        (10, '无隐患'),
        (20, '一般隐患'),
        (30, '重大隐患')
    )
    H_STATE = (
        (10, '待提交'),
        (20, '流程中'),
        (30, '已结束')
    )
    number = models.CharField('编号', max_length=20, null=True, blank=True)
    checkwork = models.ForeignKey(CheckWork, on_delete=models.SET_NULL, verbose_name='关联检查工作', null=True, blank=True)
    state = models.PositiveSmallIntegerField('隐患流程状态', default=10, choices=H_STATE)
    time_discover = models.DateTimeField('发现时间')
    dept_res = models.ForeignKey('system.dept', on_delete=models.SET_NULL, verbose_name='责任部门', related_name='hazard_dept_res', null=True, blank=True)
    user_discover = models.ForeignKey('system.user', on_delete=models.CASCADE, verbose_name='发现人', related_name='hazard_user_discover')
    area = models.ForeignKey('am.area', on_delete=models.CASCADE, verbose_name='所在区域', null=True, blank=True)
    place = models.CharField('具体地点', max_length=100, null=True, blank=True)
    level = models.PositiveSmallIntegerField('隐患等级', default=10, choices=H_LEVEL)
    harm_access = models.PositiveSmallIntegerField('伤害评估', default=10, choices=H_HARM)
    category = models.ForeignKey('system.dictionary', on_delete=models.CASCADE, verbose_name='隐患类别', null=True, blank=True)
    description = models.TextField('隐患描述', null=True, blank=True)
    reason = models.TextField('原因分析', null=True, blank=True)
    correct_measure = models.TextField('整改措施', null=True, blank=True)
    correct_solution = models.ForeignKey('system.file', verbose_name='整改方案', on_delete=models.SET_NULL, null=True, blank=True)
    correct_deadline = models.DateTimeField('整改期限', null=True, blank=True)
    photos = models.ManyToManyField('system.file', verbose_name='现场照片', related_name='hazard_photos', blank=True)
    correct_description = models.TextField('整改描述', null=True, blank=True)
    photos_done = models.ManyToManyField('system.file', verbose_name='整改后照片', related_name='hazard_photos_done', blank=True)
    dept_correct = models.ForeignKey('system.dept', on_delete=models.CASCADE, verbose_name='整改部门', related_name='hazard_dept_correct', null=True, blank=True)
    user_correct = models.ForeignKey('system.user', on_delete=models.SET_NULL, verbose_name='整改责任人', related_name='hazard_user_correct', null=True, blank=True)
    correct_time = models.DateTimeField('整改完成时间', null=True, blank=True)
    # user_access = models.ForeignKey('system.user', on_delete=models.CASCADE, verbose_name='评估人', related_name='hazard_user_access')
    # opinion_access = models.TextField('评估意见', null=True, blank=True)
    # user_check = models.ForeignKey('system.user', on_delete=models.CASCADE, verbose_name='复查人', related_name='hazard_user_check')
    # opinion_check = models.TextField('复查意见', null=True, blank=True)
    # user_audit = models.ForeignKey('system.user', on_delete=models.CASCADE, verbose_name='审核人', related_name='hazard_user_audit')
    # opinion_audit = models.TextField('审核意见', null=True, blank=True)
    ticket = models.ForeignKey('wf.ticket', on_delete=models.SET_NULL, verbose_name='关联工单', related_name='hazard_ticket', null=True, blank=True)


class CheckItem(BaseModel):
    """
    TN:检查工作-隐患关联表
    """
    CITEM_RESULT = (
        (10, '未检查'),
        (20, '合格'),
        (30, '不合格')
    )
    checkwork = models.ForeignKey(CheckWork, verbose_name='关联检查工作', related_name='citem_checkwork', on_delete=models.CASCADE)
    risk = models.ForeignKey(Risk, verbose_name='关联风险', related_name='citem_risk', on_delete=models.SET_NULL, null=True, blank=True)
    hazard = models.ForeignKey(Hazard, verbose_name='关联隐患', related_name='citem_hazard', on_delete=models.SET_NULL, null=True, blank=True)
    result = models.PositiveSmallIntegerField('检查结果', default=10)
    note = models.TextField('备注', null=True, blank=True)
