111 lines
2.8 KiB
Go
Executable File
111 lines
2.8 KiB
Go
Executable File
package mediabookshelfdata
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"91porn-server/common/cachev2"
|
|
"91porn-server/common/db"
|
|
"91porn-server/models"
|
|
"91porn-server/models/v/mediabookshelfmod"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
func getKey(uid uint64) string {
|
|
return fmt.Sprintf("%s-%d", models.MediaBookshelf, uid)
|
|
}
|
|
|
|
type ListRes struct {
|
|
List []mediabookshelfmod.MediaBookshelf
|
|
Count int64
|
|
HasNext bool
|
|
}
|
|
|
|
// GetListFromCache 从缓存获取列表数据
|
|
func GetListFromCache(uid uint64, cond bson.M, skip, limit int64, sort bson.D) ([]mediabookshelfmod.MediaBookshelf, int64, bool, error) {
|
|
var data ListRes
|
|
_, err := cachev2.Classes().CacheTime(10*time.Minute).AutoListKey(getKey(uid)).ResBind(&data).Cache(GetListData, cond, skip, limit, sort)
|
|
return data.List, data.Count, data.HasNext, err
|
|
}
|
|
|
|
// GetListData 获取列表数据并组装适合缓存格式返回
|
|
func GetListData(cond bson.M, skip, limit int64, sort bson.D) (res ListRes, err error) {
|
|
res.List, res.Count, res.HasNext, err = mediabookshelfmod.GetList(cond, skip, limit, sort)
|
|
return
|
|
}
|
|
|
|
func GetInfoByCond(cond bson.M) (mediabookshelfmod.MediaBookshelf, error) {
|
|
return mediabookshelfmod.GetInfoByCond(cond)
|
|
}
|
|
|
|
// InsertData 插入数据
|
|
func InsertData(t *db.MongoTool, uid uint64, data mediabookshelfmod.MediaBookshelf) (primitive.ObjectID, error) {
|
|
newID, err := mediabookshelfmod.Insert(t, data)
|
|
if err != nil {
|
|
return newID, err
|
|
}
|
|
// 清除缓存数据
|
|
_, err = cachev2.Classes().Table(getKey(uid)).AutoClear(true, nil)
|
|
if err != nil {
|
|
return newID, err
|
|
}
|
|
return newID, nil
|
|
}
|
|
|
|
// UpdateData 更新数据
|
|
func UpdateData(t *db.MongoTool, id string, data map[string]interface{}) (int64, error) {
|
|
objID, err := primitive.ObjectIDFromHex(id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
affect, err := mediabookshelfmod.UpdateByID(t, objID, data)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
|
|
// 清理缓存
|
|
_, _ = cachev2.Classes().Table(models.MediaBookshelf).AutoClear(true, nil)
|
|
|
|
return affect, err
|
|
}
|
|
|
|
// DeleteDataByCond 删除数据
|
|
func DeleteDataByCond(t *db.MongoTool, uid uint64, cond bson.M) (err error) {
|
|
err = mediabookshelfmod.DeleteByCond(t, cond)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 清理缓存
|
|
_, _ = cachev2.Classes().Table(getKey(uid)).AutoClear(true, nil)
|
|
return
|
|
}
|
|
|
|
// DeleteDataByIds 删除数据
|
|
func DeleteDataByIds(t *db.MongoTool, uid uint64, ids []primitive.ObjectID) (err error) {
|
|
err = mediabookshelfmod.DeleteByIDS(t, uid, ids)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 清理缓存
|
|
_, _ = cachev2.Classes().Table(getKey(uid)).AutoClear(true, nil)
|
|
return
|
|
}
|
|
|
|
// DeleteData 删除数据
|
|
func DeleteData(t *db.MongoTool, id primitive.ObjectID) (err error) {
|
|
err = mediabookshelfmod.DeleteByID(t, id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// 清理缓存
|
|
all := "*"
|
|
_, _ = cachev2.Classes().Table(models.MediaBookshelf).AutoClear(true, &all)
|
|
return
|
|
}
|