Skip to main content

br_fields/
location.rs

1use json::{JsonValue, object};
2use crate::Field;
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!("{} varchar({}){} default '{}'", self.field, length, not_null, self.def)
66            }
67            "pgsql" => {
68                let length = 39;
69                format!(
70                    r#""{}" varchar({}){} default '{}'"#,
71                    self.field, length, not_null, self.def
72                )
73            }
74            "mysql" => {
75                let sql = format!("`{}` POINT SRID {}{}", self.field, self.length, not_null);
76                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def, self.length)
77            }
78            _ => {
79                let sql = format!("`{}` varchar({}){} default '{}'", self.field, self.length, not_null, self.def);
80                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.title, self.mode, self.require, self.def, self.length)
81            }
82        }
83    }
84    fn hide(&mut self) -> &mut Self {
85        self.show = false;
86        self
87    }
88    fn describe(&mut self, text: &str) -> &mut Self {
89        self.describe = text.to_string();
90        self
91    }
92
93    fn field(&mut self) -> JsonValue {
94        let mut field = object! {};
95        field.insert("require", JsonValue::from(self.require)).unwrap();
96        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
97        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
98        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
99        field.insert("length", JsonValue::from(self.length)).unwrap();
100        field.insert("show", JsonValue::from(self.show)).unwrap();
101        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
102        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
103        field.insert("example", self.example.clone()).unwrap();
104        field
105    }
106
107    fn swagger(&mut self) -> JsonValue {
108        object! {
109            "type": self.mode.clone(),
110            "example": self.example.clone(),
111        }
112    }
113
114    fn example(&mut self, data: JsonValue) -> &mut Self {
115        self.example = data.clone();
116        self
117    }
118}
119
120
121/// 附件
122///
123/// * require 是否必填
124/// * field 字段名
125/// * mode 模式 file
126/// * title 字段描述
127/// * length 附件数量
128/// * def 默认值
129pub struct Polygon {
130    pub require: bool,
131    pub field: String,
132    pub mode: String,
133    pub title: String,
134    pub def: JsonValue,
135    pub length: i32,
136
137    pub show: bool,
138    pub describe: String,
139    pub items: JsonValue,
140    pub example: JsonValue,
141    pub max_items: i32,
142}
143
144impl Polygon {
145    pub fn new(require: bool, field: &str, title: &str, def: JsonValue) -> Self {
146        Self {
147            require,
148            field: field.to_string(),
149            mode: "polygon".to_string(),
150            title: title.to_string(),
151            def,
152            length: 0,
153            show: true,
154            describe: "".to_string(),
155            items: JsonValue::Null,
156            example: JsonValue::Null,
157            max_items: 0,
158        }
159    }
160    /// 最小长度 TEXT 最多 65535 个字符
161    /// 中等长度 MEDIUMTEXT 最多 16777215 个字符
162    /// 最长长度 LONGTEXT 最多 4294967295 个字符
163    pub fn length(mut self, length: i32) -> Self {
164        self.length = length;
165        self
166    }
167    pub fn items(mut self, item: JsonValue) -> Self {
168        self.items = item;
169        self
170    }
171    pub fn max_items(mut self, mun: i32) -> Self {
172        self.max_items = mun;
173        self
174    }
175}
176
177impl Field for Polygon {
178    fn sql(&mut self, model: &str) -> String {
179        let not_null = if self.require { " not null" } else { "" };
180        match model {
181            "sqlite" => {
182                format!("{} TEXT{} default '{}'", self.field, not_null, self.def)
183            }
184            "pgsql" => {
185                format!(
186                    r#""{}" TEXT{} default '{}'"#,
187                    self.field, not_null, self.def
188                )
189            }
190            _ => {
191                let sql = match self.length {
192                    1 => format!("`{}` MEDIUMTEXT{} default '{}'", self.field, not_null, self.def),
193                    2 => format!("`{}` LONGTEXT{} default '{}'", self.field, not_null, self.def),
194                    _ => format!("`{}` TEXT{} default '{}'", self.field, not_null, self.def),
195                };
196                format!(
197                    "{} comment '{}|{}|{}'",
198                    sql.clone(),
199                    self.mode,
200                    self.title,
201                    self.length
202                )
203            }
204        }
205    }
206    fn hide(&mut self) -> &mut Self {
207        self.show = false;
208        self
209    }
210    fn describe(&mut self, text: &str) -> &mut Self {
211        self.describe = text.to_string();
212        self
213    }
214
215    fn field(&mut self) -> JsonValue {
216        let mut field = object! {};
217        field.insert("require", JsonValue::from(self.require)).unwrap();
218        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
219        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
220        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
221        field.insert("length", JsonValue::from(self.length)).unwrap();
222
223        field.insert("def", self.def.clone()).unwrap();
224
225        field.insert("show", JsonValue::from(self.show)).unwrap();
226        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
227        field.insert("items", self.items.clone()).unwrap();
228        field.insert("example", self.example.clone()).unwrap();
229        field.insert("max_items", JsonValue::from(self.max_items)).unwrap();
230        field
231    }
232
233    fn swagger(&mut self) -> JsonValue {
234        object! {
235            "type": self.mode.clone(),
236            "example": self.example.clone(),
237        }
238    }
239
240    fn example(&mut self, data: JsonValue) -> &mut Self {
241        self.example = data.clone();
242        self
243    }
244}
245
246
247#[cfg(test)]
248mod test {
249    use crate::Field;
250
251    #[test]
252    fn test() {
253        let res = crate::location::Location::new(true, "str", "地理定位").describe("1313").length(0).field();
254        println!("{res:#}");
255    }
256}