208 lines
5.7 KiB
Go
208 lines
5.7 KiB
Go
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
|
|
}
|