dao.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package elec
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-view/conf"
  7. "go-common/app/interface/main/app-view/model/elec"
  8. "go-common/library/ecode"
  9. httpx "go-common/library/net/http/blademaster"
  10. "go-common/library/net/metadata"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _elec = "/api/elec/info/query"
  15. _elecTotal = "/api/v2/rank/total/av/query"
  16. _elecMonthRank = "1"
  17. )
  18. // Dao is elec dao.
  19. type Dao struct {
  20. client *httpx.Client
  21. elec string
  22. elecTotal string
  23. }
  24. // New elec dao
  25. func New(c *conf.Config) (d *Dao) {
  26. d = &Dao{
  27. client: httpx.NewClient(c.HTTPClient),
  28. elec: c.Host.Elec + _elec,
  29. elecTotal: c.Host.Elec + _elecTotal,
  30. }
  31. return
  32. }
  33. // TotalInfo mid+aid total elec info
  34. func (d *Dao) TotalInfo(c context.Context, mid int64, aid int64) (data *elec.Info, err error) {
  35. params := url.Values{}
  36. params.Set("mid", strconv.FormatInt(mid, 10))
  37. params.Set("av_no", strconv.FormatInt(aid, 10))
  38. var res struct {
  39. Code int `json:"code"`
  40. Data *elec.Info `json:"data"`
  41. }
  42. if err = d.client.Get(c, d.elecTotal, "", params, &res); err != nil {
  43. return
  44. }
  45. if res.Code != ecode.OK.Code() {
  46. err = errors.Wrap(ecode.Int(res.Code), d.elecTotal+"?"+params.Encode())
  47. return
  48. }
  49. data = res.Data
  50. return
  51. }
  52. // Info elec info
  53. func (d *Dao) Info(c context.Context, mid, paymid int64) (data *elec.Info, err error) {
  54. ip := metadata.String(c, metadata.RemoteIP)
  55. params := url.Values{}
  56. params.Set("mid", strconv.FormatInt(mid, 10))
  57. if paymid > 0 {
  58. params.Set("pay_mid", strconv.FormatInt(paymid, 10))
  59. }
  60. params.Set("type", _elecMonthRank)
  61. var res struct {
  62. Code int `json:"code"`
  63. Data *elec.Info `json:"data"`
  64. }
  65. if err = d.client.Get(c, d.elec, ip, params, &res); err != nil {
  66. return
  67. }
  68. if res.Code != ecode.OK.Code() {
  69. if res.Code == 500011 {
  70. return
  71. }
  72. err = errors.Wrap(ecode.Int(res.Code), d.elec+"?"+params.Encode())
  73. return
  74. }
  75. data = res.Data
  76. return
  77. }