Files
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

110 lines
3.1 KiB
Go

package annoumod
import (
"fmt"
"91porn-server/common/db"
"91porn-server/common/log"
"91porn-server/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var mdbAnnounce *db.MongoDB
const tableAnnounce = models.Announce
func collAnnounce(t *db.MongoTool) *db.MongoTool {
if t == nil {
return mdbAnnounce.Coll(tableAnnounce)
}
return t.Coll(tableAnnounce)
}
// InitIndex 初始化索引
func initAnnounceIndex() {
indexModels := []mongo.IndexModel{
{
Keys: bson.D{{Key: "active", Value: 1}},
Options: options.Index(),
},
}
if _, err := collAnnounce(nil).CreateIndex(indexModels); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", tableAnnounce, err))
}
}
// AddAnnounce 增加会员中心跑马灯
func AddAnnounce(announce []Announce) error {
if _, err := collAnnounce(nil).InsertMany(&announce); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddAnnounce", tableAnnounce, "InsertMany", err), log.Any("announce", announce))
return err
}
return nil
}
// DeleteAnnounce 删除会员中心跑马灯
func DeleteAnnounce(ids []primitive.ObjectID) (int64, error) {
if ids == nil {
ids = []primitive.ObjectID{}
}
cond := bson.M{"_id": bson.M{"$in": ids}}
result, err := collAnnounce(nil).DeleteMany(cond)
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteAnnounce", tableAnnounce, "DeleteMany", err), log.Any("ids", ids))
return 0, err
}
return result.DeletedCount, nil
}
// UpdateAnnounce 更新会员中心跑马灯
func UpdateAnnounce(id primitive.ObjectID, update map[string]interface{}) (int64, error) {
cond := bson.M{"_id": id}
result, err := collAnnounce(nil).UpdateMany(cond, bson.M{"$set": bson.M(update)})
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "UpdateAnnou", tableAnnounce, "UpdateMany", err),
log.Any("id", id),
log.Any("update", update),
)
return 0, err
}
return result.ModifiedCount, nil
}
// GetAnnounce 查询会员中心跑马灯
func GetAnnounceApi(field string, desc int) ([]Announce, error) {
cond := bson.M{"active": true, "type": 0}
sort := bson.D{{Key: field, Value: desc}}
opts := options.FindOptions{}
opts.SetSort(sort)
var back []Announce
if err := collAnnounce(nil).Find(&back, cond, &opts); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnounce", tableAnnounce, "Find", err),
log.Any("field", field),
log.Any("desc", desc),
)
return nil, err
}
return back, nil
}
// GetAnnounce 查询会员中心跑马灯
func GetAnnounce(field string, desc int) ([]Announce, error) {
cond := bson.M{"type": 0}
sort := bson.D{{Key: field, Value: desc}}
opts := options.FindOptions{}
opts.SetSort(sort)
var back []Announce
if err := collAnnounce(nil).Find(&back, cond, &opts); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnounce", tableAnnounce, "Find", err),
log.Any("field", field),
log.Any("desc", desc),
)
return nil, err
}
return back, nil
}