# coding=utf-8


from django.db import models
from django.conf import settings

from apps.tenant.models import Tenant


class Notices(models.Model):
    NOTICE = 1
    MESSAGE = 2
    TYPE_CHOICES = (
        (NOTICE, u'集体通知'),
        (MESSAGE, u'个人消息'),
    )

    ALL = 1
    ADMIN = 2
    EMPLOYEE = 3
    REPAIR = 4
    RANGE_CHOICES = (
        (NOTICE, u'所有人'),
        (ADMIN, u'管理者'),
        (EMPLOYEE, u'检修人'),
        (REPAIR, u'报修人'),
    )

    content = models.TextField(verbose_name=u"内容")
    title = models.CharField(max_length=50, verbose_name=u'标题')
    create_time = models.DateTimeField(verbose_name='发布时间', auto_now_add=True)
    create_user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"创建者", on_delete=models.PROTECT,
                                    editable=False)
    tenant = models.ForeignKey(Tenant, verbose_name=u'企业', on_delete=models.PROTECT, blank=True)
    type = models.PositiveSmallIntegerField(verbose_name=u'通知类型', choices=TYPE_CHOICES, default=NOTICE)
    range = models.PositiveSmallIntegerField(verbose_name=u'通知范围', choices=RANGE_CHOICES, default=ALL)
    end_time = models.DateField(verbose_name='截止时间', blank=True)

    class Meta:
        db_table = "notices"
        verbose_name = u"通知"
        ordering = ['-id']

class NoticesToUser(models.Model):
    notice = models.ForeignKey(Notices, verbose_name=u"接收人", on_delete=models.PROTECT, related_name='notice_user')
    user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=u"接收人", on_delete=models.PROTECT,
                             editable=False)
    read_count = models.IntegerField(verbose_name=u"阅读次数",default=0)

    class Meta:
        db_table = "notices_user"
        verbose_name = u"通知消息接收人"
        ordering = ['-id']
