Skip to main content

br_fields/
dict.rs

1use crate::Field;
2use json::{object, JsonValue};
3
4/// 字典字段
5///
6/// * field 字段名
7/// * mode 模式 string
8/// * title 字段描述
9/// * length 字段总长度(含小数位)
10/// * default 默认值
11/// * empty 是否可空
12/// * dec 小数位
13pub struct Dict {
14    pub require: bool,
15    pub field: String,
16    pub mode: String,
17    pub title: String,
18    pub table: String,
19    pub fields: String,
20    pub api: String,
21    pub def: String,
22    pub show: bool,
23    pub describe: String,
24    pub multiple: bool,
25    pub multiple_count: i32,
26    pub example: JsonValue,
27}
28
29impl Dict {
30    pub fn new(
31        require: bool,
32        field: &str,
33        title: &str,
34        table: &str,
35        fields: &str,
36        api: &str,
37    ) -> Self {
38        Self {
39            require,
40            field: field.to_string(),
41            mode: "dict".to_string(),
42            title: title.to_string(),
43            table: table.to_string(),
44            api: api.to_string(),
45            show: true,
46            fields: fields.to_string(),
47            describe: "".to_string(),
48            def: "".to_string(),
49            multiple: false,
50            multiple_count: 1,
51            example: JsonValue::Null,
52        }
53    }
54    /// 多选数量
55    pub fn multiple(&mut self, count: i32) -> &mut Dict {
56        self.multiple = count > 1;
57        self.multiple_count = count;
58        self
59    }
60    /// 默认值
61    pub fn default(&mut self, def: &str) -> &mut Dict {
62        self.def = def.to_string();
63        self
64    }
65}
66
67impl Field for Dict {
68    fn sql(&mut self, model: &str) -> String {
69        let not_null = if self.require { " not null" } else { "" };
70        let length = if self.multiple {
71            64 * self.multiple_count + self.multiple_count
72        } else {
73            64
74        };
75        match model {
76            "sqlite" => {
77                format!(
78                    "`{}` varchar({}){} default '{}'",
79                    self.field, length, not_null, self.def
80                )
81            }
82            "pgsql" => {
83                let sql = format!(
84                    r#""{}" varchar({}) default '{}'"#,
85                    self.field, length, self.def
86                );
87                format!(
88                    "{} --{}|{}|{}|{}|{}|{}",
89                    sql,
90                    self.mode,
91                    self.title,
92                    self.require,
93                    self.multiple,
94                    self.multiple_count,
95                    self.def
96                )
97            }
98            _ => {
99                let sql = format!(
100                    "`{}` varchar({}){} default '{}'",
101                    self.field, length, not_null, self.def
102                );
103                format!(
104                    "{} comment '{}|{}|{}|{}|{}|{}'",
105                    sql.clone(),
106                    self.mode,
107                    self.title,
108                    self.require,
109                    self.multiple,
110                    self.multiple_count,
111                    self.def
112                )
113            }
114        }
115    }
116    fn hide(&mut self) -> &mut Self {
117        self.show = false;
118        self
119    }
120    fn describe(&mut self, text: &str) -> &mut Self {
121        self.describe = text.to_string();
122        self
123    }
124
125    fn field(&mut self) -> JsonValue {
126        let mut field = object! {};
127        field
128            .insert("require", JsonValue::from(self.require))
129            .unwrap();
130        field
131            .insert("field", JsonValue::from(self.field.clone()))
132            .unwrap();
133        field
134            .insert("mode", JsonValue::from(self.mode.clone()))
135            .unwrap();
136        field
137            .insert("title", JsonValue::from(self.title.clone()))
138            .unwrap();
139        field
140            .insert("table", JsonValue::from(self.table.clone()))
141            .unwrap();
142        field
143            .insert("api", JsonValue::from(self.api.clone()))
144            .unwrap();
145        field
146            .insert("fields", JsonValue::from(self.fields.clone()))
147            .unwrap();
148        field
149            .insert("def", JsonValue::from(self.def.clone()))
150            .unwrap();
151        field
152            .insert("multiple", JsonValue::from(self.multiple))
153            .unwrap();
154        field
155            .insert("multiple_count", JsonValue::from(self.multiple_count))
156            .unwrap();
157
158        field.insert("show", JsonValue::from(self.show)).unwrap();
159        field
160            .insert("describe", JsonValue::from(self.describe.clone()))
161            .unwrap();
162        field.insert("example", self.example.clone()).unwrap();
163        field
164    }
165
166    fn swagger(&mut self) -> JsonValue {
167        object! {
168            "type": self.mode.clone(),
169            "example": self.example.clone(),
170        }
171    }
172
173    fn example(&mut self, data: JsonValue) -> &mut Self {
174        self.example = data.clone();
175        self
176    }
177}