model.go.tpl 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. {{- if .IsAdd}}
  2. // 在结构体中新增如下字段
  3. {{- range .Fields}}
  4. {{ GenerateField . }}
  5. {{- end }}
  6. {{ else }}
  7. // 自动生成模板{{.StructName}}
  8. package {{.Package}}
  9. {{- if not .OnlyTemplate}}
  10. import (
  11. {{- if .GvaModel }}
  12. "{{.Module}}/global"
  13. {{- end }}
  14. {{- if or .HasTimer }}
  15. "time"
  16. {{- end }}
  17. {{- if .NeedJSON }}
  18. "gorm.io/datatypes"
  19. {{- end }}
  20. )
  21. {{- end }}
  22. // {{.Description}} 结构体 {{.StructName}}
  23. type {{.StructName}} struct {
  24. {{- if not .OnlyTemplate}}
  25. {{- if .GvaModel }}
  26. global.GVA_MODEL
  27. {{- end }}
  28. {{- range .Fields}}
  29. {{ GenerateField . }}
  30. {{- 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. {{- if .IsTree }}
  37. Children []*{{.StructName}} `json:"children" gorm:"-"` //子节点
  38. ParentID int `json:"parentID" gorm:"column:parent_id;comment:父节点"`
  39. {{- end }}
  40. {{- end }}
  41. }
  42. {{ if .TableName }}
  43. // TableName {{.Description}} {{.StructName}}自定义表名 {{.TableName}}
  44. func ({{.StructName}}) TableName() string {
  45. return "{{.TableName}}"
  46. }
  47. {{ end }}
  48. {{if .IsTree }}
  49. // GetChildren 实现TreeNode接口
  50. func (s *{{.StructName}}) GetChildren() []*{{.StructName}} {
  51. return s.Children
  52. }
  53. // SetChildren 实现TreeNode接口
  54. func (s *{{.StructName}}) SetChildren(children *{{.StructName}}) {
  55. s.Children = append(s.Children, children)
  56. }
  57. // GetID 实现TreeNode接口
  58. func (s *{{.StructName}}) GetID() int {
  59. return int({{if not .GvaModel}}*{{- end }}s.{{.PrimaryField.FieldName}})
  60. }
  61. // GetParentID 实现TreeNode接口
  62. func (s *{{.StructName}}) GetParentID() int {
  63. return s.ParentID
  64. }
  65. {{ end }}
  66. {{ end }}