dao.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/openplatform/anti-fraud/conf"
  5. "go-common/app/service/openplatform/anti-fraud/model"
  6. "go-common/library/cache/redis"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. httpx "go-common/library/net/http/blademaster"
  10. )
  11. // Dao init dao
  12. type Dao struct {
  13. c *conf.Config
  14. db *sql.DB // db
  15. payShieldDb *sql.DB // payShieldDb
  16. redis *redis.Pool // redis
  17. client *httpx.Client
  18. payData chan *model.ShieldData
  19. }
  20. // New new a Dao and return.
  21. func New(c *conf.Config) (d *Dao) {
  22. d = &Dao{
  23. c: c,
  24. db: sql.NewMySQL(c.DB.AntiFraud),
  25. payShieldDb: sql.NewMySQL(c.DB.PayShield),
  26. redis: redis.NewPool(c.Redis.Config),
  27. client: httpx.NewClient(conf.Conf.HTTPClient.Read),
  28. payData: make(chan *model.ShieldData, 2000),
  29. }
  30. go d.SyncPayShield(context.Background())
  31. go d.PopAnswer(context.TODO())
  32. return
  33. }
  34. // Close close connections.
  35. func (d *Dao) Close() {
  36. if d.db != nil {
  37. d.db.Close()
  38. }
  39. if d.redis != nil {
  40. d.redis.Close()
  41. }
  42. }
  43. // Ping ping health.
  44. func (d *Dao) Ping(c context.Context) (err error) {
  45. if err = d.db.Ping(c); err != nil {
  46. log.Error("PingDb error(%v)", err)
  47. return
  48. }
  49. if err = d.PingRedis(c); err != nil {
  50. log.Error("PingRedis error(%v)", err)
  51. return
  52. }
  53. return
  54. }
  55. // BeginTran begin mysql transaction
  56. func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
  57. return d.db.Begin(c)
  58. }