model.go.tpl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // 自动生成模板{{.StructName}}
  2. package {{.Package}}
  3. import (
  4. {{ if .GvaModel }}"github.com/flipped-aurora/gin-vue-admin/server/global"{{ end }}
  5. {{ if or .HasTimer }}"time"{{ end }}
  6. {{ if .NeedJSON }}"gorm.io/datatypes"{{ end }}
  7. )
  8. // {{.Description}} 结构体 {{.StructName}}
  9. type {{.StructName}} struct {
  10. {{ if .GvaModel }} global.GVA_MODEL {{ end }}
  11. {{- range .Fields}}
  12. {{- if eq .FieldType "enum" }}
  13. {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};type:enum({{.DataTypeLong}});comment:{{.Comment}};" {{- if .Require }} binding:"required"{{- end -}}`
  14. {{- else if eq .FieldType "picture" }}
  15. {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}`
  16. {{- else if eq .FieldType "video" }}
  17. {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}`
  18. {{- else if eq .FieldType "file" }}
  19. {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}`
  20. {{- else if eq .FieldType "pictures" }}
  21. {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}`
  22. {{- else if eq .FieldType "richtext" }}
  23. {{.FieldName}} string `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- 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 -}}`
  24. {{- else if eq .FieldType "json" }}
  25. {{.FieldName}} datatypes.JSON `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- 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 -}}`
  26. {{- else if ne .FieldType "string" }}
  27. {{.FieldName}} *{{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}`
  28. {{- else }}
  29. {{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}" form:"{{.FieldJson}}" gorm:"{{- if .PrimaryKey -}}primarykey;{{- end -}}{{- if .DefaultValue -}}default:{{ .DefaultValue }};{{- end -}}column:{{.ColumnName}};comment:{{.Comment}};{{- if .DataTypeLong -}}size:{{.DataTypeLong}};{{- end -}}" {{- if .Require }} binding:"required"{{- end -}}`
  30. {{- end }} {{ if .FieldDesc }}//{{.FieldDesc}} {{ end }} {{- end }}
  31. {{- if .AutoCreateResource }}
  32. CreatedBy uint `gorm:"column:created_by;comment:创建者"`
  33. UpdatedBy uint `gorm:"column:updated_by;comment:更新者"`
  34. DeletedBy uint `gorm:"column:deleted_by;comment:删除者"`
  35. {{- end}}
  36. }
  37. {{ if .TableName }}
  38. // TableName {{.Description}} {{.StructName}}自定义表名 {{.TableName}}
  39. func ({{.StructName}}) TableName() string {
  40. return "{{.TableName}}"
  41. }
  42. {{ end }}