53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package active2023mod
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
)
|
|
|
|
const active2023PrizeTable = models.Active2023Prize // 用户中奖奖品详情表
|
|
|
|
func initPrizeIndex() {
|
|
coll := active2023PrizeColl(nil)
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll.CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", active2023PrizeTable, err))
|
|
}
|
|
}
|
|
|
|
func active2023PrizeColl(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(active2023PrizeTable)
|
|
}
|
|
return t.Coll(active2023PrizeTable)
|
|
}
|
|
|
|
func InsertUserPrizes(t *db.MongoTool, ps *[]Prize) ([]primitive.ObjectID, error) {
|
|
if ps == nil || len(*ps) == 0 {
|
|
return nil, nil
|
|
}
|
|
result, err := active2023PrizeColl(t).InsertMany(ps)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
insertIDsLen := len(result.InsertedIDs)
|
|
if insertIDsLen == 0 {
|
|
return nil, nil
|
|
}
|
|
ids := make([]primitive.ObjectID, insertIDsLen)
|
|
for i := 0; i < insertIDsLen; i++ {
|
|
ids[i], _ = result.InsertedIDs[i].(primitive.ObjectID)
|
|
}
|
|
return ids, nil
|
|
}
|