from django.db import models
from apps.system.models import User
from apps.third.models import TDevice
from apps.utils.models import BaseModel, CommonADModel, CommonAModel, CommonBDModel


# Create your models here.
class OplCate(CommonAModel):
    """
    TN:作业许可种类
    """
    code = models.CharField('标识', max_length=10)
    name = models.CharField('名称', max_length=20)
    sort = models.PositiveSmallIntegerField('排序', default=1)
    description = models.TextField('描述', null=True, blank=True)
    template_export = models.TextField('导出word模板', null=True, blank=True)
    risk_options = models.ManyToManyField('system.dictionary',
                                          verbose_name='风险分析',
                                          related_name='oplcate_risk_options')
    measure_options = models.ManyToManyField('system.dictionary',
                                             verbose_name='控制措施',
                                             related_name='oplcate_measure_options')
    close_options = models.ManyToManyField('system.dictionary',
                                           verbose_name='关闭工作',
                                           related_name='oplcate_close_options')


class Operation(CommonBDModel):
    """TN:作业表"""
    OP_CREATE = 10
    OP_AUDIT = 20
    OP_WAIT = 30
    OP_WORK = 40
    OP_DONE = 50
    OP_STATE_CHOICES = (
        (OP_CREATE, '创建中'),
        (OP_AUDIT, '审批中'),
        (OP_WAIT, '待作业'),
        (OP_WORK, '作业中'),
        (OP_DONE, '已结束')
    )
    number = models.CharField('作业编号', max_length=20, null=True, blank=True)
    name = models.CharField('作业简述', max_length=200)
    area = models.ForeignKey('am.area', verbose_name='作业区域',
                             on_delete=models.CASCADE)
    state = models.PositiveSmallIntegerField('作业状态', default=10)
    place = models.TextField('具体地点', null=True, blank=True)
    start_time = models.DateTimeField('作业开始时间')
    end_time = models.DateTimeField('作业结束时间')
    dept_ter = models.ForeignKey('system.dept', verbose_name='属地部门',
                                 on_delete=models.CASCADE,
                                 related_name='op_dept_ter')
    dept_bus = models.ForeignKey('system.dept', verbose_name='业务部门',
                                 on_delete=models.CASCADE,
                                 related_name='op_dept_bus')
    coordinator = models.ForeignKey('system.user', verbose_name='业务部门协调员',
                                    on_delete=models.CASCADE)
    state_work = models.CharField('生产状态', max_length=20, help_text='运行/停机/检修')
    cates = models.ManyToManyField(OplCate, through='opm.opl')
    vchannels = models.ManyToManyField(TDevice, verbose_name='监控所用摄像头', blank=True)


