dao.go 779 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/sms/conf"
  5. "go-common/library/database/orm"
  6. xhttp "go-common/library/net/http/blademaster"
  7. "github.com/jinzhu/gorm"
  8. )
  9. // Dao is the appeal database access object
  10. type Dao struct {
  11. c *conf.Config
  12. DB *gorm.DB
  13. httpClient *xhttp.Client
  14. }
  15. // New will create a new appeal Dao instance
  16. func New(c *conf.Config) (d *Dao) {
  17. d = &Dao{
  18. c: c,
  19. DB: orm.NewMySQL(c.DB),
  20. httpClient: xhttp.NewClient(c.HTTPClient),
  21. }
  22. d.initORM()
  23. return
  24. }
  25. func (d *Dao) initORM() {
  26. d.DB.LogMode(true)
  27. }
  28. // Close close dao.
  29. func (d *Dao) Close() {
  30. if d.DB != nil {
  31. d.DB.Close()
  32. }
  33. }
  34. // Ping ping cpdb
  35. func (d *Dao) Ping(c context.Context) (err error) {
  36. err = d.DB.DB().PingContext(c)
  37. return
  38. }