dao.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package dao
  2. import (
  3. "context"
  4. "time"
  5. "go-common/app/job/main/credit/conf"
  6. "go-common/library/cache/memcache"
  7. "go-common/library/cache/redis"
  8. "go-common/library/database/sql"
  9. bm "go-common/library/net/http/blademaster"
  10. )
  11. const (
  12. _delReplyURI = "/x/internal/v2/reply/del"
  13. _delTagURI = "/x/internal/tag/archive/del"
  14. _regReplyURI = "/x/internal/v2/reply/subject/regist"
  15. _blockAccountURI = "/x/internal/block/block"
  16. _unBlockAccountURI = "/x/internal/block/remove"
  17. _sendPendantURI = "/x/internal/pendant/multiGrantByPid"
  18. _sendMsgURI = "/api/notify/send.user.notify.do"
  19. _delDMURI = "/x/internal/dmadmin/report/judge/result"
  20. _sendMedalURI = "/x/internal/medal/grant"
  21. _addMoralURI = "/api/moral/add"
  22. _upReplyStateURI = "/x/internal/v2/reply/report/state"
  23. _modifyCoinsURI = "/x/internal/v1/coin/user/modify"
  24. _filterURI = "/x/internal/filter"
  25. _upAppealStateURI = "/x/internal/workflow/appeal/v3/public/referee"
  26. )
  27. // Dao struct info of Dao.
  28. type Dao struct {
  29. db *sql.DB
  30. c *conf.Config
  31. client *bm.Client
  32. // del path
  33. delReplyURL string
  34. delTagURL string
  35. delDMURL string
  36. blockAccountURL string
  37. unBlockAccountURL string
  38. regReplyURL string
  39. sendPendantURL string
  40. sendMsgURL string
  41. sendMedalURL string
  42. addMoralURL string
  43. upReplyStateURL string
  44. modifyCoinsURL string
  45. filterURL string
  46. upAppealStateURL string
  47. // redis // redis
  48. redis *redis.Pool
  49. redisExpire int64
  50. // memcache
  51. mc *memcache.Pool
  52. }
  53. // New new a Dao and return.
  54. func New(c *conf.Config) (d *Dao) {
  55. d = &Dao{
  56. c: c,
  57. db: sql.NewMySQL(c.Mysql),
  58. client: bm.NewClient(c.HTTPClient),
  59. delReplyURL: c.Host.APICoURI + _delReplyURI,
  60. delTagURL: c.Host.APICoURI + _delTagURI,
  61. regReplyURL: c.Host.APICoURI + _regReplyURI,
  62. blockAccountURL: c.Host.APICoURI + _blockAccountURI,
  63. unBlockAccountURL: c.Host.APICoURI + _unBlockAccountURI,
  64. sendPendantURL: c.Host.APICoURI + _sendPendantURI,
  65. sendMsgURL: c.Host.MsgCoURI + _sendMsgURI,
  66. delDMURL: c.Host.APICoURI + _delDMURI,
  67. sendMedalURL: c.Host.APICoURI + _sendMedalURI,
  68. addMoralURL: c.Host.AccountCoURI + _addMoralURI,
  69. upReplyStateURL: c.Host.APICoURI + _upReplyStateURI,
  70. modifyCoinsURL: c.Host.APICoURI + _modifyCoinsURI,
  71. filterURL: c.Host.APICoURI + _filterURI,
  72. upAppealStateURL: c.Host.APICoURI + _upAppealStateURI,
  73. // redis
  74. redis: redis.NewPool(c.Redis.Config),
  75. redisExpire: int64(time.Duration(c.Redis.Expire) / time.Second),
  76. // memcache
  77. mc: memcache.NewPool(c.Memcache.Config),
  78. }
  79. return
  80. }
  81. // Close close connections of mc, redis, db.
  82. func (d *Dao) Close() {
  83. if d.db != nil {
  84. d.db.Close()
  85. }
  86. }
  87. // Ping ping health of db.
  88. func (d *Dao) Ping(c context.Context) (err error) {
  89. return d.db.Ping(c)
  90. }