You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.7 KiB

  1. // 自动生成模板{{.StructName}}
  2. package {{.PackageName}}
  3. import (
  4. "gin-vue-admin/global"
  5. "github.com/jinzhu/gorm"
  6. "github.com/pkg/errors"
  7. )
  8. type {{.StructName}} struct {
  9. gorm.Model {{range .Fields}}
  10. {{.FieldName}} {{.FieldType}} `json:"{{.FieldJson}}"{{if .ColumnName}} gorm:"column:{{.ColumnName}}"{{end}}`{{ end }}
  11. }
  12. // 创建{{.StructName}}
  13. func ({{.Abbreviation}} *{{.StructName}})Create{{.StructName}}()(err error){
  14. err = global.GVA_DB.Create({{.Abbreviation}}).Error
  15. return err
  16. }
  17. // 删除{{.StructName}}
  18. func ({{.Abbreviation}} *{{.StructName}})Delete{{.StructName}}()(err error){
  19. err = global.GVA_DB.Delete({{.Abbreviation}}).Error
  20. return err
  21. }
  22. // 更新{{.StructName}}
  23. func ({{.Abbreviation}} *{{.StructName}})Update{{.StructName}}()(err error, re{{.Abbreviation}} {{.StructName}}){
  24. err = global.GVA_DB.Save({{.Abbreviation}}).Error
  25. return err, *{{.Abbreviation}}
  26. }
  27. // 根据ID查看单条{{.StructName}}
  28. func ({{.Abbreviation}} *{{.StructName}})FindById()(err error,re{{.Abbreviation}} {{.StructName}}){
  29. err = global.GVA_DB.Where("id = ?",{{.Abbreviation}}.ID).First(&re{{.Abbreviation}}).Error
  30. return err,re{{.Abbreviation}}
  31. }
  32. // 分页获取{{.StructName}}
  33. func ({{.Abbreviation}} *{{.StructName}})GetInfoList(info PageInfo)(err error, list interface{}, total int){
  34. limit := info.PageSize
  35. offset := info.PageSize * (info.Page - 1)
  36. db:=global.GVA_DB
  37. if err != nil {
  38. return
  39. } else {
  40. var re{{.StructName}}List []{{.StructName}}
  41. err = db.Limit(limit).Offset(offset).Find(&re{{.StructName}}List).Error
  42. return err, re{{.StructName}}List, total
  43. }
  44. }