feat: 模板方法抽离 不在模板中做逻辑

This commit is contained in:
piexlMax(奇淼
2025-04-08 18:45:41 +08:00
parent 8f7a8a9e6d
commit 0ab15cc5e2
6 changed files with 152 additions and 57 deletions

View File

@@ -2,23 +2,23 @@
// 在结构体中新增如下字段 // 在结构体中新增如下字段
{{- range .Fields}} {{- range .Fields}}
{{- if eq .FieldType "enum" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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 }} {{- 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 .FieldDesc }}//{{.FieldDesc}}{{ end }}
{{- end }} {{- end }}
@@ -47,23 +47,23 @@ type {{.StructName}} struct {
{{- end }} {{- end }}
{{- range .Fields}} {{- range .Fields}}
{{- if eq .FieldType "enum" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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" }} {{- 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 }} {{- 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 .FieldDesc }}//{{.FieldDesc}}{{ end }}
{{- end }} {{- end }}
{{- if .AutoCreateResource }} {{- if .AutoCreateResource }}

View File

@@ -3,13 +3,13 @@
{{- range .Fields}} {{- range .Fields}}
{{- if ne .FieldSearchType ""}} {{- if ne .FieldSearchType ""}}
{{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} {{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"` Start{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "start" .FieldName) false }}`
End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"` End{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "end" .FieldName) false }}`
{{- else }} {{- else }}
{{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }} {{- 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 }} {{- else }}
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" ` {{.FieldName}} *{{.FieldType}} `{{ formatFieldTag .FieldJson false }}`
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end}} {{- end}}
@@ -36,13 +36,13 @@ type {{.StructName}}Search struct{
{{- range .Fields}} {{- range .Fields}}
{{- if ne .FieldSearchType ""}} {{- if ne .FieldSearchType ""}}
{{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} {{- if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
Start{{.FieldName}} *{{.FieldType}} `json:"start{{.FieldName}}" form:"start{{.FieldName}}"` Start{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "start" .FieldName) false }}`
End{{.FieldName}} *{{.FieldType}} `json:"end{{.FieldName}}" form:"end{{.FieldName}}"` End{{.FieldName}} *{{.FieldType}} `{{ formatFieldTag (print "end" .FieldName) false }}`
{{- else }} {{- else }}
{{- if or (eq .FieldType "enum") (eq .FieldType "picture") (eq .FieldType "pictures") (eq .FieldType "video") (eq .FieldType "json") }} {{- 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 }} {{- else }}
{{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" ` {{.FieldName}} *{{.FieldType}} `{{ formatFieldTag .FieldJson false }}`
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end}} {{- end}}

View File

@@ -11,21 +11,11 @@
{{- range .Fields}} {{- range .Fields}}
{{- if .FieldSearchType}} {{- if .FieldSearchType}}
{{- if or (eq .FieldType "enum") (eq .FieldType "pictures") (eq .FieldType "picture") (eq .FieldType "video") (eq .FieldType "json") }} {{- if or (eq .FieldType "enum") (eq .FieldType "pictures") (eq .FieldType "picture") (eq .FieldType "video") (eq .FieldType "json") }}
if info.{{.FieldName}} != "" { {{ formatSearchCondition .ColumnName .FieldSearchType .FieldName .FieldType }}
{{- 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"}} {{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
if info.Start{{.FieldName}} != nil && info.End{{.FieldName}} != nil { {{ formatBetweenCondition .ColumnName .FieldSearchType .FieldName }}
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ? AND ? ",info.Start{{.FieldName}},info.End{{.FieldName}})
}
{{- else}} {{- else}}
if info.{{.FieldName}} != nil{{- if eq .FieldType "string" }} && *info.{{.FieldName}} != ""{{- end }} { {{ formatSearchCondition .ColumnName .FieldSearchType .FieldName .FieldType }}
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}*info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- end }} {{- end }}
{{- end }} {{- 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 or (eq .FieldType "enum") (eq .FieldType "pictures") (eq .FieldType "picture") (eq .FieldType "video") (eq .FieldType "json") }}
if info.{{.FieldName}} != "" { if info.{{.FieldName}} != "" {
{{- if or (eq .FieldType "enum")}} {{- 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}} {{- else}}
// 数据类型为复杂类型,请根据业务需求自行实现复杂类型的查询业务 // 数据类型为复杂类型,请根据业务需求自行实现复杂类型的查询业务
{{- end}} {{- end}}
} }
{{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}} {{- else if eq .FieldSearchType "BETWEEN" "NOT BETWEEN"}}
if info.Start{{.FieldName}} != nil && info.End{{.FieldName}} != nil { {{ formatBetweenCondition .ColumnName .FieldSearchType .FieldName }}
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ? AND ? ",info.Start{{.FieldName}},info.End{{.FieldName}})
}
{{- else}} {{- else}}
if info.{{.FieldName}} != nil{{- if eq .FieldType "string" }} && *info.{{.FieldName}} != ""{{- end }} { {{ formatSearchCondition .ColumnName .FieldSearchType .FieldName .FieldType }}
db = db.Where("{{.ColumnName}} {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}*info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end }} {{- end }}

View File

@@ -3,12 +3,6 @@ package system
import ( import (
"context" "context"
"fmt" "fmt"
"go/token"
"os"
"path/filepath"
"strings"
"text/template"
"github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/global"
common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request" common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
model "github.com/flipped-aurora/gin-vue-admin/server/model/system" 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"
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast" "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
"github.com/pkg/errors" "github.com/pkg/errors"
"go/token"
"gorm.io/gorm" "gorm.io/gorm"
"os"
"path/filepath"
"strings"
"text/template"
) )
var AutoCodePackage = new(autoCodePackage) var AutoCodePackage = new(autoCodePackage)
@@ -59,7 +58,7 @@ func (s *autoCodePackage) Create(ctx context.Context, info *request.SysAutoCodeP
} }
for key, value := range creates { // key 为 模版绝对路径 for key, value := range creates { // key 为 模版绝对路径
var files *template.Template var files *template.Template
files, err = template.ParseFiles(key) files, err = template.New(filepath.Base(key)).Funcs(utils.GetTemplateFuncMap()).ParseFiles(key)
if err != nil { if err != nil {
return errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", key) return errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", key)
} }

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/flipped-aurora/gin-vue-admin/server/utils"
"go/ast" "go/ast"
"go/format" "go/format"
"go/parser" "go/parser"
@@ -224,7 +225,7 @@ func (s *autoCodeTemplate) generate(ctx context.Context, info request.AutoCode,
code := make(map[string]strings.Builder) code := make(map[string]strings.Builder)
for key, create := range templates { for key, create := range templates {
var files *template.Template var files *template.Template
files, err = template.ParseFiles(key) files, err = template.New(filepath.Base(key)).Funcs(utils.GetTemplateFuncMap()).ParseFiles(key)
if err != nil { if err != nil {
return nil, nil, nil, errors.Wrapf(err, "[filpath:%s]读取模版文件失败!", key) 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) { 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") 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 { if err != nil {
return "", errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", tempPath) return "", errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", tempPath)
} }

View File

@@ -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")
}