222 lines
6.1 KiB
Go
222 lines
6.1 KiB
Go
package backpackmod
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
const table = models.Backpack
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
// Init 索引设置
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "uid", Value: 1},
|
|
{Key: "status", Value: 1},
|
|
{Key: "createTimt", Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "uid", Value: 1},
|
|
{Key: "status", Value: 1},
|
|
{Key: "useTime", Value: -1},
|
|
},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: "uid", Value: 1},
|
|
{Key: "expiredTime", Value: -1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("backpack model set index err ==>[%+v]", err))
|
|
}
|
|
}
|
|
|
|
// 获取个人拥有可用优惠卷列表
|
|
func GetCouponListByUID(uid uint64, goodsType int) ([]*Backpack, error) {
|
|
var out []*Backpack = make([]*Backpack, 0)
|
|
if err := coll(nil).Find(&out, bson.M{
|
|
"uid": uid,
|
|
"goodsType": goodsType,
|
|
"status": Unused,
|
|
"expiredTime": bson.M{"$gt": time.Now()},
|
|
}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCouponListByUID", models.Backpack, "Find", err),
|
|
log.Any("uid", uid),
|
|
log.Any("goodsType", goodsType),
|
|
)
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// 添加物品到背包
|
|
func AddGoods(t *db.MongoTool, uid uint64, b ...Backpack) error {
|
|
for _, item := range b {
|
|
item.GoodsName = item.GoodsType.GetName()
|
|
item.GoodsDesc = item.GoodsType.GetDesc()
|
|
}
|
|
if _, err := coll(t).InsertMany(&b); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddGoods", models.Backpack, "InsertMany", err),
|
|
log.Any("uid", uid),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AddGoodsMany 添加物品到背包
|
|
func AddGoodsMany(t *db.MongoTool, uid uint64, b []Backpack) error {
|
|
if _, err := coll(t).InsertMany(&b); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddGoodsMany", models.Backpack, "InsertMany", err),
|
|
log.Any("uid", uid),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 使用物品
|
|
func UseGoods(t *db.MongoTool, id primitive.ObjectID) error {
|
|
result, err := coll(t).UpdateOne(bson.M{"_id": id}, bson.M{"$set": bson.M{"status": Used, "useTime": time.Now()}})
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UseGoods", models.Backpack, "UpdateOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return err
|
|
}
|
|
if result.ModifiedCount == 0 {
|
|
err = errors.New("backpack UpdateOne ModifiedCount err")
|
|
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UseGoods", models.Backpack, "result.ModifiedCount == 0", err),
|
|
log.Any("id", id),
|
|
)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 查询背包列表
|
|
func GetCouponList(uid uint64, types, status int, limit, page int64) ([]*Backpack, error) {
|
|
var out []*Backpack = make([]*Backpack, 0)
|
|
cond := bson.M{"uid": uid}
|
|
sort := bson.M{}
|
|
switch GoodsStatus(status) {
|
|
case Used:
|
|
sort["useTime"] = -1
|
|
cond["status"] = status
|
|
case Unused:
|
|
cond["status"] = status
|
|
cond["expiredTime"] = bson.M{"$gt": time.Now()}
|
|
sort["createTime"] = 1
|
|
case Expired:
|
|
cond["expiredTime"] = bson.M{"$lte": time.Now()}
|
|
sort["expiredTime"] = -1
|
|
}
|
|
if types > 0 {
|
|
cond["goodsType"] = types
|
|
}
|
|
opts := options.Find().SetLimit(limit).SetSkip((page - 1) * limit).SetSort(sort)
|
|
if err := coll(nil).Find(&out, cond, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoodsList", models.Backpack, "Find", err),
|
|
log.Any("uid", uid),
|
|
)
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// 获取物品信息
|
|
func GetGoodsDetail(id primitive.ObjectID) (*Backpack, error) {
|
|
var out *Backpack
|
|
if err := coll(nil).FindOne(&out, bson.M{"_id": id}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetGoodsDetail", models.Backpack, "FindOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return nil, err
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// 根据id获取券信息
|
|
// 如果未找到, 返回nil
|
|
func GetByID(t *db.MongoTool, id primitive.ObjectID) (*Backpack, error) {
|
|
var bp Backpack
|
|
if err := coll(t).FindOne(&bp, bson.M{"_id": id}); err != nil {
|
|
return nil, err
|
|
}
|
|
if bp.ID != id {
|
|
return nil, nil
|
|
}
|
|
return &bp, nil
|
|
}
|
|
|
|
// UseManyGoods 使用物品(多件)
|
|
func UseManyGoods(t *db.MongoTool, ids []primitive.ObjectID) (int64, error) {
|
|
filter := bson.M{"_id": bson.M{"$in": ids}, "status": Unused}
|
|
result, err := coll(t).UpdateMany(filter, bson.M{"$set": bson.M{"status": Used, "useTime": time.Now()}})
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UseManyGoods", models.Backpack, "UpdateOne", err),
|
|
log.Any("id", ids),
|
|
)
|
|
return 0, err
|
|
}
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// UnuseManyGoods 退换物品(多件)
|
|
func UnuseManyGoods(t *db.MongoTool, ids []primitive.ObjectID) (int64, error) {
|
|
result, err := coll(t).UpdateMany(bson.M{"_id": bson.M{"$in": ids}, "status": Used}, bson.M{"$set": bson.M{"status": Unused}})
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UseManyGoods", models.Backpack, "UpdateOne", err),
|
|
log.Any("id", ids),
|
|
)
|
|
return 0, err
|
|
}
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// QueryAllList 分页查询文档
|
|
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*Backpack, err error) {
|
|
if err = coll(nil).Find(&out, filter, opts...); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllList", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
log.Any("opts", opts),
|
|
)
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// QueryAllCount 查询文档条目数
|
|
func QueryAllCount(filter primitive.M) (int64, error) {
|
|
if count, err := coll(nil).Count(filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllCount", table, "Count", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return 0, err
|
|
} else {
|
|
return count, nil
|
|
}
|
|
}
|