use json::{JsonValue, object};
use crate::{Field};
use crate::datetime::Datetime;
pub struct Str {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
}
impl Str {
pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Str {
Self {
require,
field: field.to_string(),
mode: "string".to_string(),
title: title.to_string(),
def: default.to_string(),
length,
show: true,
describe: "".to_string(),
}
}
}
impl Field for Str {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Pass {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
}
impl Pass {
pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "pass".to_string(),
title: title.to_string(),
def: default.to_string(),
length,
show: true,
describe: "".to_string(),
}
}
}
impl Field for Pass {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Key {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub length: usize,
pub def: String,
pub auto: bool,
pub show: bool,
pub describe: String,
}
impl Key {
pub fn new(require: bool, field: &str, title: &str, length: usize) -> Self {
Self {
require,
field: field.to_string(),
mode: "key".to_string(),
title: title.to_string(),
length,
auto: false,
show: true,
def: "".to_string(),
describe: "".to_string(),
}
}
pub fn auto(mut self, auto: bool) -> Self {
if auto {
self.length = 0;
}
self.auto = auto;
self
}
}
impl Field for Key {
fn sql(&mut self, model: &str) -> String {
match model {
"sqlite" => {
match self.auto {
true => {
let sql = format!("`{}` INTEGER PRIMARY KEY", self.field);
format!("{} AUTOINCREMENT", sql.clone())
}
false => {
format!("`{}` varchar({}) PRIMARY KEY", self.field, self.length)
}
}
}
_ => {
match self.auto {
true => {
let sql = format!("`{}` int AUTO_INCREMENT", self.field);
format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
}
false => {
let mut sql = format!("`{}` varchar({}) ", self.field, self.length);
sql = format!("{}", sql.clone());
format!("{} comment '{}|{}|{}'", sql.clone(), self.mode, self.title, self.length)
}
}
}
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("auto", JsonValue::from(self.auto.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Tel {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
}
impl Tel {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "tel".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 15,
show: true,
describe: "".to_string(),
}
}
}
impl Field for Tel {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Email {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
}
impl Email {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "email".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 100,
show: true,
describe: "".to_string(),
}
}
}
impl Field for Email {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn verify(&mut self, data: JsonValue) -> JsonValue {
data
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Color {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: i32,
pub show: bool,
pub describe: String,
}
impl Color {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "color".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 9,
show: true,
describe: "".to_string(),
}
}
}
impl Field for Color {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn verify(&mut self, data: JsonValue) -> JsonValue {
data
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct Code {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
pub dec: String,
}
impl Code {
pub fn new(require: bool, field: &str, title: &str, length: usize, dec: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "code".to_string(),
title: title.to_string(),
def: default.to_string(),
length: length + dec.len(),
show: true,
describe: "".to_string(),
dec: dec.to_string(),
}
}
pub fn order_no(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H%M%S%f");
let data = data.trim_end_matches("000");
format!("{}{}", prefix, data)
}
pub fn code_no(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H%M%f");
let data = data.trim_end_matches("000");
format!("{}{}", prefix, data)
}
pub fn batch_no(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H");
format!("{}{}", prefix, data)
}
}
impl Field for Code {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.dec, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field.insert("dec", JsonValue::from(self.dec.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct BarCode {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
}
impl BarCode {
pub fn new(require: bool, field: &str, title: &str, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "barcode".to_string(),
title: title.to_string(),
def: default.to_string(),
length: 20,
show: true,
describe: "".to_string(),
}
}
pub fn create_barcode(prefix: &str) -> String {
let data = Datetime::datetime_format("%Y%m%d%H");
format!("{}{}", prefix, data)
}
}
impl Field for BarCode {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}
pub struct QrCode {
pub require: bool,
pub field: String,
pub mode: String,
pub title: String,
pub def: String,
pub length: usize,
pub show: bool,
pub describe: String,
}
impl QrCode {
pub fn new(require: bool, field: &str, title: &str, length: usize, default: &str) -> Self {
Self {
require,
field: field.to_string(),
mode: "qrcode".to_string(),
title: title.to_string(),
def: default.to_string(),
length: length.clone(),
show: true,
describe: "".to_string(),
}
}
}
impl Field for QrCode {
fn sql(&mut self, model: &str) -> String {
let sql = format!("`{}` varchar({}) default '{}'", self.field, self.length, self.def);
match model {
"sqlite" => sql,
_ => format!("{} comment '{}|{}|{}|{}|{}'", sql.clone(), self.mode, self.require, self.title, self.length, self.def)
}
}
fn field(&mut self) -> JsonValue {
let mut field = object! {};
field.insert("require", JsonValue::from(self.require.clone())).unwrap();
field.insert("field", JsonValue::from(self.field.clone())).unwrap();
field.insert("mode", JsonValue::from(self.mode.clone())).unwrap();
field.insert("title", JsonValue::from(self.title.clone())).unwrap();
field.insert("length", JsonValue::from(self.length.clone())).unwrap();
field.insert("def", JsonValue::from(self.def.clone())).unwrap();
field.insert("show", JsonValue::from(self.show.clone())).unwrap();
field.insert("describe", JsonValue::from(self.describe.clone())).unwrap();
field
}
fn hide(&mut self) -> &mut Self {
self.show = false;
self
}
fn describe(&mut self, text: &str) -> &mut Self {
self.describe = text.to_string();
self
}
}