dao.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package elec
  2. import (
  3. "context"
  4. "net/url"
  5. "strconv"
  6. "go-common/app/interface/main/creative/conf"
  7. "go-common/app/interface/main/creative/model/elec"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. httpx "go-common/library/net/http/blademaster"
  11. "go-common/library/xstr"
  12. )
  13. const (
  14. _userStatURL = "/internal/member/info"
  15. _arcStatURL = "/internal/member/show"
  16. _elecRelationURI = "/api/query.elec.relation.do"
  17. )
  18. // Dao define
  19. type Dao struct {
  20. c *conf.Config
  21. // http
  22. client *httpx.Client
  23. // uri
  24. userStatURI string
  25. arcStatURI string
  26. elecRelationURI string
  27. // user
  28. userInfoURL string
  29. userJoinURL string
  30. userExitURL string
  31. // arc
  32. arcOpenURL string
  33. arcCloseURL string
  34. notifyURL string
  35. // status
  36. getStatusURL string
  37. setStatusURL string
  38. // rank
  39. recentRankURL string
  40. currentRankURL string
  41. totalRankURL string
  42. // money
  43. dailyBillURL string
  44. balanceURL string
  45. // recent elec for app
  46. recentElecURL string
  47. // elec remark
  48. remarkListURL string
  49. remarkDetailURL string
  50. remarkURL string
  51. }
  52. // New init dao
  53. func New(c *conf.Config) (d *Dao) {
  54. d = &Dao{
  55. c: c,
  56. client: httpx.NewClient(c.HTTPClient.Slow),
  57. userStatURI: c.Host.Elec + _userStatURL,
  58. arcStatURI: c.Host.Elec + _arcStatURL,
  59. elecRelationURI: c.Host.Elec + _elecRelationURI,
  60. // api elec.
  61. userInfoURL: c.Host.Elec + _userInfoURI,
  62. userJoinURL: c.Host.Elec + _userJoinURI,
  63. userExitURL: c.Host.Elec + _userExitURI,
  64. arcOpenURL: c.Host.Elec + _arcOpenURI,
  65. arcCloseURL: c.Host.Elec + _arcCloseURI,
  66. notifyURL: c.Host.Elec + _notifyURI,
  67. getStatusURL: c.Host.Elec + _getStatusURI,
  68. setStatusURL: c.Host.Elec + _setStatusURI,
  69. recentRankURL: c.Host.Elec + _recentRankURI,
  70. currentRankURL: c.Host.Elec + _currentRankURI,
  71. totalRankURL: c.Host.Elec + _totalRankURI,
  72. dailyBillURL: c.Host.Elec + _dailyBillURI,
  73. balanceURL: c.Host.Elec + _balanceURI,
  74. recentElecURL: c.Host.Elec + _recentElecURI,
  75. remarkListURL: c.Host.Elec + _remarkListURI,
  76. remarkDetailURL: c.Host.Elec + _remarkDetailURI,
  77. remarkURL: c.Host.Elec + _remarkURI,
  78. }
  79. return
  80. }
  81. // UserState get user elec state.
  82. func (d *Dao) UserState(c context.Context, mid int64, ip string) (data *elec.UserState, err error) {
  83. params := url.Values{}
  84. params.Set("mid", strconv.FormatInt(mid, 10))
  85. var res struct {
  86. Code int `json:"code"`
  87. Data struct {
  88. Mid int `json:"mid"`
  89. State int `json:"state"`
  90. Reason string `json:"reason"`
  91. Count int `json:"count"`
  92. CTime string `json:"ctime"`
  93. } `json:"data"`
  94. }
  95. if err = d.client.Get(c, d.userInfoURL, ip, params, &res); err != nil {
  96. log.Error("elec url(%s) response(%v) error(%v)", d.userInfoURL+"?"+params.Encode(), res, err)
  97. err = ecode.CreativeElecErr
  98. return
  99. }
  100. log.Info("UserState d.userInfoURL url(%s), code(%d)", d.userInfoURL+"?"+params.Encode(), res.Code)
  101. if res.Code != 0 {
  102. log.Error("elec url(%s) res(%v)", d.userInfoURL, res)
  103. err = ecode.CreativeElecErr
  104. return
  105. }
  106. data = &elec.UserState{
  107. Mid: strconv.Itoa(res.Data.Mid),
  108. State: strconv.Itoa(res.Data.State),
  109. Count: strconv.Itoa(res.Data.Count),
  110. CTime: res.Data.CTime,
  111. Reason: res.Data.Reason,
  112. }
  113. return
  114. }
  115. // ArchiveState get arc elec state.
  116. func (d *Dao) ArchiveState(c context.Context, aid, mid int64, ip string) (data *elec.ArcState, err error) {
  117. params := url.Values{}
  118. params.Set("upmid", strconv.FormatInt(mid, 10))
  119. params.Set("mid", strconv.FormatInt(mid, 10))
  120. params.Set("aid", strconv.FormatInt(aid, 10))
  121. params.Set("nolist", "1")
  122. var res struct {
  123. Code int `json:"code"`
  124. Data *elec.ArcState `json:"data"`
  125. }
  126. if err = d.client.Get(c, d.arcStatURI, ip, params, &res); err != nil {
  127. log.Error("elec url(%s) response(%v) error(%v)", d.arcStatURI+"?"+params.Encode(), res, err)
  128. err = ecode.CreativeElecErr
  129. return
  130. }
  131. log.Info("ArchiveState d.arcStatURI url(%s), code(%d)", d.arcStatURI+"?"+params.Encode(), res.Code)
  132. if res.Code != 0 {
  133. log.Error("elec url(%s) res(%v)", d.arcStatURI, res)
  134. err = ecode.CreativeElecErr
  135. return
  136. }
  137. data = res.Data
  138. return
  139. }
  140. // ElecRelation check if multi user charged.
  141. func (d *Dao) ElecRelation(c context.Context, mid int64, mids []int64, ip string) (chargeMap map[int64]int, err error) {
  142. params := url.Values{}
  143. params.Set("act", "appkey")
  144. params.Set("type", "json")
  145. params.Set("up_mid", strconv.Itoa(int(mid)))
  146. params.Set("mids", xstr.JoinInts(mids))
  147. var res struct {
  148. Code int `json:"code"`
  149. Data *elec.EleRelation `json:"data"`
  150. }
  151. if err = d.client.Get(c, d.elecRelationURI, ip, params, &res); err != nil {
  152. log.Error("elec url(%s) response(%v) error(%v)", d.elecRelationURI+"?"+params.Encode(), res, err)
  153. err = ecode.CreativeElecErr
  154. return
  155. }
  156. if res.Code != 0 {
  157. log.Error("elec url(%s) res(%v)", d.elecRelationURI, res)
  158. err = ecode.CreativeElecErr
  159. return
  160. }
  161. chargeMap = map[int64]int{}
  162. for _, v := range res.Data.RetList {
  163. if !v.IsElec {
  164. chargeMap[v.Mid] = 0
  165. } else {
  166. chargeMap[v.Mid] = 1
  167. }
  168. }
  169. return
  170. }