vote_mgo.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "git.getensh.com/common/gopkgs/logger"
  6. "go.uber.org/zap"
  7. "gopkg.in/mgo.v2"
  8. "gopkg.in/mgo.v2/bson"
  9. )
  10. const (
  11. VoteMgoDb = "db_vote"
  12. )
  13. var DbMap = map[string]bool{}
  14. func CheckDbExist(session *mgo.Session, dbname string) (bool, error) {
  15. if DbMap[dbname] {
  16. return true, nil
  17. }
  18. array, err := session.DatabaseNames()
  19. if err != nil {
  20. logger.Error("mgo",
  21. zap.String("sql", "session.DatabaseNames"),
  22. zap.String("fields", ""),
  23. zap.String("error", err.Error()))
  24. return false, err
  25. }
  26. for _, v := range array {
  27. if v == dbname {
  28. DbMap[dbname] = false
  29. return true, nil
  30. }
  31. }
  32. return false, nil
  33. }
  34. func VoteCollection(gardenId int64, id int64) string {
  35. return fmt.Sprintf("t_vote_%d_%d", gardenId, id)
  36. }
  37. type AnswerItem struct {
  38. Number int64 `json:"number" bson:"number"`
  39. TopicType int32 `json:"topic_type" bson:"topic_type"`
  40. CompletionAnswer []string `json:"completion_answer" bson:"completion_answer"`
  41. ChoiceAnswer []string `json:"choice_answer" bson:"choice_answer"`
  42. StarAnswer int32 `json:"star_answer" bson:"star_answer"`
  43. }
  44. type VoteAnswer struct {
  45. Id bson.ObjectId `json:"_id" bson:"_id"`
  46. Uid int64 `json:"uid" bson:"uid"`
  47. CreatedAt int64 `json:"created_at" bson:"created_at"`
  48. UpdatedAt int64 `json:"updated_at" bson:"updated_at"`
  49. Answers []AnswerItem `json:"answers" bson:"answers"`
  50. }
  51. func (p *VoteAnswer) Insert(collection *mgo.Collection) error {
  52. insertValues := []interface{}{}
  53. bytes, _ := json.Marshal(p)
  54. tmp := map[string]interface{}{}
  55. json.Unmarshal(bytes, &tmp)
  56. delete(tmp, "_id")
  57. insertValues = append(insertValues, tmp)
  58. err := collection.Insert(insertValues...)
  59. if err != nil {
  60. logger.Error("mgo",
  61. zap.String("sql", "insert vote"),
  62. zap.String("fields", ""),
  63. zap.String("error", err.Error()))
  64. }
  65. return err
  66. }
  67. func (p *VoteAnswer) Upsert(collection *mgo.Collection, filter interface{}) error {
  68. bytes, _ := json.Marshal(p)
  69. tmp := map[string]interface{}{}
  70. json.Unmarshal(bytes, &tmp)
  71. delete(tmp, "_id")
  72. _, err := collection.Upsert(filter, &tmp)
  73. if err != nil {
  74. logger.Error("mgo",
  75. zap.String("sql", "upsert vote"),
  76. zap.String("fields", ""),
  77. zap.String("error", err.Error()))
  78. }
  79. return err
  80. }
  81. func (p *VoteAnswer) Count(collection *mgo.Collection, filter interface{}) (int64, error) {
  82. count, err := collection.Find(filter).Count()
  83. if err != nil {
  84. logger.Error("mgo",
  85. zap.String("sql", "count vote"),
  86. zap.String("fields", ""),
  87. zap.String("error", err.Error()))
  88. }
  89. return int64(count), err
  90. }
  91. func (p *VoteAnswer) List(collection *mgo.Collection, filter interface{}, limit int, skip int, sort string, result interface{}) error {
  92. query := collection.Find(filter)
  93. var err error
  94. if sort == "" {
  95. sort = "_id"
  96. }
  97. if limit < 0 {
  98. err = query.Sort(sort).All(result)
  99. } else {
  100. err = query.Skip(skip).Limit(limit).Sort(sort).All(result)
  101. }
  102. if err != nil {
  103. logger.Error("mgo",
  104. zap.String("sql", "select vote"),
  105. zap.String("fields", ""),
  106. zap.String("error", err.Error()))
  107. }
  108. return err
  109. }
  110. func (p *VoteAnswer) Pipe(collection *mgo.Collection, filter interface{}, result interface{}) error {
  111. err := collection.Pipe(filter).All(result)
  112. if err != nil {
  113. logger.Error("mgo",
  114. zap.String("sql", "pipe vote"),
  115. zap.String("fields", ""),
  116. zap.String("error", err.Error()))
  117. }
  118. return err
  119. }