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

173 lines
4.6 KiB
Go

package annoumod
import (
"fmt"
"math"
"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 mdb *db.MongoDB
const table = models.Annou
func coll(t *db.MongoTool) *db.MongoTool {
if t == nil {
return mdb.Coll(table)
}
return t.Coll(table)
}
// InitIndex 初始化索引
func initAnnouIndex() {
many := []mongo.IndexModel{
{
Keys: bson.D{{Key: "createdAt", Value: 1}},
},
{
Keys: bson.D{{Key: "active", Value: 1}},
},
{
Keys: bson.D{{Key: "updatedAt", Value: 1}},
},
{
Keys: bson.D{{Key: "type", Value: 1}},
},
}
if _, err := coll(nil).CreateIndex(many); err != nil {
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
}
}
// AddAnnou 增加公告
func AddAnnou(annou []Annou) error {
if _, err := coll(nil).InsertMany(&annou); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddAnnou", table, "InsertMany", err), log.Any("annou", annou))
return err
}
return nil
}
// DeleteAnnou 删除公告
func DeleteAnnou(ids []primitive.ObjectID) (int64, error) {
if ids == nil {
ids = []primitive.ObjectID{}
}
cond := bson.M{"_id": bson.M{"$in": ids}}
result, err := coll(nil).DeleteMany(cond)
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteAnnou", table, "DeleteMany", err), log.Any("ids", ids))
return 0, err
}
return result.DeletedCount, nil
}
// UpdateAnnou 更新公告
func UpdateAnnou(id primitive.ObjectID, update map[string]interface{}) (int64, error) {
cond := bson.M{"_id": id}
result, err := coll(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", table, "UpdateMany", err),
log.Any("id", id),
log.Any("update", update),
)
return 0, err
}
return result.ModifiedCount, nil
}
// getTotalCnt 获取查询总数总数
func getTotalCnt(cond bson.M) (int64, error) {
total, err := coll(nil).Count(cond)
if err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "getTotalCnt", table, "Count", err), log.Any("cond", cond))
return 0, err
}
return total, nil
}
// GetSkipSize 计算跳转
func getSkipSize(page, size uint64, cond bson.M) (uint64, uint64, int64, error) {
total, err := getTotalCnt(cond)
if err != nil {
return 0, 0, 0, err
}
totalpages := uint64(math.Ceil(float64(total) / float64(size)))
if page > totalpages {
page = totalpages
}
if page < 1 {
page = 1
}
return (page - 1) * size, totalpages, total, nil
}
// GetAnnou 查询公告
func GetAnnou(page, size uint64, field string, desc int) ([]*Annou, uint64, int64, error) {
cond := bson.M{}
sort := bson.D{{Key: field, Value: desc}}
skip, totalPages, total, err := getSkipSize(page, size, cond)
if err != nil {
return nil, 0, 0, err
}
opts := options.FindOptions{}
opts.SetSort(sort).SetSkip(int64(skip)).SetLimit(int64(size))
var back []*Annou
if err = coll(nil).Find(&back, cond, &opts); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnou", table, "Find", err),
log.Any("page", page),
log.Any("size", size),
log.Any("field", field),
log.Any("desc", desc),
)
return nil, totalPages, total, err
}
return back, totalPages, total, nil
}
// GetAnnouByType 获取指定类型的公告
func GetAnnouByType(types int) ([]*Annou, error) {
cond := bson.M{"active": true}
if types != 2 {
cond = bson.M{"type": types, "active": true}
}
sort := bson.D{{Key: "updatedAt", Value: -1}}
opts := options.FindOptions{}
opts.SetSort(sort)
var back []*Annou
if err := coll(nil).Find(&back, cond, &opts); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnouByType", table, "Find", err), log.Any("types", types))
return nil, err
}
return back, nil
}
const (
TotalType = 1 //系统公告
VIPUP = 3 //vip升级公告
)
// GetAnnouForDomain 获取指定类型的公告
func GetAnnouForDomain(types int) ([]AnnounInfo, error) {
cond := bson.M{"active": true}
if types != 2 {
cond = bson.M{"type": types, "active": true}
}
sort := bson.D{{Key: "updatedAt", Value: -1}}
opts := options.FindOptions{}
opts.SetSort(sort)
var back []AnnounInfo
if err := coll(nil).Find(&back, cond, &opts); err != nil {
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAnnouForDomain", table, "Find", err), log.Any("types", types))
return nil, err
}
return back, nil
}