dao.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package act
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. actmdl "go-common/app/interface/main/activity/model/like"
  7. actrpc "go-common/app/interface/main/activity/rpc/client"
  8. "go-common/app/interface/main/app-view/conf"
  9. "go-common/library/ecode"
  10. httpx "go-common/library/net/http/blademaster"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _actInfo = "/matsuri/api/get/videoviewinfo"
  15. _lotteryTimes = "/matsuri/api/get/act/mylotterytimes"
  16. )
  17. // Dao is elec dao.
  18. type Dao struct {
  19. client *httpx.Client
  20. actInfo string
  21. lotteryTimes string
  22. actRPC *actrpc.Service
  23. }
  24. // New elec dao
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: httpx.NewClient(c.HTTPClient),
  28. actInfo: c.Host.Activity + _actInfo,
  29. lotteryTimes: c.Host.Activity + _lotteryTimes,
  30. actRPC: actrpc.New(c.ActivityRPC),
  31. }
  32. return
  33. }
  34. var _emptyList = []string{}
  35. // Info mid+aid total elec info
  36. func (d *Dao) Info(c context.Context, actID int64, randomCnt int64) (gifts, winners []string, err error) {
  37. var res struct {
  38. Code int `json:"code"`
  39. Data struct {
  40. Gifts []struct {
  41. Img string `json:"sponsors_logo"`
  42. } `json:"gifts"`
  43. Winner []struct {
  44. Gift string `json:"gift"`
  45. UName string `json:"uname"`
  46. } `json:"winner"`
  47. } `json:"data"`
  48. }
  49. params := url.Values{}
  50. params.Set("act_id", strconv.FormatInt(actID, 10))
  51. params.Set("random_count", strconv.FormatInt(randomCnt, 10))
  52. if err = d.client.Get(c, d.actInfo, "", params, &res); err != nil {
  53. err = errors.Wrapf(err, "d.client.Get(%s)", d.actInfo+"?"+params.Encode())
  54. return _emptyList, _emptyList, err
  55. }
  56. if res.Code != ecode.OK.Code() {
  57. err = errors.Wrapf(ecode.Int(res.Code), "d.client.Get(%s)", d.actInfo+"?"+params.Encode())
  58. return _emptyList, _emptyList, err
  59. }
  60. for _, v := range res.Data.Gifts {
  61. gifts = append(gifts, v.Img)
  62. }
  63. for _, v := range res.Data.Winner {
  64. winners = append(winners, v.UName+" 抽到了 "+v.Gift)
  65. }
  66. if gifts == nil {
  67. gifts = _emptyList
  68. }
  69. if winners == nil {
  70. winners = _emptyList
  71. }
  72. return
  73. }
  74. // LeftLotteryTimes 剩余抽奖次数
  75. func (d *Dao) LeftLotteryTimes(c context.Context, actID, mid int64) (times int64, err error) {
  76. var res struct {
  77. Code int `json:"code"`
  78. Data struct {
  79. Times int64 `json:"times"`
  80. } `json:"data"`
  81. }
  82. params := url.Values{}
  83. params.Set("act_id", strconv.FormatInt(actID, 10))
  84. params.Set("mid", strconv.FormatInt(mid, 10))
  85. if err = d.client.Get(c, d.lotteryTimes, "", params, &res); err != nil {
  86. err = errors.Wrapf(err, "d.client.Get(%s)", d.lotteryTimes+"?"+params.Encode())
  87. return
  88. }
  89. if res.Code != ecode.OK.Code() {
  90. err = errors.Wrapf(ecode.Int(res.Code), "d.client.Get(%s)", d.lotteryTimes+"?"+params.Encode())
  91. return
  92. }
  93. times = res.Data.Times
  94. return
  95. }
  96. // ActProtocol get act subject & protocol
  97. func (d *Dao) ActProtocol(c context.Context, messionID int64) (protocol *actmdl.SubProtocol, err error) {
  98. arg := &actmdl.ArgActProtocol{Sid: messionID}
  99. protocol, err = d.actRPC.ActProtocol(c, arg)
  100. return
  101. }