+43
@@ -0,0 +1,43 @@
|
||||
package imgroupdata
|
||||
|
||||
import (
|
||||
"91porn-server/models/v/imgroupmod"
|
||||
)
|
||||
|
||||
// FormatAppDataList 格式化app列表数据
|
||||
func FormatAppDataList(origin []imgroupmod.ImGroup) (res []*imgroupmod.ImGroupInfo) {
|
||||
res = make([]*imgroupmod.ImGroupInfo, len(origin))
|
||||
if len(origin) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装返回数据
|
||||
for i, item := range origin {
|
||||
res[i] = FormatAppData(item)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// FormatAppData 格式化app数据
|
||||
func FormatAppData(item imgroupmod.ImGroup) (res *imgroupmod.ImGroupInfo) {
|
||||
if item.ID.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
// 组装返回数据
|
||||
res = &imgroupmod.ImGroupInfo{
|
||||
ID: item.ID,
|
||||
GroupId: item.GroupId,
|
||||
Name: item.Name,
|
||||
Cover: item.Cover,
|
||||
Summary: item.Summary,
|
||||
MemberNum: item.MemberNum,
|
||||
FakeMemberNum: item.FakeMemberNum,
|
||||
Price: item.Price,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package imgroupdata
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/cachev2"
|
||||
"91porn-server/common/db"
|
||||
"91porn-server/models"
|
||||
"91porn-server/models/v/imgroupmod"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type ListRes struct {
|
||||
List []imgroupmod.ImGroup
|
||||
Count int64
|
||||
HasNext bool
|
||||
}
|
||||
|
||||
// GetListFromCache 从缓存获取列表数据
|
||||
func GetListFromCache(cond bson.M, skip, limit int64, sort bson.D) ([]imgroupmod.ImGroup, int64, bool, error) {
|
||||
var data ListRes
|
||||
_, err := cachev2.Classes().CacheTime(10*time.Minute).AutoListKey(models.ImGroup).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 = imgroupmod.GetList(cond, skip, limit, sort)
|
||||
return
|
||||
}
|
||||
|
||||
// GetInfoFromCache 获取数据详情
|
||||
func GetInfoFromCache(id primitive.ObjectID) (res imgroupmod.ImGroup, err error) {
|
||||
_, err = cachev2.Classes().CacheTime(30*time.Minute).AutoInfoKey(models.ImGroup, id.Hex()).ResBind(&res).Cache(imgroupmod.GetInfo, id)
|
||||
return
|
||||
}
|
||||
|
||||
// GetAllFromCache 获取所有数据
|
||||
func GetAllFromCache(cond bson.M, sort bson.D) (res []imgroupmod.ImGroup, err error) {
|
||||
_, err = cachev2.Classes().CacheTime(30*time.Minute).AutoListKey(models.ImGroup+"-all").ResBind(&res).Cache(imgroupmod.GetAll, cond, sort)
|
||||
return
|
||||
}
|
||||
|
||||
// InsertData 插入数据
|
||||
func InsertData(t *db.MongoTool, data imgroupmod.ImGroup) (primitive.ObjectID, error) {
|
||||
newID, err := imgroupmod.Insert(t, data)
|
||||
if err != nil {
|
||||
return newID, err
|
||||
}
|
||||
// 清除缓存数据
|
||||
_, err = cachev2.Classes().Table(models.ImGroup).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 := imgroupmod.UpdateByID(t, objID, data)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
_, _ = cachev2.Classes().Table(models.ImGroup).AutoClear(true, &id)
|
||||
|
||||
return affect, err
|
||||
}
|
||||
|
||||
// DeleteData 删除数据
|
||||
func DeleteData(t *db.MongoTool, id primitive.ObjectID) (err error) {
|
||||
err = imgroupmod.DeleteByID(t, id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 清理缓存
|
||||
all := "*"
|
||||
_, _ = cachev2.Classes().Table(models.ImGroup).AutoClear(true, &all)
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user