dao.go 976 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package ad
  2. import (
  3. "context"
  4. "go-common/app/interface/main/web-show/conf"
  5. xsql "go-common/library/database/sql"
  6. "go-common/library/log"
  7. httpx "go-common/library/net/http/blademaster"
  8. "go-common/library/stat/prom"
  9. )
  10. // Dao define db struct
  11. type Dao struct {
  12. cpt *xsql.DB
  13. // sql
  14. selAdsStmt *xsql.Stmt
  15. // cpt
  16. httpClient *httpx.Client
  17. cpmURL string
  18. }
  19. const (
  20. _cpmURL = "/bce/api/bce/pc"
  21. )
  22. // PromError err
  23. func PromError(name string, format string, args ...interface{}) {
  24. prom.BusinessErrCount.Incr(name)
  25. log.Error(format, args...)
  26. }
  27. // New init mysql db
  28. func New(c *conf.Config) (dao *Dao) {
  29. dao = &Dao{
  30. cpt: xsql.NewMySQL(c.MySQL.Cpt),
  31. httpClient: httpx.NewClient(c.HTTPClient),
  32. cpmURL: c.Host.Ad + _cpmURL,
  33. }
  34. dao.selAdsStmt = dao.cpt.Prepared(_selAds)
  35. return
  36. }
  37. // Close close the resource.
  38. func (dao *Dao) Close() {
  39. dao.cpt.Close()
  40. }
  41. // Ping ping mysql
  42. func (dao *Dao) Ping(c context.Context) error {
  43. return dao.cpt.Ping(c)
  44. }