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
+73
View File
@@ -0,0 +1,73 @@
package coupon_record_mod
import (
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
// AppListReq 优惠券列表请求
type AppListReq struct {
Type int `form:"type" json:"type" ` // 优惠券使用类型 0表示返回全部 1表示只返回有效(未使用且未过期)2 返回已过期
PageNumber int64 `form:"pageNumber" json:"pageNumber"` // 当前页码
PageSize int64 `form:"pageSize" json:"pageSize"` // 每页数量
}
type QueryAllRes struct {
List []*AppCouponRecordRes `json:"list"`
Total int64 `json:"total"`
}
func (receiver *AppListReq) Filter(uid uint64) bson.M {
var query = bson.M{}
if receiver.Type == 1 {
query["expireTime"] = bson.M{"$gte": time.Now()}
query["used"] = false
}
if receiver.Type == 2 {
query["expireTime"] = bson.M{"$lt": time.Now()}
}
query["uid"] = uid
query["isDelete"] = false
return query
}
func (receiver AppListReq) Options() *options.FindOptions {
return options.Find().SetSkip((receiver.PageNumber - 1) * receiver.PageSize).SetLimit(receiver.PageSize).SetSort(bson.M{"createTime": -1})
}
type AppGainUserInfoReq struct {
UID uint64 `json:"uid"` // 用户ID
UserName string `json:"user_name"` // 用户名称
AppID int32 `json:"app_id"` // APPId
TotalRecharge int64 `json:"total_recharge"` // 用户累计充值金额
Balance int64 `json:"balance"` // 用户余额
Token string `json:"token"` // Token
}
// AppCouponRecordRes 用户优惠券记录响应
type AppCouponRecordRes struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // 文档ID
CID string `json:"cId" bson:"cId"` // 优惠券唯一ID
Name string `json:"name" bson:"name"` // 奖品名称
Type PrizeType `json:"type" bson:"type"` // 奖品类型
Count int32 `json:"count" bson:"count"` // 优惠券数量
Price int64 `json:"price" bson:"price"` // 奖品价值
Value int64 `json:"value" bson:"value"` // 折扣率/活跃值
Used bool `json:"used" bson:"used"` // 优惠券是否使用
ExpireTime time.Time `json:"expireTime" bson:"expireTime"` // 过期时间
CreateTime time.Time `json:"createTime" bson:"createTime"` // 创建时间
}
type AppGainUserInfoRes struct {
UID uint64 `json:"uid"` // 用户ID
LotteryTimes int64 `json:"lottery_times"` // 用户生剩余次数
Data string `json:"data"` // 数据
}
type EditSelector struct {
Used bool `json:"used" bson:"used"` // 优惠券是否使用
UpdateTime time.Time `json:"updateTime" bson:"updateTime"` // 修改时间
}
+236
View File
@@ -0,0 +1,236 @@
package coupon_record_mod
import (
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/models"
"context"
"errors"
"fmt"
"time"
"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"
)
var mdb *db.MongoDB
const table = models.CouponRecord
// InitIndex 设置index
func initIndex() {
coll := coll(nil)
many := []mongo.IndexModel{
{
Keys: bson.D{{"uid", 1}},
},
{
Keys: bson.D{{"cId", 1}},
Options: options.Index().SetUnique(true),
},
{
Keys: bson.D{{"createdAt", 1}},
},
{
Keys: bson.D{{"expireTime", 1}},
},
}
_, err := coll.CreateIndex(many)
if 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 Init() {
mdb = db.Init(table)
initIndex()
}
func InsertOne(cfg CouponRecord) error {
cfg.CreateTime = time.Now()
cfg.UpdateTime = time.Now()
if _, err := coll(nil).InsertOne(cfg); err != nil {
return err
}
return nil
}
func InsertManyCouponRecords(t *db.MongoTool, trans []CouponRecord) (err error) {
ops := options.InsertMany().SetOrdered(false)
_, err = coll(t).InsertMany(trans, ops)
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "InsertManyCouponRecords", table, "InsertMany", err))
}
return
}
func FindOneById(id primitive.ObjectID) (CouponRecord, error) {
cfg := CouponRecord{}
err := coll(nil).FindOne(&cfg, bson.M{"_id": id})
if err != nil {
return cfg, err
}
return cfg, err
}
func FindOneByCId(cId string) (CouponRecord, error) {
cfg := CouponRecord{}
err := coll(nil).FindOne(&cfg, bson.M{"cId": cId})
if err != nil {
return cfg, err
}
return cfg, err
}
func IsExist(uid uint64, videoId primitive.ObjectID) (bool, error) {
if count, err := coll(nil).Count(bson.M{"uid": uid, "videoId": videoId}); err != nil {
return false, err
} else if count == 0 {
return false, nil
}
return true, nil
}
// SubmitDocument 提交文档
func SubmitDocument(document CouponRecord) error {
result, err := coll(nil).InsertOne(&document)
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "SubmitDocument", table, "InsertOne", err),
log.Any("document", document),
)
return err
}
if result.InsertedID.(primitive.ObjectID).IsZero() {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "SubmitDocument", table, "result.InsertedID", "InsertedID is null"),
log.Any("document", document),
)
return errors.New("result is null")
}
return nil
}
// Remove 删除
func Remove(filter primitive.M) (err error) {
_, err = coll(nil).DeleteOne(filter)
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Remove", table, "DeleteOne", err),
log.Any("filter", filter),
)
return
}
return
}
// Edit 修改文档
func Edit(filter, update primitive.M) error {
result, err := coll(nil).UpdateOne(filter, update)
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Edit", table, "UpdateOne", err),
log.Any("filter", filter),
log.Any("update", update),
)
return err
}
if result.ModifiedCount == 0 {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Edit", table, "result.ModifiedCount", "ModifiedCount is zero"),
log.Any("filter", filter),
log.Any("update", update),
)
return errors.New("result is null")
}
return nil
}
// QueryAllList 分页查询文档
func QueryAllList(filter primitive.M, opts ...*options.FindOptions) (out []*AppCouponRecordRes, 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
}
}
// QueryAllWebList 分页查询文档
func QueryAllWebList(filter primitive.M, opts ...*options.FindOptions) (out []*CouponRecord, err error) {
if err = coll(nil).Find(&out, filter, opts...); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllWebList", table, "Find", err),
log.Any("filter", filter),
log.Any("opts", opts),
)
return nil, err
}
return
}
// QueryAllWebCount 查询文档条目数
func QueryAllWebCount(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:", "QueryAllWebCount", table, "Count", err),
log.Any("filter", filter),
)
return 0, err
} else {
return count, nil
}
}
// QueryCouponRecord 查询用户优惠券
func QueryCouponRecord(uid uint64, cId string) (cr *CouponRecord, err error) {
cr = &CouponRecord{}
err = coll(nil).FindOne(cr, bson.M{"uid": uid, "cId": cId})
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryCouponRecord", table, "FindOne", err),
log.Any("uid", uid),
log.Any("cId", cId),
)
}
return
}
// QueryCouponRecordById 查询用户优惠券
func QueryCouponRecordById(cId string) (cr *CouponRecord, err error) {
cr = &CouponRecord{}
err = coll(nil).FindOne(cr, bson.M{"cId": cId})
if err != nil {
log.Warn(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryCouponRecordById", table, "FindOne", err),
log.Any("cId", cId),
)
}
return
}
// UpdateOneById 修改状态
func UpdateOneById(ctx context.Context, t *db.MongoTool, cId string, update primitive.M) (
err error) {
_, err = coll(t).UpdateOne(bson.M{"cId": cId, "used": false}, update)
if err != nil {
log.WarnX(ctx, fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateOne", err),
log.Any("cId", cId),
log.Any("update", update),
)
}
return
}
+39
View File
@@ -0,0 +1,39 @@
package coupon_record_mod
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type PrizeType int
// 奖品类型 1-金币 2-游戏币 3-会员卡 4-vip 5-金币加赠券 6-金币观影券 7-实物
const (
PrizeTypeGold PrizeType = iota + 1 // 1、金币
PrizeTypeGameGold // 2、棋牌游戏币
PrizeTypeVIPDiscount // 3、会员折扣卡
PrizeTypeVIPCard // 4、会员卡
PrizeTypeGoldWatch // 5、金币观影券
PrizeTypeGoldCoinBonus // 6、金币加赠券
PrizeTypeInKind // 7、实物奖励
)
// CouponRecord 用户优惠券记录
type CouponRecord struct {
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"` // 文档ID
CID string `json:"cId" bson:"cId"` // 优惠券记录唯一ID
PID primitive.ObjectID `json:"pId" bson:"pId"` // 优惠券ID
UID uint64 `json:"uid" bson:"uid"` // 用户ID
UserName string `json:"userName" bson:"userName"` // 用户名
Name string `json:"name" bson:"name"` // 奖品名称
Type PrizeType `json:"type" bson:"type"` // 奖品类型
Count int32 `json:"count" bson:"count"` // 优惠券数量
Price int64 `json:"price" bson:"price"` // 奖品价值
Value int64 `json:"value" bson:"value"` // 折扣率/活跃值
Used bool `json:"used" bson:"used"` // 优惠券是否使用
IsDelete bool `json:"isDelete" bson:"isDelete"` // 优惠券是否删除
ExpireTime time.Time `json:"expireTime" bson:"expireTime"` // 过期时间
CreateTime time.Time `json:"createTime" bson:"createTime"` // 创建时间
UpdateTime time.Time `json:"updateTime" bson:"updateTime"` // 修改时间
}
+34
View File
@@ -0,0 +1,34 @@
package coupon_record_mod
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
)
// WebListReq 优惠券列表请求
type WebListReq struct {
Used *bool `form:"used" json:"used" ` // 优惠券使用类型 0表示返回全部 1表示只返回有效(未使用且未过期)
Uid *uint64 `form:"uid" json:"uid" ` // 用户ID
PageNumber int64 `form:"pageNumber" json:"pageNumber"` // 当前页码
PageSize int64 `form:"pageSize" json:"pageSize"` // 每页数量
}
type QueryAllWebRes struct {
List []*CouponRecord `json:"list"`
Total int64 `json:"total"`
}
func (receiver *WebListReq) Filter() bson.M {
var query = bson.M{}
if receiver.Used != nil {
query["used"] = receiver.Used
}
if receiver.Uid != nil {
query["uid"] = receiver.Uid
}
return query
}
func (receiver WebListReq) Options() *options.FindOptions {
return options.Find().SetSkip((receiver.PageNumber - 1) * receiver.PageSize).SetLimit(receiver.PageSize).SetSort(bson.M{"createTime": -1})
}