175 lines
4.9 KiB
Go
175 lines
4.9 KiB
Go
package exchcodemod
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common"
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
"91porn-server/models"
|
|
"91porn-server/models/commod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var mdb *db.MongoDB
|
|
|
|
const table = models.ExchCode
|
|
|
|
// initIndex 设置index
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{ //batch set indexes //value is the type 1 or -1
|
|
{
|
|
Keys: bson.D{{Key: "code", Value: 1}},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "batchNum", Value: 1}, {Key: "createdAt", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "app", Value: 1}, {Key: "createdAt", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "channel", Value: 1}, {Key: "createdAt", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "authority", Value: 1}, {Key: "createdAt", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "status", Value: 1}, {Key: "createdAt", Value: 1}},
|
|
},
|
|
{
|
|
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 coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func BulkWrite(docs []ExchCode) error {
|
|
writeModels := make([]mongo.WriteModel, len(docs))
|
|
for i, doc := range docs {
|
|
writeModels[i] = mongo.NewInsertOneModel().SetDocument(doc)
|
|
}
|
|
if _, err := coll(nil).Bulk(writeModels); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "BulkWrite", table, "Bulk", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Insert(e ExchCode) error {
|
|
if _, err := coll(nil).InsertOne(e); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 获取兑换码
|
|
func GetExchangeCodeByCode(code string) (data *ExchCode, err error) {
|
|
if err = coll(nil).FindOne(&data, bson.M{"code": code}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetExchangeCodeByCode", table, "FindOne", err),
|
|
log.Any("code", code),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 获取兑换码
|
|
func GetExchangeCodeByID(id ObjectID) (data *ExchCode, err error) {
|
|
if err = coll(nil).FindOne(&data, bson.M{"_id": id}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetExchangeCodeByCode", table, "FindOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 兑换
|
|
func Exchange(code string) (err error) {
|
|
if _, err = coll(nil).UpdateOne(bson.M{"code": code}, bson.M{"$inc": bson.M{"usableNum": -1}, "$set": bson.M{"updatedAt": time.Now()}}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Exchange", table, "UpdateOne", err),
|
|
log.Any("code", code),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 获取推广码列表
|
|
func GetCodeList(filterParams FilterDoc, status *ExchangeStatus, page commod.Page, isSuperAdmin bool) (total int64, data []ExchCode, err error) {
|
|
filter, _ := common.ToBsonM(filterParams)
|
|
if !isSuperAdmin {
|
|
filter["authority"] = bson.M{"$ne": AuthorityGold}
|
|
}
|
|
if status != nil {
|
|
switch *status {
|
|
case StatusExpired:
|
|
filter["invalidAt"] = bson.M{"$lt": time.Now()}
|
|
case StatusUnused:
|
|
filter["invalidAt"] = bson.M{"$gte": time.Now()}
|
|
filter["status"] = *status
|
|
case StatusUsed:
|
|
filter["status"] = *status
|
|
}
|
|
}
|
|
var skip = int64((page.PageNumber - 1) * page.PageSize)
|
|
var limit = int64(page.PageSize)
|
|
var opts = options.Find()
|
|
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}}).SetSkip(skip).SetLimit(limit)
|
|
if err = coll(nil).Find(&data, filter, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCodeList", table, "Find", err),
|
|
log.Any("queryParams", filterParams),
|
|
log.Any("page", page),
|
|
)
|
|
return
|
|
}
|
|
if total, err = coll(nil).Count(filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetCodeList", table, "Count", err),
|
|
log.Any("queryParams", filterParams),
|
|
log.Any("page", page),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
func UpdateExchangeCode(id ObjectID, doc UpdateDoc) (err error) {
|
|
update, _ := common.ToBsonM(doc)
|
|
if _, err = coll(nil).UpdateOne(bson.M{"_id": id}, bson.M{"$set": update}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateExchangeCode", table, "UpdateOne", err),
|
|
log.Any("id", id),
|
|
log.Any("doc", doc),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// 获取最近的批次号
|
|
func GetLatestBatchNumber(app AppType) (data ExchCode, err error) {
|
|
var opts = options.FindOne()
|
|
opts.SetSort(bson.D{{Key: "createdAt", Value: -1}})
|
|
if err = coll(nil).FindOne(&data, bson.M{"app": app}, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetLatestBatchNumber", table, "Find", err),
|
|
log.Any("app", app),
|
|
)
|
|
return
|
|
}
|
|
return
|
|
}
|