Initial commit

This commit is contained in:
2026-05-20 16:13:58 +08:00
commit ceffbe382e
500 changed files with 157469 additions and 0 deletions
+220
View File
@@ -0,0 +1,220 @@
package advertiseMod
import (
"context"
"fmt"
"stat/common/log"
"stat/common/mongos"
"stat/common/stdLog"
"stat/common/utils"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func Init() {
// 获取数据库表
db = mongos.New(mongos.MgoManager, CollAdvertise)
// 初始化索引
indexes := []mongo.IndexModel{
{
Keys: bson.D{{Key: "appId", Value: 1}},
},
{
Keys: bson.D{{Key: "appId", Value: 1}, {Key: "isActive", Value: 1}, {Key: "startTime", Value: 1}, {Key: "endTime", Value: 1}},
},
}
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollAdvertise, err))
}
}
// 新增广告
func AddAdvertise(ctx context.Context, doc *Advertise) (err error) {
if _, err = db.InsertOne(ctx, doc); err != nil {
log.Error("advertise AddAdvertise InsertOne err", log.E(err))
return
}
return
}
// 修改广告
func UpdateAdvertise(ctx context.Context, id int64, doc UpdateAds) (err error) {
filter := bson.M{"_id": id}
update, _ := utils.ToBsonM(doc)
if _, err = db.UpdateOne(ctx, filter, bson.M{"$set": update}); err != nil {
log.Error("advertise UpdateAdvertise UpdateOne err", log.E(err))
return
}
return
}
// 删除广告
func DelAdvertise(ctx context.Context, id int64) (err error) {
filter := bson.M{"_id": id}
if _, err = db.DeleteOne(ctx, filter); err != nil {
log.Error("advertise DelAdvertise DeleteOne err", log.E(err))
return
}
return
}
func FindAdvertiseList(ctx context.Context, appId *int32, isActive *bool, locId *int32, skip, limit int64,
link, title, daoLiangTitle *string) (count int64, data []Advertise, err error) {
filter := bson.M{}
if appId != nil {
filter["appId"] = appId
}
if isActive != nil {
filter["isActive"] = isActive
}
if locId != nil {
filter["locId"] = *locId
}
if link != nil {
filter["links"] = bson.M{"$elemMatch": bson.M{"link": *link}}
}
if title != nil {
filter["title"] = bson.M{"$regex": *title}
}
if daoLiangTitle != nil {
filter["daoLiangTitle"] = bson.M{"$regex": *daoLiangTitle}
}
sort := bson.D{{Key: "sort", Value: 1}}
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit)
if err = db.Find(ctx, &data, filter, opts); err != nil {
log.Error("advertise FindAdvertiseList Find err", log.E(err))
return
}
if count, err = db.Count(ctx, filter); err != nil {
log.Error("advertise FindAdvertiseList Count err", log.E(err))
return
}
return
}
// 根据appId和locId更新位置id
func UpdateLocInfo(ctx context.Context, appId, oldLocId, newLocID int32) (err error) {
filter := bson.M{"appId": appId, "locId": oldLocId}
update := bson.M{"$set": bson.M{"locId": newLocID}}
if _, err = db.UpdateMany(ctx, filter, update); err != nil {
log.Error("advertise UpdateLocInfo UpdateMany err", log.E(err))
return
}
return
}
// 根据app和位置获取广告
func FindAdsByAppAndLocation(ctx context.Context, appId int32, locId *int32) (data []Advertise, err error) {
now := time.Now()
filter := bson.M{"appId": appId, "isActive": true, "startTime": bson.M{"$lt": now}, "endTime": bson.M{"$gte": now}}
if locId != nil {
filter["locId"] = locId
}
sort := bson.D{{Key: "sort", Value: 1}}
opts := options.Find().SetSort(sort)
if err = db.Find(ctx, &data, filter, opts); err != nil {
log.Error("advertise FindAdsByAppAndLocation Find err", log.E(err))
return
}
return
}
// 根据app和位置获取广告
func FindAdsById(ctx context.Context, id int64) (data Advertise, err error) {
filter := bson.M{"_id": id}
if err = db.FindOne(ctx, &data, filter); err != nil {
log.Error("advertise FindAdsById FindOne err", log.E(err))
return
}
return
}
// 增加广告点击量
func IncAdsClickCount(ctx context.Context, id int64) (err error) {
filter := bson.M{"_id": id}
update := bson.M{"$inc": bson.M{"clickCount": 1}}
if _, err = db.UpdateOne(ctx, filter, update); err != nil {
log.Error("advertise IncAdsClickCount UpdateOne err", log.E(err))
return
}
return
}
// FindAds 根据app和位置获取广告
func FindAds(ctx context.Context) (data []Advertise, err error) {
filter := bson.M{}
sort := bson.D{{Key: "sort", Value: 1}}
opts := options.Find().SetSort(sort)
if err = db.Find(ctx, &data, filter, opts); err != nil {
log.Error("advertise FindAdsByAppAndLocation Find err", log.E(err))
return
}
return
}
// FindOneAdvertise 查找一个应用
func FindOneAdvertise(ctx context.Context, filter bson.M) (
*Advertise, error) {
var advertise Advertise
if err := db.FindOneEx(ctx, &advertise, filter); err != nil {
return nil, err
}
return &advertise, nil
}
// GetAllAdvertiseMap 获取ads id map
func GetAllAdvertiseMap(ctx context.Context, filter bson.M) (map[int64]*Advertise, error) {
dict := make(map[int64]*Advertise)
list := make([]*Advertise, 0)
if err := db.Find(ctx, &list, filter); err != nil {
log.Error("Advertise GetAllAdvertiseMap Find err", log.E(err))
return dict, err
}
for _, ads := range list {
dict[ads.Id] = ads
}
return dict, nil
}
// FindAds 根据app和位置获取广告
func FindAdvertiselist(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (data []Advertise, serr *stdLog.SrvErr) {
if err := db.Find(ctx, &data, filter, opts...); err != nil {
if err != mongo.ErrNoDocuments {
log.Error("advertise FindAdvertiselist err", log.E(err))
return nil, stdLog.New(stdLog.DataEmpty, "", err.Error())
}
log.Error("advertise FindAdvertiselist Find err", log.E(err))
return nil, stdLog.New(stdLog.QueryDataError, "", err.Error())
}
return
}
+67
View File
@@ -0,0 +1,67 @@
package advertiseMod
import (
"stat/common/mongos"
"time"
)
const CollAdvertise = "advertise"
var db *mongos.MongoTool
type JumpType = int64
const (
JumpTypeOutsideBrowser JumpType = iota // 0:外部浏览器跳转
JumpTypeInsideBrowser // 1:内部浏览器跳转
JumpTypeInsideApp // 2:app内部跳转
)
type LinkInfo struct {
Link string `bson:"link"` // 链接地址
Foreign bool `bson:"foreign"` // true 国外用户
Weight int `bson:"weight"` // 权重
}
type Advertise struct {
Id int64 `bson:"_id"` // 广告id
AppId int32 `bson:"appId"` // 应用id
LocId int32 `bson:"locId"` // 位置id
Title string `bson:"title"` // 广告标题
DaoLiangTitle string `bson:"daoLiangTitle"` // 广告导量标题
Tags string `bson:"tags"` // 广告标签
CoverImg string `bson:"coverImg"` // 封面
VisibleChannels []string `bson:"visibleChannels"` // 可见的渠道
JumpType JumpType `bson:"jumpType"` // 跳转方式
Link string `bson:"link"` // 链接
Links []LinkInfo `bson:"links"` // 链接地址
Sort int64 `bson:"sort"` // 排序值--优先级
IsActive bool `bson:"isActive"` // 是否激活
StartTime time.Time `bson:"startTime"` // 开始时间
EndTime time.Time `bson:"endTime"` // 结束时间
Remark string `bson:"remark"` // 备注
ClickCount int64 `bson:"clickCount"` // 点击次数
AmbientColor string `bson:"ambientColor"` // 氛围色(逼哩漫画)
CreatedAt time.Time `bson:"createAt"` // 创建时间
UpdatedAt time.Time `bson:"updatedAt"` // 更新时间
}
type UpdateAds struct {
AppId *int32 `bson:"appId,omitempty"` // 应用id
LocId *int32 `bson:"locId,omitempty"` // 位置id
Title *string `bson:"title,omitempty"` // 广告标题
DaoLiangTitle *string `bson:"daoLiangTitle,omitempty"` // 广告导量标题
Tags *string `bson:"tags,omitempty"` // 广告标签
CoverImg *string `bson:"coverImg,omitempty"` // 封面
VisibleChannels *[]string `bson:"visibleChannels,omitempty"` // 可见的渠道
Location *int64 `bson:"location,omitempty"` // 位置
JumpType *JumpType `bson:"jumpType,omitempty"` // 跳转方式
Links *[]LinkInfo `bson:"links,omitempty"` // 链接地址
Sort *int64 `bson:"sort,omitempty"` // 排序值--优先级
IsActive *bool `bson:"isActive,omitempty"` // 是否激活
StartTime *time.Time `bson:"startTime,omitempty"` // 开始时间
EndTime *time.Time `bson:"endTime,omitempty"` // 结束时间
Remark *string `bson:"remark,omitempty"` // 备注
AmbientColor *string `bson:"ambientColor"` // 氛围色(逼哩漫画)
UpdatedAt *time.Time `bson:"updatedAt,omitempty"` // 更新时间
}