exp.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. "go-common/app/service/main/member/model"
  9. "go-common/library/cache/memcache"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _expLogID = 11
  15. )
  16. // Exp get user exp from cache,if miss get from db.
  17. func (d *Dao) Exp(c context.Context, mid int64) (exp int64, err error) {
  18. if exp, err = d.expCache(c, mid); err == nil {
  19. return
  20. }
  21. if err != nil && err != memcache.ErrNotFound {
  22. return
  23. }
  24. if exp, err = d.ExpDB(c, mid); err != nil {
  25. return
  26. }
  27. d.SetExpCache(c, mid, exp)
  28. return
  29. }
  30. // Exps get exps by mids.
  31. func (d *Dao) Exps(c context.Context, mids []int64) (exps map[int64]int64, err error) {
  32. exps, miss, err := d.expsCache(c, mids)
  33. if err != nil {
  34. return
  35. }
  36. if len(miss) == 0 {
  37. return
  38. }
  39. for _, mid := range miss {
  40. mid := mid
  41. exp, err := d.ExpDB(c, mid)
  42. if err != nil {
  43. log.Error("exp mid %d err %v", mid, err)
  44. err = nil
  45. continue
  46. }
  47. exps[mid] = exp
  48. d.cache.Do(c, func(ctx context.Context) {
  49. d.SetExpCache(ctx, mid, exp)
  50. })
  51. }
  52. return
  53. }
  54. // ExpLog is
  55. func (d *Dao) ExpLog(ctx context.Context, mid int64, ip string) ([]*model.UserLog, error) {
  56. t := time.Now().Add(-time.Hour * 24 * 7)
  57. params := url.Values{}
  58. params.Set("mid", strconv.FormatInt(mid, 10))
  59. params.Set("appid", "log_user_action")
  60. params.Set("business", strconv.FormatInt(_expLogID, 10))
  61. params.Set("pn", "1")
  62. params.Set("ps", "1000")
  63. params.Set("ctime_from", t.Format("2006-01-02 00:00:00"))
  64. params.Set("sort", "desc")
  65. params.Set("order", "ctime")
  66. res := &model.SearchResult{}
  67. if err := d.client.Get(ctx, d.c.Host.Search+_searchLogURI, ip, params, res); err != nil {
  68. return nil, err
  69. }
  70. if res.Code != 0 {
  71. return nil, ecode.Int(res.Code)
  72. }
  73. logs := asExpLog(res)
  74. return logs, nil
  75. }
  76. func asExpLog(res *model.SearchResult) []*model.UserLog {
  77. logs := make([]*model.UserLog, 0, len(res.Data.Result))
  78. for _, r := range res.Data.Result {
  79. ts, err := time.ParseInLocation("2006-01-02 15:04:05", r.Ctime, time.Local)
  80. if err != nil {
  81. log.Warn("Failed to parse log ctime: ctime: %s: %+v", r.Ctime, err)
  82. continue
  83. }
  84. content := map[string]string{
  85. "from_exp": "",
  86. "operater": "",
  87. "reason": "",
  88. "to_exp": "",
  89. "log_id": "",
  90. }
  91. if err := json.Unmarshal([]byte(r.ExtraData), &content); err != nil {
  92. log.Warn("Failed to parse extra data in exp log: mid: %d, extra_data: %s: %+v", r.Mid, r.ExtraData, err)
  93. continue
  94. }
  95. l := &model.UserLog{
  96. Mid: r.Mid,
  97. IP: r.IP,
  98. TS: ts.Unix(),
  99. LogID: content["log_id"],
  100. Content: content,
  101. }
  102. logs = append(logs, l)
  103. }
  104. return logs
  105. }