# coding=utf-8

from django.db import models
from django.conf import settings
from django.utils import timezone
from apps.account.models import Department, User
from apps.exceptions import CustomError
from libs.filefield import PathAndRename


class Notice(models.Model):
    NORMAL = 0
    URGENT = 1
    PRIORITY_CHOICES = (
        (NORMAL, u'普通'),
        (URGENT, u'紧急'),
    )

    department = models.ForeignKey(Department, verbose_name=u"部门", on_delete=models.PROTECT)
    priority = models.PositiveSmallIntegerField(choices=PRIORITY_CHOICES, verbose_name=u"优先级", default=NORMAL)
    title = models.CharField(max_length=100, verbose_name=u"标题")
    content = models.CharField(max_length=1024, verbose_name=u"公告内容")
    dendline = models.DateTimeField(verbose_name=u"截止时间")
    company = models.ForeignKey(Department, verbose_name=u'公司', editable=False, related_name='notice_ref_company')
    create_user = models.ForeignKey(User, editable=False, related_name='notice_ref_user', verbose_name=u"发布人", on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"发布时间", default=timezone.now)
    hits = models.PositiveIntegerField(verbose_name=u"查看次数", default=0, editable=False)
    create_user_department = models.ForeignKey(Department, verbose_name=u"创建人部门", null=True, blank=True, on_delete=models.PROTECT,
                                   editable=False, related_name='notice_ref_create_user_department')

    class Meta:
        db_table = "office_notice"
        verbose_name = u"信息管理"
        ordering = ('-id',)
        default_permissions = ()
        permissions = (
            ("view_notice", u"浏览"),
            ("add_notice", u"添加"),
            ("delete_notice", u"删除"),
        )

    @staticmethod
    def getById(id):
        instance = Notice.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的公告')
        return instance

class NoticeBrowseRecord(models.Model):
    notice = models.ForeignKey(Notice, verbose_name=u"通知")
    browse_user = models.ForeignKey(User, editable=False,verbose_name=u"浏览人", on_delete=models.PROTECT)
    department = models.ForeignKey(Department, verbose_name=u"浏览人部门", null=True, blank=True, on_delete=models.PROTECT,
                                   editable=False, related_name='office_notice_browse_record_ref_department')
    create_time = models.DateTimeField(verbose_name=u"浏览时间", default=timezone.now)

    class Meta:
        db_table = "office_notice_browse_record"
        verbose_name = u"公告浏览记录"
        ordering = ('-id',)
        default_permissions = ()
        permissions = (
            ("view_browserecord", u"浏览"),
        )

    @staticmethod
    def getByNotice(id):
        instances = NoticeBrowseRecord.objects.filter(notice_id=id)
        return instances

    @staticmethod
    def getById(id):
        instance = NoticeBrowseRecord.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的浏览记录')
        return instance

class NoticeReply(models.Model):
    notice = models.ForeignKey(Notice, verbose_name=u"通知")
    content = models.CharField(max_length=500, verbose_name=u"内容")
    create_user = models.ForeignKey(User, editable=False, related_name='noticereply_ref_user', verbose_name=u"回复人", on_delete=models.PROTECT)
    create_time = models.DateTimeField(verbose_name=u"回复时间", default=timezone.now)
    department = models.ForeignKey(Department, verbose_name=u"回复人部门", null=True, blank=True, on_delete=models.PROTECT,
                                   editable=False, related_name='office_notice_reply_ref_department')
    class Meta:
        db_table = "office_notice_reply"
        verbose_name = u"公告回复"
        ordering = ("-id",)

    @staticmethod
    def getByNotice(id):
        instances = NoticeReply.objects.filter(notice_id=id)
        return instances

    @staticmethod
    def getById(id):
        instance = NoticeReply.objects.filter(pk=id).first()
        if not instance:
            raise CustomError(u'未找到相应的回复')
        return instance

class NoticeAttachment(models.Model):
    notice = models.ForeignKey(Notice, verbose_name=u"通知")
    file = models.CharField(max_length=100, verbose_name=u"文件")

    class Meta:
        db_table = "office_notice_attachment"
        verbose_name = u"公告附件"

    @staticmethod
    def getByNotice(id):
        instances = NoticeAttachment.objects.filter(notice_id=id)
        return instances

path_and_rename = PathAndRename("office_notice/files/")
