//! `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};

/// 租户默认隔离级别（1: 共享行, 2: 独立表, 3: 独立 Schema, 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 TenantIsolationLevel {
    /// 共享行
    #[sea_orm(num_value = 1)]
    SharedRow = 1,
    /// 独立表
    #[sea_orm(num_value = 2)]
    SeparateTable = 2,
    /// 独立 Schema
    #[sea_orm(num_value = 3)]
    SeparateSchema = 3,
    /// 独立库
    #[sea_orm(num_value = 4)]
    SeparateDatabase = 4,
}

/// 租户状态（1: 正常, 2: 禁用, 3: 待开通, 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 TenantStatus {
    /// 正常
    #[sea_orm(num_value = 1)]
    Enabled = 1,
    /// 禁用
    #[sea_orm(num_value = 2)]
    Disabled = 2,
    /// 待开通
    #[sea_orm(num_value = 3)]
    PendingProvision = 3,
    /// 已归档
    #[sea_orm(num_value = 4)]
    Archived = 4,
}

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "tenant", table_name = "tenant")]
pub struct Model {
    /// 主键 ID
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 租户业务唯一标识
    #[sea_orm(unique)]
    pub tenant_id: String,
    /// 租户名称
    pub tenant_name: String,
    /// 默认隔离级别
    pub default_isolation_level: TenantIsolationLevel,
    /// 联系人
    pub contact_name: String,
    /// 联系邮箱
    pub contact_email: String,
    /// 联系电话
    pub contact_phone: String,
    /// 到期时间
    pub expire_time: Option<DateTime>,
    /// 租户状态
    pub status: TenantStatus,
    /// 业务配置
    #[sea_orm(column_type = "JsonBinary")]
    pub config: Json,
    /// 扩展元数据
    #[sea_orm(column_type = "JsonBinary")]
    pub metadata: Json,
    /// 备注
    pub remark: String,
    /// 创建人
    pub create_by: String,
    /// 创建时间
    pub create_time: DateTime,
    /// 更新人
    pub update_by: String,
    /// 更新时间
    pub update_time: DateTime,
}

#[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)
    }
}
