//! `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 NoticeTargetType {
    /// 角色
    #[sea_orm(num_value = 1)]
    Role = 1,
    /// 用户
    #[sea_orm(num_value = 2)]
    User = 2,
}
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "sys", table_name = "notice_target")]
pub struct Model {
    /// 主键ID
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 公告ID，逻辑关联 sys_notice.id
    #[sea_orm(unique_key = "uk_sys_notice_target_notice_target")]
    pub notice_id: i64,
    /// 目标类型：1=角色 2=用户
    #[sea_orm(unique_key = "uk_sys_notice_target_notice_target")]
    pub target_type: NoticeTargetType,
    /// 目标ID；target_type=1 时对应 sys_role.id，target_type=2 时对应 sys_user.id
    #[sea_orm(unique_key = "uk_sys_notice_target_notice_target")]
    pub target_id: i64,
    /// 创建时间
    pub create_time: DateTime,
    /// 关联公告（多对一）
    #[sea_orm(belongs_to, from = "notice_id", to = "id", skip_fk)]
    pub notice: Option<super::sys_notice::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();
        if insert {
            self.create_time = sea_orm::Set(now);
        }
        Ok(self)
    }
}
