dao.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/admin/main/creative/conf"
  5. article "go-common/app/interface/openplatform/article/rpc/client"
  6. accapi "go-common/app/service/main/account/api"
  7. archive "go-common/app/service/main/archive/api/gorpc"
  8. "go-common/library/database/elastic"
  9. "go-common/library/database/orm"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. bm "go-common/library/net/http/blademaster"
  13. "github.com/jinzhu/gorm"
  14. )
  15. const (
  16. _msgURL = "/api/notify/send.user.notify.do"
  17. )
  18. // Dao dao.
  19. type Dao struct {
  20. c *conf.Config
  21. DB *gorm.DB
  22. DBArchive *gorm.DB
  23. acc accapi.AccountClient
  24. arc *archive.Service2
  25. art *article.Service
  26. es *elastic.Elastic
  27. msgURL string
  28. // http
  29. client *bm.Client
  30. }
  31. // New new a dao and return.
  32. func New(c *conf.Config) (d *Dao) {
  33. d = &Dao{
  34. c: c,
  35. DB: orm.NewMySQL(c.ORM),
  36. DBArchive: orm.NewMySQL(c.ORMArchive),
  37. arc: archive.New2(c.ArchiveRPC),
  38. art: article.New(c.ArticleRPC),
  39. es: elastic.NewElastic(nil),
  40. // http client
  41. client: bm.NewClient(c.HTTPClient),
  42. }
  43. d.msgURL = c.Host.Msg + _msgURL
  44. d.initORM()
  45. var err error
  46. if d.acc, err = accapi.NewClient(c.AccClient); err != nil {
  47. panic(err)
  48. }
  49. return
  50. }
  51. func (d *Dao) initORM() {
  52. d.DB.LogMode(true)
  53. d.DBArchive.LogMode(true)
  54. d.DB.SingularTable(true)
  55. }
  56. // Ping check connection of db , mc.
  57. func (d *Dao) Ping(c context.Context) (err error) {
  58. if d.DB != nil {
  59. err = d.DB.DB().PingContext(c)
  60. }
  61. if d.DBArchive != nil {
  62. err = d.DBArchive.DB().PingContext(c)
  63. }
  64. return
  65. }
  66. // Close close connection of db , mc.
  67. func (d *Dao) Close() {
  68. if d.DB != nil {
  69. d.DB.Close()
  70. }
  71. if d.DBArchive != nil {
  72. d.DBArchive.Close()
  73. }
  74. }
  75. // ProfileStat get account.
  76. func (d *Dao) ProfileStat(c context.Context, mid int64) (res *accapi.ProfileStatReply, err error) {
  77. arg := &accapi.MidReq{Mid: mid}
  78. if res, err = d.acc.ProfileWithStat3(c, arg); err != nil {
  79. log.Error("d.acc.ProfileWithStat3() error(%v)", err)
  80. err = ecode.CreativeAccServiceErr
  81. }
  82. return
  83. }