//! `SeaORM` Entity, @generated by sea-orm-codegen

use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "sys", table_name = "notice_user")]
pub struct Model {
    /// 主键ID
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 公告ID，逻辑关联 sys_notice.id
    #[sea_orm(unique_key = "uk_sys_notice_user_notice_user")]
    pub notice_id: i64,
    /// 接收用户ID，逻辑关联 sys_user.id
    #[sea_orm(unique_key = "uk_sys_notice_user_notice_user")]
    pub user_id: i64,
    /// 是否已读
    pub read_flag: bool,
    /// 已读时间
    pub read_time: Option<DateTime>,
    /// 接收时间
    pub create_time: DateTime,
    /// 更新时间
    pub update_time: DateTime,
    /// 关联公告（多对一）
    #[sea_orm(belongs_to, from = "notice_id", to = "id", skip_fk)]
    pub notice: Option<super::sys_notice::Entity>,
    /// 关联用户（多对一）
    #[sea_orm(belongs_to, from = "user_id", to = "id", skip_fk)]
    pub user: Option<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)
    }
}
