dao.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "go-common/app/job/openplatform/open-sug/conf"
  7. "go-common/library/cache/redis"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "gopkg.in/olivere/elastic.v5"
  11. )
  12. // Dao dao
  13. type Dao struct {
  14. c *conf.Config
  15. redis *redis.Pool
  16. mallDB *xsql.DB
  17. ugcDB *xsql.DB
  18. ticketDB *xsql.DB
  19. client *http.Client
  20. ItemSalesMax map[string]int
  21. ItemSalesMin map[string]int
  22. ItemWishMax map[string]int
  23. ItemWishMin map[string]int
  24. ItemCommentMax map[string]int
  25. ItemCommentMin map[string]int
  26. es *elastic.Client
  27. }
  28. // New init mysql db
  29. func New(c *conf.Config) (dao *Dao) {
  30. var (
  31. err error
  32. es *elastic.Client
  33. )
  34. dao = &Dao{
  35. c: c,
  36. redis: redis.NewPool(c.Redis),
  37. mallDB: xsql.NewMySQL(c.MallMySQL),
  38. ugcDB: xsql.NewMySQL(c.MallUgcMySQL),
  39. ticketDB: xsql.NewMySQL(c.TicketMySQL),
  40. client: &http.Client{Timeout: time.Second * 5},
  41. ItemSalesMax: make(map[string]int),
  42. ItemSalesMin: make(map[string]int),
  43. ItemWishMax: make(map[string]int),
  44. ItemWishMin: make(map[string]int),
  45. ItemCommentMax: make(map[string]int),
  46. ItemCommentMin: make(map[string]int),
  47. }
  48. es, err = elastic.NewClient(
  49. elastic.SetURL(c.ElasticSearch.Addr...),
  50. elastic.SetSniff(false),
  51. elastic.SetHealthcheckInterval(time.Duration(c.ElasticSearch.Check)),
  52. elastic.SetErrorLog(&elog{}),
  53. elastic.SetInfoLog(&ilog{}),
  54. )
  55. if err != nil {
  56. panic(err)
  57. }
  58. dao.es = es
  59. return
  60. }
  61. // Close close the resource.
  62. func (d *Dao) Close() {
  63. d.redis.Close()
  64. d.mallDB.Close()
  65. d.ugcDB.Close()
  66. d.ticketDB.Close()
  67. }
  68. // Ping dao ping
  69. func (d *Dao) Ping(c context.Context) error {
  70. // TODO: if you need use mc,redis, please add
  71. return d.mallDB.Ping(c)
  72. }
  73. type ilog struct{}
  74. type elog struct{}
  75. // Printf printf.
  76. func (l *ilog) Printf(format string, v ...interface{}) {
  77. log.Info(format, v...)
  78. }
  79. // Printf printf.
  80. func (l *elog) Printf(format string, v ...interface{}) {
  81. log.Error(format, v...)
  82. }