result.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "git.getensh.com/common/gopkgs/database"
  9. "git.getensh.com/common/gopkgs/logger"
  10. "go.uber.org/zap"
  11. "google.golang.org/grpc/status"
  12. "property-garden/errors"
  13. dbmodel "property-garden/model"
  14. "property-garden/parser"
  15. pb_v1 "property-garden/pb/v1"
  16. "property-garden/utils"
  17. )
  18. func checkVoteResultListParam(req *pb_v1.VoteResultListRequest) error {
  19. switch {
  20. case req.GardenId == 0:
  21. return status.Error(10003, "小区不能为空")
  22. case req.Id == 0:
  23. return status.Error(10003, "投票id不能为空")
  24. case req.Number == 0:
  25. return status.Error(10003, "题目编号不能为空")
  26. }
  27. if req.PageSize == 0 {
  28. req.PageSize = 10
  29. }
  30. if req.Page == 0 {
  31. req.Page = 1
  32. }
  33. return nil
  34. }
  35. func getUserInfo(uids []int64, dbname string) (map[int64]dbmodel.THouseholdUser, error) {
  36. p := dbmodel.NewHouseholdUser(dbname)
  37. where := map[string]interface{}{
  38. "id in": uids,
  39. }
  40. list, err := p.List(database.DB(), where, nil, -1, -1)
  41. if err != nil {
  42. return nil, errors.DataBaseError
  43. }
  44. ret := map[int64]dbmodel.THouseholdUser{}
  45. for _, v := range list {
  46. ret[v.ID] = v
  47. }
  48. return ret, nil
  49. }
  50. func VoteResultList(ctx context.Context, req *pb_v1.VoteResultListRequest) (reply *pb_v1.VoteResultListReply, err error) {
  51. reply = &pb_v1.VoteResultListReply{}
  52. // 捕获各个task中的异常并返回给调用者
  53. defer func() {
  54. if r := recover(); r != nil {
  55. err = fmt.Errorf("%+v", r)
  56. e := &status.Status{}
  57. if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
  58. logger.Error("err",
  59. zap.String("system_err", err.Error()),
  60. zap.Stack("stacktrace"))
  61. }
  62. }
  63. }()
  64. // 参数检查
  65. err = checkVoteResultListParam(req)
  66. if err != nil {
  67. return nil, err
  68. }
  69. dbname := utils.GetGardenDbName(req.GardenId)
  70. vote, err := getVoteOrigin(dbname, req.Id)
  71. if err != nil {
  72. return nil, err
  73. }
  74. var target *pb_v1.VoteTopic
  75. for _, v := range vote.Topics {
  76. if v.Number == req.Number {
  77. target = v
  78. break
  79. }
  80. }
  81. if target == nil {
  82. return nil, errors.ErrRecordNotFound
  83. }
  84. choiceM := map[string]string{}
  85. for _, v := range target.ChoiceItems {
  86. choiceM[v.Flag] = v.Text
  87. }
  88. p := dbmodel.VoteAnswer{}
  89. list := []dbmodel.VoteAnswer{}
  90. exist, err := dbmodel.CheckDbExist(parser.Session, dbmodel.VoteMgoDb)
  91. if err != nil {
  92. return nil, errors.DataBaseError
  93. }
  94. if !exist {
  95. return reply, nil
  96. }
  97. collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, req.Id))
  98. reply.Total, err = p.Count(collection, nil)
  99. if err != nil {
  100. logger.Error("mgo",
  101. zap.String("error", err.Error()))
  102. return nil, errors.DataBaseError
  103. }
  104. if reply.Total == 0 {
  105. return reply, nil
  106. }
  107. err = p.List(collection, nil, int(req.PageSize), int(req.PageSize*(req.Page-1)), "", &list)
  108. if err != nil {
  109. logger.Error("mgo",
  110. zap.String("error", err.Error()))
  111. return nil, errors.DataBaseError
  112. }
  113. reply.Page = req.Page
  114. reply.List = make([]*pb_v1.VoteResultUserAnswerItem, len(list))
  115. uids := make([]int64, len(list))
  116. for i, _ := range list {
  117. uids[i] = list[i].Uid
  118. }
  119. userInfoM, err := getUserInfo(uids, dbname)
  120. if err != nil {
  121. return nil, err
  122. }
  123. for i, v := range list {
  124. for _, a := range v.Answers {
  125. if a.Number != req.Number {
  126. continue
  127. }
  128. // 将标签(A,b,c,d)替换为具体的选项类容
  129. for j, sub := range a.ChoiceAnswer {
  130. a.ChoiceAnswer[j] = choiceM[sub]
  131. }
  132. answer := &pb_v1.VoteResultData{
  133. TopicType: target.TopicType,
  134. TopicName: target.TopicName,
  135. // 总星数,当为评分题时有效
  136. Star: target.Star,
  137. // 是否必选
  138. Must: target.Must,
  139. // 题目编号
  140. Number: target.Number,
  141. // 选择题的选项
  142. ChoiceItems: target.ChoiceItems,
  143. // 问答题回答,当题目类型为问答题时有效
  144. CompletionAnswers: a.CompletionAnswer,
  145. // 选择题回答,当题目类型为选择题时有效
  146. ChoiceAnswers: a.ChoiceAnswer,
  147. // 评分题回答,当题目类型为评分题时有效
  148. StarAnswers: a.StarAnswer,
  149. }
  150. reply.List[i] = &pb_v1.VoteResultUserAnswerItem{
  151. Uid: v.Uid,
  152. Answer: answer,
  153. UserName: userInfoM[v.Uid].NickName,
  154. }
  155. break
  156. }
  157. }
  158. return reply, nil
  159. }