1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::fmt::Display;

use lcode_config::config::global::USER_CONFIG;
use sea_orm::{entity::prelude::*, sea_query::OnConflict, IntoActiveModel};
use tracing::error;

use crate::{dao::glob_db, leetcode::question::pb_list};

#[derive(Default, Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "new_index")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub title_slug:           String,
    pub title:                String,
    pub title_cn:             Option<String>,
    pub is_favor:             bool,
    pub frontend_question_id: String,
    pub paid_only:            bool,
    pub difficulty:           String,
    pub status:               String,
    #[sea_orm(column_type = "Double", nullable)]
    pub ac_rate:              f64,
    // pub topic_tags: String,
}

impl Display for Model {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let name = if USER_CONFIG.config.translate {
            let mut name = self
                .title_cn
                .as_deref()
                .unwrap_or_default();
            if name.is_empty() {
                name = self.title.as_str();
            }
            name
        }
        else {
            self.title.as_str()
        };

        format!(
            "{id}: {tit}, {st}",
            id = self.frontend_question_id,
            tit = name,
            st = self.status
        )
        .fmt(f)
    }
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl Related<super::topic_tags::Entity> for Entity {
    fn to() -> RelationDef {
        super::qs_tag::Relation::TagRelation.def()
    }
    fn via() -> Option<RelationDef> {
        Some(
            super::qs_tag::Relation::TitleSlug
                .def()
                .rev(),
        )
    }
}

impl ActiveModelBehavior for ActiveModel {}

impl From<pb_list::NewIndex> for Model {
    fn from(value: pb_list::NewIndex) -> Self {
        Self {
            title_slug:           value.title_slug,
            title:                value.title,
            title_cn:             value.title_cn,
            is_favor:             value.is_favor,
            frontend_question_id: value.frontend_question_id,
            paid_only:            value.paid_only,
            difficulty:           value.difficulty,
            status:               value.status.unwrap_or_default(),
            ac_rate:              value.ac_rate,
        }
    }
}

impl Model {
    pub async fn insert_to_db(self) {
        if let Err(err) = Entity::insert(self.into_active_model())
            .on_conflict(
                OnConflict::column(Column::TitleSlug)
                    .update_columns([
                        Column::TitleSlug,
                        Column::Title,
                        Column::TitleCn,
                        Column::PaidOnly,
                        Column::IsFavor,
                        Column::FrontendQuestionId,
                        Column::Status,
                        Column::Difficulty,
                        Column::AcRate,
                    ])
                    .to_owned(),
            )
            .exec(glob_db().await)
            .await
        {
            error!("{}", err);
        }
    }
}