//! `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 ValueType {
    /// 文本
    #[sea_orm(num_value = 1)]
    Text = 1,
    /// 数字
    #[sea_orm(num_value = 2)]
    Number = 2,
    /// 布尔
    #[sea_orm(num_value = 3)]
    Boolean = 3,
    /// 文本域
    #[sea_orm(num_value = 4)]
    Textarea = 4,
    /// 下拉单选
    #[sea_orm(num_value = 5)]
    Select = 5,
    /// JSON
    #[sea_orm(num_value = 6)]
    Json = 6,
    /// 密码
    #[sea_orm(num_value = 7)]
    Password = 7,
    /// 图片
    #[sea_orm(num_value = 8)]
    Image = 8,
}
#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(schema_name = "sys", table_name = "config")]
pub struct Model {
    /// 配置ID
    #[sea_orm(primary_key)]
    pub id: i64,
    /// 配置名称
    pub config_name: String,
    /// 配置键（唯一标识，如 sys.site.name）
    #[sea_orm(unique)]
    pub config_key: String,
    /// 当前配置值，统一按字符串存储，按 value_type 解析
    #[sea_orm(column_type = "Text")]
    pub config_value: String,
    /// 默认配置值，用于重置或回退
    #[sea_orm(column_type = "Text")]
    pub default_value: String,
    /// 值类型：1=文本 2=数字 3=布尔 4=文本域 5=下拉单选 6=JSON 7=密码 8=图片
    pub value_type: ValueType,
    /// 配置分组ID，逻辑关联 sys_config_group.id（不建立数据库外键）
    pub config_group_id: i64,
    /// 候选项字典类型编码，当 value_type=5 时使用，对应 sys_dict_type.dict_type
    pub option_dict_type: String,
    /// 同分组内排序，值越小越靠前
    pub config_sort: i32,
    /// 是否启用
    pub enabled: bool,
    /// 是否系统内置（防止误删）
    pub is_system: bool,
    /// 备注
    pub remark: String,
    /// 创建人
    pub create_by: String,
    /// 创建时间
    pub create_time: DateTime,
    /// 更新人
    pub update_by: String,
    /// 更新时间
    pub update_time: DateTime,
    /// 关联配置分组（多对一）
    #[sea_orm(belongs_to, from = "config_group_id", to = "id", skip_fk)]
    pub group: Option<super::sys_config_group::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)
    }
}
