294 lines
7.7 KiB
Go
Executable File
294 lines
7.7 KiB
Go
Executable File
package mediabookshelfmod
|
|
|
|
import (
|
|
"91porn-server/models"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"91porn-server/common/db"
|
|
"91porn-server/common/log"
|
|
|
|
"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"
|
|
)
|
|
|
|
const table = models.MediaBookshelf
|
|
|
|
func coll(t *db.MongoTool) *db.MongoTool {
|
|
if t == nil {
|
|
return mdb.Coll(table)
|
|
}
|
|
return t.Coll(table)
|
|
}
|
|
|
|
func Init() {
|
|
mdb = db.Init(table)
|
|
initIndex()
|
|
}
|
|
|
|
func initIndex() {
|
|
many := []mongo.IndexModel{
|
|
{
|
|
Keys: bson.D{{Key: "mid", Value: 1}, {Key: "uid", Value: 1}},
|
|
},
|
|
{
|
|
Keys: bson.D{{Key: "uid", Value: 1}, {Key: "shelfType", Value: 1}},
|
|
},
|
|
}
|
|
if _, err := coll(nil).CreateIndex(many); err != nil {
|
|
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
|
}
|
|
}
|
|
|
|
// GetList 获取列表
|
|
func GetList(cond bson.M, skip, limit int64, sort bson.D) (res []MediaBookshelf, count int64, hasNext bool, err error) {
|
|
count, err = coll(nil).Count(cond)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetList", table, "Count", err),
|
|
log.Any("skip", skip),
|
|
log.Any("limit", limit),
|
|
log.Any("cond", cond),
|
|
log.Any("sort", sort),
|
|
)
|
|
return
|
|
}
|
|
|
|
if len(sort) == 0 {
|
|
sort = bson.D{{Key: "_id", Value: -1}}
|
|
}
|
|
opts := options.Find().SetSort(sort).SetSkip(skip).SetLimit(limit + 1)
|
|
if err = coll(nil).Find(&res, cond, opts); err != nil {
|
|
return
|
|
}
|
|
// 判断下一页
|
|
if len(res) > int(limit) {
|
|
hasNext = true
|
|
res = res[:limit]
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// GetDramaMediaIDs 返回有效短剧收藏,并支持按收藏时间或短剧最近更新时间排序。
|
|
func GetDramaMediaIDs(uid uint64, skip, limit int64, sortByMediaUpdate bool) (
|
|
ids []primitive.ObjectID,
|
|
total int64,
|
|
hasNext bool,
|
|
err error,
|
|
) {
|
|
sortStage := bson.D{{Key: "readAt", Value: -1}, {Key: "_id", Value: -1}}
|
|
if sortByMediaUpdate {
|
|
sortStage = bson.D{
|
|
{Key: "media.latestPublishedAt", Value: -1},
|
|
{Key: "media.updateTime", Value: -1},
|
|
{Key: "_id", Value: -1},
|
|
}
|
|
}
|
|
type row struct {
|
|
MID primitive.ObjectID `bson:"mid"`
|
|
}
|
|
type count struct {
|
|
Total int64 `bson:"total"`
|
|
}
|
|
result := make([]struct {
|
|
Rows []row `bson:"rows"`
|
|
Count []count `bson:"count"`
|
|
}, 0, 1)
|
|
pipeline := []bson.M{
|
|
{"$match": bson.M{"uid": uid, "shelfType": "drama"}},
|
|
{"$lookup": bson.M{
|
|
"from": models.Media, "localField": "mid", "foreignField": "_id", "as": "media",
|
|
}},
|
|
{"$unwind": "$media"},
|
|
{"$match": bson.M{
|
|
"media.mediaType": "drama", "media.status": 1, "media.isDelete": false,
|
|
}},
|
|
{"$sort": sortStage},
|
|
{"$facet": bson.M{
|
|
"rows": []bson.M{
|
|
{"$skip": skip}, {"$limit": limit + 1}, {"$project": bson.M{"mid": 1}},
|
|
},
|
|
"count": []bson.M{{"$count": "total"}},
|
|
}},
|
|
}
|
|
if err = coll(nil).Aggregate(&result, pipeline); err != nil || len(result) == 0 {
|
|
return
|
|
}
|
|
if len(result[0].Count) > 0 {
|
|
total = result[0].Count[0].Total
|
|
}
|
|
rows := result[0].Rows
|
|
if len(rows) > int(limit) {
|
|
hasNext = true
|
|
rows = rows[:limit]
|
|
}
|
|
ids = make([]primitive.ObjectID, 0, len(rows))
|
|
for _, item := range rows {
|
|
ids = append(ids, item.MID)
|
|
}
|
|
return
|
|
}
|
|
|
|
// GetAll 查询全部文档
|
|
func GetAll(filter primitive.M, sort bson.D) (out []*MediaBookshelf, err error) {
|
|
if len(sort) == 0 {
|
|
sort = bson.D{{Key: "_id", Value: -1}}
|
|
}
|
|
opts := options.Find().SetSort(sort)
|
|
if err = coll(nil).Find(&out, filter, opts); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetAll", table, "Find", err),
|
|
log.Any("filter", filter),
|
|
log.Any("opts", opts),
|
|
)
|
|
return nil, err
|
|
}
|
|
return
|
|
}
|
|
|
|
// QueryAllCount 查询文档条目数
|
|
func QueryAllCount(filter primitive.M) (int64, error) {
|
|
if count, err := coll(nil).Count(filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "QueryAllCount", table, "Count", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return 0, err
|
|
} else {
|
|
return count, nil
|
|
}
|
|
}
|
|
|
|
// GetInfo 通过id获取详细信息
|
|
func GetInfo(id primitive.ObjectID) (MediaBookshelf, error) {
|
|
v := MediaBookshelf{}
|
|
if err := coll(nil).FindOne(&v, bson.M{"_id": id}); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfo", table, "FindOne", err),
|
|
log.Any("id", id),
|
|
)
|
|
return v, err
|
|
}
|
|
|
|
if v.ID.IsZero() {
|
|
return v, errors.New("record not found")
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
// GetInfoByCond 通过id获取详细信息
|
|
func GetInfoByCond(filter bson.M) (MediaBookshelf, error) {
|
|
v := MediaBookshelf{}
|
|
if err := coll(nil).FindOne(&v, filter); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetInfo", table, "FindOne", err),
|
|
log.Any("filter", filter),
|
|
)
|
|
return v, err
|
|
}
|
|
|
|
return v, nil
|
|
}
|
|
|
|
// HasAddBookshelf 用户是否添加书架
|
|
func HasAddBookshelf(uid uint64, mIds []primitive.ObjectID) (map[primitive.ObjectID]bool, error) {
|
|
if mIds == nil {
|
|
mIds = []primitive.ObjectID{}
|
|
}
|
|
m := make(map[primitive.ObjectID]bool)
|
|
var infos []MediaBookshelf
|
|
query := bson.M{"uid": uid, "mid": bson.M{"$in": mIds}}
|
|
if err := coll(nil).Find(&infos, query); err != nil {
|
|
log.Error("IsAddBookshelf error", log.Any("uid", uid), log.Any("mIds", mIds), log.E(err))
|
|
return m, err
|
|
}
|
|
for _, i := range infos {
|
|
m[i.MID] = true
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// Insert 插入记录
|
|
func Insert(t *db.MongoTool, d MediaBookshelf) (data primitive.ObjectID, err error) {
|
|
result, err := coll(t).InsertOne(&d)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "InsertOne", err))
|
|
return
|
|
}
|
|
byteID, err := json.Marshal(result.InsertedID)
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "Marshal", err))
|
|
return
|
|
}
|
|
if err = data.UnmarshalJSON(byteID); err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Insert", table, "UnmarshalJSON", err))
|
|
return
|
|
}
|
|
return
|
|
}
|
|
|
|
// UpdateByID 根据id更新数据
|
|
func UpdateByID(t *db.MongoTool, id primitive.ObjectID, data map[string]interface{}) (int64, error) {
|
|
cond := bson.M{"_id": id}
|
|
return update(t, cond, data)
|
|
}
|
|
|
|
// update 更新数据
|
|
func update(t *db.MongoTool, cond primitive.M, data map[string]interface{}) (int64, error) {
|
|
result, err := coll(t).UpdateMany(cond, bson.M{"$set": bson.M(data)})
|
|
if err != nil {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "Update", table, "UpdateMany", err),
|
|
log.Any("cond", cond),
|
|
log.Any("update", data),
|
|
)
|
|
return 0, err
|
|
}
|
|
|
|
return result.ModifiedCount, nil
|
|
}
|
|
|
|
// DeleteByID 删除数据
|
|
func DeleteByID(t *db.MongoTool, id primitive.ObjectID) error {
|
|
_, err := coll(t).DeleteMany(bson.M{"_id": id})
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil
|
|
} else {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByID", table, "DeleteMany", err), log.Any("id", id))
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// DeleteByIDS 删除数据
|
|
func DeleteByIDS(t *db.MongoTool, uid uint64, ids []primitive.ObjectID) error {
|
|
_, err := coll(t).DeleteMany(bson.M{"mid": bson.M{"$in": ids}, "uid": uid})
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil
|
|
} else {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByIDS", table, "DeleteMany", err), log.Any("ids", ids))
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// DeleteByCond 删除数据
|
|
func DeleteByCond(t *db.MongoTool, cond bson.M) error {
|
|
_, err := coll(t).DeleteOne(cond)
|
|
if err != nil {
|
|
if err == mongo.ErrNoDocuments {
|
|
return nil
|
|
} else {
|
|
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "DeleteByID", table, "DeleteOne", err), log.Any("cond", cond))
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
// GetCount 获取书架总数
|
|
func GetCount(uid uint64) (total int64, err error) {
|
|
total, err = coll(nil).Count(bson.M{"uid": uid})
|
|
return
|
|
}
|