neighbor_article_list.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package neighbor
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "property-garden/errors"
  9. dbmodel "property-garden/model"
  10. pb_v1 "property-garden/pb/v1"
  11. "property-garden/utils"
  12. "strings"
  13. "git.getensh.com/common/gopkgs/database"
  14. "git.getensh.com/common/gopkgs/logger"
  15. "go.uber.org/zap"
  16. "google.golang.org/grpc/status"
  17. )
  18. func checkNeighborArticleListParam(req *pb_v1.NeighborArticleListRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. }
  23. if req.PageSize == 0 {
  24. req.PageSize = 10
  25. }
  26. if req.Page == 0 {
  27. req.Page = 1
  28. }
  29. return nil
  30. }
  31. func getLikeInfo(uid int64, likeIds []int64, dbname string, likeType int32) (map[int64]bool, error) {
  32. m := map[int64]bool{}
  33. if uid == 0 || len(likeIds) == 0 {
  34. return m, nil
  35. }
  36. p := dbmodel.NewNeighborLike(dbname)
  37. where := map[string]interface{}{
  38. "uid": uid,
  39. "like_type": likeType,
  40. "like_id in": likeIds,
  41. }
  42. list, err := p.List(database.DB(), where, nil, -1, -1)
  43. if err != nil {
  44. return m, errors.DataBaseError
  45. }
  46. for _, v := range list {
  47. m[v.LikeId] = true
  48. }
  49. return m, nil
  50. }
  51. func getUserInfo(uids []int64, dbname string) (map[int64]dbmodel.THouseholdUser, error) {
  52. p := dbmodel.NewHouseholdUser(dbname)
  53. where := map[string]interface{}{
  54. "id in": uids,
  55. }
  56. list, err := p.List(database.DB(), where, nil, -1, -1)
  57. if err != nil {
  58. return nil, errors.DataBaseError
  59. }
  60. ret := map[int64]dbmodel.THouseholdUser{}
  61. for _, v := range list {
  62. ret[v.ID] = v
  63. }
  64. return ret, nil
  65. }
  66. func NeighborArticleList(ctx context.Context, req *pb_v1.NeighborArticleListRequest) (reply *pb_v1.NeighborArticleListReply, err error) {
  67. reply = &pb_v1.NeighborArticleListReply{}
  68. // 捕获各个task中的异常并返回给调用者
  69. defer func() {
  70. if r := recover(); r != nil {
  71. err = fmt.Errorf("%+v", r)
  72. e := &status.Status{}
  73. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  74. logger.Error("err",
  75. zap.String("system_err", err.Error()),
  76. zap.Stack("stacktrace"))
  77. }
  78. }
  79. }()
  80. // 参数检查
  81. err = checkNeighborArticleListParam(req)
  82. if err != nil {
  83. return nil, err
  84. }
  85. dbname := utils.GetGardenDbName(req.GardenId)
  86. p := dbmodel.NewNeighborArticle(dbname)
  87. where := map[string]interface{}{
  88. //"garden_id":req.GardenId,
  89. }
  90. if req.Title != "" {
  91. where["title like"] = "%" + req.Title
  92. }
  93. if req.ClassId > 0 {
  94. where["class_id"] = req.ClassId
  95. }
  96. if req.IsMe {
  97. where["uid"] = req.Uid
  98. }
  99. reply.Page = req.Page
  100. reply.Total, err = p.Count(database.DB(), where, nil)
  101. if err != nil {
  102. return nil, errors.DataBaseError
  103. }
  104. if reply.Total == 0 {
  105. return reply, nil
  106. }
  107. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  108. if err != nil {
  109. return nil, errors.DataBaseError
  110. }
  111. likeIds := make([]int64, len(list))
  112. uids := []int64{}
  113. for i, v := range list {
  114. likeIds[i] = v.ID
  115. uids = append(uids, v.Uid)
  116. }
  117. userInfoM, err := getUserInfo(uids, dbname)
  118. likeInfoM, err := getLikeInfo(req.Uid, likeIds, dbname, 1)
  119. if err != nil {
  120. return nil, err
  121. }
  122. reply.List = make([]*pb_v1.NeighborArticleItem, len(list))
  123. for i, v := range list {
  124. item := &pb_v1.NeighborArticleItem{
  125. ClassId: v.ClassId,
  126. Title: v.Title,
  127. Content: v.Content,
  128. Pics: strings.Split(v.Pics, ";"),
  129. Id: v.ID,
  130. CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
  131. UpdatedAt: v.UpdatedAt.Format("2006-01-02 15:04:05"),
  132. Likes: v.Likes,
  133. Like: likeInfoM[v.ID],
  134. Uid: v.Uid,
  135. UserIcon: v.UserIcon,
  136. UserName: userInfoM[v.Uid].RealName,
  137. UserPhone: userInfoM[v.Uid].Phone,
  138. CommentCount: v.CommentCount,
  139. Owner: req.Uid == v.Uid,
  140. NickName: v.NickName,
  141. }
  142. reply.List[i] = item
  143. }
  144. return reply, nil
  145. }