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
use std::fmt::Display;

use colored::Colorize;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use unicode_width::UnicodeWidthChar;

#[derive(Default, Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "problem_index")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    #[serde(default)]
    pub question_id:          u32,
    #[serde(default)]
    pub question_title:       String,
    #[serde(default)]
    pub question_title_slug:  String,
    #[serde(default)]
    pub total_acs:            u32,
    #[serde(default)]
    pub total_submitted:      u32,
    #[serde(default)]
    pub frontend_question_id: String,
    #[serde(default)]
    pub status:               Option<String>,
    #[serde(default)]
    pub difficulty:           u32,
    #[serde(default)]
    pub paid_only:            bool,
    #[serde(default)]
    pub is_favor:             bool,
    #[serde(default)]
    pub frequency:            u32,
    #[serde(default)]
    pub progress:             u32,
    #[serde(default)]
    pub category:             String,
    #[serde(default)]
    pub pass_rate:            Option<f64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_one = "super::detail::Entity")]
    Detail,
}

impl Related<super::detail::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Detail.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}

impl Display for Model {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let diff = match self.difficulty {
            1 => "⛳Easy".green(),
            2 => "🕎Medium".yellow(),
            3 => "💀Hard".red(),
            _ => " Unknown".blue(),
        };

        let mut widthid = 14;
        let mut count = 0;
        for ch in self.frontend_question_id.chars() {
            if UnicodeWidthChar::width(ch).unwrap_or_default() == 2 {
                count += 1;
            }
        }
        widthid -= count;

        let mut widtit = 66;
        let mut count1 = 0;
        for ch in self.question_title.chars() {
            if UnicodeWidthChar::width(ch).unwrap_or_default() == 2 {
                count1 += 1;
            }
        }
        widtit -= count1;

        format!(
            "🆔[{id:07}]|{fid:widthid$}|{cg:11}|🇹: {tit:widtit$}|Pass: {percent:.2}%|PaidOnly: \
             {po:6}|{diff:8}|{st}",
            fid = self.frontend_question_id,
            id = self.question_id,
            cg = self.category,
            tit = self.question_title,
            percent = self.pass_rate.unwrap_or_default(),
            po = self.paid_only,
            diff = diff,
            st = if self.status.is_some() { "👍" } else { "" }
        )
        .fmt(f)
    }
}