dao.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package lottery
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/app/interface/main/creative/model/lottery"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. "net/url"
  10. "strconv"
  11. )
  12. const (
  13. _userCheckURI = "/lottery_svr/v0/lottery_svr/user_check"
  14. _noticeURI = "/lottery_svr/v0/lottery_svr/lottery_notice"
  15. )
  16. // Dao define
  17. type Dao struct {
  18. c *conf.Config
  19. client *bm.Client
  20. UserCheckURL, NoticeURL string
  21. }
  22. // New init dao
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. c: c,
  26. client: bm.NewClient(c.HTTPClient.Normal),
  27. UserCheckURL: c.Host.Dynamic + _userCheckURI,
  28. NoticeURL: c.Host.Dynamic + _noticeURI,
  29. }
  30. return
  31. }
  32. // UserCheck fn
  33. func (d *Dao) UserCheck(c context.Context, mid int64, ip string) (ret int, err error) {
  34. params := url.Values{}
  35. params.Set("sender_uid", strconv.FormatInt(mid, 10))
  36. params.Set("business_type", "8")
  37. var res struct {
  38. Code int `json:"code"`
  39. Data struct {
  40. Result int `json:"result"`
  41. } `json:"data"`
  42. }
  43. if err = d.client.Get(c, d.UserCheckURL, ip, params, &res); err != nil {
  44. log.Error("UserCheck url(%s) response(%v) error(%v)", d.UserCheckURL+"?"+params.Encode(), res, err)
  45. err = ecode.CreativeLotteryAPIErr
  46. return
  47. }
  48. log.Info("UserCheck d.UserCheckURL url(%s) code(%d)", d.UserCheckURL+"?"+params.Encode(), res.Code)
  49. if res.Code != 0 {
  50. log.Error("UserCheck url(%s) res(%v)", d.UserCheckURL, res)
  51. err = ecode.CreativeLotteryAPIErr
  52. return
  53. }
  54. ret = res.Data.Result
  55. return
  56. }
  57. // Notice fn
  58. func (d *Dao) Notice(c context.Context, aid, mid int64, ip string) (ret *lottery.Notice, err error) {
  59. ret = &lottery.Notice{}
  60. params := url.Values{}
  61. params.Set("sender_uid", strconv.FormatInt(mid, 10))
  62. params.Set("business_type", "8")
  63. params.Set("business_id", strconv.FormatInt(aid, 10))
  64. var res struct {
  65. Code int `json:"code"`
  66. Data *lottery.Notice `json:"data"`
  67. }
  68. if err = d.client.Get(c, d.NoticeURL, ip, params, &res); err != nil {
  69. log.Error("Notice url(%s) response(%v) error(%v)", d.NoticeURL+"?"+params.Encode(), res, err)
  70. err = ecode.CreativeLotteryAPIErr
  71. return
  72. }
  73. log.Info("Notice d.NoticeURL url(%s) code(%d)", d.NoticeURL+"?"+params.Encode(), res.Code)
  74. if res.Code != 0 {
  75. log.Error("Notice url(%s) res(%v)", d.NoticeURL, res)
  76. if res.Code == -9999 {
  77. err = ecode.NothingFound
  78. } else {
  79. err = ecode.CreativeLotteryAPIErr
  80. }
  81. return
  82. }
  83. ret = res.Data
  84. return
  85. }