forked from all_user/stat_center
463 lines
15 KiB
Go
463 lines
15 KiB
Go
package activitymod
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"stat/common/log"
|
|
"stat/common/mongos"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var dbAct *mongos.MongoTool
|
|
var dbLotCodeInfo *mongos.MongoTool
|
|
var dbLotGift *mongos.MongoTool
|
|
var dbLotGetGift *mongos.MongoTool
|
|
|
|
func Init() {
|
|
ctx := context.Background()
|
|
InitAct(ctx)
|
|
InitLotCodeInfo(ctx)
|
|
InitLotGift(ctx)
|
|
InitLotGiftCode(ctx)
|
|
}
|
|
|
|
func InitAct(ctx context.Context) {
|
|
// 获取数据库表
|
|
dbAct = mongos.New(mongos.MgoManager, TN.ActivityInfo)
|
|
|
|
// 初始化
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: FN.ActivityInfo.Name, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.ActivityInfo.Enable, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.ActivityInfo.SortCode, Value: -1}},
|
|
},
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.ActivityInfo.StartTime, Value: -1},
|
|
{Key: FN.ActivityInfo.EndTime, Value: -1},
|
|
},
|
|
},
|
|
}
|
|
if _, err := dbAct.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", TN.ActivityInfo, err))
|
|
}
|
|
}
|
|
|
|
func InitLotCodeInfo(ctx context.Context) {
|
|
// 获取数据库表
|
|
dbLotCodeInfo = mongos.New(mongos.MgoManager, TN.LottertyCodeInfo)
|
|
|
|
// 初始化
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: FN.LottertyCodeInfo.LotteryCode, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LottertyCodeInfo.ActId, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LottertyCodeInfo.UserIp, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LottertyCodeInfo.IsUsed, Value: 1}},
|
|
},
|
|
}
|
|
if _, err := dbLotCodeInfo.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", TN.LottertyCodeInfo, err))
|
|
}
|
|
}
|
|
func InitLotGift(ctx context.Context) {
|
|
// 获取数据库表
|
|
dbLotGift = mongos.New(mongos.MgoManager, TN.LotteryGift)
|
|
|
|
// 初始化
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: FN.LotteryGift.ActId, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LotteryGift.GiftName, Value: 1}},
|
|
},
|
|
}
|
|
if _, err := dbLotGift.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", TN.LotteryGift, err))
|
|
}
|
|
}
|
|
|
|
func InitLotGiftCode(ctx context.Context) {
|
|
// 获取数据库表
|
|
dbLotGetGift = mongos.New(mongos.MgoManager, TN.LotteryGetGift)
|
|
|
|
// 初始化
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: FN.LotteryGetGift.GiftCode, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LotteryGetGift.AppName, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LotteryGetGift.ActId, Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: FN.LotteryGetGift.GiftId, Value: 1}},
|
|
},
|
|
}
|
|
if _, err := dbLotGetGift.CreateIndex(context.Background(), indexes); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", TN.LotteryGetGift, err))
|
|
}
|
|
}
|
|
|
|
//InsertOneActivityInfo 插入一个活动信息
|
|
func InsertOneActivityInfo(ctx context.Context, data *ActivityInfo) error {
|
|
if _, err := dbAct.InsertOne(ctx, data); err != nil {
|
|
log.Warn("activitymod:InsertOneActivityInfo db.InsertOne failed", log.E(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindOneActivityInfo 查找一个活动信息
|
|
func FindOneActivityInfo(ctx context.Context, filter bson.M) (*ActivityInfo, error) {
|
|
var data ActivityInfo
|
|
if err := dbAct.FindOneEx(ctx, &data, filter); err != nil {
|
|
log.Warn("activitymod:FindOneActivityInfo db.FindOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
|
|
return &data, nil
|
|
}
|
|
|
|
//DelOneActivityInfo 删除一个活动信息
|
|
func DelOneActivityInfo(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbAct.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("activitymod:DelOneActivityInfo db.DeleteOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateOneActivityInfo 更新一个活动信息
|
|
func UpdateOneActivityInfo(ctx context.Context, filter bson.M, update bson.M) error {
|
|
result, err := dbAct.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("activitymod:UpdateOneActivityInfo activitymod:UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return mongo.ErrNoDocuments
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//CountActivityInfos 统计活动信息数量
|
|
func CountActivityInfos(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbAct.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("activitymod:CountActivityInfos db.Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateManyActivityInfo 更新满足条件的活动信息
|
|
func UpdateManyActivityInfo(ctx context.Context, filter bson.M, update bson.M) error {
|
|
_, err := dbAct.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("user:UpdateManyActivityInfo db.UpdateMany failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindManyActivityInfo 查找一批活动信息数据
|
|
func FindManyActivityInfo(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (ActivityInfoList, error) {
|
|
data := make(ActivityInfoList, 0)
|
|
if err := dbAct.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("activitymod:FindManyActivityInfo db.Find failed", log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//FindManyActivityInfoWithBind 用自己定义结构查找一批活动信息数据
|
|
func FindManyActivityInfoWithBind(ctx context.Context, bind interface{}, filter bson.M, opts ...*options.FindOptions) error {
|
|
if err := dbAct.Find(ctx, bind, filter, opts...); err != nil {
|
|
log.Warn("activitymod:FindManyActivityInfoWithBind db.Find failed", log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//InsertOneLottertyCodeInfo 插入一个抽奖码信息
|
|
func InsertOneLottertyCodeInfo(ctx context.Context, data *LottertyCodeInfo) error {
|
|
if _, err := dbLotCodeInfo.InsertOne(ctx, data); err != nil {
|
|
log.Warn("activitymod:InsertOneLottertyCodeInfo db.InsertOne failed", log.E(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindOneLottertyCodeInfo 查找一个抽奖码信息
|
|
func FindOneLottertyCodeInfo(ctx context.Context, filter bson.M) (*LottertyCodeInfo, error) {
|
|
var data LottertyCodeInfo
|
|
if err := dbLotCodeInfo.FindOneEx(ctx, &data, filter); err != nil {
|
|
log.Warn("LottertyCodeInfo:FindOneLottertyCodeInfo activitymod:FindOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return &data, nil
|
|
}
|
|
|
|
//DelOneLottertyCodeInfo 删除一个抽奖码信息
|
|
func DelOneLottertyCodeInfo(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbLotCodeInfo.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("LottertyCodeInfo:DelOneLottertyCodeInfo activitymod:DeleteOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateOneLottertyCodeInfo 更新一个抽奖码信息
|
|
func UpdateOneLottertyCodeInfo(ctx context.Context, filter bson.M, update bson.M) error {
|
|
result, err := dbLotCodeInfo.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("UpdateOneLottertyCodeInfo activitymod:UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return mongo.ErrNoDocuments
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//CountLottertyCodeInfos 统计抽奖码信息数量
|
|
func CountLottertyCodeInfos(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbLotCodeInfo.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("LottertyCodeInfo:CountLottertyCodeInfos activitymod:Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateManyLottertyCodeInfo 更新满足条件的抽奖码信息
|
|
func UpdateManyLottertyCodeInfo(ctx context.Context, filter bson.M, update bson.M) error {
|
|
_, err := dbLotCodeInfo.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("user:UpdateManyLottertyCodeInfo activitymod:UpdateMany failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindManyLottertyCodeInfo 查找一批抽奖码信息数据
|
|
func FindManyLottertyCodeInfo(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (LottertyCodeInfoList, error) {
|
|
data := make(LottertyCodeInfoList, 0)
|
|
if err := dbLotCodeInfo.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("LottertyCodeInfo:FindManyLottertyCodeInfo activitymod:Find failed", log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//FindManyLottertyCodeInfoWithBind 用自己定义结构查找一批抽奖码信息数据
|
|
func FindManyLottertyCodeInfoWithBind(ctx context.Context, bind interface{}, filter bson.M, opts ...*options.FindOptions) error {
|
|
if err := dbLotCodeInfo.Find(ctx, bind, filter, opts...); err != nil {
|
|
log.Warn("LottertyCodeInfo:FindManyLottertyCodeInfoWithBind activitymod:Find failed", log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//InsertOneLotteryGift 插入一个奖品信息
|
|
func InsertOneLotteryGift(ctx context.Context, data *LotteryGift) error {
|
|
if _, err := dbLotGift.InsertOne(ctx, data); err != nil {
|
|
log.Warn("activitymod:InsertOneLotteryGift db.InsertOne failed", log.E(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindOneLotteryGift 查找一个奖品信息
|
|
func FindOneLotteryGift(ctx context.Context, filter bson.M) (*LotteryGift, error) {
|
|
var data LotteryGift
|
|
if err := dbLotGift.FindOneEx(ctx, &data, filter); err != nil {
|
|
log.Warn("LotteryGift:FindOneLotteryGift dbLotGift.FindOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return &data, nil
|
|
}
|
|
|
|
//DelOneLotteryGift 删除一个奖品信息
|
|
func DelOneLotteryGift(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbLotGift.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("LotteryGift:DelOneLotteryGift dbLotGift.DeleteOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateOneLotteryGift 更新一个奖品信息
|
|
func UpdateOneLotteryGift(ctx context.Context, filter bson.M, update bson.M) error {
|
|
result, err := dbLotGift.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("UpdateOneLotteryGift dbLotGift.UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return mongo.ErrNoDocuments
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//CountLotteryGifts 统计奖品信息数量
|
|
func CountLotteryGifts(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbLotGift.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("LotteryGift:CountLotteryGifts dbLotGift.Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateManyLotteryGift 更新满足条件的奖品信息
|
|
func UpdateManyLotteryGift(ctx context.Context, filter bson.M, update bson.M) error {
|
|
_, err := dbLotGift.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("user:UpdateManyLotteryGift dbLotGift.UpdateMany failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindManyLotteryGift 查找一批奖品信息数据
|
|
func FindManyLotteryGift(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (LotteryGiftList, error) {
|
|
data := make(LotteryGiftList, 0)
|
|
if err := dbLotGift.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("LotteryGift:FindManyLotteryGift dbLotGift.Find failed", log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//FindManyLotteryGiftWithBind 用自己定义结构查找一批奖品信息数据
|
|
func FindManyLotteryGiftWithBind(ctx context.Context, bind interface{}, filter bson.M, opts ...*options.FindOptions) error {
|
|
if err := dbLotGift.Find(ctx, bind, filter, opts...); err != nil {
|
|
log.Warn("LotteryGift:FindManyLotteryGiftWithBind dbLotGift.Find failed", log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//InsertOneLotteryGetGift 插入一个中奖兑换相关信息
|
|
func InsertOneLotteryGetGift(ctx context.Context, media *LotteryGetGift) error {
|
|
if _, err := dbLotGetGift.InsertOne(ctx, media); err != nil {
|
|
log.Warn("activitymod:InsertOneLotteryGetGift dbLotGetGift.InsertOne failed", log.E(err))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindOneLotteryGetGift 查找一个中奖兑换相关信息
|
|
func FindOneLotteryGetGift(ctx context.Context, filter bson.M) (*LotteryGetGift, error) {
|
|
var data LotteryGetGift
|
|
if err := dbLotGetGift.FindOneEx(ctx, &data, filter); err != nil {
|
|
log.Warn("LotteryGetGift:FindOneLotteryGetGift dbLotGetGift.FindOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return &data, nil
|
|
}
|
|
|
|
//DelOneLotteryGetGift 删除一个中奖兑换相关信息
|
|
func DelOneLotteryGetGift(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbLotGetGift.DeleteOne(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("LotteryGetGift:DelOneLotteryGetGift dbLotGetGift.DeleteOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateOneLotteryGetGift 更新一个中奖兑换相关信息
|
|
func UpdateOneLotteryGetGift(ctx context.Context, filter bson.M, update bson.M) error {
|
|
result, err := dbLotGetGift.UpdateOne(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("UpdateOneLotteryGetGift dbLotGetGift.UpdateOne failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
if result.MatchedCount == 0 {
|
|
return mongo.ErrNoDocuments
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//CountLotteryGetGifts 统计中奖兑换相关信息数量
|
|
func CountLotteryGetGifts(ctx context.Context, filter bson.M) (int64, error) {
|
|
count, err := dbLotGetGift.Count(ctx, filter)
|
|
if err != nil {
|
|
log.Warn("LotteryGetGift:CountLotteryGetGifts dbLotGetGift.Count failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return 0, err
|
|
}
|
|
return count, nil
|
|
}
|
|
|
|
//UpdateManyLotteryGetGift 更新满足条件的中奖兑换相关信息
|
|
func UpdateManyLotteryGetGift(ctx context.Context, filter bson.M, update bson.M) error {
|
|
_, err := dbLotGetGift.UpdateMany(ctx, filter, update)
|
|
if err != nil {
|
|
log.Warn("user:UpdateManyLotteryGetGift dbLotGetGift.UpdateMany failed",
|
|
log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//FindManyLotteryGetGift 查找一批渠道中奖兑换相关信息
|
|
func FindManyLotteryGetGift(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (LotteryGetGiftList, error) {
|
|
data := make(LotteryGetGiftList, 0)
|
|
if err := dbLotGetGift.Find(ctx, &data, filter, opts...); err != nil {
|
|
log.Warn("LotteryGetGift:FindManyLotteryGetGift dbLotGetGift.Find failed", log.E(err), log.Any("filter", filter))
|
|
return nil, err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
//FindManyLotteryGetGiftWithBind 用自己定义结构查找一批渠道中奖兑换相关信息
|
|
func FindManyLotteryGetGiftWithBind(ctx context.Context, bind interface{}, filter bson.M, opts ...*options.FindOptions) error {
|
|
if err := dbLotGetGift.Find(ctx, bind, filter, opts...); err != nil {
|
|
log.Warn("LotteryGetGift:FindManyLotteryGetGiftWithBind dbLotGetGift.Find failed", log.E(err), log.Any("filter", filter))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|