123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package vote
- import (
- "context"
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- "property-garden/errors"
- dbmodel "property-garden/model"
- "property-garden/parser"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- )
- func checkVoteResultListParam(req *pb_v1.VoteResultListRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Id == 0:
- return status.Error(10003, "投票id不能为空")
- case req.Number == 0:
- return status.Error(10003, "题目编号不能为空")
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- if req.Page == 0 {
- req.Page = 1
- }
- return nil
- }
- func getUserInfo(uids []int64, dbname string) (map[int64]dbmodel.THouseholdUser, error) {
- p := dbmodel.NewHouseholdUser(dbname)
- where := map[string]interface{}{
- "id in": uids,
- }
- list, err := p.List(database.DB(), where, nil, -1, -1)
- if err != nil {
- return nil, errors.DataBaseError
- }
- ret := map[int64]dbmodel.THouseholdUser{}
- for _, v := range list {
- ret[v.ID] = v
- }
- return ret, nil
- }
- func VoteResultList(ctx context.Context, req *pb_v1.VoteResultListRequest) (reply *pb_v1.VoteResultListReply, err error) {
- reply = &pb_v1.VoteResultListReply{}
- // 捕获各个task中的异常并返回给调用者
- defer func() {
- if r := recover(); r != nil {
- err = fmt.Errorf("%+v", r)
- e := &status.Status{}
- if er := json.Unmarshal([]byte(err.Error()), e); er != nil {
- logger.Error("err",
- zap.String("system_err", err.Error()),
- zap.Stack("stacktrace"))
- }
- }
- }()
- // 参数检查
- err = checkVoteResultListParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- vote, err := getVoteOrigin(dbname, req.Id)
- if err != nil {
- return nil, err
- }
- var target *pb_v1.VoteTopic
- for _, v := range vote.Topics {
- if v.Number == req.Number {
- target = v
- break
- }
- }
- if target == nil {
- return nil, errors.ErrRecordNotFound
- }
- choiceM := map[string]string{}
- for _, v := range target.ChoiceItems {
- choiceM[v.Flag] = v.Text
- }
- p := dbmodel.VoteAnswer{}
- list := []dbmodel.VoteAnswer{}
- exist, err := dbmodel.CheckDbExist(parser.Session, dbmodel.VoteMgoDb)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if !exist {
- return reply, nil
- }
- collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, req.Id))
- reply.Total, err = p.Count(collection, nil)
- if err != nil {
- logger.Error("mgo",
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- err = p.List(collection, nil, int(req.PageSize), int(req.PageSize*(req.Page-1)), "", &list)
- if err != nil {
- logger.Error("mgo",
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- reply.Page = req.Page
- reply.List = make([]*pb_v1.VoteResultUserAnswerItem, len(list))
- uids := make([]int64, len(list))
- for i, _ := range list {
- uids[i] = list[i].Uid
- }
- userInfoM, err := getUserInfo(uids, dbname)
- if err != nil {
- return nil, err
- }
- for i, v := range list {
- for _, a := range v.Answers {
- if a.Number != req.Number {
- continue
- }
- // 将标签(A,b,c,d)替换为具体的选项类容
- for j, sub := range a.ChoiceAnswer {
- a.ChoiceAnswer[j] = choiceM[sub]
- }
- answer := &pb_v1.VoteResultData{
- TopicType: target.TopicType,
- TopicName: target.TopicName,
- // 总星数,当为评分题时有效
- Star: target.Star,
- // 是否必选
- Must: target.Must,
- // 题目编号
- Number: target.Number,
- // 选择题的选项
- ChoiceItems: target.ChoiceItems,
- // 问答题回答,当题目类型为问答题时有效
- CompletionAnswers: a.CompletionAnswer,
- // 选择题回答,当题目类型为选择题时有效
- ChoiceAnswers: a.ChoiceAnswer,
- // 评分题回答,当题目类型为评分题时有效
- StarAnswers: a.StarAnswer,
- }
- reply.List[i] = &pb_v1.VoteResultUserAnswerItem{
- Uid: v.Uid,
- Answer: answer,
- UserName: userInfoM[v.Uid].NickName,
- }
- break
- }
- }
- return reply, nil
- }
|