forked from all_user/stat_center
175 lines
5.1 KiB
Go
175 lines
5.1 KiB
Go
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
|
|
}
|