来源:hgdjnet-server 9bfbafe10d417deea959bd53117bc88a10e60a83 项目身份 HKDJ / JHA-216,独立官网仓库,Jenkins 守卫键 hkdjnet 测试目录 /server/hkdj_net,App/Web API 30331/30332 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package ipdailystatmod
|
|
|
|
import (
|
|
"context"
|
|
"hkdj-web-server/pub/mongos"
|
|
"time"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
var db *mongos.Collection
|
|
|
|
func Init(ctx context.Context) {
|
|
db = mongos.Shop.Coll(TN.IpDailyStat)
|
|
|
|
indexes := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{
|
|
{Key: FN.IpDailyStat.AppId, Value: 1},
|
|
{Key: FN.IpDailyStat.Date, Value: 1},
|
|
},
|
|
Options: options.Index().SetUnique(true),
|
|
},
|
|
}
|
|
_, _ = db.CreateIndex(ctx, indexes)
|
|
}
|
|
|
|
func UpsertCount(ctx context.Context, appId int64, date string, count int64) error {
|
|
filter := bson.M{
|
|
FN.IpDailyStat.AppId: appId,
|
|
FN.IpDailyStat.Date: date,
|
|
}
|
|
update := bson.M{
|
|
"$set": bson.M{
|
|
FN.IpDailyStat.Count: count,
|
|
FN.IpDailyStat.UpdatedAt: time.Now(),
|
|
},
|
|
}
|
|
_, err := db.UpsertOne(ctx, filter, update)
|
|
return err
|
|
}
|
|
|
|
func FindCount(ctx context.Context, appId int64, date string) (int64, error) {
|
|
filter := bson.M{
|
|
FN.IpDailyStat.AppId: appId,
|
|
FN.IpDailyStat.Date: date,
|
|
}
|
|
var stat IpDailyStat
|
|
err := db.FindOneEx(ctx, &stat, filter)
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return 0, nil
|
|
}
|
|
return 0, err
|
|
}
|
|
return stat.Count, nil
|
|
}
|