package util
import (
"strings"
"text/template"
)
var FuncMap = template.FuncMap{
"toGoType": ToGoType,
"formatJsonName": FormatJsonName,
"formatSqlToVueType": formatSqlToVueType,
"smallHump": SmallHump,
"bigHump": BigHump,
"queryType": QueryType,
"ElDialog": ElDialog,
"ElFrom": ElFrom,
"ElTableColumn": ElTableColumn,
"ToLower": strings.ToLower,
}
// ToGoType sql类型转go类型
func ToGoType(sqlType string) string {
switch sqlType {
case "varchar":
return "string"
case "VARCHAR":
return "string"
case "CHAR":
return "string"
case "char":
return "string"
case "INT":
return "int64"
case "int":
return "int64"
case "decimal":
return "float64"
case "datetime":
return "time.Time"
case "longblob":
return "string"
default:
println("未找到类型", sqlType)
return "string"
}
}
func FormatJsonName(sqlName string) string {
split := strings.Split(sqlName, "_")
var str = split[0]
for i := 1; i < len(split); i++ {
str += strings.Title(split[i])
}
return str
}
func formatSqlToVueType(str string) string {
str = strings.ToLower(str)
switch str {
case "varchar":
return "string"
case "char":
return "string"
case "int":
return "number"
case "decimal":
return "number"
case "datetime":
return "string"
case "longblob":
return "string"
default:
panic("未定义的类型:" + str)
}
}
func BigHump(str string) string {
split := strings.Split(str, "_")
if len(split) == 1 {
return strings.Title(str)
}
res := strings.Title(split[0])
for i := 1; i < len(split); i++ {
res += strings.Title(split[i])
}
return res
}
func SmallHump(str string) string {
split := strings.Split(str, "_")
if len(split) == 1 {
return strings.ToLower(str[0:1]) + str[1:]
}
res := strings.ToLower(split[0][0:1]) + strings.ToLower(split[0][1:])
for i := 1; i < len(split); i++ {
res += strings.ToUpper(split[i][0:1]) + strings.ToLower(split[i][1:])
}
return res
}
func QueryType(GoType, GoField, QueryType, ColumnName, ColumnComment string) string {
if GoType == "int" || GoType == "int64" || GoType == "float64" {
if QueryType == "BETWEEN" {
return GoField + "Start " + GoType + " `xorm:\"" + ColumnName + "\" json:\"" + SmallHump(ColumnName) + "\"` // " + ColumnComment + "\n\t" +
GoField + "End " + GoType + " `xorm:\"" + ColumnName + "\" json:\"" + SmallHump(ColumnName) + "\"` // " + ColumnComment
} else {
return GoField + " " + GoType + " `xorm:\"" + ColumnName + "\" json:\"" + SmallHump(ColumnName) + "\"` // " + ColumnComment
}
}
if GoType == "time.Time" {
if QueryType == "BETWEEN" {
return GoField + "Start string `xorm:\"" + ColumnName + "\" json:\"" + SmallHump(ColumnName) + "\"` // " + ColumnComment + "\n\t" +
GoField + "End string `xorm:\"" + ColumnName + "\" json:" + SmallHump(ColumnName) + "\"` // " + ColumnComment
} else {
return GoField + " string `xorm:\"" + ColumnName + "\" json:\"" + SmallHump(ColumnName) + "\"` // " + ColumnComment
}
}
return GoField + " string `xorm:\"" + ColumnName + "\" json:\"" + SmallHump(ColumnName) + "\"` // " + ColumnComment
}
func ElFrom(name, value, typeName, dictType string) string {
typeName = strings.ToLower(typeName)
if typeName == "input" {
return "\n " +
"\n " +
"\n "
} else if typeName == "number" {
return "\n " +
"\n " +
"\n "
} else if typeName == "textarea" {
return "\n " +
"\n " +
"\n "
} else if typeName == "select" {
return "\n " +
"\n " +
"\n " +
"\n " +
"\n "
} else if typeName == "radio" {
if dictType == "" {
return "\n " +
" " +
"\n " +
"\n {{ item[1] }}" +
"\n " +
"\n " +
"\n "
} else {
return "\n " +
"\n " +
"\n " +
"\n {{item.dictLabel}}" +
"\n " +
"\n " +
"\n "
}
} else if typeName == "checkbox" {
return "\n " +
"\n " +
"\n " +
"\n " +
"\n "
} else if typeName == "time" {
return "\n " +
" " +
"\n "
} else if typeName == "image" {
return "\n " +
" " +
"\n "
} else if typeName == "file" {
} else if typeName == "editor" {
return "\n " +
" " +
"\n "
} else if typeName == "text" {
return "\n " +
" {{query." + value + "}}" +
"\n "
}
//默认的就显示文本
return "\n " +
" {{query." + value + "}}" +
"\n "
}
func ElTableColumn(name, value, typeName string, dictType string) string {
if typeName == "input" || typeName == "textarea" || typeName == "select" || typeName == "radio" ||
typeName == "checkbox" || typeName == "time" || typeName == "text" {
if dictType == "" {
return "\n "
} else {
return "\n " +
"\n " +
"\n " +
"\n " +
"\n "
}
}
if typeName == "image" {
return "\n " +
"\n " +
"\n " +
"\n " +
"\n "
}
return "\n "
}
func ElDialog(name, value, typeName, dictType string) string {
typeName = strings.ToLower(typeName)
if typeName == "input" {
return "\n " +
"\n " +
"\n "
} else if typeName == "number" {
return "\n " +
"\n " +
"\n "
} else if typeName == "textarea" {
return "\n " +
"\n " +
"\n "
} else if typeName == "select" {
return "\n " +
"\n " +
"\n " +
"\n " +
"\n "
} else if typeName == "radio" {
if dictType == "" {
return "\n " +
" " +
"\n " +
"\n {{ item[1] }}" +
"\n " +
"\n " +
"\n "
} else {
return "\n " +
"\n " +
"\n " +
"\n {{item.dictLabel}}" +
"\n " +
"\n " +
"\n "
}
} else if typeName == "checkbox" {
return "\n " +
"\n " +
"\n " +
"\n " +
"\n "
} else if typeName == "time" {
return "\n " +
" " +
"\n "
} else if typeName == "image" {
return "\n " +
" " +
"\n "
} else if typeName == "file" {
} else if typeName == "editor" {
return "\n " +
" " +
"\n "
} else if typeName == "text" {
return "\n " +
" {{dialogForm." + value + "}}" +
"\n "
}
//默认的就显示文本
return "\n " +
" {{dialogForm." + value + "}}" +
"\n "
}