model.go.tpl 1.7 KB

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