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