dao.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package elec
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/app-interface/conf"
  7. "go-common/app/interface/main/app-interface/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. _elecMonthRank = "1"
  16. )
  17. // Dao is elec dao.
  18. type Dao struct {
  19. client *httpx.Client
  20. elec string
  21. }
  22. // New elec dao
  23. func New(c *conf.Config) (d *Dao) {
  24. d = &Dao{
  25. client: httpx.NewClient(c.HTTPClient),
  26. elec: c.Host.Elec + _elec,
  27. }
  28. return
  29. }
  30. func (d *Dao) Info(c context.Context, mid, paymid int64) (data *elec.Info, err error) {
  31. ip := metadata.String(c, metadata.RemoteIP)
  32. params := url.Values{}
  33. params.Set("mid", strconv.FormatInt(mid, 10))
  34. params.Set("pay_mid", strconv.FormatInt(paymid, 10))
  35. params.Set("type", _elecMonthRank)
  36. var res struct {
  37. Code int `json:"code"`
  38. Data *elec.Info `json:"data"`
  39. }
  40. if err = d.client.Get(c, d.elec, ip, params, &res); err != nil {
  41. return
  42. }
  43. if res.Code != ecode.OK.Code() {
  44. if res.Code == 500011 {
  45. return
  46. }
  47. err = errors.Wrap(ecode.Int(res.Code), d.elec+"?"+params.Encode())
  48. return
  49. }
  50. data = res.Data
  51. return
  52. }