Skip to main content

br_fields/
location.rs

1use crate::Field;
2use json::{object, JsonValue};
3
4/// 附件
5///
6/// * require 是否必填
7/// * field 字段名
8/// * mode 模式 file
9/// * title 字段描述
10/// * length 附件数量
11/// * def 默认值
12#[derive(Debug, Clone)]
13pub struct Location {
14    pub require: bool,
15    pub field: String,
16    pub mode: String,
17    pub title: String,
18    pub length: i32,
19    pub show: bool,
20    pub describe: String,
21    pub def: String,
22    pub example: JsonValue,
23}
24
25impl Location {
26    pub fn new(require: bool, field: &str, title: &str) -> Self {
27        Self {
28            require,
29            field: field.to_string(),
30            mode: "location".to_string(),
31            title: title.to_string(),
32            length: 0,
33            def: "".to_string(),
34            show: true,
35            describe: "".to_string(),
36            example: JsonValue::Null,
37        }
38    }
39    pub fn def(&mut self, def: &str) -> &mut Self {
40        self.def = def.to_string();
41        self
42    }
43    #[allow(dead_code)]
44    /// 设定坐标系统类型
45    /// * code
46    ///   SRID    |  名称 / 标准 |坐标系统类型 |单位 |应用场景
47    ///   4326    |  WGS 84 (EPSG:4326)   | 地理坐标系         | 度(经纬度)    |   GPS、Google Maps、全球定位标准
48    ///   3857    |  Web Mercator         | 投影坐标系         | 米             |   Web 地图(Google Maps、OpenStreetMap)
49    ///   4979    |  WGS 84 3D            | 地理坐标(含高度)  | 度 + 米        |   三维定位(含海拔)
50    ///   32633   |   UTM Zone 33N |投影坐标系 |米 |地方高精度测绘(如某地区工程制图)
51    ///   900913  |   Google Mercator(旧名) | 同 3857 |米 | 老地图系统使用
52    ///   0       |   SRID 未定义 |—— |—— |未设置坐标参考系
53    fn length(&mut self, code: i32) -> &mut Self {
54        self.length = code;
55        self
56    }
57}
58
59impl Field for Location {
60    fn sql(&mut self, model: &str) -> String {
61        let not_null = if self.require { " not null" } else { "" };
62        match model {
63            "sqlite" => {
64                let length = 39;
65                format!(
66                    "`{}` varchar({}){} default '{}'",
67                    self.field, length, not_null, self.def
68                )
69            }
70            "pgsql" => {
71                let length = 39;
72                let sql = format!(
73                    r#""{}" varchar({}) default '{}'"#,
74                    self.field, length, self.def
75                );
76                format!(
77                    "{} --{}|{}|{}|{}|{}",
78                    sql, self.title, self.mode, self.require, self.def, self.length
79                )
80            }
81            "mysql" => {
82                let sql = format!("`{}` POINT SRID {}{}", self.field, self.length, not_null);
83                format!(
84                    "{} comment '{}|{}|{}|{}|{}'",
85                    sql, self.title, self.mode, self.require, self.def, self.length
86                )
87            }
88            _ => {
89                let sql = format!(
90                    "`{}` varchar({}){} default '{}'",
91                    self.field, self.length, not_null, self.def
92                );
93                format!(
94                    "{} comment '{}|{}|{}|{}|{}'",
95                    sql, self.title, self.mode, self.require, self.def, self.length
96                )
97            }
98        }
99    }
100    fn hide(&mut self) -> &mut Self {
101        self.show = false;
102        self
103    }
104    fn describe(&mut self, text: &str) -> &mut Self {
105        self.describe = text.to_string();
106        self
107    }
108
109    fn field(&mut self) -> JsonValue {
110        let mut field = object! {};
111        field
112            .insert("require", JsonValue::from(self.require))
113            .unwrap();
114        field
115            .insert("field", JsonValue::from(self.field.clone()))
116            .unwrap();
117        field
118            .insert("mode", JsonValue::from(self.mode.clone()))
119            .unwrap();
120        field
121            .insert("title", JsonValue::from(self.title.clone()))
122            .unwrap();
123        field
124            .insert("length", JsonValue::from(self.length))
125            .unwrap();
126        field.insert("show", JsonValue::from(self.show)).unwrap();
127        field
128            .insert("describe", JsonValue::from(self.describe.clone()))
129            .unwrap();
130        field
131            .insert("def", JsonValue::from(self.def.clone()))
132            .unwrap();
133        field.insert("example", self.example.clone()).unwrap();
134        field
135    }
136
137    fn swagger(&mut self) -> JsonValue {
138        object! {
139            "type": self.mode.clone(),
140            "example": self.example.clone(),
141        }
142    }
143
144    fn example(&mut self, data: JsonValue) -> &mut Self {
145        self.example = data.clone();
146        self
147    }
148}
149
150/// 附件
151///
152/// * require 是否必填
153/// * field 字段名
154/// * mode 模式 file
155/// * title 字段描述
156/// * length 附件数量
157/// * def 默认值
158pub struct Polygon {
159    pub require: bool,
160    pub field: String,
161    pub mode: String,
162    pub title: String,
163    pub def: JsonValue,
164    pub length: i32,
165
166    pub show: bool,
167    pub describe: String,
168    pub items: JsonValue,
169    pub example: JsonValue,
170    pub max_items: i32,
171}
172
173impl Polygon {
174    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
175        Self {
176            require,
177            field: field.to_string(),
178            mode: "polygon".to_string(),
179            title: title.to_string(),
180            def,
181            length: 0,
182            show: true,
183            describe: "".to_string(),
184            items: JsonValue::Null,
185            example: JsonValue::Null,
186            max_items: 0,
187        }
188    }
189    /// 最小长度 TEXT 最多 65535 个字符
190    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
191    /// 最长长度 LONGTEXT 最多 4294967295 个字符
192    pub fn length(mut self, length: i32) -> Self {
193        self.length = length;
194        self
195    }
196    pub fn items(mut self, item: JsonValue) -> Self {
197        self.items = item;
198        self
199    }
200    pub fn max_items(mut self, mun: i32) -> Self {
201        self.max_items = mun;
202        self
203    }
204}
205
206impl Field for Polygon {
207    fn sql(&mut self, model: &str) -> String {
208        let not_null = if self.require { " not null" } else { "" };
209        match model {
210            "sqlite" => {
211                format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def)
212            }
213            "pgsql" => {
214                let sql = format!(r#""{}" TEXT default '{}'"#, self.field, self.def);
215                format!("{} --{}|{}|{}", sql, self.mode, self.title, self.length)
216            }
217            _ => {
218                let sql = match self.length {
219                    1 => format!(
220                        "`{}` MEDIUMTEXT{} default '{}'",
221                        self.field, not_null, self.def
222                    ),
223                    2 => format!(
224                        "`{}` LONGTEXT{} default '{}'",
225                        self.field, not_null, self.def
226                    ),
227                    _ => format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def),
228                };
229                format!(
230                    "{} comment '{}|{}|{}'",
231                    sql, self.mode, self.title, self.length
232                )
233            }
234        }
235    }
236    fn hide(&mut self) -> &mut Self {
237        self.show = false;
238        self
239    }
240    fn describe(&mut self, text: &str) -> &mut Self {
241        self.describe = text.to_string();
242        self
243    }
244
245    fn field(&mut self) -> JsonValue {
246        let mut field = object! {};
247        field
248            .insert("require", JsonValue::from(self.require))
249            .unwrap();
250        field
251            .insert("field", JsonValue::from(self.field.clone()))
252            .unwrap();
253        field
254            .insert("mode", JsonValue::from(self.mode.clone()))
255            .unwrap();
256        field
257            .insert("title", JsonValue::from(self.title.clone()))
258            .unwrap();
259        field
260            .insert("length", JsonValue::from(self.length))
261            .unwrap();
262
263        field.insert("def", self.def.clone()).unwrap();
264
265        field.insert("show", JsonValue::from(self.show)).unwrap();
266        field
267            .insert("describe", JsonValue::from(self.describe.clone()))
268            .unwrap();
269        field.insert("items", self.items.clone()).unwrap();
270        field.insert("example", self.example.clone()).unwrap();
271        field
272            .insert("max_items", JsonValue::from(self.max_items))
273            .unwrap();
274        field
275    }
276
277    fn swagger(&mut self) -> JsonValue {
278        object! {
279            "type": self.mode.clone(),
280            "example": self.example.clone(),
281        }
282    }
283
284    fn example(&mut self, data: JsonValue) -> &mut Self {
285        self.example = data.clone();
286        self
287    }
288}
289
290#[cfg(test)]
291mod test {
292    use crate::Field;
293
294    #[test]
295    fn test() {
296        let res = crate::location::Location::new(true, "str", "地理定位")
297            .describe("1313")
298            .length(0)
299            .field();
300        println!("{res:#}");
301    }
302}