//! `SeaORM` Entity, @generated by sea-orm-codegen
use schemars::JsonSchema;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use serde_repr::{Deserialize_repr, Serialize_repr};
/// 公告级别
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    EnumIter,
    DeriveActiveEnum,
    Serialize_repr,
    Deserialize_repr,
    JsonSchema,
)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
#[repr(i16)]
pub enum NoticeLevel {
    /// 普通
    #[sea_orm(num_value = 1)]
    Normal = 1,
    /// 提示
    #[sea_orm(num_value = 2)]
    Info = 2,
    /// 重要
    #[sea_orm(num_value = 3)]
    Important = 3,
    /// 紧急
    #[sea_orm(num_value = 4)]
    Urgent = 4,
}
/// 公告范围
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    EnumIter,
    DeriveActiveEnum,
    Serialize_repr,
    Deserialize_repr,
    JsonSchema,
)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
#[repr(i16)]
pub enum NoticeScope {
    /// 全体后台用户
    #[sea_orm(num_value = 1)]
    AllAdmin = 1,
    /// 指定角色
    #[sea_orm(num_value = 2)]
    Role = 2,
    /// 指定用户
    #[sea_orm(num_value = 3)]
    User = 3,
}
/// 发布状态
#[derive(
    Debug,
    Clone,
    Copy,
    PartialEq,
    Eq,
    EnumIter,
    DeriveActiveEnum,
    Serialize_repr,
    Deserialize_repr,
    JsonSchema,
)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
#[repr(i16)]
pub enum PublishStatus {
    /// 草稿
    #[sea_orm(num_value = 1)]
    Draft = 1,
    /// 已发布
    #[sea_orm(num_value = 2)]
    Published = 2,
    /// 已撤回
    #[sea_orm(num_value = 3)]
    Revoked = 3,
}

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "sys", table_name = "notice")]
pub struct Model {
    /// 公告ID
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 公告标题
    pub notice_title: String,
    /// 公告正文内容
    #[sea_orm(column_type = "Text")]
    pub notice_content: String,
    /// 公告级别：1=普通 2=提示 3=重要 4=紧急
    pub notice_level: NoticeLevel,
    /// 公告范围：1=全体后台用户 2=指定角色 3=指定用户
    pub notice_scope: NoticeScope,
    /// 发布状态：1=草稿 2=已发布 3=已撤回
    pub publish_status: PublishStatus,
    /// 是否置顶
    pub pinned: bool,
    /// 是否启用
    pub enabled: bool,
    /// 排序，值越小越靠前
    pub sort: i32,
    /// 发布人
    pub publish_by: String,
    /// 发布时间
    pub publish_time: Option<DateTime>,
    /// 过期时间，为空表示不过期
    pub expire_time: Option<DateTime>,
    /// 备注
    pub remark: String,
    /// 创建人
    pub create_by: String,
    /// 创建时间
    pub create_time: DateTime,
    /// 更新人
    pub update_by: String,
    /// 更新时间
    pub update_time: DateTime,
    /// sys_notice -> sys_notice_target（一对多）
    #[sea_orm(has_many)]
    pub targets: HasMany<super::sys_notice_target::Entity>,
    /// sys_notice -> sys_notice_user（一对多）
    #[sea_orm(has_many)]
    pub notice_users: HasMany<super::sys_notice_user::Entity>,
    /// sys_notice -> sys_user（多对多，通过 sys_notice_user）
    #[sea_orm(has_many, via = "sys_notice_user")]
    pub users: HasMany<super::sys_user::Entity>,
}

#[sea_orm::entity::prelude::async_trait::async_trait]
impl sea_orm::ActiveModelBehavior for self::ActiveModel {
    async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, sea_orm::DbErr>
    where
        C: sea_orm::ConnectionTrait,
    {
        let now = chrono::Local::now().naive_local();
        self.update_time = sea_orm::Set(now);
        if insert {
            self.create_time = sea_orm::Set(now);
        }
        Ok(self)
    }
}
