forked from all_user/stat_center
Initial commit
This commit is contained in:
@@ -0,0 +1,219 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package appInfoMod
|
||||
|
||||
//type DateMonitor struct {
|
||||
// NewAdd
|
||||
//}
|
||||
@@ -0,0 +1,34 @@
|
||||
package appInfoMod
|
||||
|
||||
type TagAppInfo struct {
|
||||
AppId string
|
||||
Name string
|
||||
LandPages string
|
||||
IconImg string
|
||||
Used string
|
||||
AdvertiseNum string
|
||||
AdvertiseNewNum string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
AppInfo TagAppInfo
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
AppInfo string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.AppInfo.AppId = "appId"
|
||||
FN.AppInfo.Name = "name"
|
||||
FN.AppInfo.LandPages = "landPages"
|
||||
FN.AppInfo.IconImg = "iconImg"
|
||||
FN.AppInfo.Used = "used"
|
||||
FN.AppInfo.AdvertiseNum = "advertiseNum"
|
||||
FN.AppInfo.AdvertiseNewNum = "advertiseNewNum"
|
||||
FN.AppInfo.CreatedAt = "createdAt"
|
||||
FN.AppInfo.UpdatedAt = "updatedAt"
|
||||
TN.AppInfo = CollAppInfo
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package appInfoMod
|
||||
|
||||
import (
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 表名
|
||||
const CollAppInfo = "app_info"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type AppInfo struct {
|
||||
AppId int32 `bson:"appId"` // appId,唯一id
|
||||
Name string `bson:"name"` // app名称
|
||||
LandPages []string `bson:"landPages"` // 落地页列表
|
||||
IconImg string `bson:"iconImg"` // 图标
|
||||
Used bool `bson:"used"` // 是否正在使用
|
||||
AdvertiseNum int64 `bson:"advertiseNum"` // 广告系数
|
||||
AdvertiseNewNum int64 `bson:"advertiseNewNum"` // 广告新增系数
|
||||
CreatedAt time.Time `bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `bson:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
type AppInfoList []*AppInfo
|
||||
|
||||
type AppInfoUpdate struct {
|
||||
Name *string `bson:"name,omitempty"` // app名称
|
||||
IconImg *string `bson:"iconImg,omitempty"` // 图标
|
||||
LandPages *[]string `bson:"landPages,omitempty"` // 落地页列表
|
||||
AdvertiseNum *int64 `bson:"advertiseNum,omitempty"` // 广告系数
|
||||
AdvertiseNewNum *int64 `bson:"advertiseNewNum,omitempty"` // 广告新增系数
|
||||
UpdatedAt *time.Time `bson:"updatedAt,omitempty"` // 更新时间
|
||||
}
|
||||
Reference in New Issue
Block a user