123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package vote
- import (
- "context"
- "encoding/json"
- "fmt"
- "gopkg.in/mgo.v2/bson"
- "property-garden/errors"
- dbmodel "property-garden/model"
- "property-garden/parser"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- "strings"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- func checkVoteListForHouseholdParam(req *pb_v1.VoteListForHouseholdRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- }
- if req.PageSize == 0 {
- req.PageSize = 10
- }
- if req.Page == 0 {
- req.Page = 1
- }
- return nil
- }
- func getVoteAnswerForHousehold(req *pb_v1.VoteListForHouseholdRequest, id int64) (map[int64]dbmodel.AnswerItem, error) {
- p := dbmodel.VoteAnswer{}
- collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, id))
- count, err := collection.Find(bson.M{"uid": req.Uid}).Count()
- if err != nil {
- logger.Error("mgo",
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- if count == 0 {
- return nil, nil
- }
- err = collection.Find(bson.M{"uid": req.Uid}).One(&p)
- if err != nil {
- logger.Error("mgo",
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- m := map[int64]dbmodel.AnswerItem{}
- for _, v := range p.Answers {
- m[v.Number] = v
- }
- return m, nil
- }
- func VoteListForHousehold(ctx context.Context, req *pb_v1.VoteListForHouseholdRequest) (reply *pb_v1.VoteListForHouseholdReply, err error) {
- reply = &pb_v1.VoteListForHouseholdReply{}
- // 捕获各个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 = checkVoteListForHouseholdParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- p := dbmodel.NewVote(dbname)
- where := map[string]interface{}{
- //"garden_id":req.GardenId,
- }
- if req.Title != "" {
- where["title like"] = "%" + req.Title + "%"
- }
- reply.Page = req.Page
- reply.Total, err = p.Count(database.DB(), where, nil)
- if err != nil {
- return nil, errors.DataBaseError
- }
- if reply.Total == 0 {
- return reply, nil
- }
- list, err := p.List(database.DB(), where, nil, int(req.Page), int(req.PageSize))
- if err != nil {
- return nil, errors.DataBaseError
- }
- reply.List = make([]*pb_v1.VoteListForHouseholdItem, len(list))
- for i, v := range list {
- answerM, err := getVoteAnswerForHousehold(req, v.ID)
- if err != nil {
- return nil, err
- }
- item := &pb_v1.VoteListForHouseholdItem{
- Title: v.Title,
- Start: v.Start,
- End: v.End,
- Id: v.ID,
- CreatedAt: v.CreatedAt.Unix(),
- Answered: false,
- }
- if v.Pics != "" {
- item.Pics = strings.Split(v.Pics, ";")
- }
- if len(answerM) > 0 {
- item.Answered = true
- }
- topics := []*pb_v1.VoteTopic{}
- json.Unmarshal([]byte(v.Topics), &topics)
- item.Topics = make([]*pb_v1.VoteTopicForHouseholdItem, len(topics))
- for j, w := range topics {
- item.Topics[j] = &pb_v1.VoteTopicForHouseholdItem{
- // 题目类型 1 填空题 2 单选题 3 多选题 4 评分题
- TopicType: w.TopicType,
- TopicName: w.TopicName,
- // 总星数,当为评分题时有效
- Star: w.Star,
- // 是否必选
- Must: w.Must,
- // 题目编号
- Number: w.Number,
- // 选择题的选项
- ChoiceItems: w.ChoiceItems,
- // 当已投票时,以下字段有效
- // 问答题回答,当题目类型为问答题时有效
- CompletionAnswers: answerM[w.Number].CompletionAnswer,
- // 选择题回答,当题目类型为选择题时有效
- ChoiceAnswers: answerM[w.Number].ChoiceAnswer,
- // 评分题回答,当题目类型为评分题时有效
- StarAnswers: answerM[w.Number].StarAnswer,
- }
- if len(item.Pics) == 0 {
- item.Pics = []string{parser.Conf.Oss.Protocol + "://" + parser.Conf.Oss.Endpoint + "/" + parser.Conf.Oss.FixBucket + "/" + parser.Conf.Oss.VoteObj}
- }
- }
- reply.List[i] = item
- }
- return reply, nil
- }
|