br_fields/
str.rs

1use crate::datetime::Datetime;
2use crate::Field;
3use json::{object, JsonValue};
4
5/// 字符串
6///
7/// * require 是否必填
8/// * field 字段名
9/// * mode 模式 string
10/// * title 字段描述
11/// * length 字段总长度(含小数位)
12/// * def 默认值
13/// * dec 后缀内容
14pub struct Str {
15    pub require: bool,
16    pub field: String,
17    pub mode: String,
18    pub title: String,
19    pub def: String,
20    pub length: usize,
21    pub show: bool,
22    pub describe: String,
23    pub example: JsonValue,
24}
25
26impl Str {
27    pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Str {
28        Self {
29            require,
30            field: field.to_string(),
31            mode: "string".to_string(),
32            title: title.to_string(),
33            def: default.to_string(),
34            length,
35            show: true,
36            describe: "".to_string(),
37            example: JsonValue::Null,
38        }
39    }
40}
41
42impl Field for Str {
43    fn sql(&mut self, model: &str) -> String {
44        match model {
45            "sqlite" => format!(
46                "{} varchar({})  default '{}'",
47                self.field, self.length, self.def
48            ),
49            "pgsql" => {
50                let sql = format!(
51                    r#""{}" varchar({})  default '{}'"#,
52                    self.field, self.length, self.def
53                );
54                format!(
55                    "{} comment '{}|{}|{}|{}|{}'",
56                    sql.clone(),
57                    self.mode,
58                    self.require,
59                    self.title,
60                    self.length,
61                    self.def
62                )
63            }
64            _ => {
65                let sql = format!(
66                    "`{}` varchar({})  default '{}'",
67                    self.field, self.length, self.def
68                );
69                format!(
70                    "{} comment '{}|{}|{}|{}|{}'",
71                    sql.clone(),
72                    self.mode,
73                    self.require,
74                    self.title,
75                    self.length,
76                    self.def
77                )
78            }
79        }
80    }
81    fn hide(&mut self) -> &mut Self {
82        self.show = false;
83        self
84    }
85    fn describe(&mut self, text: &str) -> &mut Self {
86        self.describe = text.to_string();
87        self
88    }
89    fn field(&mut self) -> JsonValue {
90        let mut field = object! {};
91        field.insert("require", JsonValue::from(self.require)).unwrap();
92        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
93        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
94        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
95        field.insert("length", JsonValue::from(self.length)).unwrap();
96        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
97
98        field.insert("show", JsonValue::from(self.show)).unwrap();
99        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
100        field.insert("example", self.example.clone()).unwrap();
101
102        field
103    }
104
105    fn swagger(&mut self) -> JsonValue {
106        object! {
107            "type": self.mode.clone(),
108            "example": self.example.clone(),
109        }
110    }
111    fn example(&mut self, data: JsonValue) -> &mut Self {
112        self.example = data;
113        self
114    }
115}
116
117/// 密码
118///
119/// * field 字段名
120/// * mode 模式 pass
121/// * title 字段描述
122/// * length 字段总长度(含小数位)
123/// * default 默认值
124/// * empty 是否可空
125pub struct Pass {
126    pub require: bool,
127    pub field: String,
128    pub mode: String,
129    pub title: String,
130    pub def: String,
131    pub length: usize,
132    pub show: bool,
133    pub describe: String,
134    pub example: JsonValue,
135}
136
137impl Pass {
138    pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
139        Self {
140            require,
141            field: field.to_string(),
142            mode: "pass".to_string(),
143            title: title.to_string(),
144            def: default.to_string(),
145            length,
146            show: true,
147            describe: "".to_string(),
148            example: json::Null,
149        }
150    }
151}
152
153impl Field for Pass {
154    fn sql(&mut self, model: &str) -> String {
155        match model {
156            "sqlite" => format!(
157                "`{}` varchar({})  default '{}'",
158                self.field, self.length, self.def
159            ),
160            "pgsql" => {
161                let sql = format!(
162                    r#""{}" varchar({})  default '{}'"#,
163                    self.field, self.length, self.def
164                );
165                format!(
166                    "{} comment '{}|{}|{}|{}|{}'",
167                    sql.clone(),
168                    self.mode,
169                    self.require,
170                    self.title,
171                    self.length,
172                    self.def
173                )
174            }
175            _ => {
176                let sql = format!(
177                    "`{}` varchar({})  default '{}'",
178                    self.field, self.length, self.def
179                );
180                format!(
181                    "{} comment '{}|{}|{}|{}|{}'",
182                    sql.clone(),
183                    self.mode,
184                    self.require,
185                    self.title,
186                    self.length,
187                    self.def
188                )
189            }
190        }
191    }
192    fn hide(&mut self) -> &mut Self {
193        self.show = false;
194        self
195    }
196    fn describe(&mut self, text: &str) -> &mut Self {
197        self.describe = text.to_string();
198        self
199    }
200    fn field(&mut self) -> JsonValue {
201        let mut field = object! {};
202        field.insert("require", JsonValue::from(self.require)).unwrap();
203        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
204        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
205        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
206        field.insert("length", JsonValue::from(self.length)).unwrap();
207        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
208
209        field.insert("show", JsonValue::from(self.show)).unwrap();
210        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
211        field.insert("example", self.example.clone()).unwrap();
212
213        field
214    }
215
216    fn swagger(&mut self) -> JsonValue {
217        object! {
218            "type": self.mode.clone(),
219            "example": self.example.clone(),
220        }
221    }
222
223    fn example(&mut self, data: JsonValue) -> &mut Self {
224        self.example = data;
225        self
226    }
227}
228
229/// 自定义主键
230///
231/// * field 字段名
232/// * mode 模式
233/// * title 字段描述
234/// * length 字段总长度(含小数位)
235/// * def 默认值
236pub struct Key {
237    pub require: bool,
238    pub field: String,
239    pub mode: String,
240    pub title: String,
241    pub length: usize,
242
243    pub def: String,
244
245    pub auto: bool,
246    pub show: bool,
247    pub describe: String,
248    pub example: JsonValue,
249}
250
251impl Key {
252    pub fn new(require: bool, field: &str, title: &str, length: usize) -> Self {
253        Self {
254            require,
255            field: field.to_string(),
256            mode: "key".to_string(),
257            title: title.to_string(),
258            length,
259            auto: false,
260            show: true,
261            def: "".to_string(),
262            describe: "".to_string(),
263            example: JsonValue::Null,
264        }
265    }
266    pub fn auto(mut self, auto: bool) -> Self {
267        if auto {
268            self.length = 0;
269        }
270        self.auto = auto;
271        self
272    }
273}
274
275impl Field for Key {
276    fn sql(&mut self, model: &str) -> String {
277        match model {
278            "sqlite" => match self.auto {
279                true => {
280                    let sql = format!("`{}` INTEGER  PRIMARY KEY", self.field);
281                    format!("{} AUTOINCREMENT", sql.clone())
282                }
283                false => {
284                    format!(
285                        "`{}` varchar({}) default '' constraint {}_key primary key",
286                        self.field, self.length, self.field
287                    )
288                }
289            },
290            "pgsql" => match self.auto {
291                true => {
292                    let sql = format!(r#""{}" int  AUTO_INCREMENT"#, self.field);
293                    format!(
294                        "{} comment '{}|{}|{}'",
295                        sql.clone(),
296                        self.mode,
297                        self.title,
298                        self.length
299                    )
300                }
301                false => {
302                    let mut sql = format!(r#""{}" varchar({}) not null"#, self.field, self.length);
303                    sql = sql.clone().to_string();
304                    format!(
305                        "{} comment '{}|{}|{}'",
306                        sql.clone(),
307                        self.mode,
308                        self.title,
309                        self.length
310                    )
311                }
312            },
313            _ => match self.auto {
314                true => {
315                    let sql = format!("`{}` int  AUTO_INCREMENT", self.field);
316                    format!(
317                        "{} comment '{}|{}|{}'",
318                        sql.clone(),
319                        self.mode,
320                        self.title,
321                        self.length
322                    )
323                }
324                false => {
325                    let mut sql = format!("`{}`  varchar({}) ", self.field, self.length);
326                    sql = sql.clone().to_string();
327                    format!(
328                        "{} comment '{}|{}|{}'",
329                        sql.clone(),
330                        self.mode,
331                        self.title,
332                        self.length
333                    )
334                }
335            },
336        }
337    }
338    fn hide(&mut self) -> &mut Self {
339        self.show = false;
340        self
341    }
342
343    fn describe(&mut self, text: &str) -> &mut Self {
344        self.describe = text.to_string();
345        self
346    }
347    fn field(&mut self) -> JsonValue {
348        let mut field = object! {};
349        field.insert("require", JsonValue::from(self.require)).unwrap();
350        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
351        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
352        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
353        field.insert("length", JsonValue::from(self.length)).unwrap();
354        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
355        field.insert("auto", JsonValue::from(self.auto)).unwrap();
356
357        field.insert("show", JsonValue::from(self.show)).unwrap();
358        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
359        field.insert("example", self.example.clone()).unwrap();
360
361        field
362    }
363    fn example(&mut self, data: JsonValue) -> &mut Self {
364        self.example = data;
365        self
366    }
367
368    fn swagger(&mut self) -> JsonValue {
369        object! {
370            "type": self.mode.clone(),
371            "example": self.example.clone(),
372        }
373    }
374}
375
376/// 电话
377///
378/// * field 字段名
379/// * mode 模式 pass
380/// * title 字段描述
381/// * length 字段总长度(含小数位)
382/// * default 默认值
383/// * empty 是否可空
384pub struct Tel {
385    pub require: bool,
386    pub field: String,
387    pub mode: String,
388    pub title: String,
389    pub def: String,
390    pub length: i32,
391
392    pub show: bool,
393    pub describe: String,
394    pub example: JsonValue,
395}
396
397impl Tel {
398    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
399        Self {
400            require,
401            field: field.to_string(),
402            mode: "tel".to_string(),
403            title: title.to_string(),
404            def: default.to_string(),
405            length: 15,
406
407            show: true,
408            describe: "".to_string(),
409            example: JsonValue::Null,
410        }
411    }
412}
413
414impl Field for Tel {
415    fn sql(&mut self, model: &str) -> String {
416        let dot = if model == "pgsql" {""} else {"`"};
417        let sql = format!(
418            "{dot}{}{dot} varchar({})  default '{}'",
419            self.field, self.length, self.def
420        );
421        match model {
422            "sqlite" => sql,
423            _ => format!(
424                "{} comment '{}|{}|{}|{}|{}'",
425                sql.clone(),
426                self.mode,
427                self.require,
428                self.title,
429                self.length,
430                self.def
431            ),
432        }
433    }
434    fn hide(&mut self) -> &mut Self {
435        self.show = false;
436        self
437    }
438    fn describe(&mut self, text: &str) -> &mut Self {
439        self.describe = text.to_string();
440        self
441    }
442
443    fn field(&mut self) -> JsonValue {
444        let mut field = object! {};
445        field.insert("require", JsonValue::from(self.require)).unwrap();
446        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
447        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
448        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
449        field.insert("length", JsonValue::from(self.length)).unwrap();
450        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
451
452        field.insert("show", JsonValue::from(self.show)).unwrap();
453        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
454        field.insert("example", self.example.clone()).unwrap();
455
456        field
457    }
458
459    fn swagger(&mut self) -> JsonValue {
460        object! {
461            "type": self.mode.clone(),
462            "example": self.example.clone(),
463        }
464    }
465    fn example(&mut self, data: JsonValue) -> &mut Self {
466        self.example = data;
467        self
468    }
469}
470
471/// 身份证
472///
473/// * field 字段名
474/// * mode 模式 ident
475/// * title 字段描述
476/// * length 字段总长度(含小数位)
477/// * default 默认值
478/// * empty 是否可空
479pub struct Ident {
480    pub require: bool,
481    pub field: String,
482    pub mode: String,
483    pub title: String,
484    pub def: String,
485    pub length: i32,
486
487    pub show: bool,
488    pub describe: String,
489    pub example: JsonValue,
490}
491
492impl Ident {
493    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
494        Self {
495            require,
496            field: field.to_string(),
497            mode: "ident".to_string(),
498            title: title.to_string(),
499            def: default.to_string(),
500            length: 18,
501
502            show: true,
503            describe: "".to_string(),
504            example: JsonValue::Null,
505        }
506    }
507}
508
509impl Field for Ident {
510    fn sql(&mut self, model: &str) -> String {
511        let dot = if model == "pgsql" {""} else {"`"};
512        let sql = format!(
513            "{dot}{}{dot} varchar({})  default '{}'",
514            self.field, self.length, self.def
515        );
516        match model {
517            "sqlite" => sql,
518            _ => format!(
519                "{} comment '{}|{}|{}|{}|{}'",
520                sql.clone(),
521                self.mode,
522                self.require,
523                self.title,
524                self.length,
525                self.def
526            ),
527        }
528    }
529    fn hide(&mut self) -> &mut Self {
530        self.show = false;
531        self
532    }
533    fn describe(&mut self, text: &str) -> &mut Self {
534        self.describe = text.to_string();
535        self
536    }
537
538    fn field(&mut self) -> JsonValue {
539        let mut field = object! {};
540        field.insert("require", JsonValue::from(self.require)).unwrap();
541        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
542        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
543        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
544        field.insert("length", JsonValue::from(self.length)).unwrap();
545        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
546
547        field.insert("show", JsonValue::from(self.show)).unwrap();
548        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
549        field.insert("example", self.example.clone()).unwrap();
550
551        field
552    }
553
554    fn swagger(&mut self) -> JsonValue {
555        object! {
556            "type": self.mode.clone(),
557            "example": self.example.clone(),
558        }
559    }
560    fn example(&mut self, data: JsonValue) -> &mut Self {
561        self.example = data;
562        self
563    }
564}
565/// 电子邮件
566///
567/// * field 字段名
568/// * mode 模式 pass
569/// * title 字段描述
570/// * length 字段总长度(含小数位)
571/// * default 默认值
572/// * empty 是否可空
573pub struct Email {
574    pub require: bool,
575    pub field: String,
576    pub mode: String,
577    pub title: String,
578    pub def: String,
579    pub length: i32,
580
581    pub show: bool,
582    pub describe: String,
583    pub example: JsonValue,
584}
585
586impl Email {
587    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
588        Self {
589            require,
590            field: field.to_string(),
591            mode: "email".to_string(),
592            title: title.to_string(),
593            def: default.to_string(),
594            length: 100,
595
596            show: true,
597            describe: "".to_string(),
598            example: JsonValue::Null,
599        }
600    }
601}
602
603impl Field for Email {
604    fn sql(&mut self, model: &str) -> String {
605        let sql = format!("`{}` varchar({})  default '{}'", self.field, self.length, self.def);
606        match model {
607            "sqlite" => sql,
608            "pgsql" => {
609                let sql = format!("{} varchar({})  default '{}'", self.field, self.length, self.def);
610                format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
611            }
612            _ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def),
613        }
614    }
615    fn hide(&mut self) -> &mut Self {
616        self.show = false;
617        self
618    }
619    fn describe(&mut self, text: &str) -> &mut Self {
620        self.describe = text.to_string();
621        self
622    }
623    fn field(&mut self) -> JsonValue {
624        let mut field = object! {};
625        field.insert("require", JsonValue::from(self.require)).unwrap();
626        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
627        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
628        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
629        field.insert("length", JsonValue::from(self.length)).unwrap();
630        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
631
632        field.insert("show", JsonValue::from(self.show)).unwrap();
633        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
634        field.insert("example", self.example.clone()).unwrap();
635        field
636    }
637
638    fn verify(&mut self, data: JsonValue) -> JsonValue {
639        data
640    }
641
642    fn swagger(&mut self) -> JsonValue {
643        object! {
644            "type": self.mode.clone(),
645            "example": self.example.clone(),
646        }
647    }
648    fn example(&mut self, data: JsonValue) -> &mut Self {
649        self.example = data.clone();
650        self
651    }
652}
653
654/// 颜色
655///
656/// * field 字段名
657/// * mode 模式 pass
658/// * title 字段描述
659/// * length 字段总长度(含小数位)
660pub struct Color {
661    pub require: bool,
662    pub field: String,
663    pub mode: String,
664    pub title: String,
665    pub def: String,
666    pub length: i32,
667
668    pub show: bool,
669    pub describe: String,
670    pub example: JsonValue,
671}
672
673impl Color {
674    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
675        Self {
676            require,
677            field: field.to_string(),
678            mode: "color".to_string(),
679            title: title.to_string(),
680            def: default.to_string(),
681            length: 9,
682
683            show: true,
684            describe: "".to_string(),
685            example: JsonValue::Null,
686        }
687    }
688}
689
690impl Field for Color {
691    fn sql(&mut self, model: &str) -> String {
692        let dot = if model == "pgsql" {""} else {"`"};
693        let sql = format!(
694            "{dot}{}{dot} varchar({})  default '{}'",
695            self.field, self.length, self.def
696        );
697        match model {
698            "sqlite" => sql,
699            _ => format!(
700                "{} comment '{}|{}|{}|{}|{}'",
701                sql.clone(),
702                self.mode,
703                self.require,
704                self.title,
705                self.length,
706                self.def
707            ),
708        }
709    }
710    fn hide(&mut self) -> &mut Self {
711        self.show = false;
712        self
713    }
714    fn describe(&mut self, text: &str) -> &mut Self {
715        self.describe = text.to_string();
716        self
717    }
718    fn field(&mut self) -> JsonValue {
719        let mut field = object! {};
720        field.insert("require", JsonValue::from(self.require)).unwrap();
721        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
722        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
723        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
724        field.insert("length", JsonValue::from(self.length)).unwrap();
725        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
726
727        field.insert("show", JsonValue::from(self.show)).unwrap();
728        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
729        field.insert("example", self.example.clone()).unwrap();
730
731        field
732    }
733
734    fn verify(&mut self, data: JsonValue) -> JsonValue {
735        data
736    }
737
738    fn swagger(&mut self) -> JsonValue {
739        object! {
740            "type": self.mode.clone(),
741            "example": self.example.clone(),
742        }
743    }
744    fn example(&mut self, data: JsonValue) -> &mut Self {
745        self.example = data.clone();
746        self
747    }
748}
749
750/// 编号
751///
752/// * field 字段名
753/// * mode 模式 pass
754/// * title 字段描述
755/// * length 字段总长度(含小数位)
756/// * default 默认值
757/// * empty 是否可空
758/// * dec 编号前缀
759pub struct Code {
760    pub require: bool,
761    pub field: String,
762    pub mode: String,
763    pub title: String,
764    pub def: String,
765    pub length: usize,
766
767    pub show: bool,
768    pub describe: String,
769
770    /// 编号前缀
771    pub dec: String,
772    pub example: JsonValue,
773}
774
775impl Code {
776    pub fn new(
777        require: bool,
778        field: &str,
779        title: &str,
780        length: usize,
781        dec: &str,
782        default: &str,
783    ) -> Self {
784        Self {
785            require,
786            field: field.to_string(),
787            mode: "code".to_string(),
788            title: title.to_string(),
789            def: default.to_string(),
790            length: length + dec.len(),
791            show: true,
792            describe: "".to_string(),
793            dec: dec.to_string(),
794            example: JsonValue::Null,
795        }
796    }
797    /// 生成X+20位订单号
798    pub fn order_no(prefix: &str) -> String {
799        let data = Datetime::datetime_format("%Y%m%d%H%M%S%f");
800        let data = data.trim_end_matches("000");
801        format!("{}{}", prefix, data)
802    }
803    /// 生成X+18位编号
804    pub fn code_no(prefix: &str) -> String {
805        let data = Datetime::datetime_format("%Y%m%d%H%M%f");
806        let data = data.trim_end_matches("000");
807        format!("{}{}", prefix, data)
808    }
809
810    /// 生成X+13位编号
811    pub fn code_no_13(prefix: &str) -> String {
812        let data = Datetime::datetime_format("%Y%m%d%H%f");
813        let data = data.trim_end_matches("00");
814        format!("{}{}", prefix, data)
815    }
816    /// 生成批次号X+10位编号
817    pub fn batch_no(prefix: &str) -> String {
818        let data = Datetime::datetime_format("%Y%m%d%H");
819        format!("{}{}", prefix, data)
820    }
821}
822
823impl Field for Code {
824    fn sql(&mut self, model: &str) -> String {
825        let dot = if model == "pgsql" {""} else {"`"};
826        let sql = format!(
827            "{dot}{}{dot} varchar({})  default '{}'",
828            self.field, self.length, self.def
829        );
830        match model {
831            "sqlite" => sql,
832            _ => format!(
833                "{} comment '{}|{}|{}|{}|{}|{}'",
834                sql.clone(),
835                self.mode,
836                self.require,
837                self.title,
838                self.length,
839                self.dec,
840                self.def
841            ),
842        }
843    }
844    fn hide(&mut self) -> &mut Self {
845        self.show = false;
846        self
847    }
848    fn describe(&mut self, text: &str) -> &mut Self {
849        self.describe = text.to_string();
850        self
851    }
852
853    fn field(&mut self) -> JsonValue {
854        let mut field = object! {};
855        field.insert("require", JsonValue::from(self.require)).unwrap();
856        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
857        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
858        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
859        field.insert("length", JsonValue::from(self.length)).unwrap();
860        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
861
862        field.insert("show", JsonValue::from(self.show)).unwrap();
863        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
864        field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
865        field.insert("example", self.example.clone()).unwrap();
866
867        field
868    }
869
870    fn swagger(&mut self) -> JsonValue {
871        object! {
872            "type": self.mode.clone(),
873            "example": self.example.clone(),
874        }
875    }
876    fn example(&mut self, data: JsonValue) -> &mut Self {
877        self.example = data.clone();
878        self
879    }
880}
881
882/// 条码
883pub struct BarCode {
884    /// 必填
885    pub require: bool,
886    /// 字段名
887    pub field: String,
888    /// 模式
889    pub mode: String,
890    /// 字段描述
891    pub title: String,
892    /// 默认值
893    pub def: String,
894    /// 字段总长度
895    pub length: usize,
896    /// 是否显示
897    pub show: bool,
898    /// 功能描述
899    pub describe: String,
900    pub example: JsonValue,
901}
902
903impl BarCode {
904    pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
905        Self {
906            require,
907            field: field.to_string(),
908            mode: "barcode".to_string(),
909            title: title.to_string(),
910            def: default.to_string(),
911            length: 20,
912            show: true,
913            describe: "".to_string(),
914            example: JsonValue::Null,
915        }
916    }
917    /// 生成条码
918    pub fn create_barcode(prefix: &str) -> String {
919        let data = Datetime::datetime_format("%Y%m%d%H");
920        format!("{}{}", prefix, data)
921    }
922}
923
924impl Field for BarCode {
925    fn sql(&mut self, model: &str) -> String {
926        let dot = if model == "pgsql" {""} else {"`"};
927        let sql = format!(
928            "{dot}{}{dot} varchar({})  default '{}'",
929            self.field, self.length, self.def
930        );
931        match model {
932            "sqlite" => sql,
933            _ => format!(
934                "{} comment '{}|{}|{}|{}|{}'",
935                sql.clone(),
936                self.mode,
937                self.require,
938                self.title,
939                self.length,
940                self.def
941            ),
942        }
943    }
944    fn hide(&mut self) -> &mut Self {
945        self.show = false;
946        self
947    }
948    fn describe(&mut self, text: &str) -> &mut Self {
949        self.describe = text.to_string();
950        self
951    }
952
953    fn field(&mut self) -> JsonValue {
954        let mut field = object! {};
955        field.insert("require", JsonValue::from(self.require)).unwrap();
956        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
957        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
958        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
959        field.insert("length", JsonValue::from(self.length)).unwrap();
960        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
961
962        field.insert("show", JsonValue::from(self.show)).unwrap();
963        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
964        field.insert("example", self.example.clone()).unwrap();
965        field
966    }
967
968    fn swagger(&mut self) -> JsonValue {
969        object! {
970            "type": self.mode.clone(),
971            "example": self.example.clone(),
972        }
973    }
974
975    fn example(&mut self, data: JsonValue) -> &mut Self {
976        self.example = data.clone();
977        self
978    }
979}
980
981/// QrCode 二维码
982pub struct QrCode {
983    /// 必填
984    pub require: bool,
985    /// 字段名
986    pub field: String,
987    /// 模式
988    pub mode: String,
989    /// 字段描述
990    pub title: String,
991    /// 默认值
992    pub def: String,
993    /// 字段总长度
994    pub length: usize,
995    /// 是否显示
996    pub show: bool,
997    /// 功能描述
998    pub describe: String,
999    pub example: JsonValue,
1000}
1001
1002impl QrCode {
1003    pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
1004        Self {
1005            require,
1006            field: field.to_string(),
1007            mode: "qrcode".to_string(),
1008            title: title.to_string(),
1009            def: default.to_string(),
1010            length,
1011            show: true,
1012            describe: "".to_string(),
1013            example: JsonValue::Null,
1014        }
1015    }
1016}
1017
1018impl Field for QrCode {
1019    fn sql(&mut self, model: &str) -> String {
1020        let dot = if model == "pgsql" {""} else {"`"};
1021        let sql = format!(
1022            "{dot}{}{dot} varchar({})  default '{}'",
1023            self.field, self.length, self.def
1024        );
1025        match model {
1026            "sqlite" => sql,
1027            _ => format!(
1028                "{} comment '{}|{}|{}|{}|{}'",
1029                sql.clone(),
1030                self.mode,
1031                self.require,
1032                self.title,
1033                self.length,
1034                self.def
1035            ),
1036        }
1037    }
1038    fn hide(&mut self) -> &mut Self {
1039        self.show = false;
1040        self
1041    }
1042    fn describe(&mut self, text: &str) -> &mut Self {
1043        self.describe = text.to_string();
1044        self
1045    }
1046
1047    fn field(&mut self) -> JsonValue {
1048        let mut field = object! {};
1049        field.insert("require", JsonValue::from(self.require)).unwrap();
1050        field.insert("field", JsonValue::from(self.field.clone())).unwrap();
1051        field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
1052        field.insert("title", JsonValue::from(self.title.clone())).unwrap();
1053        field.insert("length", JsonValue::from(self.length)).unwrap();
1054        field.insert("def", JsonValue::from(self.def.clone())).unwrap();
1055
1056        field.insert("show", JsonValue::from(self.show)).unwrap();
1057        field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
1058        field.insert("example", self.example.clone()).unwrap();
1059
1060        field
1061    }
1062
1063    fn swagger(&mut self) -> JsonValue {
1064        object! {
1065            "type": self.mode.clone(),
1066            "example": self.example.clone(),
1067        }
1068    }
1069
1070    fn example(&mut self, data: JsonValue) -> &mut Self {
1071        self.example = data.clone();
1072        self
1073    }
1074}