list_for_household.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. "gopkg.in/mgo.v2/bson"
  9. "property-garden/errors"
  10. dbmodel "property-garden/model"
  11. "property-garden/parser"
  12. pb_v1 "property-garden/pb/v1"
  13. "property-garden/utils"
  14. "strings"
  15. "git.getensh.com/common/gopkgs/database"
  16. "git.getensh.com/common/gopkgs/logger"
  17. "go.uber.org/zap"
  18. "google.golang.org/grpc/status"
  19. )
  20. func checkVoteListForHouseholdParam(req *pb_v1.VoteListForHouseholdRequest) error {
  21. switch {
  22. case req.GardenId == 0:
  23. return status.Error(10003, "小区不能为空")
  24. }
  25. if req.PageSize == 0 {
  26. req.PageSize = 10
  27. }
  28. if req.Page == 0 {
  29. req.Page = 1
  30. }
  31. return nil
  32. }
  33. func getVoteAnswerForHousehold(req *pb_v1.VoteListForHouseholdRequest, id int64) (map[int64]dbmodel.AnswerItem, error) {
  34. p := dbmodel.VoteAnswer{}
  35. collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, id))
  36. count, err := collection.Find(bson.M{"uid": req.Uid}).Count()
  37. if err != nil {
  38. logger.Error("mgo",
  39. zap.String("error", err.Error()))
  40. return nil, errors.DataBaseError
  41. }
  42. if count == 0 {
  43. return nil, nil
  44. }
  45. err = collection.Find(bson.M{"uid": req.Uid}).One(&p)
  46. if err != nil {
  47. logger.Error("mgo",
  48. zap.String("error", err.Error()))
  49. return nil, errors.DataBaseError
  50. }
  51. m := map[int64]dbmodel.AnswerItem{}
  52. for _, v := range p.Answers {
  53. m[v.Number] = v
  54. }
  55. return m, nil
  56. }
  57. func VoteListForHousehold(ctx context.Context, req *pb_v1.VoteListForHouseholdRequest) (reply *pb_v1.VoteListForHouseholdReply, err error) {
  58. reply = &pb_v1.VoteListForHouseholdReply{}
  59. // 捕获各个task中的异常并返回给调用者
  60. defer func() {
  61. if r := recover(); r != nil {
  62. err = fmt.Errorf("%+v", r)
  63. e := &status.Status{}
  64. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  65. logger.Error("err",
  66. zap.String("system_err", err.Error()),
  67. zap.Stack("stacktrace"))
  68. }
  69. }
  70. }()
  71. // 参数检查
  72. err = checkVoteListForHouseholdParam(req)
  73. if err != nil {
  74. return nil, err
  75. }
  76. dbname := utils.GetGardenDbName(req.GardenId)
  77. p := dbmodel.NewVote(dbname)
  78. where := map[string]interface{}{
  79. //"garden_id":req.GardenId,
  80. }
  81. if req.Title != "" {
  82. where["title like"] = "%" + req.Title + "%"
  83. }
  84. reply.Page = req.Page
  85. reply.Total, err = p.Count(database.DB(), where, nil)
  86. if err != nil {
  87. return nil, errors.DataBaseError
  88. }
  89. if reply.Total == 0 {
  90. return reply, nil
  91. }
  92. list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
  93. if err != nil {
  94. return nil, errors.DataBaseError
  95. }
  96. reply.List = make([]*pb_v1.VoteListForHouseholdItem, len(list))
  97. for i, v := range list {
  98. answerM, err := getVoteAnswerForHousehold(req, v.ID)
  99. if err != nil {
  100. return nil, err
  101. }
  102. item := &pb_v1.VoteListForHouseholdItem{
  103. Title: v.Title,
  104. Start: v.Start,
  105. End: v.End,
  106. Id: v.ID,
  107. CreatedAt: v.CreatedAt.Unix(),
  108. Answered: false,
  109. }
  110. if v.Pics != "" {
  111. item.Pics = strings.Split(v.Pics, ";")
  112. }
  113. if len(answerM) > 0 {
  114. item.Answered = true
  115. }
  116. topics := []*pb_v1.VoteTopic{}
  117. json.Unmarshal([]byte(v.Topics), &topics)
  118. item.Topics = make([]*pb_v1.VoteTopicForHouseholdItem, len(topics))
  119. for j, w := range topics {
  120. item.Topics[j] = &pb_v1.VoteTopicForHouseholdItem{
  121. // 题目类型 1 填空题 2 单选题 3 多选题 4 评分题
  122. TopicType: w.TopicType,
  123. TopicName: w.TopicName,
  124. // 总星数,当为评分题时有效
  125. Star: w.Star,
  126. // 是否必选
  127. Must: w.Must,
  128. // 题目编号
  129. Number: w.Number,
  130. // 选择题的选项
  131. ChoiceItems: w.ChoiceItems,
  132. // 当已投票时,以下字段有效
  133. // 问答题回答,当题目类型为问答题时有效
  134. CompletionAnswers: answerM[w.Number].CompletionAnswer,
  135. // 选择题回答,当题目类型为选择题时有效
  136. ChoiceAnswers: answerM[w.Number].ChoiceAnswer,
  137. // 评分题回答,当题目类型为评分题时有效
  138. StarAnswers: answerM[w.Number].StarAnswer,
  139. }
  140. if len(item.Pics) == 0 {
  141. item.Pics = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.VoteObj}
  142. }
  143. }
  144. reply.List[i] = item
  145. }
  146. return reply, nil
  147. }