moral.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. "go-common/library/net/metadata"
  13. "github.com/pkg/errors"
  14. )
  15. const (
  16. _moralLogID = 12
  17. )
  18. // Moral get user moral from cache,if miss get from db.
  19. func (d *Dao) Moral(c context.Context, mid int64) (moral *model.Moral, err error) {
  20. if moral, err = d.moralCache(c, mid); err == nil {
  21. return
  22. }
  23. if err != nil && err != memcache.ErrNotFound {
  24. log.Error("Failed to get moral from cache, mid: %d, error:%v", mid, err)
  25. return
  26. }
  27. if moral, err = d.MoralDB(c, mid); err != nil {
  28. return
  29. }
  30. if moral == nil {
  31. moral = &model.Moral{Mid: mid, Moral: model.DefaultMoral}
  32. }
  33. d.SetMoralCache(c, mid, moral)
  34. return
  35. }
  36. // MoralLog is
  37. func (d *Dao) MoralLog(ctx context.Context, mid int64) ([]*model.UserLog, error) {
  38. ip := metadata.String(ctx, metadata.RemoteIP)
  39. t := time.Now().Add(-time.Hour * 24 * 7)
  40. params := url.Values{}
  41. params.Set("mid", strconv.FormatInt(mid, 10))
  42. params.Set("appid", "log_user_action")
  43. params.Set("business", strconv.FormatInt(_moralLogID, 10))
  44. params.Set("pn", "1")
  45. params.Set("ps", "1000")
  46. params.Set("ctime_from", t.Format("2006-01-02 00:00:00"))
  47. params.Set("sort", "desc")
  48. params.Set("order", "ctime")
  49. res := &model.SearchResult{}
  50. if err := d.client.Get(ctx, d.c.Host.Search+_searchLogURI, ip, params, res); err != nil {
  51. return nil, err
  52. }
  53. if res.Code != 0 {
  54. return nil, ecode.Int(res.Code)
  55. }
  56. logs := asMoralLog(res)
  57. return logs, nil
  58. }
  59. // MoralLogByID get distinct log by log id
  60. func (d *Dao) MoralLogByID(ctx context.Context, logID string) (*model.UserLog, error) {
  61. ip := metadata.String(ctx, metadata.RemoteIP)
  62. params := url.Values{}
  63. params.Set("str_0", logID)
  64. params.Set("appid", "log_user_action")
  65. params.Set("business", strconv.FormatInt(_moralLogID, 10))
  66. params.Set("pn", "1")
  67. params.Set("ps", "1")
  68. params.Set("sort", "desc")
  69. params.Set("order", "ctime")
  70. res := &model.SearchResult{}
  71. if err := d.client.Get(ctx, d.c.Host.Search+_searchLogURI, ip, params, res); err != nil {
  72. return nil, err
  73. }
  74. if res.Code != 0 {
  75. return nil, ecode.Int(res.Code)
  76. }
  77. logs := asMoralLog(res)
  78. if len(logs) == 0 {
  79. return nil, ecode.NothingFound
  80. }
  81. return logs[0], nil
  82. }
  83. // DeleteMoralLog is
  84. func (d *Dao) DeleteMoralLog(ctx context.Context, logID string) error {
  85. ip := metadata.String(ctx, metadata.RemoteIP)
  86. return d.deleteLogReport(ctx, _moralLogID, logID, ip)
  87. }
  88. // DeleteLogReport is
  89. func (d *Dao) deleteLogReport(ctx context.Context, business int, logID string, ip string) error {
  90. if logID == "" {
  91. return errors.New("Failed to delete log with empty logID")
  92. }
  93. params := url.Values{}
  94. params.Set("str_0", logID)
  95. params.Set("appid", "log_user_action")
  96. params.Set("business", strconv.FormatInt(int64(business), 10))
  97. res := &model.SearchResult{}
  98. if err := d.client.Post(ctx, d.c.Host.Search+_deleteLogURI, ip, params, res); err != nil {
  99. return err
  100. }
  101. if res.Code != 0 {
  102. return ecode.Int(res.Code)
  103. }
  104. return nil
  105. }
  106. func asMoralLog(res *model.SearchResult) []*model.UserLog {
  107. logs := make([]*model.UserLog, 0, len(res.Data.Result))
  108. for _, r := range res.Data.Result {
  109. ts, err := time.ParseInLocation("2006-01-02 15:04:05", r.Ctime, time.Local)
  110. if err != nil {
  111. log.Warn("Failed to parse log ctime: ctime: %s: %+v", r.Ctime, err)
  112. continue
  113. }
  114. content := map[string]string{
  115. "from_moral": "",
  116. "log_id": "",
  117. "mid": "",
  118. "operater": "",
  119. "origin": "",
  120. "reason": "",
  121. "remark": "",
  122. "status": "",
  123. "to_moral": "",
  124. }
  125. if err := json.Unmarshal([]byte(r.ExtraData), &content); err != nil {
  126. log.Warn("Failed to parse extra data in moral log: mid: %d, extra_data: %s: %+v", r.Mid, r.ExtraData, err)
  127. continue
  128. }
  129. if content["mid"] == "" {
  130. content["mid"] = strconv.FormatInt(r.Mid, 10)
  131. }
  132. l := &model.UserLog{
  133. Mid: r.Mid,
  134. IP: r.IP,
  135. TS: ts.Unix(),
  136. LogID: content["log_id"],
  137. Content: content,
  138. }
  139. logs = append(logs, l)
  140. }
  141. return logs
  142. }