dao.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/activity/conf"
  5. "go-common/library/database/orm"
  6. xhttp "go-common/library/net/http/blademaster"
  7. "github.com/jinzhu/gorm"
  8. )
  9. const (
  10. _actURLAddTags = "/x/internal/tag/activity/add"
  11. _songsURL = "/x/internal/v1/audio/songs/activity/filter/info"
  12. )
  13. // Dao struct user of Dao.
  14. type Dao struct {
  15. c *conf.Config
  16. DB *gorm.DB
  17. client *xhttp.Client
  18. actURLAddTags string
  19. songsURL string
  20. }
  21. // New create a instance of Dao and return.
  22. func New(c *conf.Config) (d *Dao) {
  23. d = &Dao{
  24. c: c,
  25. DB: orm.NewMySQL(c.ORM),
  26. client: xhttp.NewClient(c.HTTPClient),
  27. actURLAddTags: c.Host.API + _actURLAddTags,
  28. songsURL: c.Host.API + _songsURL,
  29. }
  30. d.initORM()
  31. return
  32. }
  33. func (d *Dao) initORM() {
  34. gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
  35. if defaultTableName == "act_matchs" {
  36. return defaultTableName
  37. }
  38. return defaultTableName
  39. }
  40. d.DB.LogMode(true)
  41. }
  42. // Ping check connection of db , mc.
  43. func (d *Dao) Ping(c context.Context) (err error) {
  44. if d.DB != nil {
  45. err = d.DB.DB().PingContext(c)
  46. }
  47. return
  48. }
  49. // Close close connection of db , mc.
  50. func (d *Dao) Close() {
  51. if d.DB != nil {
  52. d.DB.Close()
  53. }
  54. }