dao.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package activity
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "strings"
  8. "go-common/app/interface/main/creative/conf"
  9. "go-common/app/interface/main/creative/model/activity"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. httpx "go-common/library/net/http/blademaster"
  13. )
  14. const (
  15. _activityAllList = "/activity/list/videoall"
  16. _activityUpdate = "/api/likes/upbyaid/%d"
  17. _activitySubject = "/activity/subject/%d"
  18. _activityProtocol = "/activity/protocol/%d"
  19. _actOnlineByTypeURI = "/activity/online/by/type"
  20. _actURLType16 = "https://www.bilibili.com/blackboard/x/activity-tougao-h5/detail?from=snap&id="
  21. _actLike = "/activity/likes/list/%d"
  22. )
  23. // Dao define
  24. type Dao struct {
  25. c *conf.Config
  26. // http
  27. client *httpx.Client
  28. // uri
  29. ActAllListURI string
  30. ActUpdateURI string
  31. ActSubjectURI string
  32. ActProtocolURI string
  33. ActOnlineByTypeURL string
  34. ActLikeURI string
  35. }
  36. // New init dao
  37. func New(c *conf.Config) (d *Dao) {
  38. d = &Dao{
  39. c: c,
  40. client: httpx.NewClient(c.HTTPClient.Normal),
  41. ActAllListURI: c.Host.Activity + _activityAllList,
  42. ActUpdateURI: c.Host.Act + _activityUpdate,
  43. ActSubjectURI: c.Host.Activity + _activitySubject,
  44. ActProtocolURI: c.Host.Act + _activityProtocol,
  45. ActOnlineByTypeURL: c.Host.Activity + _actOnlineByTypeURI,
  46. ActLikeURI: c.Host.Activity + _actLike,
  47. }
  48. return
  49. }
  50. // MissionOnlineByTid fn
  51. func (d *Dao) MissionOnlineByTid(c context.Context, tid, plat int16) (mm []*activity.ActWithTP, err error) {
  52. var res struct {
  53. Code int `json:"code"`
  54. Data []*activity.ActWithTP `json:"data"`
  55. }
  56. mm = make([]*activity.ActWithTP, 0)
  57. params := url.Values{}
  58. params.Set("type", strconv.Itoa(int(tid)))
  59. params.Set("plat", strconv.Itoa(int(plat)))
  60. if err = d.client.Get(c, d.ActOnlineByTypeURL, "", params, &res); err != nil {
  61. log.Error("videoup ActOnlineByTypeURL error(%v) | ActOnlineByTypeURL(%s)", err, d.ActOnlineByTypeURL+"?"+params.Encode())
  62. err = ecode.CreativeActivityErr
  63. return
  64. }
  65. if res.Code != 0 {
  66. log.Error("videoup ActOnlineByTypeURL res.Code nq zero error(%v) | ActOnlineByTypeURL(%s) res(%v)", res.Code, d.ActOnlineByTypeURL+"?"+params.Encode(), res)
  67. err = ecode.CreativeActivityErr
  68. return
  69. }
  70. for _, m := range res.Data {
  71. miss := &activity.ActWithTP{
  72. ID: m.ID,
  73. Name: m.Name,
  74. Hot: m.Hot,
  75. Protocol: m.Protocol,
  76. Types: m.Types,
  77. }
  78. if len(m.Tags) > 0 {
  79. miss.Tags = strings.Split(m.Tags, ",")[0]
  80. } else {
  81. miss.Tags = m.Name
  82. }
  83. if m.Type == 16 && len(m.ActURL) == 0 {
  84. miss.ActURL = _actURLType16 + strconv.FormatInt(miss.ID, 10)
  85. } else {
  86. miss.ActURL = m.ActURL
  87. }
  88. mm = append(mm, miss)
  89. }
  90. return
  91. }
  92. // Activities get activity list.
  93. func (d *Dao) Activities(c context.Context) (act []*activity.Activity, err error) {
  94. var (
  95. url string
  96. res struct {
  97. Code int `json:"code"`
  98. Data []*activity.Activity `json:"data"`
  99. }
  100. )
  101. url = d.ActAllListURI
  102. if err = d.client.Get(c, url, "", nil, &res); err != nil {
  103. log.Error("ActivityList url(%s) response(%v) error(%v)", url, res, err)
  104. err = ecode.CreativeActivityErr
  105. return
  106. }
  107. if res.Code != 0 {
  108. log.Error("ActivityList url(%s) res(%v)", url, res)
  109. err = ecode.CreativeActivityErr
  110. return
  111. }
  112. for _, v := range res.Data {
  113. if v.Type == 16 && len(v.ActURL) == 0 {
  114. v.ActURL = _actURLType16 + strconv.FormatInt(v.ID, 10)
  115. }
  116. }
  117. act = res.Data
  118. return
  119. }
  120. // Unbind update the aid a status of this activity
  121. func (d *Dao) Unbind(c context.Context, aid, missionID int64, ip string) (err error) {
  122. params := url.Values{}
  123. params.Set("mission_id", strconv.FormatInt(missionID, 10))
  124. params.Set("aid", strconv.FormatInt(aid, 10))
  125. params.Set("state", "-1")
  126. ms := json.RawMessage{}
  127. if err = d.client.RESTfulPost(c, d.ActUpdateURI, ip, params, &ms, missionID); err != nil {
  128. log.Error("ActUpdateURI url(%s) missionID(%d) error(%v)", d.ActUpdateURI, missionID, err)
  129. err = ecode.CreativeActivityErr
  130. return
  131. }
  132. log.Info("d.UpdateByAid url(%s) params(%s) res(%s)", d.ActUpdateURI, params.Encode(), string(ms))
  133. return
  134. }
  135. //Subject get any exist activity by missionID
  136. func (d *Dao) Subject(c context.Context, missionID int64) (sub *activity.Activity, err error) {
  137. var res struct {
  138. Code int `json:"code"`
  139. Data *activity.Subject `json:"data"`
  140. }
  141. if err = d.client.RESTfulGet(c, d.ActSubjectURI, "", url.Values{}, &res, missionID); err != nil {
  142. log.Error("ActSubjectURI url(%s) missionID(%d) error(%v)", d.ActSubjectURI, missionID, err)
  143. err = ecode.CreativeActivityErr
  144. return
  145. }
  146. if res.Code != 0 {
  147. log.Error("ActSubjectURI url(%s) res(%v)", d.ActSubjectURI, res)
  148. err = ecode.CreativeActivityErr
  149. return
  150. }
  151. var ID int
  152. if ID, err = strconv.Atoi(res.Data.ID); err != nil {
  153. log.Error("ActSubjectURI url(%s) res(%v)", d.ActSubjectURI, res)
  154. err = ecode.CreativeActivityErr
  155. return
  156. }
  157. sub = &activity.Activity{
  158. Name: res.Data.Name,
  159. ID: int64(ID),
  160. }
  161. return
  162. }
  163. // Protocol fn
  164. func (d *Dao) Protocol(c context.Context, missionID int64) (p *activity.Protocol, err error) {
  165. var res struct {
  166. Code int `json:"code"`
  167. Data *activity.Protocol `json:"data"`
  168. }
  169. if err = d.client.RESTfulGet(c, d.ActProtocolURI, "", url.Values{}, &res, missionID); err != nil {
  170. log.Error("ActProtocolURI url(%s) missionID(%d) error(%v)", d.ActProtocolURI, missionID, err)
  171. err = ecode.CreativeActivityErr
  172. return
  173. }
  174. if res.Code != 0 {
  175. log.Error("ActProtocolURI url(%s) res(%v)", d.ActProtocolURI, res)
  176. err = ecode.CreativeActivityErr
  177. return
  178. }
  179. p = res.Data
  180. return
  181. }
  182. //Likes fn
  183. func (d *Dao) Likes(c context.Context, missionID int64) (likeCnt int, err error) {
  184. var res struct {
  185. Code int `json:"code"`
  186. Data *activity.Like `json:"data"`
  187. }
  188. if err = d.client.RESTfulGet(c, d.ActLikeURI, "", url.Values{}, &res, missionID); err != nil {
  189. log.Error("ActLikeURI url(%s) missionID(%d) error(%v)", d.ActLikeURI, missionID, err)
  190. err = ecode.CreativeActivityErr
  191. return
  192. }
  193. if res.Code != 0 {
  194. log.Error("ActLikeURI url(%s) res(%v)", d.ActLikeURI, res)
  195. err = ecode.CreativeActivityErr
  196. return
  197. }
  198. if res.Data != nil {
  199. likeCnt = res.Data.Count
  200. }
  201. return
  202. }