dao.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package resource
  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. "go-common/library/stat/prom"
  8. )
  9. //Dao struct
  10. type Dao struct {
  11. db *xsql.DB
  12. videodb *xsql.DB
  13. // ad_active
  14. selAllVdoActStmt *xsql.Stmt
  15. selVdoActMTCntStmt *xsql.Stmt
  16. delAllVdoActStmt *xsql.Stmt
  17. // ad
  18. selAdVdoActStmt *xsql.Stmt
  19. selAdMtCntVdoStmt *xsql.Stmt
  20. // res
  21. selAllResStmt *xsql.Stmt
  22. selAllAssignStmt *xsql.Stmt
  23. selDefBannerStmt *xsql.Stmt
  24. }
  25. // New init mysql db
  26. func New(c *conf.Config) (dao *Dao) {
  27. dao = &Dao{
  28. db: xsql.NewMySQL(c.MySQL.Res),
  29. videodb: xsql.NewMySQL(c.MySQL.Ads),
  30. }
  31. dao.initActive()
  32. dao.initRes()
  33. dao.initAd()
  34. return
  35. }
  36. // Close close the resource.
  37. func (dao *Dao) Close() {
  38. dao.db.Close()
  39. }
  40. // PromError err
  41. func PromError(name string, format string, args ...interface{}) {
  42. prom.BusinessErrCount.Incr(name)
  43. log.Error(format, args...)
  44. }
  45. // Ping Dao
  46. func (dao *Dao) Ping(c context.Context) (err error) {
  47. if err = dao.db.Ping(c); err != nil {
  48. log.Error("dao.db.Ping error(%v)", err)
  49. return
  50. }
  51. if err = dao.videodb.Ping(c); err != nil {
  52. log.Error("dao.videodb.Ping error(%v)", err)
  53. }
  54. return
  55. }
  56. //BeginTran Dao
  57. func (dao *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
  58. tx, err = dao.videodb.Begin(c)
  59. return
  60. }