list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package vote
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "property-garden/errors"
  9. dbmodel "property-garden/model"
  10. "property-garden/parser"
  11. pb_v1 "property-garden/pb/v1"
  12. "property-garden/utils"
  13. "strings"
  14. "git.getensh.com/common/gopkgs/database"
  15. "git.getensh.com/common/gopkgs/logger"
  16. "go.uber.org/zap"
  17. "google.golang.org/grpc/status"
  18. )
  19. func checkVoteListParam(req *pb_v1.VoteListRequest) error {
  20. switch {
  21. case req.GardenId == 0:
  22. return status.Error(10003, "小区不能为空")
  23. }
  24. if req.PageSize == 0 {
  25. req.PageSize = 10
  26. }
  27. if req.Page == 0 {
  28. req.Page = 1
  29. }
  30. return nil
  31. }
  32. func VoteList(ctx context.Context, req *pb_v1.VoteListRequest) (reply *pb_v1.VoteListReply, err error) {
  33. reply = &pb_v1.VoteListReply{}
  34. // 捕获各个task中的异常并返回给调用者
  35. defer func() {
  36. if r := recover(); r != nil {
  37. err = fmt.Errorf("%+v", r)
  38. e := &status.Status{}
  39. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  40. logger.Error("err",
  41. zap.String("system_err", err.Error()),
  42. zap.Stack("stacktrace"))
  43. }
  44. }
  45. }()
  46. // 参数检查
  47. err = checkVoteListParam(req)
  48. if err != nil {
  49. return nil, err
  50. }
  51. dbname := utils.GetGardenDbName(req.GardenId)
  52. p := dbmodel.NewVote(dbname)
  53. where := map[string]interface{}{
  54. //"garden_id":req.GardenId,
  55. }
  56. if req.Title != "" {
  57. where["title like"] = "%" + req.Title + "%"
  58. }
  59. reply.Page = req.Page
  60. reply.Total, err = p.Count(database.DB(), where, nil)
  61. if err != nil {
  62. return nil, errors.DataBaseError
  63. }
  64. if reply.Total == 0 {
  65. return reply, nil
  66. }
  67. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  68. if err != nil {
  69. return nil, errors.DataBaseError
  70. }
  71. reply.List = make([]*pb_v1.VoteItem, len(list))
  72. for i, v := range list {
  73. item := &pb_v1.VoteItem{
  74. Title: v.Title,
  75. Start: v.Start,
  76. End: v.End,
  77. Id: v.ID,
  78. CreatedAt: v.CreatedAt.Unix(),
  79. }
  80. if v.Pics != "" {
  81. item.Pics = strings.Split(v.Pics, ";")
  82. }
  83. topics := []*pb_v1.VoteTopic{}
  84. json.Unmarshal([]byte(v.Topics), &topics)
  85. item.Topics = topics
  86. voteMgo := dbmodel.VoteAnswer{}
  87. exist, err := dbmodel.CheckDbExist(parser.Session, dbmodel.VoteMgoDb)
  88. if err != nil {
  89. return nil, errors.DataBaseError
  90. }
  91. if exist {
  92. collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, v.ID))
  93. count, err := voteMgo.Count(collection, nil)
  94. if err != nil {
  95. logger.Error("mgo",
  96. zap.String("error", err.Error()))
  97. return nil, errors.DataBaseError
  98. }
  99. item.UserCount = count
  100. }
  101. if len(item.Pics) == 0 {
  102. item.Pics = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.VoteObj}
  103. }
  104. reply.List[i] = item
  105. }
  106. return reply, nil
  107. }