archive.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/url"
  7. "strconv"
  8. "go-common/library/cache/redis"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. "go-common/library/net/metadata"
  12. "go-common/library/xstr"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _keyArcAppeal = "arc_appeal_%d_%d"
  17. _testerGroup = "20"
  18. )
  19. // ArcReport add archive report
  20. func (d *Dao) ArcReport(c context.Context, mid, aid, tp int64, reason, pics string) (err error) {
  21. var (
  22. params = url.Values{}
  23. ip = metadata.String(c, metadata.RemoteIP)
  24. )
  25. params.Set("mid", strconv.FormatInt(mid, 10))
  26. params.Set("aid", strconv.FormatInt(aid, 10))
  27. params.Set("type", strconv.FormatInt(tp, 10))
  28. params.Set("reason", reason)
  29. params.Set("pics", pics)
  30. var res struct {
  31. Code int `json:"code"`
  32. }
  33. if err = d.httpW.Post(c, d.arcReportURL, ip, params, &res); err != nil {
  34. return
  35. }
  36. if res.Code != ecode.OK.Code() {
  37. log.Error("archive report(%s) param(%v) ecode err(%d)", d.arcReportURL, params, res.Code)
  38. err = ecode.Int(res.Code)
  39. }
  40. return
  41. }
  42. // ArcAppeal add archive appeal.
  43. func (d *Dao) ArcAppeal(c context.Context, mid int64, data map[string]string, business int) (err error) {
  44. var (
  45. params = url.Values{}
  46. ip = metadata.String(c, metadata.RemoteIP)
  47. )
  48. for name, value := range data {
  49. params.Set(name, value)
  50. }
  51. params.Set("mid", strconv.FormatInt(mid, 10))
  52. params.Set("business", strconv.Itoa(business))
  53. if v, ok := data["attach"]; ok && v != "" {
  54. params.Set("attachments", v)
  55. }
  56. var res struct {
  57. Code int `json:"code"`
  58. }
  59. if err = d.httpW.Post(c, d.arcAppealURL, ip, params, &res); err != nil {
  60. return
  61. }
  62. if res.Code != ecode.OK.Code() {
  63. log.Error("archive report(%s) ecode err(%d)", d.arcAppealURL+"?"+params.Encode(), res.Code)
  64. err = ecode.Int(res.Code)
  65. }
  66. return
  67. }
  68. // AppealTags get appeal tags.
  69. func (d *Dao) AppealTags(c context.Context, business int) (rs json.RawMessage, err error) {
  70. var (
  71. params = url.Values{}
  72. ip = metadata.String(c, metadata.RemoteIP)
  73. )
  74. params.Set("business", strconv.Itoa(business))
  75. var res struct {
  76. Code int `json:"code"`
  77. Data json.RawMessage `json:"data"`
  78. }
  79. if err = d.httpR.Get(c, d.appealTagsURL, ip, params, &res); err != nil {
  80. return
  81. }
  82. if res.Code != ecode.OK.Code() {
  83. log.Error("archive report(%s) param(%v) ecode err(%d)", d.arcReportURL, params, res.Code)
  84. err = ecode.Int(res.Code)
  85. }
  86. rs = res.Data
  87. return
  88. }
  89. // RelatedAids get related aids from bigdata
  90. func (d *Dao) RelatedAids(c context.Context, aid int64) (aids []int64, err error) {
  91. var (
  92. params = url.Values{}
  93. ip = metadata.String(c, metadata.RemoteIP)
  94. )
  95. params.Set("key", strconv.FormatInt(aid, 10))
  96. var res struct {
  97. Code int `json:"code"`
  98. Data []*struct {
  99. Value string `json:"value"`
  100. } `json:"data"`
  101. }
  102. if err = d.httpR.Get(c, d.relatedURL, ip, params, &res); err != nil {
  103. log.Error("realte url(%s) error(%v) ", d.relatedURL+"?"+params.Encode(), err)
  104. return
  105. }
  106. if res.Code != 0 {
  107. log.Error("url(%s) res code(%d) or res.result(%v)", d.relatedURL+"?"+params.Encode(), res.Code, res.Data)
  108. err = ecode.Int(res.Code)
  109. return
  110. }
  111. if res.Data == nil {
  112. err = nil
  113. return
  114. }
  115. if len(res.Data) > 0 {
  116. if aids, err = xstr.SplitInts(res.Data[0].Value); err != nil {
  117. log.Error("realte aids url(%s) error(%v)", d.relatedURL+"?"+params.Encode(), err)
  118. }
  119. }
  120. return
  121. }
  122. func keyArcAppealLimit(mid, aid int64) string {
  123. return fmt.Sprintf(_keyArcAppeal, mid, aid)
  124. }
  125. // SetArcAppealCache set arc appeal cache.
  126. func (d *Dao) SetArcAppealCache(c context.Context, mid, aid int64) (err error) {
  127. key := keyArcAppealLimit(mid, aid)
  128. conn := d.redisBak.Get(c)
  129. defer conn.Close()
  130. if err = conn.Send("SET", key, "1"); err != nil {
  131. log.Error("SetArcAppealCache conn.Send(SET, %s) error(%v)", key, err)
  132. return
  133. }
  134. if err = conn.Send("EXPIRE", key, d.redisAppealLimitExpire); err != nil {
  135. log.Error("SetArcAppealCache conn.Send(Expire, %s, %d) error(%v)", key, d.redisAppealLimitExpire, err)
  136. return
  137. }
  138. if err = conn.Flush(); err != nil {
  139. log.Error("SetArcAppealCache conn.Flush error(%v)", err)
  140. return
  141. }
  142. for i := 0; i < 2; i++ {
  143. if _, err = conn.Receive(); err != nil {
  144. log.Error("SetArcAppealCache conn.Receive() error(%v)", err)
  145. return
  146. }
  147. }
  148. return
  149. }
  150. // ArcAppealCache get arc appeal cache.
  151. func (d *Dao) ArcAppealCache(c context.Context, mid, aid int64) (err error) {
  152. key := keyArcAppealLimit(mid, aid)
  153. conn := d.redisBak.Get(c)
  154. defer conn.Close()
  155. if _, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  156. if err == redis.ErrNil {
  157. err = nil
  158. return
  159. }
  160. log.Error("ArcAppealCache conn.Do(GET, %s) error(%v)", key, err)
  161. }
  162. err = ecode.ArcAppealLimit
  163. return
  164. }
  165. // Special manager special mid.
  166. func (d *Dao) Special(c context.Context) (midsM map[int64]struct{}, err error) {
  167. params := url.Values{}
  168. params.Set("group_id", _testerGroup)
  169. var res struct {
  170. Code int `json:"code"`
  171. Data []struct {
  172. Mid int64 `json:"mid"`
  173. } `json:"data"`
  174. }
  175. if err = d.httpR.Get(c, d.special, "", params, &res); err != nil {
  176. err = errors.Wrap(err, d.special+"?"+params.Encode())
  177. return
  178. }
  179. if res.Code != ecode.OK.Code() {
  180. err = errors.Wrap(ecode.Int(res.Code), d.special+"?"+params.Encode())
  181. return
  182. }
  183. midsM = make(map[int64]struct{}, len(res.Data))
  184. for _, l := range res.Data {
  185. midsM[l.Mid] = struct{}{}
  186. }
  187. return
  188. }