Initial commit
This commit is contained in:
@@ -0,0 +1,207 @@
|
||||
package activitySer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"stat/api/proto"
|
||||
"stat/common/stdLog"
|
||||
"stat/models/activityVisit"
|
||||
"stat/models/activitymod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type ChannelParams struct {
|
||||
PC string `json:"i" bson:"i"` //推广码
|
||||
DC string `json:"dc" bson:"dc"` //商区序列
|
||||
CH string `json:"ch" bson:"ch"` //渠道
|
||||
}
|
||||
|
||||
// 活动列表
|
||||
func GetActivityList(ctx context.Context, param proto.ActivityListReq) (stdLog.CodeType, *proto.ActivityListResp, error) {
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
if param.PageNum <= 0 {
|
||||
param.PageNum = 1
|
||||
}
|
||||
data := &proto.ActivityListResp{List: make([]*proto.ActivityInfo, 0)}
|
||||
filter := bson.M{}
|
||||
if param.Name != nil {
|
||||
filter[activitymod.FN.ActivityInfo.Name] = bson.M{"$regex": *param.Name}
|
||||
}
|
||||
if param.Enable != nil {
|
||||
filter[activitymod.FN.ActivityInfo.Enable] = *param.Enable
|
||||
}
|
||||
count, err := activitymod.CountActivityInfos(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.Total = count
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
skip := (param.PageNum - 1) * param.PageSize
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(param.PageSize)
|
||||
opt.SetSort(bson.M{activitymod.FN.ActivityInfo.SortCode: -1})
|
||||
err = activitymod.FindManyActivityInfoWithBind(ctx, &data.List, filter, opt)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
for _, v := range data.List {
|
||||
lcFilter := bson.M{activitymod.FN.LottertyCodeInfo.ActId: v.Id.Hex()}
|
||||
v.LotteryCodeNum, _ = activitymod.CountLottertyCodeInfos(ctx, lcFilter)
|
||||
|
||||
lcFilter[activitymod.FN.LottertyCodeInfo.IsUsed] = true
|
||||
v.UsedLotteryCodeNum, _ = activitymod.CountLottertyCodeInfos(ctx, lcFilter)
|
||||
|
||||
gcFilter := bson.M{activitymod.FN.LotteryGetGift.ActId: v.Id.Hex()}
|
||||
v.GiftCodeNum, _ = activitymod.CountLotteryGetGifts(ctx, gcFilter)
|
||||
|
||||
gcgFilter := bson.M{
|
||||
activitymod.FN.LotteryGetGift.ActId: v.Id.Hex(),
|
||||
activitymod.FN.LotteryGetGift.IsGaveOut: true,
|
||||
}
|
||||
v.GiftCodeGivedNum, _ = activitymod.CountLotteryGetGifts(ctx, gcgFilter)
|
||||
|
||||
gcuFilter := bson.M{
|
||||
activitymod.FN.LotteryGetGift.ActId: v.Id.Hex(),
|
||||
activitymod.FN.LotteryGetGift.IsUsed: true,
|
||||
}
|
||||
v.GiftCodeUsedNum, _ = activitymod.CountLotteryGetGifts(ctx, gcuFilter)
|
||||
|
||||
}
|
||||
return stdLog.Success, data, nil
|
||||
}
|
||||
|
||||
// 添加活动
|
||||
func AddOneAct(ctx context.Context, param proto.AddActivityReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
doc := activitymod.ActivityInfo{
|
||||
Id: primitive.NewObjectID(),
|
||||
Name: param.Name,
|
||||
Desc: param.Desc,
|
||||
SortCode: param.SortCode,
|
||||
H5Domain: param.H5Domain,
|
||||
GiftDesc: param.GiftDesc,
|
||||
Enable: param.Enable,
|
||||
Background: param.Background,
|
||||
StartTime: param.StartTime,
|
||||
EndTime: param.EndTime,
|
||||
CreatedAt: now,
|
||||
Banner: param.Banner,
|
||||
}
|
||||
err := activitymod.InsertOneActivityInfo(ctx, &doc)
|
||||
if err != nil {
|
||||
return stdLog.InsertDataError, err
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 更新一个活动
|
||||
func UpdateOneAct(ctx context.Context, param *proto.UpdateActivityReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
param.UpdatedAt = now
|
||||
filter := bson.M{activitymod.FN.ActivityInfo.Id: param.Id}
|
||||
updateDoc := bson.M{"$set": param}
|
||||
err := activitymod.UpdateOneActivityInfo(ctx, filter, updateDoc)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 删除一个活动
|
||||
func DeleteOneAct(ctx context.Context, param proto.DelActivityReq) (stdLog.CodeType, error) {
|
||||
filter := bson.M{activitymod.FN.ActivityInfo.Id: param.Id}
|
||||
count, err := activitymod.DelOneActivityInfo(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
if count == 0 {
|
||||
return stdLog.DataNotExist, nil
|
||||
}
|
||||
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
func UpdateActivityVisitCount(ctx context.Context, peerIp string, param proto.ActivityVisitCountReq) (stdLog.CodeType, error) {
|
||||
if param.Ip == "" {
|
||||
param.Ip = peerIp
|
||||
}
|
||||
aa := &activityVisit.ActivityVisit{
|
||||
AppId: param.AppId,
|
||||
IP: param.Ip,
|
||||
CH: param.CH,
|
||||
ActivityId: param.ActivityId,
|
||||
ActivityName: param.ActivityName,
|
||||
PeerIP: peerIp,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
if err := activityVisit.InsertActivityVisit(ctx, aa); err != nil {
|
||||
return stdLog.InsertDataError, err
|
||||
}
|
||||
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 活动列表
|
||||
func GetActivityVisitList(ctx context.Context, param proto.ActivityVisitCountListReq) (stdLog.CodeType, *proto.ActivityVisitListResp, error) {
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
if param.PageNum <= 0 {
|
||||
param.PageNum = 1
|
||||
}
|
||||
data := &proto.ActivityVisitListResp{List: make([]*proto.ActivityVisitInfo, 0)}
|
||||
filter := bson.M{}
|
||||
if param.ActivityId != nil {
|
||||
filter["activityId"] = *param.ActivityId
|
||||
}
|
||||
if param.AppId != nil {
|
||||
filter["appId"] = *param.AppId
|
||||
}
|
||||
if param.CH != nil {
|
||||
filter["ch"] = *param.CH
|
||||
}
|
||||
count, err := activityVisit.CountActivityVisit(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.Total = count
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
skip := (param.PageNum - 1) * param.PageSize
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(param.PageSize)
|
||||
opt.SetSort(bson.M{"createdAt": -1})
|
||||
lists, err := activityVisit.FindManyActivityList(ctx, filter, opt)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
for _, v := range lists {
|
||||
data.List = append(data.List, &proto.ActivityVisitInfo{
|
||||
AppId: v.AppId,
|
||||
IP: v.IP,
|
||||
CH: v.CH,
|
||||
DevType: v.DevType,
|
||||
SysType: v.SysType,
|
||||
PeerIP: v.PeerIP,
|
||||
ActivityId: v.ActivityId.Hex(),
|
||||
ActivityName: v.ActivityName,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
})
|
||||
|
||||
}
|
||||
return stdLog.Success, data, nil
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
package activitySer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"stat/api/proto"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"stat/common/redis"
|
||||
"stat/common/stdLog"
|
||||
"stat/common/utils"
|
||||
"stat/models/activitymod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
var ipDrawLimit = 20 // 同一IP每天抽奖限制
|
||||
|
||||
// H5活动详情
|
||||
func GetActivityInfo(ctx context.Context, param proto.ActivityInfoReq) (stdLog.CodeType, *proto.ActivityInfoResp, error) {
|
||||
now := time.Now()
|
||||
res := &proto.ActivityInfoResp{}
|
||||
filter := bson.M{activitymod.FN.ActivityInfo.Id: param.ActId}
|
||||
|
||||
data, err := activitymod.FindOneActivityInfo(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, res, err
|
||||
}
|
||||
if data == nil {
|
||||
return stdLog.ActivityNoExistErr, res, fmt.Errorf("data does not exist")
|
||||
}
|
||||
if now.Before(data.StartTime) || now.After(data.EndTime) || !data.Enable {
|
||||
return stdLog.ActivityNoExistErr, res, fmt.Errorf("activity is closed")
|
||||
}
|
||||
res = &proto.ActivityInfoResp{
|
||||
Id: data.Id,
|
||||
Name: data.Name,
|
||||
Desc: data.Desc,
|
||||
H5Domain: data.H5Domain,
|
||||
GiftDesc: data.GiftDesc,
|
||||
Enable: data.Enable,
|
||||
StartTime: data.StartTime,
|
||||
EndTime: data.EndTime,
|
||||
Banner: data.Banner,
|
||||
}
|
||||
return stdLog.Success, res, nil
|
||||
}
|
||||
|
||||
// H5页面奖品信息
|
||||
func GetActivityGiftInfo(ctx context.Context, param proto.ActivityGiftInfoReq) (stdLog.CodeType, *proto.ActivityGiftInfoResp, error) {
|
||||
data := &proto.ActivityGiftInfoResp{List: make([]*proto.H5GiftInfo, 0)}
|
||||
filter := bson.M{activitymod.FN.LotteryGift.ActId: param.ActId}
|
||||
count, err := activitymod.CountLotteryGifts(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.Count = count
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
opt.SetSort(bson.M{activitymod.FN.LotteryGift.Sort: 1})
|
||||
if err := activitymod.FindManyLotteryGiftWithBind(ctx, &data.List, filter, opt); err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
return stdLog.Success, data, nil
|
||||
}
|
||||
|
||||
// 抽奖
|
||||
func Draw(ctx context.Context, param *proto.DrawReq) (stdLog.CodeType, *proto.DrawResp, error) {
|
||||
res := &proto.DrawResp{}
|
||||
now := time.Now()
|
||||
|
||||
// 活动是否存在
|
||||
actFilter := bson.M{activitymod.FN.ActivityInfo.Id: param.ActId}
|
||||
data, err := activitymod.FindOneActivityInfo(ctx, actFilter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, res, err
|
||||
}
|
||||
if data == nil {
|
||||
return stdLog.ActivityNoExistErr, res, fmt.Errorf("data does not exist")
|
||||
}
|
||||
if now.Before(data.StartTime) || now.After(data.EndTime) || !data.Enable {
|
||||
return stdLog.ActivityNoExistErr, res, fmt.Errorf("activity is closed")
|
||||
}
|
||||
|
||||
// 抽奖码是否有效
|
||||
lcFilter := bson.M{
|
||||
activitymod.FN.LottertyCodeInfo.LotteryCode: param.LotteryCode,
|
||||
activitymod.FN.LottertyCodeInfo.ActId: param.ActId.Hex(),
|
||||
}
|
||||
lcInfo, err := activitymod.FindOneLottertyCodeInfo(ctx, lcFilter)
|
||||
if err != nil || lcInfo == nil {
|
||||
return stdLog.LotteryCodeIsInvalid, res, fmt.Errorf("lottery code is invalid")
|
||||
}
|
||||
if lcInfo.IsUsed {
|
||||
return stdLog.LotteryCodeIsUsed, res, fmt.Errorf("lottery code is used")
|
||||
}
|
||||
|
||||
// 根据用户IP限制抽奖次数
|
||||
if !CanUserDraw(param.Ip, param.ActId.Hex()) {
|
||||
return stdLog.LotteryNumReachedTheLimit, res, fmt.Errorf("the number of lottery reached the limit")
|
||||
}
|
||||
|
||||
// 抽奖
|
||||
gFilter := bson.M{activitymod.FN.LotteryGift.ActId: param.ActId.Hex()}
|
||||
opt := &options.FindOptions{}
|
||||
opt.SetSort(bson.M{activitymod.FN.LotteryGift.Rate: -1})
|
||||
giftList, err := activitymod.FindManyLotteryGift(ctx, gFilter, opt)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, res, fmt.Errorf("lottery gift data err")
|
||||
}
|
||||
var giftRand int32
|
||||
for _, v := range giftList {
|
||||
giftRand += v.Rate
|
||||
}
|
||||
|
||||
flag := true
|
||||
for {
|
||||
randNum := rand.Int31n(giftRand)
|
||||
// fmt.Printf("\ngiftRand is %d at 1st\n", giftRand)
|
||||
// fmt.Printf("\nrandNum is %d at 1st\n", randNum)
|
||||
for _, w := range giftList {
|
||||
// fmt.Printf("randNum is %d before minus\n", randNum)
|
||||
// fmt.Printf("\ngiftRand is %d before minus\n", giftRand)
|
||||
randNum -= w.Rate
|
||||
giftRand -= w.Rate
|
||||
// fmt.Printf("\nAfter minus randNum is %d\nw.Rate is %d\ngift info %+v\n", randNum, w.Rate, w)
|
||||
// fmt.Printf("\nAfter minus giftRand is %d\n", giftRand)
|
||||
if randNum <= 0 {
|
||||
var notgaveNum int64 // total 每种奖品的总数量, givedNum 每种奖品未被抽中的数量
|
||||
gFilter := bson.M{
|
||||
activitymod.FN.LotteryGetGift.GiftId: w.Id.Hex(),
|
||||
activitymod.FN.LotteryGetGift.ActId: w.ActId,
|
||||
activitymod.FN.LotteryGetGift.LotteryCode: "",
|
||||
}
|
||||
info, _ := activitymod.FindManyLotteryGetGift(ctx, gFilter)
|
||||
notgaveNum = int64(len(info))
|
||||
if notgaveNum == 0 {
|
||||
randNum = rand.Int31n(giftRand + 1)
|
||||
continue
|
||||
}
|
||||
res.GiftId = w.Id.Hex()
|
||||
res.GiftName = w.GiftName
|
||||
res.GiftCode = info[0].GiftCode
|
||||
flag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if !flag {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
fn := func(sctx mongo.SessionContext) error {
|
||||
// 更新lotterycodeinfo
|
||||
lciFilter := bson.M{
|
||||
activitymod.FN.LottertyCodeInfo.LotteryCode: param.LotteryCode,
|
||||
activitymod.FN.LottertyCodeInfo.ActId: param.ActId.Hex(),
|
||||
}
|
||||
lciUpdate := bson.M{"$set": bson.M{
|
||||
activitymod.FN.LottertyCodeInfo.UserIp: param.Ip,
|
||||
activitymod.FN.LottertyCodeInfo.LotteryAt: now,
|
||||
activitymod.FN.LottertyCodeInfo.UpdatedAt: now,
|
||||
activitymod.FN.LottertyCodeInfo.IsUsed: true,
|
||||
activitymod.FN.LottertyCodeInfo.GiftCode: res.GiftCode,
|
||||
activitymod.FN.LottertyCodeInfo.Remark: "已使用",
|
||||
}}
|
||||
err := activitymod.UpdateOneLottertyCodeInfo(sctx, lciFilter, lciUpdate)
|
||||
if err != nil {
|
||||
log.Warn("Draw: UpdateOneLottertyCodeInfo err", log.Any("err", err))
|
||||
return err
|
||||
}
|
||||
|
||||
// 更新lotterygetgift
|
||||
lggFilter := bson.M{
|
||||
activitymod.FN.LotteryGetGift.GiftCode: res.GiftCode,
|
||||
activitymod.FN.LotteryGetGift.ActId: param.ActId.Hex(),
|
||||
}
|
||||
lggUpdate := bson.M{"$set": bson.M{
|
||||
activitymod.FN.LotteryGetGift.UpdatedAt: now,
|
||||
activitymod.FN.LotteryGetGift.LotteryCode: param.LotteryCode,
|
||||
activitymod.FN.LotteryGetGift.IsGaveOut: true,
|
||||
activitymod.FN.LotteryGetGift.Remark: "兑换码已发放",
|
||||
}}
|
||||
err = activitymod.UpdateOneLotteryGetGift(sctx, lggFilter, lggUpdate)
|
||||
if err != nil {
|
||||
log.Error("Draw: UpdateOneLotteryGetGift err", log.Any("err", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
if err := mongos.Transaction(ctx, fn); err != nil {
|
||||
return stdLog.Failure, &proto.DrawResp{}, err
|
||||
}
|
||||
return stdLog.Success, res, nil
|
||||
}
|
||||
|
||||
// 验证用户Ip是否能抽奖
|
||||
func CanUserDraw(ip, actId string) bool {
|
||||
redisKey := fmt.Sprintf("actId:%s:userIp:%s", actId, ip)
|
||||
numStr, err := redis.StatRds.Get(redisKey)
|
||||
if err != nil {
|
||||
log.Error("canUserDraw redis get err", log.E(err))
|
||||
return false
|
||||
}
|
||||
var num = int64(0)
|
||||
if numStr != nil {
|
||||
num, err = strconv.ParseInt(*numStr, 10, 64)
|
||||
if err != nil {
|
||||
log.Error("canUserDraw str to int64 err", log.E(err))
|
||||
return false
|
||||
}
|
||||
}
|
||||
if num > int64(ipDrawLimit) {
|
||||
log.Warn("canUserDraw user IP triggered the limit")
|
||||
return false
|
||||
}
|
||||
val, err := redis.StatRds.Incr(redisKey)
|
||||
if err != nil {
|
||||
log.Error("canUserDraw redis get err", log.E(err))
|
||||
return false
|
||||
}
|
||||
if val == 1 {
|
||||
duration := redis.StatRds.TTL(redisKey)
|
||||
if duration == time.Duration(time.Second*-1) {
|
||||
expiredAt := utils.EndOfToday().UTC().Sub(time.Now().UTC())
|
||||
// expiredAt = expiredAt - (time.Second * 36200)
|
||||
// str := expiredAt.String()
|
||||
// fmt.Printf("\nexpiredAt is %d\n", expiredAt)
|
||||
// fmt.Printf("\nstr is %s\n", str)
|
||||
redis.StatRds.Expire(redisKey, expiredAt)
|
||||
}
|
||||
}
|
||||
if val > int64(ipDrawLimit) {
|
||||
log.Warn("canUserDraw user IP triggered the limit")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package activitySer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"stat/common/log"
|
||||
"stat/common/redis"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func initRedis() error {
|
||||
rds, err := redis.NewManagerWithUrl("redis://:B6vJHlYiTseLj77z@118.107.44.18:16789/7")
|
||||
if err != nil {
|
||||
log.Error("rds init err", log.E(err))
|
||||
return err
|
||||
}
|
||||
|
||||
redis.StatRds = rds
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestCanUserDraw(t *testing.T) {
|
||||
|
||||
// 初始化redis
|
||||
if err := initRedis(); err != nil {
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
res := CanUserDraw("610.0.21.100", "6063296c6d2c51859840b072")
|
||||
fmt.Print(res)
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package activitySer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"stat/api/proto"
|
||||
"stat/common/gencode"
|
||||
"stat/common/stdLog"
|
||||
"stat/models/activitymod"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 生成活动抽奖码
|
||||
func GenLotteryCodes(ctx context.Context, param proto.GenLotteryCodeReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
filter := bson.M{activitymod.FN.ActivityInfo.Id: param.ActId}
|
||||
info, err := activitymod.FindOneActivityInfo(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, err
|
||||
}
|
||||
doc := &activitymod.LottertyCodeInfo{
|
||||
ActId: param.ActId.Hex(),
|
||||
ActName: info.Name,
|
||||
IsUsed: false,
|
||||
CreatedAt: now,
|
||||
}
|
||||
for i := 0; i < int(param.CodeNum); i++ {
|
||||
code, err := genLotteryCode(ctx, int(param.CodeLen))
|
||||
if err != nil {
|
||||
return stdLog.Failure, err
|
||||
}
|
||||
doc.LotteryCode = code
|
||||
doc.Id = primitive.NewObjectID()
|
||||
if err := activitymod.InsertOneLottertyCodeInfo(ctx, doc); err != nil {
|
||||
return stdLog.InsertDataError, err
|
||||
}
|
||||
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
func genLotteryCode(ctx context.Context, len int) (string, error) {
|
||||
// 生成码
|
||||
for {
|
||||
code := strings.ToUpper(gencode.CodeGenerate(len))
|
||||
filter := bson.M{activitymod.FN.LottertyCodeInfo.LotteryCode: code}
|
||||
_, err := activitymod.FindOneLottertyCodeInfo(ctx, filter)
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return "", err
|
||||
}
|
||||
return code, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 修改抽奖码信息
|
||||
func UpdateLotteryCode(ctx context.Context, param proto.UpdateLotteryCodeReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
filter := bson.M{activitymod.FN.LottertyCodeInfo.Id: param.LotteryCodeId}
|
||||
updateDoc := bson.M{"$set": bson.M{
|
||||
activitymod.FN.LottertyCodeInfo.Remark: param.Mark,
|
||||
activitymod.FN.LottertyCodeInfo.UpdatedAt: now,
|
||||
}}
|
||||
err := activitymod.UpdateOneLottertyCodeInfo(ctx, filter, updateDoc)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 删除一个活动抽奖码
|
||||
func DeleteOneLotteryCode(ctx context.Context, param proto.DelLotteryCodeReq) (stdLog.CodeType, error) {
|
||||
filter := bson.M{activitymod.FN.ActivityInfo.Id: param.LotteryCodeId}
|
||||
count, err := activitymod.DelOneLottertyCodeInfo(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
if count == 0 {
|
||||
return stdLog.DataNotExist, nil
|
||||
}
|
||||
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 活动抽奖码列表
|
||||
func GetLotteryCodeList(ctx context.Context, param proto.LotteryCodeListReq) (stdLog.CodeType, *proto.LotteryCodeListResp, error) {
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
if param.PageNum <= 0 {
|
||||
param.PageNum = 1
|
||||
}
|
||||
data := &proto.LotteryCodeListResp{List: make([]*activitymod.LottertyCodeInfo, 0)}
|
||||
filter := bson.M{}
|
||||
if param.LotteryCode != nil {
|
||||
filter[activitymod.FN.LottertyCodeInfo.LotteryCode] = *param.LotteryCode
|
||||
}
|
||||
if param.ActId != nil {
|
||||
filter[activitymod.FN.LottertyCodeInfo.ActId] = *param.ActId
|
||||
}
|
||||
if param.ActName != nil {
|
||||
filter[activitymod.FN.LottertyCodeInfo.ActName] = bson.M{"$regex": *param.ActName}
|
||||
}
|
||||
if param.IsUsed != nil {
|
||||
filter[activitymod.FN.LottertyCodeInfo.IsUsed] = *param.IsUsed
|
||||
}
|
||||
if param.GiftCode != nil {
|
||||
filter[activitymod.FN.LottertyCodeInfo.GiftCode] = *param.GiftCode
|
||||
}
|
||||
count, err := activitymod.CountLottertyCodeInfos(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.Total = count
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
skip := (param.PageNum - 1) * param.PageSize
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(param.PageSize)
|
||||
opt.SetSort(bson.M{activitymod.FN.LottertyCodeInfo.CreatedAt: -1})
|
||||
list, err := activitymod.FindManyLottertyCodeInfo(ctx, filter, opt)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.List = list
|
||||
return stdLog.Success, data, nil
|
||||
}
|
||||
@@ -0,0 +1,174 @@
|
||||
package activitySer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"stat/api/proto"
|
||||
"stat/common/gencode"
|
||||
"stat/common/stdLog"
|
||||
"stat/models/activitymod"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// 生成活动奖品兑换码
|
||||
func GenManyGiftCode(ctx context.Context, param proto.GenGiftCodeRes) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
filter := bson.M{activitymod.FN.LotteryGift.Id: param.GiftId}
|
||||
info, err := activitymod.FindOneLotteryGift(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, err
|
||||
}
|
||||
doc := &activitymod.LotteryGetGift{
|
||||
ActId: info.ActId,
|
||||
ActName: info.ActName,
|
||||
GiftId: info.Id.Hex(),
|
||||
GiftName: info.GiftName,
|
||||
CreatedAt: now,
|
||||
}
|
||||
for i := 0; i < int(param.CodeNum); i++ {
|
||||
code, err := genGiftCode(ctx, int(param.CodeLen))
|
||||
if err != nil {
|
||||
return stdLog.Failure, err
|
||||
}
|
||||
doc.GiftCode = code
|
||||
doc.Id = primitive.NewObjectID()
|
||||
if err = activitymod.InsertOneLotteryGetGift(ctx, doc); err != nil {
|
||||
return stdLog.InsertDataError, err
|
||||
}
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
func genGiftCode(ctx context.Context, len int) (string, error) {
|
||||
// 生成码
|
||||
for {
|
||||
code := strings.ToUpper(gencode.CodeGenerate(len))
|
||||
filter := bson.M{activitymod.FN.LotteryGetGift.GiftCode: code}
|
||||
_, err := activitymod.FindOneLotteryGetGift(ctx, filter)
|
||||
if err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
return "", err
|
||||
}
|
||||
return code, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 修改奖品兑换信息
|
||||
func UpdateOneRecvGiftInfo(ctx context.Context, param *proto.UpdateGetGiftReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
if param.GiftCode == "" {
|
||||
return stdLog.ParamError, fmt.Errorf("giftcode param err")
|
||||
}
|
||||
filter := bson.M{
|
||||
activitymod.FN.LotteryGetGift.Id: param.Id,
|
||||
activitymod.FN.LotteryGetGift.GiftCode: param.GiftCode,
|
||||
}
|
||||
doc := bson.M{}
|
||||
if param.UserId == nil || *param.UserId == "" {
|
||||
return stdLog.ParamError, fmt.Errorf("userId can not be nil")
|
||||
} else {
|
||||
doc[activitymod.FN.LotteryGetGift.UserId] = *param.UserId
|
||||
}
|
||||
if param.AppName == nil || *param.AppName == "" {
|
||||
return stdLog.ParamError, fmt.Errorf("appName can not be nil")
|
||||
} else {
|
||||
doc[activitymod.FN.LotteryGetGift.AppName] = *param.AppName
|
||||
}
|
||||
if param.Remark != nil {
|
||||
doc[activitymod.FN.LotteryGetGift.Remark] = *param.Remark
|
||||
}
|
||||
doc[activitymod.FN.LotteryGetGift.GetGiftAt] = now
|
||||
doc[activitymod.FN.LotteryGetGift.UpdatedAt] = now
|
||||
doc[activitymod.FN.LotteryGetGift.IsUsed] = true
|
||||
updateDoc := bson.M{"$set": doc}
|
||||
err := activitymod.UpdateOneLotteryGetGift(ctx, filter, updateDoc)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 删除一个活动奖品兑换信息
|
||||
func DelOneRecvGiftInfo(ctx context.Context, param proto.DelGetGiftReq) (stdLog.CodeType, error) {
|
||||
filter := bson.M{activitymod.FN.LotteryGetGift.Id: param.Id}
|
||||
count, err := activitymod.DelOneLotteryGetGift(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
if count == 0 {
|
||||
return stdLog.DataNotExist, nil
|
||||
}
|
||||
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 奖品兑换详情列表
|
||||
func GetReceiveGiftInfoList(ctx context.Context, param proto.GetGiftInfoReq) (stdLog.CodeType, *proto.GetGiftInfoResp, error) {
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
if param.PageNum <= 0 {
|
||||
param.PageNum = 1
|
||||
}
|
||||
data := &proto.GetGiftInfoResp{List: make(activitymod.LotteryGetGiftList, 0)}
|
||||
filter := bson.M{}
|
||||
if param.LotteryCode != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.LotteryCode] = *param.LotteryCode
|
||||
}
|
||||
if param.ActId != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.ActId] = bson.M{"$regex": *param.ActId}
|
||||
}
|
||||
if param.ActName != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.ActName] = bson.M{"$regex": *param.ActName}
|
||||
}
|
||||
if param.GiftId != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.GiftId] = bson.M{"$regex": *param.GiftId}
|
||||
}
|
||||
if param.GiftName != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.GiftName] = *param.GiftName
|
||||
}
|
||||
if param.UserId != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.UserId] = *param.UserId
|
||||
}
|
||||
if param.AppName != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.AppName] = *param.AppName
|
||||
}
|
||||
if param.IsUsed != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.IsUsed] = *param.IsUsed
|
||||
}
|
||||
if param.GiftCode != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.GiftCode] = *param.GiftCode
|
||||
}
|
||||
if param.GiftCode != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.GiftCode] = *param.GiftCode
|
||||
}
|
||||
if param.IsGaveOut != nil {
|
||||
filter[activitymod.FN.LotteryGetGift.IsGaveOut] = *param.IsGaveOut
|
||||
}
|
||||
count, err := activitymod.CountLotteryGetGifts(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.Total = count
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
skip := (param.PageNum - 1) * param.PageSize
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(param.PageSize)
|
||||
opt.SetSort(bson.M{activitymod.FN.LotteryGetGift.CreatedAt: -1})
|
||||
if err := activitymod.FindManyLotteryGetGiftWithBind(ctx, &data.List, filter, opt); err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
return stdLog.Success, data, nil
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package activitySer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"stat/api/proto"
|
||||
"stat/common/stdLog"
|
||||
"stat/models/activitymod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
// 添加活动奖品
|
||||
func AddOneGift(ctx context.Context, param proto.AddGiftReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
filter := bson.M{activitymod.FN.ActivityInfo.Id: param.ActId}
|
||||
info, err := activitymod.FindOneActivityInfo(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, err
|
||||
}
|
||||
doc := &activitymod.LotteryGift{
|
||||
Id: primitive.NewObjectID(),
|
||||
ActId: info.Id.Hex(),
|
||||
ActName: info.Name,
|
||||
GiftName: param.GiftName,
|
||||
GiftDesc: param.GiftDesc,
|
||||
Rate: param.Rate,
|
||||
Sort: param.Sort,
|
||||
CreatedAt: now,
|
||||
}
|
||||
if err = activitymod.InsertOneLotteryGift(ctx, doc); err != nil {
|
||||
return stdLog.InsertDataError, err
|
||||
}
|
||||
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 修改奖品信息
|
||||
func UpdateOneGift(ctx context.Context, param *proto.UpdateGiftReq) (stdLog.CodeType, error) {
|
||||
now := time.Now()
|
||||
param.UpdatedAt = now
|
||||
filter := bson.M{activitymod.FN.LotteryGift.Id: param.Id}
|
||||
updateDoc := bson.M{"$set": param}
|
||||
err := activitymod.UpdateOneLotteryGift(ctx, filter, updateDoc)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 删除一个活动奖品
|
||||
func DeleteOneGift(ctx context.Context, param proto.DelGiftReq) (stdLog.CodeType, error) {
|
||||
filter := bson.M{activitymod.FN.LotteryGift.Id: param.Id}
|
||||
count, err := activitymod.DelOneLotteryGift(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.UpdateDataError, err
|
||||
}
|
||||
if count == 0 {
|
||||
return stdLog.DataNotExist, nil
|
||||
}
|
||||
|
||||
return stdLog.Success, nil
|
||||
}
|
||||
|
||||
// 活动奖品列表
|
||||
func GetLotteryGiftList(ctx context.Context, param proto.GiftListReq) (stdLog.CodeType, *proto.GiftListResp, error) {
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
if param.PageNum <= 0 {
|
||||
param.PageNum = 1
|
||||
}
|
||||
data := &proto.GiftListResp{List: make([]*proto.GiftInfo, 0)}
|
||||
filter := bson.M{}
|
||||
if param.ActId != nil {
|
||||
filter[activitymod.FN.LotteryGift.ActId] = *param.ActId
|
||||
}
|
||||
if param.GiftName != nil {
|
||||
filter[activitymod.FN.LotteryGift.GiftName] = bson.M{"$regex": *param.GiftName}
|
||||
}
|
||||
if param.GiftDesc != nil {
|
||||
filter[activitymod.FN.LotteryGift.GiftDesc] = bson.M{"$regex": *param.GiftDesc}
|
||||
}
|
||||
if param.Rate != nil {
|
||||
filter[activitymod.FN.LotteryGift.Rate] = *param.Rate
|
||||
}
|
||||
count, err := activitymod.CountLotteryGifts(ctx, filter)
|
||||
if err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
data.Total = count
|
||||
|
||||
opt := &options.FindOptions{}
|
||||
skip := (param.PageNum - 1) * param.PageSize
|
||||
if param.PageSize <= 0 {
|
||||
param.PageSize = 10
|
||||
}
|
||||
opt.SetSkip(skip)
|
||||
opt.SetLimit(param.PageSize)
|
||||
opt.SetSort(bson.M{activitymod.FN.LotteryGift.Sort: 1})
|
||||
if err := activitymod.FindManyLotteryGiftWithBind(ctx, &data.List, filter, opt); err != nil {
|
||||
return stdLog.QueryDataError, data, err
|
||||
}
|
||||
for _, v := range data.List {
|
||||
gFilter := bson.M{activitymod.FN.LotteryGetGift.GiftId: v.Id.Hex()}
|
||||
gCount, _ := activitymod.CountLotteryGetGifts(ctx, gFilter)
|
||||
v.GiftNum = gCount
|
||||
|
||||
gFilter[activitymod.FN.LotteryGetGift.IsUsed] = true
|
||||
usedCount, _ := activitymod.CountLotteryGetGifts(ctx, gFilter)
|
||||
v.ReceivedGiftNum = usedCount
|
||||
}
|
||||
return stdLog.Success, data, nil
|
||||
}
|
||||
Reference in New Issue
Block a user