br_fields/
table.rs

1use json::{JsonValue, object};
2use crate::Field;
3
4/// 关联表
5///
6/// * field 字段名
7/// * mode 模式 string
8/// * title 字段描述
9/// * length 字段总长度(含小数位)
10/// * default 默认值
11/// * empty 是否可空
12/// * dec 小数位
13pub struct Tree {
14    pub require: bool,
15    pub field: String,
16    pub mode: String,
17    pub title: String,
18    pub table: String,
19    pub fields: Vec<String>,
20    pub api: String,
21    pub show: bool,
22    pub describe: String,
23    pub def: String,
24    pub multiple: bool,
25    pub multiple_count: i32,
26    pub example: JsonValue,
27}
28
29impl Tree {
30    pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
31        Self {
32            require,
33            field: field.to_string(),
34            mode: "tree".to_string(),
35            title: title.to_string(),
36            table: table.to_string(),
37            api: api.to_string(),
38            show: true,
39            fields: fields.iter().map(|c| c.to_string()).collect(),
40            describe: "".to_string(),
41            def: "".to_string(),
42            multiple: false,
43            multiple_count: 1,
44            example: JsonValue::Null,
45        }
46    }
47    /// 多选数量
48    pub fn multiple(&mut self, count: i32) -> &mut Tree {
49        self.multiple = count > 1;
50        self.multiple_count = count;
51        self
52    }
53}
54
55impl Field for Tree {
56    fn sql(&mut self, model: &str) -> String {
57        match model {
58            "sqlite" => {
59                format!("`{}` varchar({})   default ''", self.field, {
60                    if self.multiple {
61                        64 * self.multiple_count + self.multiple_count
62                    } else {
63                        64
64                    }
65                })
66            }
67            "pgsql" => {
68                let sql = format!("{} varchar({})   default ''", self.field, {
69                    if self.multiple {
70                        64 * self.multiple_count + self.multiple_count
71                    } else {
72                        64
73                    }
74                });
75                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
76            }
77            _ => {
78                let sql = format!("`{}` varchar({})   default ''", self.field, {
79                    if self.multiple {
80                        64 * self.multiple_count + self.multiple_count
81                    } else {
82                        64
83                    }
84                });
85                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
86            }
87        }
88    }
89    fn hide(&mut self) -> &mut Self {
90        self.show = false;
91        self
92    }
93    fn describe(&mut self, text: &str) -> &mut Self {
94        self.describe = text.to_string();
95        self
96    }
97
98    fn field(&mut self) -> JsonValue {
99        let mut field = object! {};
100        field.insert("require", JsonValue::from(self.require)).unwrap();
101        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
102        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
103        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
104        field.insert("table", JsonValue::from(self.table.clone())).unwrap();
105        field.insert("api", JsonValue::from(self.api.clone())).unwrap();
106        field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
107
108        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
109
110        field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
111        field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
112
113        field.insert("show", JsonValue::from(self.show)).unwrap();
114        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
115        field.insert("example", self.example.clone()).unwrap();
116        field
117    }
118
119    fn swagger(&mut self) -> JsonValue {
120        object! {
121            "type": self.mode.clone(),
122            "example": self.example.clone(),
123        }
124    }
125
126    fn example(&mut self, data: JsonValue) -> &mut Self {
127        self.example = data.clone();
128        self
129    }
130}
131
132/// 关联表
133///
134/// * field 字段名
135/// * mode 模式 string
136/// * title 字段描述
137/// * length 字段总长度(含小数位)
138/// * default 默认值
139/// * empty 是否可空
140/// * dec 小数位
141#[derive(Debug, Clone)]
142pub struct Table {
143    pub require: bool,
144    pub field: String,
145    pub mode: String,
146    pub title: String,
147    pub table: String,
148    pub fields: Vec<String>,
149    pub api: String,
150    pub show: bool,
151    pub describe: String,
152    pub def: String,
153    pub multiple: bool,
154    pub multiple_count: i32,
155    pub example: JsonValue,
156}
157
158impl Table {
159    pub fn new(require: bool, field: &str, title: &str, table: &str, fields: Vec<&str>, api: &str) -> Self {
160        Self {
161            require,
162            field: field.to_string(),
163            mode: "table".to_string(),
164            title: title.to_string(),
165            table: table.to_string(),
166            api: api.to_string(),
167            show: true,
168            fields: fields.iter().map(|c| c.to_string()).collect(),
169            describe: "".to_string(),
170            def: "".to_string(),
171            multiple: false,
172            multiple_count: 1,
173            example: JsonValue::Null,
174        }
175    }
176    /// 多选数量
177    pub fn multiple(&mut self, count: i32) -> &mut Table {
178        self.multiple = count > 1;
179        self.multiple_count = count;
180        self
181    }
182}
183
184impl Field for Table {
185    fn sql(&mut self, model: &str) -> String {
186        match model {
187            "sqlite" => {
188                format!("`{}` varchar({})   default ''", self.field, {
189                    if self.multiple {
190                        64 * self.multiple_count + self.multiple_count
191                    } else {
192                        64
193                    }
194                })
195            }
196            "pgsql" => {
197                let sql = format!("{} varchar({})   default ''", self.field, {
198                    if self.multiple {
199                        64 * self.multiple_count + self.multiple_count
200                    } else {
201                        64
202                    }
203                });
204                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
205            }
206            _ => {
207                let sql = format!("`{}` varchar({})   default ''", self.field, {
208                    if self.multiple {
209                        64 * self.multiple_count + self.multiple_count
210                    } else {
211                        64
212                    }
213                });
214                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.title, self.require, self.multiple, self.multiple_count)
215            }
216        }
217    }
218    fn hide(&mut self) -> &mut Self {
219        self.show = false;
220        self
221    }
222    fn describe(&mut self, text: &str) -> &mut Self {
223        self.describe = text.to_string();
224        self
225    }
226
227    fn field(&mut self) -> JsonValue {
228        let mut field = object! {};
229        field.insert("require", JsonValue::from(self.require)).unwrap();
230        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
231        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
232        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
233        field.insert("table", JsonValue::from(self.table.clone())).unwrap();
234        field.insert("api", JsonValue::from(self.api.clone())).unwrap();
235        field.insert("fields", JsonValue::from(self.fields.clone())).unwrap();
236
237        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
238
239        field.insert("multiple", JsonValue::from(self.multiple)).unwrap();
240        field.insert("multiple_count", JsonValue::from(self.multiple_count)).unwrap();
241
242        field.insert("show", JsonValue::from(self.show)).unwrap();
243        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
244        field.insert("example", self.example.clone()).unwrap();
245        field
246    }
247
248    fn swagger(&mut self) -> JsonValue {
249        object! {
250            "type": self.mode.clone(),
251            "example": self.example.clone(),
252        }
253    }
254    fn example(&mut self, data: JsonValue) -> &mut Self {
255        self.example = data;
256        self
257    }
258}