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
129
130
131
132
use std::fmt::Display;

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

use crate::leetcode::question::qs_index::QsIndex;

#[derive(Clone)]
#[derive(Debug)]
#[derive(Default)]
#[derive(PartialEq)]
#[derive(Serialize, Deserialize)]
#[derive(DeriveEntityModel)]
#[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>,
}

impl From<QsIndex> for Model {
    fn from(value: QsIndex) -> Self {
        Self {
            question_id:          value.stat.question_id,
            question_title:       value.stat.question_title.clone(),
            question_title_slug:  value.stat.question_title_slug.clone(),
            total_acs:            value.stat.total_acs,
            total_submitted:      value.stat.total_submitted,
            frontend_question_id: value.stat.frontend_question_id.clone(),
            status:               value.status.clone(),
            difficulty:           value.difficulty.level,
            paid_only:            value.paid_only,
            is_favor:             value.is_favor,
            frequency:            value.frequency,
            progress:             value.progress,
            category:             String::new(),
            pass_rate:            Some(
                value.stat.total_acs as f64 / value.stat.total_submitted as f64 * 100.0,
            ),
        }
    }
}

#[derive(Clone, Copy)]
#[derive(Debug)]
#[derive(Default)]
#[derive(PartialEq, Eq)]
#[derive(EnumIter, DeriveRelation)]
pub enum Relation {
    #[default]
    #[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)
    }
}