models.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package model
  2. import (
  3. "github.com/pkg/errors"
  4. "kpt.xdmy/apiserver/dao"
  5. "kpt.xdmy/pkg/log"
  6. )
  7. var d *dao.Dao
  8. var m *Model
  9. type Model struct {
  10. d *dao.Dao
  11. }
  12. func ModelInit(dao *dao.Dao) {
  13. d = dao
  14. }
  15. // type Model struct {
  16. // ID int `json:"id" gorm:"column:id" `
  17. // }
  18. type Request interface {
  19. Validate() bool
  20. }
  21. func First(pi interface{}) error {
  22. e := d.DB.Where(pi).First(pi).Error
  23. return errors.Wrapf(e, "model First ")
  24. }
  25. func Insert(pi interface{}) error {
  26. e := d.DB.Create(pi).Error
  27. return errors.Wrapf(e, "model Insert")
  28. }
  29. func Find(c map[string]interface{}, pi interface{}) error {
  30. e := d.DB.Where(c).Find(pi).Error
  31. log.ErrorJ(e, "model Find ", c)
  32. return e
  33. }
  34. func Update(c interface{}, pi interface{}) error {
  35. e := d.DB.Where(c).Updates(pi).Error
  36. log.Errorf(e, "model Update %v, %v", c, pi)
  37. return errors.Wrapf(e, "model Update ")
  38. }
  39. func UpdateAll(c map[string]interface{}, pi interface{}) error {
  40. e := d.DB.Where(c).Select("*").Updates(pi).Error
  41. log.ErrorJ(e, "model UpdateAll", c, pi)
  42. return e
  43. }
  44. func Raw(sql string, args map[string]interface{}, r interface{}) error {
  45. e := d.DB.Raw(sql, args).Scan(r).Error
  46. return errors.Wrapf(e, "model Raw %s, %v", sql, args)
  47. }