diff --git a/server/resource/plugin/server/model/model.go.template b/server/resource/plugin/server/model/model.go.template index 04837716..c2235512 100644 --- a/server/resource/plugin/server/model/model.go.template +++ b/server/resource/plugin/server/model/model.go.template @@ -2,23 +2,23 @@ // 在结构体中新增如下字段 {{- range .Fields}} {{- if eq .FieldType "enum" }} -{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};type:enum({{.DataTypeLong}});comment:{{.Comment}};" {{- if .Require }} binding:"required"{{- end -}}` +{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:enum({{.DataTypeLong}});" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "picture" }} -{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}` +{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "video" }} -{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}` +{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "file" }} -{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` +{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` {{- else if eq .FieldType "pictures" }} -{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` +{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` {{- else if eq .FieldType "richtext" }} -{{.FieldName}} *string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}type:text;" {{- if .Require }} binding:"required"{{- end -}}` +{{.FieldName}} *string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:text;" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "json" }} -{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"object"` +{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"object"` {{- else if eq .FieldType "array" }} -{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` +{{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` {{- else }} -{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}` +{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end -}}` {{- end }} {{ if .FieldDesc }}//{{.FieldDesc}}{{ end }} {{- end }} @@ -47,23 +47,23 @@ type {{.StructName}} struct { {{- end }} {{- range .Fields}} {{- if eq .FieldType "enum" }} - {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};type:enum({{.DataTypeLong}});comment:{{.Comment}};" {{- if .Require }} binding:"required"{{- end -}}` + {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:enum({{.DataTypeLong}});" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "picture" }} - {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}` + {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "video" }} - {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}` + {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "file" }} - {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` + {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` {{- else if eq .FieldType "pictures" }} - {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` + {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` {{- else if eq .FieldType "richtext" }} - {{.FieldName}} *string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}type:text;" {{- if .Require }} binding:"required"{{- end -}}` + {{.FieldName}} *string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:text;" {{- if .Require }} binding:"required"{{- end -}}` {{- else if eq .FieldType "json" }} - {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"object"` + {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"object"` {{- else if eq .FieldType "array" }} - {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` + {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};type:text;" {{- if .Require }} binding:"required"{{- end }} swaggertype:"array,object"` {{- else }} - {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if ne .FieldIndexType "" -}}{{ .FieldIndexType }};{{- end -}}{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}` + {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{ formatGormTag .FieldIndexType .PrimaryKey .DefaultValue .ColumnName .Comment .DataTypeLong }};" {{- if .Require }} binding:"required"{{- end -}}` {{- end }} {{ if .FieldDesc }}//{{.FieldDesc}}{{ end }} {{- end }} {{- if .AutoCreateResource }} diff --git a/server/resource/plugin/server/model/request/request.go.template b/server/resource/plugin/server/model/request/request.go.template index 2100a641..3d09ce69 100644 --- a/server/resource/plugin/server/model/request/request.go.template +++ b/server/resource/plugin/server/model/request/request.go.template @@ -3,13 +3,13 @@ {{- range .Fields}} {{- if ne .FieldSearchType ""}} {{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} -Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"` -End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"` +Start{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "start" .FieldName) false }}` +End{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "end" .FieldName) false }}` {{- else }} {{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }} -{{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" ` +{{.FieldName}} string `{{ formatFieldTag .FieldJson false }}` {{- else }} -{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" ` +{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag .FieldJson false }}` {{- end }} {{- end }} {{- end}} @@ -36,13 +36,13 @@ type {{.StructName}}Search struct{ {{- range .Fields}} {{- if ne .FieldSearchType ""}} {{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} - Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"` - End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"` + Start{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "start" .FieldName) false }}` + End{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "end" .FieldName) false }}` {{- else }} {{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }} - {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" ` + {{.FieldName}} string `{{ formatFieldTag .FieldJson false }}` {{- else }} - {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" ` + {{.FieldName}} *{{.FieldType}} `{{ formatFieldTag .FieldJson false }}` {{- end }} {{- end }} {{- end}} diff --git a/server/resource/plugin/server/service/service.go.template b/server/resource/plugin/server/service/service.go.template index 6d11da1d..f3c71ee0 100644 --- a/server/resource/plugin/server/service/service.go.template +++ b/server/resource/plugin/server/service/service.go.template @@ -11,21 +11,11 @@ {{- range .Fields}} {{- if .FieldSearchType}} {{- if or (eq .FieldType "enum") (eq .FieldType "pictures") (eq .FieldType "picture") (eq .FieldType "video") (eq .FieldType "json") }} -if info.{{.FieldName}} != "" { - {{- if or (eq .FieldType "enum") }} - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}*info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) - {{- else}} -// 数据类型为复杂类型,请根据业务需求自行实现复杂类型的查询业务 - {{- end}} -} - {{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} -if info.Start{{.FieldName}} != nil && info.End{{.FieldName}} != nil { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ? AND ? ",info.Start{{.FieldName}},info.End{{.FieldName}}) -} - {{- else}} -if info.{{.FieldName}} != nil{{- if eq .FieldType "string" }} && *info.{{.FieldName}} != ""{{- end }} { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}*info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) -} +{{ formatSearchCondition .ColumnName .FieldSearchType .FieldName .FieldType }} + {{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} +{{ formatBetweenCondition .ColumnName .FieldSearchType .FieldName }} + {{- else}} +{{ formatSearchCondition .ColumnName .FieldSearchType .FieldName .FieldType }} {{- end }} {{- end }} {{- end }} @@ -190,19 +180,15 @@ func (s *{{.Abbreviation}}) Get{{.StructName}}InfoList(ctx context.Context, info {{- if or (eq .FieldType "enum") (eq .FieldType "pictures") (eq .FieldType "picture") (eq .FieldType "video") (eq .FieldType "json") }} if info.{{.FieldName}} != "" { {{- if or (eq .FieldType "enum")}} - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}*info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) + {{ formatLikeCondition .ColumnName .FieldName }} {{- else}} // 数据类型为复杂类型,请根据业务需求自行实现复杂类型的查询业务 {{- end}} } {{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} - if info.Start{{.FieldName}} != nil && info.End{{.FieldName}} != nil { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ? AND ? ",info.Start{{.FieldName}},info.End{{.FieldName}}) - } + {{ formatBetweenCondition .ColumnName .FieldSearchType .FieldName }} {{- else}} - if info.{{.FieldName}} != nil{{- if eq .FieldType "string" }} && *info.{{.FieldName}} != ""{{- end }} { - db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}*info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }}) - } + {{ formatSearchCondition .ColumnName .FieldSearchType .FieldName .FieldType }} {{- end }} {{- end }} {{- end }} diff --git a/server/service/system/auto_code_package.go b/server/service/system/auto_code_package.go index fbd9eb89..653980d3 100644 --- a/server/service/system/auto_code_package.go +++ b/server/service/system/auto_code_package.go @@ -3,12 +3,6 @@ package system import ( "context" "fmt" - "go/token" - "os" - "path/filepath" - "strings" - "text/template" - "github.com/flipped-aurora/gin-vue-admin/server/global" common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" model "github.com/flipped-aurora/gin-vue-admin/server/model/system" @@ -16,7 +10,12 @@ import ( "github.com/flipped-aurora/gin-vue-admin/server/utils" "github.com/flipped-aurora/gin-vue-admin/server/utils/ast" "github.com/pkg/errors" + "go/token" "gorm.io/gorm" + "os" + "path/filepath" + "strings" + "text/template" ) var AutoCodePackage = new(autoCodePackage) @@ -59,7 +58,7 @@ func (s *autoCodePackage) Create(ctx context.Context, info *request.SysAutoCodeP } for key, value := range creates { // key 为 模版绝对路径 var files *template.Template - files, err = template.ParseFiles(key) + files, err = template.New(filepath.Base(key)).Funcs(utils.GetTemplateFuncMap()).ParseFiles(key) if err != nil { return errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", key) } diff --git a/server/service/system/auto_code_template.go b/server/service/system/auto_code_template.go index 39e6f229..a72305f9 100644 --- a/server/service/system/auto_code_template.go +++ b/server/service/system/auto_code_template.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/flipped-aurora/gin-vue-admin/server/utils" "go/ast" "go/format" "go/parser" @@ -224,7 +225,7 @@ func (s *autoCodeTemplate) generate(ctx context.Context, info request.AutoCode, code := make(map[string]strings.Builder) for key, create := range templates { var files *template.Template - files, err = template.ParseFiles(key) + files, err = template.New(filepath.Base(key)).Funcs(utils.GetTemplateFuncMap()).ParseFiles(key) if err != nil { return nil, nil, nil, errors.Wrapf(err, "[filpath:%s]读取模版文件失败!", key) } @@ -322,7 +323,7 @@ func (s *autoCodeTemplate) GetApiAndServer(info request.AutoFunc) (map[string]st func (s *autoCodeTemplate) getTemplateStr(t string, info request.AutoFunc) (string, error) { tempPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", "function", t+".tpl") - files, err := template.ParseFiles(tempPath) + files, err := template.New(filepath.Base(tempPath)).Funcs(utils.GetTemplateFuncMap()).ParseFiles(tempPath) if err != nil { return "", errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", tempPath) } diff --git a/server/utils/template_funcs.go b/server/utils/template_funcs.go new file mode 100644 index 00000000..6c08f749 --- /dev/null +++ b/server/utils/template_funcs.go @@ -0,0 +1,109 @@ +package utils + +import ( + "strings" + "text/template" +) + +// GetTemplateFuncMap 返回模板函数映射,用于在模板中使用 +func GetTemplateFuncMap() template.FuncMap { + return template.FuncMap{ + "formatGormTag": FormatGormTag, + "formatFieldTag": FormatFieldTag, + "formatSearchCondition": FormatSearchCondition, + "formatBetweenCondition": FormatBetweenCondition, + "formatLikeCondition": FormatLikeCondition, + "formatModelField": FormatModelField, + "formatRequestField": FormatRequestField, + "indent": Indent, + } +} + +// FormatGormTag 格式化GORM标签 +func FormatGormTag(fieldIndexType, primaryKey, defaultValue, columnName, comment, dataTypeLong string) string { + var tags []string + + if fieldIndexType != "" { + tags = append(tags, fieldIndexType) + } + if primaryKey == "true" { + tags = append(tags, "primarykey") + } + if defaultValue != "" { + tags = append(tags, "default:"+defaultValue) + } + tags = append(tags, "column:"+columnName) + if comment != "" { + tags = append(tags, "comment:"+comment) + } + if dataTypeLong != "" { + tags = append(tags, "size:"+dataTypeLong) + } + + return strings.Join(tags, ";") +} + +// FormatFieldTag 格式化字段标签 +func FormatFieldTag(fieldJson string, required bool) string { + result := `json:"` + fieldJson + `" form:"` + fieldJson + `"` + if required { + result += ` binding:"required"` + } + return result +} + +// FormatSearchCondition 格式化搜索条件 +func FormatSearchCondition(columnName, fieldSearchType, fieldName, fieldType string) string { + var condition string + if fieldType == "string" { + condition = `if info.` + fieldName + ` != nil && *info.` + fieldName + ` != "" {` + } else { + condition = `if info.` + fieldName + ` != nil {` + } + + var whereClause string + if fieldSearchType == "LIKE" { + whereClause = `db = db.Where("` + columnName + ` ` + fieldSearchType + ` ?","%"+*info.` + fieldName + `+"%")` + } else { + whereClause = `db = db.Where("` + columnName + ` ` + fieldSearchType + ` ?",*info.` + fieldName + `)` + } + + return condition + "\n " + whereClause + "\n}" +} + +// FormatBetweenCondition 格式化BETWEEN条件 +func FormatBetweenCondition(columnName, fieldSearchType, fieldName string) string { + return `if info.Start` + fieldName + ` != nil && info.End` + fieldName + ` != nil {` + "\n" + + ` db = db.Where("` + columnName + ` ` + fieldSearchType + ` ? AND ? ",info.Start` + fieldName + `,info.End` + fieldName + `)` + "\n" + + `}` +} + +// FormatLikeCondition 格式化LIKE条件 +func FormatLikeCondition(columnName, fieldName string) string { + return `db = db.Where("` + columnName + ` LIKE ?","%"+ *info.` + fieldName + `+"%")` +} + +// FormatModelField 格式化模型字段 +func FormatModelField(fieldName, fieldType, fieldJson, gormTag string, fieldDesc string) string { + result := fieldName + " " + fieldType + " `" + fieldJson + " " + gormTag + "`" + if fieldDesc != "" { + result += " //" + fieldDesc + } + return result +} + +// FormatRequestField 格式化请求字段 +func FormatRequestField(fieldName, fieldType, fieldJson string) string { + return fieldName + " " + fieldType + " `json:\"" + fieldJson + "\" form:\"" + fieldJson + "\" `" +} + +// Indent 缩进文本 +func Indent(text string, indent string) string { + lines := strings.Split(text, "\n") + for i, line := range lines { + if line != "" { + lines[i] = indent + line + } + } + return strings.Join(lines, "\n") +}