57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package questionnremod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const table = models.Questionnaire
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// initIndex 索引设置
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "level", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "level", Value: 1}, {Key: "questionnaireId", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
func Insert(q Questionnaire) error {
|
|
q.CreatedAt = time.Now()
|
|
_, err := coll(nil).InsertOne(q)
|
|
return err
|
|
}
|
|
|
|
func GetByUID(uid uint64) ([]Questionnaire, error) {
|
|
data := make([]Questionnaire, 0)
|
|
f := bson.M{"uid": uid}
|
|
return data, coll(nil).Find(&data, f)
|
|
}
|