Files
stat_center/models/appInfoMod/appInfo.go
T
2026-05-20 16:13:58 +08:00

220 lines
5.2 KiB
Go

package appInfoMod
import (
"context"
"fmt"
"stat/common/log"
"stat/common/mongos"
"stat/common/stdLog"
"stat/common/utils"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// 初始化model
func Init() {
// 获取数据库表
db = mongos.New(mongos.MgoManager, CollAppInfo)
// 初始化索引
indexes := []mongo.IndexModel{
{
Keys: bson.D{{Key: "appId", Value: 1}},
Options: options.Index().SetUnique(true),
},
}
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", CollAppInfo, err))
}
}
// 添加渠道
func AddAppInfo(ctx context.Context, doc *AppInfo) (err error) {
if _, err = db.InsertOne(ctx, doc); err != nil {
log.Error("appInfo AddAppInfo InsertOne err", log.E(err))
return
}
return
}
// 修改app信息
func UpdateAppInfo(ctx context.Context, appId int32, doc AppInfoUpdate) (err error) {
filter := bson.M{"appId": appId}
update, _ := utils.ToBsonM(doc)
if _, err = db.UpdateOne(ctx, filter, bson.M{"$set": update}); err != nil {
log.Error("appInfo UpdateAppInfo UpdateOne err", log.Any("appId", appId), log.E(err))
return
}
return
}
// 删除app信息
func DelAppInfo(ctx context.Context, appId int32) (err error) {
filter := bson.M{"appId": appId}
if _, err = db.DeleteOne(ctx, filter); err != nil {
log.Error("appInfo DelAppInfo DeleteOne err", log.Any("appId", appId), log.E(err))
return
}
return
}
// 根据appId 获取appInfo
func GetAppInfoById(ctx context.Context, appId int32) (data AppInfo, err error) {
filter := bson.M{"appId": appId}
if err = db.FindOne(ctx, &data, filter); err != nil {
log.Error("appInfo GetAppInfoById FindOne err", log.Any("appId", appId), log.E(err))
return
}
return
}
// 根据appId 获取appInfo
func GetAppInfoByIdEx(ctx context.Context, appId int32) (*AppInfo, error) {
var err error
app := AppInfo{}
if err = db.FindOneEx(ctx, &app, bson.M{"appId": appId}); err != nil {
log.Error("appInfo GetAppInfoByIdEx FindOneEx err", log.Any("appId", appId), log.E(err))
return nil, err
}
return &app, nil
}
// 获取app列表
func GetAppInfoList(ctx context.Context, appName *string, used *bool, skip, limit int64) (count int64, data []AppInfo, err error) {
filter := bson.M{}
/*if used == nil {
filter["used"] = true
}
*/
if appName != nil {
filter["name"] = bson.M{"$regex": fmt.Sprintf("^%s", *appName)}
}
opts := options.Find().SetSkip(skip).SetLimit(limit)
if err = db.Find(ctx, &data, filter, opts); err != nil {
log.Error("appInfo GetAppInfoList Find err", log.E(err))
return
}
if count, err = db.Count(ctx, filter); err != nil {
log.Error("appInfo GetAppInfoList Count err", log.E(err))
return
}
return
}
// 获取app根据appIds
func GetAppInfosByAppIds(ctx context.Context, appIds []int32) (data []AppInfo, err error) {
if len(appIds) == 0 {
return
}
filter := bson.M{"appId": bson.M{"$in": appIds}}
if err = db.Find(ctx, &data, filter); err != nil {
log.Error("appInfo GetAppInfosByAppIds Find err", log.Any("appIds", appIds), log.E(err))
return
}
return
}
// 获取app ID列表
func GetAppIds(ctx context.Context) (ids []int32, err error) {
filter := bson.M{
"used": true,
}
data := make([]AppInfo, 0)
if err = db.Find(ctx, &data, filter); err != nil {
log.Error("appInfo GetAppIds Find err", log.E(err))
return
}
ids = make([]int32, 0)
for _, v := range data {
ids = append(ids, v.AppId)
}
return
}
// 获取app ID列表
func GetAllAppInfo(ctx context.Context) (data []AppInfo, err error) {
filter := bson.M{}
if err = db.Find(ctx, &data, filter); err != nil {
log.Error("appInfo GetAppIds Find err", log.E(err))
return
}
return
}
// 获取app ID列表
func GetAllAppInfoEx(ctx context.Context) (data []*AppInfo, err error) {
filter := bson.M{}
if err = db.Find(ctx, &data, filter); err != nil {
log.Error("appInfo GetAppIds Find err", log.E(err))
return
}
return
}
// 获取app ID列表
func GetAllAppIdMap(ctx context.Context, filter bson.M) (map[int32]*AppInfo, error) {
dict := make(map[int32]*AppInfo)
apps := make([]*AppInfo, 0)
if err := db.Find(ctx, &apps, filter); err != nil {
log.Error("appInfo GetAppIds Find err", log.E(err))
return dict, err
}
for _, app := range apps {
dict[app.AppId] = app
}
return dict, nil
}
// FindOneApplication 查找一个应用
func FindOneApplication(ctx context.Context, filter bson.M) (
*AppInfo, *stdLog.SrvErr) {
var Application AppInfo
if err := db.FindOneEx(
ctx, &Application, filter); err != nil {
if err != mongo.ErrNoDocuments {
log.Info("appinfo:FindOneApplication db.FindOne failed",
log.E(err), log.Any("filter", filter))
return nil, stdLog.New(stdLog.QueryDataError, "", "")
}
return nil, stdLog.New(stdLog.ErrApplicationNotExist, "", "")
}
return &Application, nil
}
// 获取app ID-Name列表
func GetAllAppIdNameMap(ctx context.Context) (map[int32]string, error) {
filter := bson.M{}
dict := make(map[int32]string)
apps := make([]*AppInfo, 0)
if err := db.Find(ctx, &apps, filter); err != nil {
log.Error("appInfo GetAppIds Find err", log.E(err))
return dict, err
}
for _, app := range apps {
dict[app.AppId] = app.Name
}
return dict, nil
}