dao.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package order
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/videoup/conf"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. xtime "go-common/library/time"
  11. )
  12. const (
  13. _executeOrders = "/api/open_api/v2/execute_orders"
  14. _ups = "/api/open_api/v2/ups"
  15. _launchtime = "/api/open_api/v2/execute_orders/launch_time"
  16. _useExeOrder = "/api/open_api/v2/execute_orders/use"
  17. )
  18. // Dao define
  19. type Dao struct {
  20. c *conf.Config
  21. // http
  22. client *bm.Client
  23. // uri
  24. executeOrdersURI string
  25. upsURI string
  26. launchTimeURI string
  27. useExeOrderURI string
  28. }
  29. // New init dao
  30. func New(c *conf.Config) (d *Dao) {
  31. d = &Dao{
  32. c: c,
  33. client: bm.NewClient(c.HTTPClient.Chaodian),
  34. executeOrdersURI: c.Host.Chaodian + _executeOrders,
  35. upsURI: c.Host.Chaodian + _ups,
  36. launchTimeURI: c.Host.Chaodian + _launchtime,
  37. useExeOrderURI: c.Host.Chaodian + _useExeOrder,
  38. }
  39. return
  40. }
  41. // ExecuteOrders execute order ids.
  42. func (d *Dao) ExecuteOrders(c context.Context, mid int64, ip string) (orderIds map[int64]int64, err error) {
  43. params := url.Values{}
  44. params.Set("up_mid", strconv.FormatInt(mid, 10))
  45. var res struct {
  46. Code int `json:"code"`
  47. Orders []*struct {
  48. ExeOdID int64 `json:"execute_order_id"`
  49. } `json:"data"`
  50. }
  51. if err = d.client.Get(c, d.executeOrdersURI, ip, params, &res); err != nil {
  52. log.Error("chaodian url(%s) response(%+v) error(%v)", d.executeOrdersURI+"?"+params.Encode(), res, err)
  53. err = ecode.VideoupOrderAPIErr
  54. return
  55. }
  56. log.Info("chaodian url(%s)", d.executeOrdersURI+"?"+params.Encode())
  57. if res.Code != 0 {
  58. log.Error("chaodian url(%s) res(%v)", d.executeOrdersURI, res)
  59. err = ecode.VideoupOrderAPIErr
  60. return
  61. }
  62. orderIds = make(map[int64]int64)
  63. for _, v := range res.Orders {
  64. orderIds[v.ExeOdID] = v.ExeOdID
  65. }
  66. return
  67. }
  68. // Ups order ups.
  69. func (d *Dao) Ups(c context.Context) (ups map[int64]int64, err error) {
  70. params := url.Values{}
  71. var res struct {
  72. Code int `json:"code"`
  73. Ups []int64 `json:"data"`
  74. }
  75. if err = d.client.Get(c, d.upsURI, "", params, &res); err != nil {
  76. log.Error("chaodian url(%s) response(%+v) error(%v)", d.upsURI+"?"+params.Encode(), res, err)
  77. err = ecode.VideoupOrderAPIErr
  78. return
  79. }
  80. log.Info("chaodian url(%s)", d.upsURI+"?"+params.Encode())
  81. if res.Code != 0 {
  82. log.Error("chaodian url(%s) res(%v)", d.upsURI, res)
  83. err = ecode.VideoupOrderAPIErr
  84. return
  85. }
  86. ups = make(map[int64]int64)
  87. for _, v := range res.Ups {
  88. ups[v] = v
  89. }
  90. return
  91. }
  92. // BindOrder bind order with up.
  93. func (d *Dao) BindOrder(c context.Context, mid, aid, orderID int64, ip string) (err error) {
  94. params := url.Values{}
  95. params.Set("execute_order_id", strconv.FormatInt(orderID, 10))
  96. params.Set("av_id", strconv.FormatInt(aid, 10))
  97. var res struct {
  98. Code int `json:"code"`
  99. Message string `json:"message"`
  100. }
  101. if err = d.client.Post(c, d.useExeOrderURI, ip, params, &res); err != nil {
  102. log.Error("d.client.Do uri(%s) aid(%d) mid(%d) orderID(%d) error(%v)", d.useExeOrderURI+"?"+params.Encode(), mid, aid, orderID, err)
  103. err = ecode.VideoupOrderAPIErr
  104. return
  105. }
  106. log.Info("chaodian url(%s)", d.useExeOrderURI+"?"+params.Encode())
  107. if res.Code != 0 {
  108. log.Error("d.client.Do uri(%s) aid(%d) mid(%d) orderID(%d) res.code(%d) error(%v)", d.useExeOrderURI+"?"+params.Encode(), mid, aid, orderID, res.Code, err)
  109. err = ecode.VideoupOrderAPIErr
  110. return
  111. }
  112. return
  113. }
  114. // PubTime publish time from order id.
  115. func (d *Dao) PubTime(c context.Context, mid, orderID int64, ip string) (ptime xtime.Time, err error) {
  116. params := url.Values{}
  117. params.Set("execute_order_id", strconv.FormatInt(orderID, 10))
  118. var res struct {
  119. Code int `json:"code"`
  120. Data struct {
  121. BeginDate xtime.Time `json:"begin_date"`
  122. } `json:"data"`
  123. }
  124. if err = d.client.Get(c, d.launchTimeURI, "", params, &res); err != nil {
  125. log.Error("chaodian url(%s) response(%+v) error(%v)", d.launchTimeURI+"?"+params.Encode(), res, err)
  126. err = ecode.VideoupOrderAPIErr
  127. return
  128. }
  129. log.Info("chaodian url(%s)", d.launchTimeURI+"?"+params.Encode())
  130. if res.Code != 0 {
  131. log.Error("chaodian url(%s) res(%v)", d.launchTimeURI, res)
  132. err = ecode.VideoupOrderAPIErr
  133. return
  134. }
  135. ptime = res.Data.BeginDate
  136. return
  137. }