123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package model
- import (
- "encoding/json"
- "fmt"
- "git.getensh.com/common/gopkgs/logger"
- "go.uber.org/zap"
- "gopkg.in/mgo.v2"
- "gopkg.in/mgo.v2/bson"
- )
- const (
- VoteMgoDb = "db_vote"
- )
- var DbMap = map[string]bool{}
- func CheckDbExist(session *mgo.Session, dbname string) (bool, error) {
- if DbMap[dbname] {
- return true, nil
- }
- array, err := session.DatabaseNames()
- if err != nil {
- logger.Error("mgo",
- zap.String("sql", "session.DatabaseNames"),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- return false, err
- }
- for _, v := range array {
- if v == dbname {
- DbMap[dbname] = false
- return true, nil
- }
- }
- return false, nil
- }
- func VoteCollection(gardenId int64, id int64) string {
- return fmt.Sprintf("t_vote_%d_%d", gardenId, id)
- }
- type AnswerItem struct {
- Number int64 `json:"number" bson:"number"`
- TopicType int32 `json:"topic_type" bson:"topic_type"`
- CompletionAnswer []string `json:"completion_answer" bson:"completion_answer"`
- ChoiceAnswer []string `json:"choice_answer" bson:"choice_answer"`
- StarAnswer int32 `json:"star_answer" bson:"star_answer"`
- }
- type VoteAnswer struct {
- Id bson.ObjectId `json:"_id" bson:"_id"`
- Uid int64 `json:"uid" bson:"uid"`
- CreatedAt int64 `json:"created_at" bson:"created_at"`
- UpdatedAt int64 `json:"updated_at" bson:"updated_at"`
- Answers []AnswerItem `json:"answers" bson:"answers"`
- }
- func (p *VoteAnswer) Insert(collection *mgo.Collection) error {
- insertValues := []interface{}{}
- bytes, _ := json.Marshal(p)
- tmp := map[string]interface{}{}
- json.Unmarshal(bytes, &tmp)
- delete(tmp, "_id")
- insertValues = append(insertValues, tmp)
- err := collection.Insert(insertValues...)
- if err != nil {
- logger.Error("mgo",
- zap.String("sql", "insert vote"),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *VoteAnswer) Upsert(collection *mgo.Collection, filter interface{}) error {
- bytes, _ := json.Marshal(p)
- tmp := map[string]interface{}{}
- json.Unmarshal(bytes, &tmp)
- delete(tmp, "_id")
- _, err := collection.Upsert(filter, &tmp)
- if err != nil {
- logger.Error("mgo",
- zap.String("sql", "upsert vote"),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *VoteAnswer) Count(collection *mgo.Collection, filter interface{}) (int64, error) {
- count, err := collection.Find(filter).Count()
- if err != nil {
- logger.Error("mgo",
- zap.String("sql", "count vote"),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return int64(count), err
- }
- func (p *VoteAnswer) List(collection *mgo.Collection, filter interface{}, limit int, skip int, sort string, result interface{}) error {
- query := collection.Find(filter)
- var err error
- if sort == "" {
- sort = "_id"
- }
- if limit < 0 {
- err = query.Sort(sort).All(result)
- } else {
- err = query.Skip(skip).Limit(limit).Sort(sort).All(result)
- }
- if err != nil {
- logger.Error("mgo",
- zap.String("sql", "select vote"),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
- func (p *VoteAnswer) Pipe(collection *mgo.Collection, filter interface{}, result interface{}) error {
- err := collection.Pipe(filter).All(result)
- if err != nil {
- logger.Error("mgo",
- zap.String("sql", "pipe vote"),
- zap.String("fields", ""),
- zap.String("error", err.Error()))
- }
- return err
- }
|