53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package newactivity
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func BulkStocks(insertSqls []mongo.WriteModel) (err error) {
|
|
if len(insertSqls) > 0 {
|
|
_, err = collStock(nil).Bulk(insertSqls)
|
|
}
|
|
return
|
|
}
|
|
|
|
// 嫩模列表
|
|
func UpdateStock(t *db.MongoTool, req BuyReq, date time.Time) (giftLeft uint32, location string, err error) {
|
|
res := struct {
|
|
GiftLeft uint32 `bson:"giftLeft"` //剩余礼物数目
|
|
Location string `bson:"location"` //模特地区
|
|
}{}
|
|
conf := bson.M{
|
|
"modelId": req.ModelId,
|
|
"date": date,
|
|
"soldOut": false,
|
|
}
|
|
var update bson.M
|
|
if req.BuyOut {
|
|
update = bson.M{"$set": bson.M{"soldOut": true}}
|
|
} else {
|
|
conf["giftLeft"] = bson.M{"$gte": req.Quantity}
|
|
update = bson.M{"$inc": bson.M{"giftLeft": -req.Quantity}}
|
|
}
|
|
opts := options.FindOneAndUpdate().SetProjection(bson.M{
|
|
"_id": 0,
|
|
"giftLeft": 1,
|
|
"location": 1,
|
|
})
|
|
if err = collStock(t).FindOneAndUpdateReturnTiny(&res, conf, update, false, opts); err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
err = errors.New("礼物卖完了😯")
|
|
}
|
|
}
|
|
giftLeft = res.GiftLeft
|
|
location = res.Location
|
|
return
|
|
}
|