Initial commit
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
package announInfoMod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"stat/common/log"
|
||||
"stat/common/mongos"
|
||||
"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, CollAnnounInfo)
|
||||
|
||||
// 初始化索引
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "appId", Value: 1}},
|
||||
},
|
||||
}
|
||||
|
||||
if _, err := db.CreateIndex(context.Background(), indexes); err != nil {
|
||||
panic(fmt.Sprintf("Model %s InitIndex error %+v", CollAnnounInfo, err))
|
||||
}
|
||||
}
|
||||
|
||||
// 获取公告列表
|
||||
func FindAnnounceList(ctx context.Context, appId *int32, skip int64, limit int64) (count int64, data []AnnounInfo, err error) {
|
||||
filter := bson.M{}
|
||||
|
||||
if appId != nil {
|
||||
filter["appId"] = appId
|
||||
}
|
||||
|
||||
opts := options.Find().SetSkip(skip).SetLimit(limit)
|
||||
|
||||
if err = db.Find(ctx, &data, filter, opts); err != nil {
|
||||
log.Error("announce FindAnnounceList Find err", log.E(err))
|
||||
return
|
||||
}
|
||||
if count, err = db.Count(ctx, filter); err != nil {
|
||||
log.Error("announce FindAnnounceList Count err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 新增管理员
|
||||
func AddAnnounce(ctx context.Context, doc *AnnounInfo) (err error) {
|
||||
if _, err = db.InsertOne(ctx, doc); err != nil {
|
||||
log.Error("announce AddAnnounce InsertOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 更新管理员
|
||||
func UpdateAnnounce(ctx context.Context, id string, doc UpdateDoc) (err error) {
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
|
||||
objId, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
log.Error("announce UpdateAnnounce ObjectIDFromHex err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
filter := bson.M{"_id": objId}
|
||||
update, _ := utils.ToBsonM(doc)
|
||||
|
||||
if _, err = db.UpdateOne(ctx, filter, bson.M{"$set": update}); err != nil {
|
||||
log.Error("announce UpdateAdmin UpdateOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// 删除管理员
|
||||
func DelAnnounce(ctx context.Context, id string) (err error) {
|
||||
if id == "" {
|
||||
return
|
||||
}
|
||||
|
||||
objId, err := primitive.ObjectIDFromHex(id)
|
||||
if err != nil {
|
||||
log.Error("announce DelAnnounce ObjectIDFromHex err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
filter := bson.M{"_id": objId}
|
||||
|
||||
if _, err = db.DeleteOne(ctx, filter); err != nil {
|
||||
log.Error("announce DelAnnounce DeleteOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 根据appId获取公告
|
||||
func FindAnnounceByAppId(ctx context.Context, appId int32) (data []AnnounInfo, err error) {
|
||||
now := time.Now()
|
||||
|
||||
filer := bson.M{"appId": appId, "startTime": bson.M{"$lt": now}, "endTime": bson.M{"$gt": now}}
|
||||
|
||||
if err = db.Find(ctx, &data, filer); err != nil {
|
||||
log.Error("announce DelAnnounce DeleteOne err", log.E(err))
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package announInfoMod
|
||||
|
||||
import (
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"stat/common/mongos"
|
||||
"time"
|
||||
)
|
||||
|
||||
const CollAnnounInfo = "announ_info"
|
||||
|
||||
var db *mongos.MongoTool
|
||||
|
||||
type AnnounInfo struct {
|
||||
ID primitive.ObjectID `bson:"_id,omitempty"` // 公告id
|
||||
AppId int32 `bson:"appId"` // 应用id
|
||||
Title string `bson:"title"` // 公告标题
|
||||
Content string `bson:"content"` // 公告内容
|
||||
Cover string `bson:"cover"` // 公告封面图
|
||||
Href string `bson:"href"` // 公告跳转地址
|
||||
Type int `bson:"type"` // 公告类型 0,外部连接;1,直接安装app
|
||||
Active bool `bson:"active"` // 是否激活
|
||||
StartTime time.Time `bson:"startTime"` // 开始时间
|
||||
EndTime time.Time `bson:"endTime"` // 结束时间
|
||||
}
|
||||
|
||||
type UpdateDoc struct {
|
||||
AppId *int32 `bson:"appId,omitempty"` // 应用id
|
||||
Title *string `bson:"title,omitempty"` // 公告标题
|
||||
Content *string `bson:"content,omitempty"` // 公告内容
|
||||
Cover *string `bson:"cover,omitempty"` // 公告封面图
|
||||
Href *string `bson:"href,omitempty"` // 公告跳转地址
|
||||
Type *int `bson:"type,omitempty"` // 公告类型 0,外部连接;1,直接安装app
|
||||
Active *bool `bson:"active,omitempty"` // 是否激活
|
||||
StartTime *time.Time `bson:"startTime,omitempty"` // 开始时间
|
||||
EndTime *time.Time `bson:"endTime,omitempty"` // 结束时间
|
||||
}
|
||||
Reference in New Issue
Block a user