from django.db import models
from apps.hrm.models import Certificate, Employee
from apps.system.models import Dept, Dictionary, File, User
from apps.utils.models import CommonAModel, CommonBDModel, CommonBModel, CommonADModel, BaseModel
from apps.wf.models import Ticket


# Create your models here.
class Rparty(CommonBModel):
    """
    TN:相关方
    """
    dept = models.OneToOneField(Dept, verbose_name='关联部门',
                                on_delete=models.CASCADE,
                                null=True, blank=True)
    name = models.CharField('名称', max_length=20)
    number = models.CharField('信用代码', max_length=50, null=True, blank=True)
    lawer = models.CharField('法人', max_length=20, null=True, blank=True)
    contacter = models.CharField('联系人', max_length=20)
    phone = models.CharField('联系电话', max_length=20)
    email = models.EmailField('邮箱', null=True, blank=True)
    address = models.CharField('企业地址', max_length=200, null=True, blank=True)
    description = models.TextField('概述', null=True, blank=True)
    admin = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)


class BlackList(CommonBDModel):
    """TN:黑名单
    """
    name = models.CharField('名称', max_length=20)
    number = models.CharField('信用代码', max_length=50)
    reason = models.TextField('原因')


class Rfile(CommonBDModel):
    """
    TN:相关方文件库
    """
    file_cate = models.ForeignKey(Dictionary, verbose_name='文件种类',
                                  on_delete=models.CASCADE, null=True, blank=True)
    files = models.ManyToManyField(File, verbose_name='文件')
    rparty = models.ForeignKey(Rparty, verbose_name='关联相关方', on_delete=models.CASCADE)


class Rpj(CommonBDModel):
    """
    TN:相关方项目
    """
    RPJ_CREATE = 10
    RPJ_AUDIT = 20
    RPJ_ENTER = 30
    RPJ_WORKING = 40
    RPJ_DONE = 50
    RPJ_CLOSE = 60
    RP_STATE_CHOICES = (
        (10, '创建中'),
        (20, '审批中'),
        (30, '待入厂'),
        (40, '进行中'),
        (50, '已完成'),
        (60, '已关闭')
    )
    RP_TYPE_CHOICES = (
        (10, '建筑施工'),
        (20, '设备设施检维修'),
        (30, '保安保洁服务'),
        (40, '其他')
    )
    name = models.CharField('名称', max_length=20)
    type = models.PositiveSmallIntegerField('项目类型', default=10)
    contract_number = models.CharField('合同编号', max_length=20)
    come_time = models.DateTimeField('进厂时间')
    leave_time = models.DateTimeField('离厂时间')
    state = models.PositiveSmallIntegerField('状态', default=10)
    rparty = models.ForeignKey(Rparty, verbose_name='关联相关方', on_delete=models.CASCADE)
    remployees = models.ManyToManyField('rpm.remployee', through='rpm.rpjmember', related_name='rpj_remployees')
    # belong_dept是业务部门可以带过来
    ticket = models.ForeignKey(Ticket, verbose_name='关联工单',
                               on_delete=models.SET_NULL, related_name='rpj_ticket', null=True, blank=True)


class Rpjfile(BaseModel):
    """
    TN:相关方项目文件库
    """
    file_cate = models.ForeignKey(Dictionary, verbose_name='文件种类',
                                  on_delete=models.CASCADE)
    files = models.ManyToManyField(File, verbose_name='文件', blank=True)
    rpj = models.ForeignKey(Rpj, verbose_name='关联相关方项目', on_delete=models.CASCADE)


class Remployee(CommonBModel):
    """
    TN:相关方成员
    """
    employee = models.ForeignKey(Employee, verbose_name='成员信息',
                                 on_delete=models.SET_NULL, null=True, blank=True)
    name = models.CharField('姓名', max_length=20)
    phone = models.CharField('手机号', max_length=11)
    photo = models.CharField('证件照', max_length=1000, null=True, blank=True)
    id_number = models.CharField('身份证号', max_length=100, null=True, blank=True)
    rparty = models.ForeignKey(Rparty, verbose_name='所属相关方', on_delete=models.CASCADE)
    rpj = models.ForeignKey(Rpj, verbose_name='最近所属相关方项目', on_delete=models.SET_NULL,
                            null=True, blank=True)


class Rcertificate(CommonBModel):
    """
    TN:相关方证书
    """
    certificate = models.ForeignKey(Certificate, verbose_name='关联认可证书',
                                    on_delete=models.SET_NULL,
                                    null=True, blank=True)
    remployee = models.ForeignKey(Remployee, verbose_name='关联相关方成员',
                                  on_delete=models.CASCADE)
    name = models.CharField('证书名称', max_length=20)
    number = models.CharField('证书编号', max_length=20)
    type = models.PositiveSmallIntegerField('证书类型', default=10, choices=Certificate.CERTIFICATE_TYPE_CHOICES)
    issue_date = models.DateField('发证日期')
    expiration_date = models.DateField('有效期')
    review_date = models.DateField('下一次复审日期')
    file = models.CharField('文件地址', max_length=1000, null=True, blank=True)


class Rpjmember(BaseModel):
    """
    TN:相关方项目成员
    """
    rpj = models.ForeignKey(Rpj, verbose_name='关联项目', on_delete=models.CASCADE)
    remployee = models.ForeignKey(Remployee, verbose_name='关联人员',
                                  on_delete=models.CASCADE)
    duty = models.CharField('职责', max_length=20, null=True, blank=True)
    is_manager = models.BooleanField('是否项目负责人', default=False)
    rcertificates = models.ManyToManyField(Rcertificate, through='rpm.rpjcertificate', blank=True)
    note = models.CharField('备注', max_length=20, null=True, blank=True, help_text='后入厂/提前离厂')


class Rpjcertificate(BaseModel):
    """
    TN:相关方项目人员证书
    """
    rpj_member = models.ForeignKey(Rpjmember, verbose_name='关联项目成员', on_delete=models.CASCADE)
    rcertificate = models.ForeignKey(Rcertificate, verbose_name='关联相关方证书', on_delete=models.SET_NULL,
                                     null=True, blank=True)
    name = models.CharField('证书名称', max_length=20)
    number = models.CharField('证书编号', max_length=20)
    type = models.PositiveSmallIntegerField('证书类型', default=10, choices=Certificate.CERTIFICATE_TYPE_CHOICES)
    issue_date = models.DateField('发证日期')
    expiration_date = models.DateField('有效期')
    review_date = models.DateField('下一次复审日期')
    file = models.CharField('文件地址', max_length=1000, null=True, blank=True)


class RpjLog(CommonADModel):
    """TN:项目变更日志

    Args:
        BaseModel (_type_): _description_
    """
    rpj = models.ForeignKey(Rpj, verbose_name='关联项目', on_delete=models.CASCADE)
    operation = models.CharField('操作类型', max_length=20, help_text='member_come/member_leave')
    remployee = models.ForeignKey(Remployee, verbose_name='关联人员', on_delete=models.SET_NULL, null=True, blank=True)
    reason = models.CharField('原因', max_length=100, null=True, blank=True)
