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