+116
@@ -0,0 +1,116 @@
|
||||
package mediabookshelfdata
|
||||
|
||||
import (
|
||||
"91porn-server/models/v/mediabookshelfmod"
|
||||
"91porn-server/models/v/mediamod"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AppData 需要组装返回的数据
|
||||
type AppData struct {
|
||||
BaseInfo *mediabookshelfmod.MediaBookshelfInfo `json:"baseInfo"` // 基础信息
|
||||
}
|
||||
|
||||
// FormatAppDataList 格式化app列表数据
|
||||
func FormatAppDataList(origin []mediabookshelfmod.MediaBookshelf) (res []AppData) {
|
||||
res = make([]AppData, len(origin))
|
||||
if len(origin) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装返回数据
|
||||
for i, item := range origin {
|
||||
res[i] = FormatAppData(item)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FormatAppData 格式化app数据
|
||||
func FormatAppData(item mediabookshelfmod.MediaBookshelf) (res AppData) {
|
||||
if item.ID.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装返回数据
|
||||
res.BaseInfo = &mediabookshelfmod.MediaBookshelfInfo{
|
||||
ID: item.ID,
|
||||
Name: item.Name,
|
||||
UID: item.UID,
|
||||
ReadAt: item.ReadAt,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdateTime: item.UpdateTime,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// AppSimpleData 仅返回基础数据
|
||||
type AppSimpleData *mediabookshelfmod.MediaBookshelfInfo
|
||||
|
||||
// FormatAppSimpleDataList 格式化app列表数据
|
||||
func FormatAppSimpleDataList(origin []mediabookshelfmod.MediaBookshelf, skip, limit int64, sort bson.D) (res []AppSimpleData) {
|
||||
res = make([]AppSimpleData, len(origin))
|
||||
if len(origin) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var mids []primitive.ObjectID
|
||||
for i := range origin {
|
||||
mids = append(mids, origin[i].MID)
|
||||
}
|
||||
|
||||
filter := bson.M{"_id": bson.M{"$in": mids}}
|
||||
opts := options.Find().SetSkip(skip).SetLimit(limit)
|
||||
if len(sort) > 0 {
|
||||
opts = opts.SetSort(sort)
|
||||
}
|
||||
media, err := mediamod.QueryMediaByCond(filter, opts)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
m := make(map[primitive.ObjectID]*mediamod.Media)
|
||||
for i := range media {
|
||||
m[media[i].ID] = media[i]
|
||||
}
|
||||
// 组装返回数据
|
||||
for i, item := range origin {
|
||||
res[i] = FormatAppSimpleData(item, m[item.MID])
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FormatAppSimpleData 格式化app数据
|
||||
func FormatAppSimpleData(item mediabookshelfmod.MediaBookshelf, media *mediamod.Media) (res AppSimpleData) {
|
||||
if item.ID.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装返回数据
|
||||
res = &mediabookshelfmod.MediaBookshelfInfo{
|
||||
ID: item.MID,
|
||||
//MID: item.MID,
|
||||
Name: media.Title,
|
||||
VerticalCover: media.VerticalCover,
|
||||
TotalEpisode: media.TotalEpisode,
|
||||
CurrentEpisode: media.CurrentEpisode,
|
||||
MediaType: media.MediaType,
|
||||
MediaSubType: int(media.MediaSubType),
|
||||
UpdateStatus: media.UpdateStatus,
|
||||
CountBrowse: media.CountBrowse,
|
||||
Permission: media.Permission,
|
||||
Price: media.Price,
|
||||
UID: item.UID,
|
||||
ReadAt: item.ReadAt,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdateTime: item.UpdateTime,
|
||||
}
|
||||
if time.Now().Add(-time.Hour*24*7).Unix() < media.ContentUpdateTime.Unix() {
|
||||
res.IsNew = true
|
||||
}
|
||||
return
|
||||
}
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user