dto.go.template 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package dto
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gorm.io/gorm"
  5. "device-manage/app/admin/models"
  6. "device-manage/common/dto"
  7. "device-manage/common/log"
  8. common "device-manage/common/models"
  9. "device-manage/tools"
  10. )
  11. type {{.ClassName}}Search struct {
  12. dto.Pagination `search:"-"`
  13. {{ $tablename := .TBName -}}
  14. {{ range .Columns -}}
  15. {{$z := .IsQuery}}
  16. {{- if ($z) -}}
  17. {{.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}}"`
  18. {{ end -}}
  19. {{- end }}
  20. }
  21. func (m *{{.ClassName}}Search) GetNeedSearch() interface{} {
  22. return *m
  23. }
  24. func (m *{{.ClassName}}Search) Bind(ctx *gin.Context) error {
  25. msgID := tools.GenerateMsgIDFromContext(ctx)
  26. err := ctx.ShouldBind(m)
  27. if err != nil {
  28. log.Debugf("MsgID[%s] ShouldBind error: %s", msgID, err.Error())
  29. }
  30. return err
  31. }
  32. func (m *{{.ClassName}}Search) Generate() dto.Index {
  33. o := *m
  34. return &o
  35. }
  36. type {{.ClassName}}Control struct {
  37. {{ range .Columns -}}
  38. {{$x := .Pk}}
  39. {{- if ($x) }}
  40. {{.GoField}} uint `uri:"{{.JsonField}}" comment:"{{.ColumnComment}}"` // {{.ColumnComment}}
  41. {{- else if eq .GoField "CreatedAt" -}}
  42. {{- else if eq .GoField "UpdatedAt" -}}
  43. {{- else if eq .GoField "DeletedAt" -}}
  44. {{- else if eq .GoField "CreateBy" -}}
  45. {{- else if eq .GoField "UpdateBy" -}}
  46. {{- else }}
  47. {{.GoField}} {{.GoType}} `json:"{{.JsonField}}" comment:"{{.ColumnComment}}"`
  48. {{end -}}
  49. {{- end }}
  50. }
  51. func (s *{{.ClassName}}Control) Bind(ctx *gin.Context) error {
  52. msgID := tools.GenerateMsgIDFromContext(ctx)
  53. err := ctx.ShouldBindUri(s)
  54. if err != nil {
  55. log.Debugf("MsgID[%s] ShouldBindUri error: %s", msgID, err.Error())
  56. return err
  57. }
  58. err = ctx.ShouldBind(s)
  59. if err != nil {
  60. log.Debugf("MsgID[%s] ShouldBind error: %#v", msgID, err.Error())
  61. }
  62. return err
  63. }
  64. func (s *{{.ClassName}}Control) Generate() dto.Control {
  65. cp := *s
  66. return &cp
  67. }
  68. func (s *{{.ClassName}}Control) GenerateM() (common.ActiveRecord, error) {
  69. return &models.{{.ClassName}}{
  70. {{ range .Columns -}}
  71. {{$x := .Pk}}
  72. {{- if ($x) }}
  73. Model: gorm.Model{ID: s.ID},
  74. {{- else if eq .GoField "CreatedAt" -}}
  75. {{- else if eq .GoField "UpdatedAt" -}}
  76. {{- else if eq .GoField "DeletedAt" -}}
  77. {{- else if eq .GoField "CreateBy" -}}
  78. {{- else if eq .GoField "UpdateBy" -}}
  79. {{- else }}
  80. {{.GoField}}: s.{{.GoField}},
  81. {{- end }}
  82. {{- end }}
  83. }, nil
  84. }
  85. func (s *{{.ClassName}}Control) GetId() interface{} {
  86. return s.{{.PkGoField}}
  87. }
  88. type {{.ClassName}}ById struct {
  89. dto.ObjectById
  90. }
  91. func (s *{{.ClassName}}ById) Generate() dto.Control {
  92. cp := *s
  93. return &cp
  94. }
  95. func (s *{{.ClassName}}ById) GenerateM() (common.ActiveRecord, error) {
  96. return &models.{{.ClassName}}{}, nil
  97. }