Files
go-admin/template/v2/dto.go.template
T
2020-09-14 20:02:55 +08:00

110 lines
2.9 KiB
Plaintext

package dto
import (
"github.com/gin-gonic/gin"
"go-admin/app/admin/models"
"go-admin/common/dto"
common "go-admin/common/models"
"gorm.io/gorm"
)
type {{.ClassName}}Search struct {
dto.Pagination `search:"-"`
{{ $tablename := .TBName -}}
{{ range .Columns -}}
{{$z := .IsQuery}}
{{- if ($z) -}}
{{.GoField}} {{.GoType}} `form:"{{.JsonField}}" search:"type:{{if eq .QueryType "EQ"}}exact{{ else if eq .QueryType "NE"}}iexact{{ else if eq .QueryType "LIKE"}}contains{{ else if eq .QueryType "GT"}}gt{{ else if eq .QueryType "GTE"}}gte{{ else if eq .QueryType "LT"}}lt{{ else if eq .QueryType "LTE"}}lte{{- end }};column:{{.ColumnName}};table:{{$tablename}}" comment:"{{.ColumnComment}}"`
{{ end -}}
{{- end }}
}
func (m *{{.ClassName}}Search) GetNeedSearch() interface{} {
return *m
}
func (m *{{.ClassName}}Search) Bind(ctx *gin.Context) error {
return ctx.Bind(m)
}
func (m *{{.ClassName}}Search) Generate() dto.Index {
o := *m
return &o
}
func (m *{{.ClassName}}Search) GetPageIndex() int {
return m.PageIndex
}
func (m *{{.ClassName}}Search) GetPageSize() int {
return m.PageSize
}
type {{.ClassName}}Control struct {
{{ range .Columns -}}
{{$x := .Pk}}
{{- if ($x) }}
{{.GoField}} uint `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}}
{{- else if eq .GoField "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}}
{{- else if eq .GoField "DeletedAt" -}}
{{- else if eq .GoField "CreateBy" -}}
{{.GoField}} uint `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{- else if eq .GoField "UpdateBy" -}}
{{.GoField}} uint `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
{{- else }}
{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"` {{end -}}
{{- end }}
}
func (s *{{.ClassName}}Control) Bind(ctx *gin.Context) error {
return ctx.Bind(s)
}
func (s *{{.ClassName}}Control) Generate() dto.Control {
cp := *s
return &cp
}
func (s *{{.ClassName}}Control) GenerateM() (common.ActiveRecord, error) {
return &models.{{.ClassName}}{
{{ range .Columns -}}
{{$x := .Pk}}
{{- if ($x) }}
Model: gorm.Model{ID: s.Id},
{{- else if eq .GoField "CreatedAt" -}}
{{- else if eq .GoField "UpdatedAt" -}}
{{- else if eq .GoField "DeletedAt" -}}
{{- else if eq .GoField "CreateBy" -}}
ControlBy: common.ControlBy{
CreateBy: s.CreateBy,
UpdateBy: s.UpdateBy,
},
{{- else if eq .GoField "UpdateBy" -}}
{{- else }}
{{.GoField}}: s.{{.GoField}},
{{- end }}
{{- end }}
}, nil
}
func (s *{{.ClassName}}Control) GetId() interface{} {
return s.{{.PkGoField}}
}
type {{.ClassName}}ById struct {
dto.ObjectById
}
func (s *{{.ClassName}}ById) Generate() dto.Control {
cp := *s
return &cp
}
func (s *{{.ClassName}}ById) GenerateM() (common.ActiveRecord, error) {
return &models.{{.ClassName}}{}, nil
}