init: web_hgdjc_server
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
package announcementmod
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"videoweb/pub/common/log"
|
||||
"videoweb/pub/common/srvdef"
|
||||
"videoweb/pub/mongos"
|
||||
|
||||
"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.Announcement)
|
||||
indexes := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Announcement.Status, Value: 1},
|
||||
{Key: FN.Announcement.Sort, Value: -1},
|
||||
},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{
|
||||
{Key: FN.Announcement.CreatedAt, Value: -1},
|
||||
},
|
||||
},
|
||||
}
|
||||
if _, err := db.CreateIndex(ctx, indexes); err != nil {
|
||||
panic(fmt.Sprintf("(announcementmod:Init) Create Announcement Index failed! err:%+v:", err))
|
||||
}
|
||||
}
|
||||
|
||||
func InsertOneAnnouncement(ctx context.Context, doc *Announcement) *srvdef.SrvErr {
|
||||
if _, err := db.InsertOne(ctx, doc); err != nil {
|
||||
log.Warn("announcementmod:InsertOneAnnouncement db.InsertOne failed",
|
||||
log.E(err), log.Any("doc", doc))
|
||||
return srvdef.New(srvdef.ErrDbInsertError, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func DelOneAnnouncement(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := db.DeleteOne(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("announcementmod:DelOneAnnouncement db.DeleteOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbDeleteError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func FindOneAnnouncement(ctx context.Context, filter bson.M) (*Announcement, *srvdef.SrvErr) {
|
||||
var doc Announcement
|
||||
if err := db.FindOneEx(ctx, &doc, filter); err != nil {
|
||||
if err != mongo.ErrNoDocuments {
|
||||
log.Warn("announcementmod:FindOneAnnouncement db.FindOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return nil, srvdef.New(srvdef.ErrAnnouncementNotExsit, "", "")
|
||||
}
|
||||
return &doc, nil
|
||||
}
|
||||
|
||||
func FindManyAnnouncement(ctx context.Context, filter bson.M, opts ...*options.FindOptions) (AnnouncementList, *srvdef.SrvErr) {
|
||||
data := make(AnnouncementList, 0)
|
||||
if err := db.Find(ctx, &data, filter, opts...); err != nil {
|
||||
log.Warn("announcementmod:FindManyAnnouncement db.Find failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return nil, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func CountAnnouncement(ctx context.Context, filter bson.M) (int64, *srvdef.SrvErr) {
|
||||
count, err := db.Count(ctx, filter)
|
||||
if err != nil {
|
||||
log.Warn("announcementmod:CountAnnouncement db.Count failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return 0, srvdef.New(srvdef.ErrDbQueryError, "", "")
|
||||
}
|
||||
return count, nil
|
||||
}
|
||||
|
||||
func UpdateOneAnnouncement(ctx context.Context, filter bson.M, update bson.M) *srvdef.SrvErr {
|
||||
result, err := db.UpdateOne(ctx, filter, update)
|
||||
if err != nil {
|
||||
log.Warn("announcementmod:UpdateOneAnnouncement db.UpdateOne failed",
|
||||
log.E(err), log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrDbUpdateError, "", "")
|
||||
}
|
||||
if result.MatchedCount != 1 {
|
||||
log.Warn("announcementmod:UpdateOneAnnouncement not matched",
|
||||
log.Any("filter", filter))
|
||||
return srvdef.New(srvdef.ErrAnnouncementNotExsit, "", "")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package announcementmod
|
||||
|
||||
type TagAnnouncement struct {
|
||||
Id string
|
||||
Title string
|
||||
Content string
|
||||
Sort string
|
||||
Status string
|
||||
CreatedAt string
|
||||
UpdatedAt string
|
||||
}
|
||||
|
||||
var FN struct {
|
||||
Announcement TagAnnouncement
|
||||
}
|
||||
|
||||
var TN struct {
|
||||
Announcement string
|
||||
}
|
||||
|
||||
func init() {
|
||||
FN.Announcement.Id = "_id"
|
||||
FN.Announcement.Title = "title"
|
||||
FN.Announcement.Content = "content"
|
||||
FN.Announcement.Sort = "sort"
|
||||
FN.Announcement.Status = "status"
|
||||
FN.Announcement.CreatedAt = "createdAt"
|
||||
FN.Announcement.UpdatedAt = "updatedAt"
|
||||
TN.Announcement = "announcement"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package announcementmod
|
||||
|
||||
import "time"
|
||||
|
||||
// Announcement 公告
|
||||
// @table:announcement
|
||||
type Announcement struct {
|
||||
Id int64 `json:"id" bson:"_id,omitempty"`
|
||||
Title string `json:"title" bson:"title"` // 标题
|
||||
Content string `json:"content" bson:"content"` // 内容
|
||||
Sort int64 `json:"sort" bson:"sort"` // 排序,越大越靠前
|
||||
Status bool `json:"status" bson:"status"` // 是否启用
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"` // 创建时间
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"` // 更新时间
|
||||
}
|
||||
|
||||
type AnnouncementList []*Announcement
|
||||
Reference in New Issue
Block a user