123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- // 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"
- "gorm.io/gorm"
- "property-garden/errors"
- dbmodel "property-garden/model"
- "property-garden/parser"
- pb_v1 "property-garden/pb/v1"
- "property-garden/utils"
- "time"
- "git.getensh.com/common/gopkgs/database"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "google.golang.org/grpc/status"
- )
- const (
- TopicTypeCompletion = 1
- TopicTypeSingleChoice = 2
- TopicTypeMultiChoice = 3
- TopicTypeStar = 4
- )
- func checkVoteAddAnswerParam(req *pb_v1.VoteAddAnswerRequest) error {
- switch {
- case req.GardenId == 0:
- return status.Error(10003, "小区不能为空")
- case req.Id == 0:
- return status.Error(10003, "id不能为空")
- case req.Uid == 0:
- return status.Error(10003, "住户uid不能为空")
- }
- return nil
- }
- func getVoteOrigin(dbname string, id int64) (pb_v1.VoteItem, error) {
- p := dbmodel.NewVote(dbname)
- ret := pb_v1.VoteItem{}
- where := map[string]interface{}{
- "id": id,
- }
- err := p.Find(database.DB(), where)
- if err != nil && err != gorm.ErrRecordNotFound {
- return ret, errors.DataBaseError
- }
- if p.ID == 0 {
- return ret, errors.ErrRecordNotFound
- }
- ret = pb_v1.VoteItem{
- Title: p.Title,
- Start: p.Start,
- End: p.End,
- Id: p.ID,
- }
- topics := []*pb_v1.VoteTopic{}
- json.Unmarshal([]byte(p.Topics), &topics)
- ret.Topics = topics
- return ret, nil
- }
- func checkChoiceAnswer(origin []*pb_v1.VoteTopicChoiceItem, answer []string) bool {
- if len(answer) == 0 {
- return true
- }
- m := map[string]bool{}
- for _, v := range origin {
- m[v.Flag] = true
- }
- for _, v := range answer {
- if !m[v] {
- return false
- }
- }
- return true
- }
- func VoteAddAnswer(ctx context.Context, req *pb_v1.VoteAddAnswerRequest) (reply *pb_v1.VoteAddAnswerReply, err error) {
- reply = &pb_v1.VoteAddAnswerReply{}
- // 捕获各个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 = checkVoteAddAnswerParam(req)
- if err != nil {
- return nil, err
- }
- dbname := utils.GetGardenDbName(req.GardenId)
- vote, err := getVoteOrigin(dbname, req.Id)
- if err != nil {
- return nil, err
- }
- now := time.Now()
- if vote.Start > now.Unix() {
- return nil, status.Error(10003, "投票未开始")
- }
- if vote.End < now.Unix() {
- return nil, status.Error(10003, "投票已结束")
- }
- if len(vote.Topics) != len(req.Answers) {
- return nil, status.Error(10003, "题目数量不匹配")
- }
- voteM := map[int64]*pb_v1.VoteTopic{}
- for _, v := range vote.Topics {
- voteM[v.Number] = v
- }
- dst := dbmodel.VoteAnswer{
- Uid: req.Uid,
- CreatedAt: now.Unix(),
- UpdatedAt: now.Unix(),
- }
- dst.Answers = make([]dbmodel.AnswerItem, len(req.Answers))
- for i, v := range req.Answers {
- topic := voteM[v.Number]
- if topic == nil {
- return nil, status.Error(10003, "题目不存在")
- }
- dst.Answers[i] = dbmodel.AnswerItem{
- Number: v.Number,
- TopicType: v.TopicType,
- CompletionAnswer: []string{},
- ChoiceAnswer: []string{},
- StarAnswer: 0,
- }
- switch {
- case v.TopicType == TopicTypeCompletion:
- if len(v.CompletionAnswers) == 0 && topic.Must {
- return nil, status.Error(10003, fmt.Sprintf("第%d题为必选题,不能为空", v.Number))
- }
- dst.Answers[i].CompletionAnswer = v.CompletionAnswers
- case v.TopicType == TopicTypeSingleChoice:
- if len(v.ChoiceAnswers) == 0 && topic.Must {
- return nil, status.Error(10003, fmt.Sprintf("第%d题为必选题,不能为空", v.Number))
- }
- if !checkChoiceAnswer(topic.ChoiceItems, v.ChoiceAnswers) {
- return nil, status.Error(10003, fmt.Sprintf("第%d题选项标签错误", v.Number))
- }
- dst.Answers[i].ChoiceAnswer = v.ChoiceAnswers
- case v.TopicType == TopicTypeMultiChoice:
- if len(v.ChoiceAnswers) == 0 && topic.Must {
- return nil, status.Error(10003, fmt.Sprintf("第%d题为必选题,不能为空", v.Number))
- }
- if !checkChoiceAnswer(topic.ChoiceItems, v.ChoiceAnswers) {
- return nil, status.Error(10003, fmt.Sprintf("第%d题选项标签错误", v.Number))
- }
- dst.Answers[i].ChoiceAnswer = v.ChoiceAnswers
- case v.TopicType == TopicTypeStar:
- if v.StarAnswers == 0 && topic.Must {
- return nil, status.Error(10003, fmt.Sprintf("第%d题为必选题,不能为空", v.Number))
- }
- dst.Answers[i].StarAnswer = v.StarAnswers
- }
- }
- collection := parser.Session.DB(dbmodel.VoteMgoDb).C(dbmodel.VoteCollection(req.GardenId, req.Id))
- err = dst.Upsert(collection, bson.M{"uid": req.Uid})
- if err != nil {
- logger.Error("mgo",
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- StatisticDelCache(req.GardenId, req.Id)
- return reply, nil
- }
|