model.go.template 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package models
  2. import (
  3. orm "go-admin/global"
  4. "go-admin/utils"
  5. "time"
  6. )
  7. type {{.ClassName}} struct {
  8. {{ range .Columns -}}
  9. {{$x := .Pk}}
  10. // {{.ColumnComment}}
  11. {{if ($x)}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"column:{{.ColumnName}};primary_key"`{{else}}{{.GoField}} {{.GoType}} `json:"{{.JsonField}}" gorm:"column:{{.ColumnName}};"`{{end}}
  12. {{ end -}}
  13. }
  14. // 创建{{.ClassName}}
  15. func (e *{{.ClassName}}) Create() ({{.ClassName}}, error) {
  16. var doc {{.ClassName}}
  17. doc.IsDel = "0"
  18. e.CreateTime = time.Now().String()
  19. result := orm.Eloquent.Table("{{.TableName}}").Create(&e)
  20. if result.Error != nil {
  21. err := result.Error
  22. return doc, err
  23. }
  24. doc = *e
  25. return doc, nil
  26. }
  27. // 获取{{.ClassName}}
  28. func (e *{{.ClassName}}) Get() ({{.ClassName}}, error) {
  29. var doc {{.ClassName}}
  30. table := orm.Eloquent.Table("{{.TableName}}")
  31. {{ range .Columns -}}
  32. {{$z := .IsQuery}}
  33. {{- if ($z) -}}if e.{{.GoField}} != "" {
  34. table = table.Where("{{.ColumnName}} = ?", e.{{.GoField}})
  35. }
  36. {{ end }}
  37. {{- end -}}
  38. if err := table.First(&doc).Error; err != nil {
  39. return doc, err
  40. }
  41. return doc, nil
  42. }
  43. // 获取{{.ClassName}}带分页
  44. func (e *{{.ClassName}}) GetPage(pageSize int, pageIndex int) ([]{{.ClassName}}, int32, error) {
  45. var doc []{{.ClassName}}
  46. table := orm.Eloquent.Table("{{.TableName}}")
  47. {{ range .Columns -}}
  48. {{$z := .IsQuery}}
  49. {{- if ($z) -}}if e.{{.GoField}} != "" {
  50. table = table.Where("{{.ColumnName}} = ?", e.{{.GoField}})
  51. }
  52. {{ end }}
  53. {{- end -}}
  54. // 数据权限控制
  55. dataPermission := new(DataPermission)
  56. dataPermission.UserId, _ = utils.StringToInt(e.DataScope)
  57. table,err := dataPermission.GetDataScope("{{.TableName}}", table)
  58. if err != nil {
  59. return nil, 0, err
  60. }
  61. var count int32
  62. table = table.Offset((pageIndex - 1) * pageSize).Limit(pageSize)
  63. if err := table.Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
  64. return nil, 0, err
  65. }
  66. return doc, count, nil
  67. }
  68. // 更新{{.ClassName}}
  69. func (e *{{.ClassName}}) Update(id int) (update {{.ClassName}}, err error) {
  70. if err = orm.Eloquent.Table("{{.TableName}}").Where("{{.PkColumn}} = ?", id).First(&update).Error; err != nil {
  71. return
  72. }
  73. //参数1:是要修改的数据
  74. //参数2:是修改的数据
  75. if err = orm.Eloquent.Table("{{.TableName}}").Model(&update).Updates(&e).Error; err != nil {
  76. return
  77. }
  78. return
  79. }
  80. // 删除{{.ClassName}}
  81. func (e *{{.ClassName}}) Delete(id int) (success bool, err error) {
  82. if err = orm.Eloquent.Table("{{.TableName}}").Where("{{.PkColumn}} = ?", id).Delete(&{{.ClassName}}{}).Error; err != nil {
  83. success = false
  84. return
  85. }
  86. success = true
  87. return
  88. }