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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use sea_orm::{sea_query::OnConflict, EntityTrait, IntoActiveModel};
use tracing::error;

use super::{pb_list::NewIndex, qs_index::QsIndex};
use crate::{
    dao::{glob_db, InsertToDB},
    entities::{index, new_index, prelude::*, qs_tag, topic_tags},
};

impl InsertToDB for QsIndex {
    type Value = String;
    type Entity = index::Entity;
    type Model = index::Model;
    type ActiveModel = index::ActiveModel;

    fn to_model(&self, category: String) -> Self::Model {
        let mut model: index::Model = self.clone().into();
        model.category = category;
        model
    }

    async fn insert_to_db(&mut self, category: String) {
        match Index::insert(
            self.to_model(category)
                .into_active_model(),
        )
        .on_conflict(Self::on_conflict())
        .exec(glob_db().await)
        .await
        {
            Ok(_) => {},
            Err(err) => error!("{}", err),
        };
    }
    fn on_conflict() -> OnConflict {
        OnConflict::column(index::Column::QuestionId)
            .update_columns([
                index::Column::QuestionTitle,
                index::Column::FrontendQuestionId,
                index::Column::TotalAcs,
                index::Column::TotalSubmitted,
                index::Column::Status,
                index::Column::Difficulty,
                index::Column::PaidOnly,
                index::Column::Frequency,
                index::Column::Progress,
                index::Column::PassRate,
            ])
            .to_owned()
    }
}

impl InsertToDB for topic_tags::Model {
    type Value = u32;
    type Entity = TopicTagsDB;
    type Model = Self;
    type ActiveModel = topic_tags::ActiveModel;

    fn on_conflict() -> OnConflict {
        OnConflict::column(topic_tags::Column::TopicSlug)
            .update_columns([
                topic_tags::Column::Id,
                topic_tags::Column::Name,
                topic_tags::Column::NameTranslated,
            ])
            .to_owned()
    }
}
impl InsertToDB for qs_tag::Model {
    type Value = u32;
    type Entity = QsTagDB;
    type Model = Self;
    type ActiveModel = qs_tag::ActiveModel;

    fn on_conflict() -> OnConflict {
        OnConflict::columns([qs_tag::Column::TopicSlug, qs_tag::Column::TitleSlug])
            .do_nothing()
            .to_owned()
    }
}
impl InsertToDB for NewIndex {
    type Value = u32;
    type Entity = NewIndexDB;
    type Model = new_index::Model;
    type ActiveModel = new_index::ActiveModel;

    fn to_model(&self, _v: Self::Value) -> Self::Model {
        self.clone().into()
    }
    async fn insert_to_db(&mut self, v: Self::Value) {
        let topic: Vec<topic_tags::ActiveModel> = self
            .topic_tags
            .clone()
            .unwrap_or_default()
            .into_iter()
            .map(|v| v.into_active_model())
            .collect();

        let mut qs_tag = Vec::with_capacity(topic.len());

        if let Some(models) = self.topic_tags.take() {
            for ele in models {
                let qst = qs_tag::Model {
                    topic_slug: ele.topic_slug,
                    title_slug: self.title_slug.clone(),
                };
                qs_tag.push(qst.into_active_model());
            }
        }

        tokio::join!(
            self.insert_one(v),
            topic_tags::Model::insert_many(topic),
            qs_tag::Model::insert_many(qs_tag)
        );
    }

    fn on_conflict() -> OnConflict {
        OnConflict::column(new_index::Column::TitleSlug)
            .update_columns([
                new_index::Column::Title,
                new_index::Column::TitleCn,
                new_index::Column::Status,
                new_index::Column::AcRate,
            ])
            .to_owned()
    }
}