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