class Opl(CommonBDModel):
    """
    TN:作业许可证
    """
    CLOSE_CHOICES1 = (
        (10, '作业正常结束'),
        (20, '因计划改变停止作业'),
        (30, '因发生异常终止作业'),
        (40, '其他')
    )
    operation = models.ForeignKey(Operation, verbose_name='关联作业',
                                  on_delete=models.CASCADE, related_name='opl_operation')
    number = models.CharField('作业许可编号', max_length=20, null=True, blank=True)
    work_scope = models.CharField('作业范围', max_length=200, null=True, blank=True)
    level = models.CharField('作业级别', max_length=20,
                             help_text='特技/三级/二级/主干道/次干道', null=True, blank=True)
    cate = models.ForeignKey(OplCate, verbose_name='作业许可种类',
                             on_delete=models.CASCADE)
    start_time = models.DateTimeField('作业开始时间', null=True, blank=True)
    end_time = models.DateTimeField('作业结束时间', null=True, blank=True)
    dept_do = models.ForeignKey('system.dept', verbose_name='作业部门',
                                on_delete=models.CASCADE)
    charger = models.ForeignKey('system.user', verbose_name='作业负责人',
                                on_delete=models.CASCADE, related_name='opl_charger')
    monitor = models.ForeignKey('system.user', verbose_name='作业监护人',
                                on_delete=models.CASCADE, related_name='opl_monitor')
    # electrician = models.ForeignKey(User, verbose_name='电工',
    #                                 on_delete=models.CASCADE,
    #                                 related_name='opl_electrician', null=True, blank=True)
    work_time = models.DateTimeField('安装/拆除时间', null=True, blank=True)
    accept_time = models.DateTimeField('验收时间', null=True, blank=True)
    work_type = models.CharField('安装/拆除', max_length=20, null=True, blank=True)
    power = models.PositiveIntegerField('用电功率', null=True, blank=True)
    power_to = models.CharField('用电地点', max_length=20, null=True, blank=True)
    power_from = models.CharField('电源接入点', max_length=20, null=True, blank=True)
    power_end_time = models.DateTimeField('用电截至', null=True, blank=True)
    power_start_time = models.DateTimeField('用电开始', null=True, blank=True)
    power_days = models.PositiveSmallIntegerField('计划用电天数', null=True, blank=True)
    other_risk = models.TextField('其他风险', null=True, blank=True)
    other_emr = models.TextField('其他应急处置', null=True, blank=True)
    escape_route = models.TextField('逃生路径', null=True, blank=True)
    risks_checked = models.JSONField('风险选择', default=list, null=False, blank=True)
    measures_checked = models.JSONField('措施选择', default=list, null=False, blank=True)
    workers = models.ManyToManyField('system.user', verbose_name='作业人员', through='opm.oplworker')
    create_imgs = models.ManyToManyField('system.file', verbose_name='作业审批照片',
                                         related_name='opl_create_imgs', blank=True)
    audit_imgs = models.ManyToManyField('system.file', verbose_name='作业审批照片',
                                        related_name='opl_audit_imgs', blank=True)
    work_imgs = models.ManyToManyField('system.file', verbose_name='作业工作照片',
                                        related_name='opl_work_imgs', blank=True)
    close_imgs = models.ManyToManyField('system.file', verbose_name='作业关闭照片',
                                        related_name='opl_close_imgs', blank=True)
    close_note = models.PositiveSmallIntegerField('作业关闭情况',
                                                  choices=CLOSE_CHOICES1,
                                                  help_text='正常结束/计划改变停止/发生异常停止/其他',
                                                  null=True, blank=True)
    close_dos = models.JSONField('关闭工作', default=list, null=False, blank=True)
    close_desc = models.TextField('作业关闭描述', null=True, blank=True)
    ticket = models.OneToOneField('wf.ticket', verbose_name='关联工单',
                                  on_delete=models.SET_NULL,
                                  related_name='opl_ticket',
                                  null=True, blank=True)
    mtask_uid = models.CharField('监控任务ID', max_length=100, null=True, blank=True)  # 废弃字段


class OplWorker(BaseModel):
    """
    TN:作业许可人员
    """
    worker = models.ForeignKey('system.user', verbose_name='作业人员',
                               on_delete=models.CASCADE, related_name='opl_worker')
    opl = models.ForeignKey(Opl, verbose_name='作业许可',
                            on_delete=models.CASCADE)
    duty = models.CharField('工作职责', max_length=100,
                            null=True, blank=True,
                            help_text='作业人员/起重司机/司索人员/起重指挥/电工')
    # is_confirmed = models.BooleanField('是否确认', default=False)
    # confirm_time = models.DateTimeField('确认时间', null=True, blank=True)
    certificates = models.ManyToManyField('hrm.certificate', verbose_name='关联证书', through='opm.oplcert')


class OplCert(BaseModel):
    """
    TN:作业许可人员证书
    """
    opl_worker = models.ForeignKey(OplWorker, verbose_name='关联作业人员',
                                   on_delete=models.CASCADE)
    certificate = models.ForeignKey('hrm.certificate', verbose_name='关联证书',
                                    on_delete=models.CASCADE)
    name = models.CharField('证书名称', max_length=20)
    number = models.CharField('证书编号', max_length=20)
    type = models.PositiveSmallIntegerField('证书类型', default=10)
    issue_date = models.DateField('发证日期')
    expiration_date = models.DateField('有效期')
    review_date = models.DateField('下一次复审日期')
    file = models.TextField('文件地址', null=True, blank=True)


class GasCheck(CommonADModel):
    """
    TN:气体检测记录
    """
    check_time = models.DateTimeField('检测时间')
    check_place = models.CharField('检测部位', max_length=100)
    o2 = models.FloatField(null=True, blank=True)
    co = models.FloatField(null=True, blank=True)
    h2s = models.FloatField(null=True, blank=True)
    lel = models.FloatField(null=True, blank=True)
    f5 = models.CharField(max_length=10, null=True, blank=True)
    is_ok = models.BooleanField('是否正常', default=True)
    img = models.TextField('图片地址', null=True, blank=True)
    checker = models.ForeignKey(User, verbose_name='检测人', on_delete=models.CASCADE)
    opl = models.ForeignKey(Opl, verbose_name='关联作业许可', on_delete=models.CASCADE)
