Initial commit

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-15 13:57:10 +08:00
co-authored by Claude Opus 5
commit 8679200f41
1897 changed files with 257900 additions and 0 deletions
+213
View File
@@ -0,0 +1,213 @@
package prdcthsomod
import (
"fmt"
"time"
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/models"
"91porn-server/models/commod"
"91porn-server/models/v/productmod"
"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.ProductHistory
// initIndex 设置index
func initIndex() {
coll := coll(nil)
many := []mongo.IndexModel{
{
Keys: bson.D{{Key: "uid", Value: 1}},
},
{
Keys: bson.D{{Key: "productID", Value: 1}},
},
{
Keys: bson.D{{Key: "name", Value: 1}},
},
{
Keys: bson.D{{Key: "amount", Value: 1}},
},
{
Keys: bson.D{{Key: "productType", Value: 1}},
},
{
Keys: bson.D{{Key: "status", Value: 1}},
},
{
Keys: bson.D{{Key: "createdAt", Value: 1}},
},
{
Keys: bson.D{{Key: "updatedAt", Value: 1}},
},
// 用户运营导出预售状态相关的订单
{
Keys: bson.D{{Key: "advanceOrderStatus", Value: 1}, {Key: "createdAt", Value: -1}},
},
{
Keys: bson.D{{Key: "deductType", Value: 1}, {Key: "createdAt", Value: -1}},
Options: options.Index().SetSparse(true),
},
{
Keys: bson.D{
{Key: "experimentId", Value: 1},
{Key: "experimentVariant", Value: 1},
{Key: "productID", Value: 1},
},
Options: options.Index().
SetName("vip_experiment_gold_order_statistics").
SetPartialFilterExpression(bson.M{"experimentId": bson.M{"$gt": ""}}),
},
}
if _, err := coll.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 IsBoughtVIP(uid uint64) (bool, error) {
p := ProductHistory{}
if err := coll(nil).FindOne(&p, bson.M{"uid": uid, "productType": VIP}); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IsBoughtVIP", table, "FindOne", err), log.Any("uid", uid))
return false, err
}
return p.UID != 0, nil
}
// GetUserLastVip 获取用户最后充值的VIP
func GetUserLastVip(uid uint64) (productID primitive.ObjectID, productName string, amount int64) {
p := ProductHistory{}
opt := options.FindOne().SetSort(bson.D{{Key: "createdAt", Value: -1}})
if err := coll(nil).FindOne(&p, bson.M{"uid": uid, "productType": bson.M{"$in": []commod.ProductType{productmod.VIP, productmod.NewUser, productmod.AdvanceCard}}}, opt); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "IsBoughtVIP", table, "FindOne", err), log.Any("uid", uid))
return primitive.NilObjectID, "", 0
}
if p.ProductType == productmod.AdvanceCard {
return productID, "", 0
}
if p.IsUpgrade {
// 假设A100 B300 C:500 如果A连续升到B,不这么处理,就会变成B=》C要付款300,实际应该给200差价就好
return p.ProductID, p.Name, p.PurchasePrice
}
return p.ProductID, p.Name, p.Amount
}
// InsertProductHistory 插入一条数据
func InsertProductHistory(t *db.MongoTool, p *ProductHistory) error {
p.CreatedAt = time.Now()
p.UpdatedAt = time.Now()
res, err := coll(t).InsertOne(p)
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertProductHistory", table, "InsertOne", err))
return err
}
p.ID = res.InsertedID.(primitive.ObjectID)
return nil
}
func InsertManyProductHistory(t *db.MongoTool, p []ProductHistory) error {
if _, err := coll(t).InsertMany(p); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertProductHistory", table, "InsertOne", err))
return err
}
return nil
}
// FindProductHistorys 查询所有ProductHistory类型
func FindProductHistorys(cond bson.M, opts *options.FindOptions) (total int64, data []*ProductHistory, err error) {
data = make([]*ProductHistory, 0)
if err = coll(nil).Find(&data, cond, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductHistorys", table, "Find", err), log.Any("cond", cond))
return
}
total, err = coll(nil).Count(cond)
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductHistorys", table, "Count", err), log.Any("cond", cond))
}
return
}
// FindList
func FindList(cond bson.M, opts *options.FindOptions) (data []*ProductHistory, err error) {
data = make([]*ProductHistory, 0)
if err = coll(nil).Find(&data, cond, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindList", table, "Find", err), log.Any("cond", cond))
return
}
return
}
func FindUserProductHistory(uid uint64) (list []ProductHistory, err error) {
f := bson.M{"uid": uid, "productType": bson.M{"$in": []commod.ProductType{productmod.VIP, productmod.OTHER,
productmod.MeetingCard, productmod.VideoDiscount,
productmod.VideoFreeCard, productmod.VideoDiscount, productmod.VideoFreeCard,
}}}
opts := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}})
if err = coll(nil).Find(&list, f, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductHistorys", table, "Find", err), log.Any("query", f))
return
}
return
}
func FindUserProductHistoryByType(uid uint64) (list []ProductHistory, err error) {
f := bson.M{"uid": uid, "productType": bson.M{"$in": []commod.ProductType{productmod.GameAdvanceCard}}}
opts := options.Find().SetSort(bson.D{{Key: "createdAt", Value: -1}})
if err = coll(nil).Find(&list, f, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductHistorys", table, "Find", err), log.Any("query", f))
return
}
return
}
// FindProductHistorysByUID 查询所有ProductHistory类型
func FindProductHistorysByUID(uid, pageNumber, pageSizse uint64) (total int64, data []*ProductHistory, hasNext bool, err error) {
data = make([]*ProductHistory, 0)
f := bson.M{"uid": uid, "productType": bson.M{"$in": []commod.ProductType{productmod.VIP, productmod.OTHER,
productmod.MeetingCard, productmod.VideoDiscount,
productmod.VideoFreeCard, productmod.VideoDiscount, productmod.VideoFreeCard,
}}}
skip := int64(pageSizse * (pageNumber - 1))
limit := int64(pageSizse + 1)
opts := options.FindOptions{
Skip: &skip,
Limit: &limit,
Sort: bson.D{{Key: "createdAt", Value: -1}},
}
total, err = coll(nil).Count(f)
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %ss fail error:%+v:", "FindProductHistorys", table, "Count", err), log.Any("query", f))
}
if err = coll(nil).Find(&data, f, &opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductHistorys", table, "Find", err), log.Any("query", f))
return
}
if len(data) > int(pageSizse) {
hasNext = true
data = data[:pageSizse]
}
return
}
// FindMany FindMany
func FindMany(start, end time.Time, opts *options.FindOptions) (data []*ProductStat, err error) {
cond := bson.M{"createdAt": bson.M{"$gte": start, "$lt": end}}
data = make([]*ProductStat, 0)
if err = coll(nil).Find(&data, cond, opts); err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "FindProductHistorys", table, "Find", err), log.Any("cond", cond))
return
}
return
}