@@ -0,0 +1,154 @@
|
||||
package userrecomod
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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.UserReco
|
||||
|
||||
func coll(t *db.MongoTool) *db.MongoTool {
|
||||
if t == nil {
|
||||
return mdb.Coll(table)
|
||||
}
|
||||
return t.Coll(table)
|
||||
}
|
||||
|
||||
func initIndex() {
|
||||
many := []mongo.IndexModel{
|
||||
{
|
||||
Keys: bson.D{{Key: "sortKey", Value: -1}, {Key: "createdAt", Value: -1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "active", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "updatedAt", Value: 1}},
|
||||
},
|
||||
{
|
||||
Keys: bson.D{{Key: "createdAt", Value: 1}},
|
||||
},
|
||||
}
|
||||
if _, err := coll(nil).CreateIndex(many); err != nil {
|
||||
panic(fmt.Sprintf("%s model set index err ==>[%+v]", table, err))
|
||||
}
|
||||
}
|
||||
|
||||
// getListCnt 获取总数
|
||||
func getListCnt(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:", "getListCnt", table, "Count", err),
|
||||
log.Any("cond", cond),
|
||||
)
|
||||
return 0, err
|
||||
}
|
||||
return total, nil
|
||||
}
|
||||
|
||||
// GetSkipSize 计算跳转
|
||||
func getSkipSize(page, size uint64, cond bson.M) (uint64, int64, error) {
|
||||
total, err := getListCnt(cond)
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
return (page - 1) * size, total, nil
|
||||
}
|
||||
|
||||
// GetUserReco 获取推荐用户
|
||||
func GetUserReco(page, size uint64, isWeb bool) ([]UserReco, int64, error) {
|
||||
var datas []UserReco
|
||||
cond := bson.M{}
|
||||
if !isWeb {
|
||||
cond = bson.M{"active": true}
|
||||
}
|
||||
sort := bson.D{{Key: "sortKey", Value: -1}, {Key: "createdAt", Value: -1}}
|
||||
skip, total, err := getSkipSize(page, size, cond)
|
||||
if err != nil {
|
||||
return datas, total, err
|
||||
}
|
||||
opts := options.FindOptions{}
|
||||
opts.SetSort(sort).SetSkip(int64(skip)).SetLimit(int64(size))
|
||||
if err = coll(nil).Find(&datas, cond, &opts); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "GetUserReco", table, "Find", err),
|
||||
log.Any("page", page),
|
||||
log.Any("size", size),
|
||||
log.Any("isWeb", isWeb),
|
||||
)
|
||||
}
|
||||
return datas, total, err
|
||||
}
|
||||
|
||||
// AddUserReco 添加一个推荐用户
|
||||
func AddUserReco(uid uint64, sortKey int, active bool) error {
|
||||
now := time.Now()
|
||||
reco := UserReco{
|
||||
UID: uid,
|
||||
SortKey: sortKey,
|
||||
Active: active,
|
||||
UpdatedAt: now,
|
||||
CreatedAt: now,
|
||||
}
|
||||
if _, err := coll(nil).InsertOne(&reco); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "AddUserReco", table, "InsertOne", err),
|
||||
log.Any("uid", uid),
|
||||
log.Any("sortKey", sortKey),
|
||||
log.Any("active", active),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getRecoUpdate(uid *uint64, sortKey *int, active *bool) bson.M {
|
||||
update := bson.M{"updatedAt": time.Now()}
|
||||
if uid != nil {
|
||||
update["uid"] = *uid
|
||||
}
|
||||
if sortKey != nil {
|
||||
update["sortKey"] = *sortKey
|
||||
}
|
||||
if active != nil {
|
||||
update["active"] = *active
|
||||
}
|
||||
return update
|
||||
}
|
||||
|
||||
// ModifyUserReco 修改推荐用户信息
|
||||
func ModifyUserReco(id primitive.ObjectID, uid *uint64, sortKey *int, active *bool) error {
|
||||
cond := bson.M{"_id": id}
|
||||
update := getRecoUpdate(uid, sortKey, active)
|
||||
if _, err := coll(nil).UpdateOne(cond, bson.M{"$set": update}); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "ModifyUserReco", table, "UpdateOne", err),
|
||||
log.Any("id", id),
|
||||
log.Any("uid", uid),
|
||||
log.Any("sortKey", sortKey),
|
||||
log.Any("active", active),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveUserReco 删除推荐用户信息
|
||||
func RemoveUserReco(id primitive.ObjectID) error {
|
||||
cond := bson.M{"_id": id}
|
||||
if _, err := coll(nil).DeleteOne(cond); err != nil {
|
||||
log.Error(fmt.Sprintf("[METHOD-%s]==> Model %s %s fail error:%+v:", "RemoveUserReco", table, "DeleteOne", err),
|
||||
log.Any("id", id),
|
||||
)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package userrecomod
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"91porn-server/common/db"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// UserReco 用户推送配置
|
||||
type UserReco struct {
|
||||
ID primitive.ObjectID `json:"id" bson:"_id,omitempty"`
|
||||
UID uint64 `json:"uid" bson:"uid"` //推荐用户id
|
||||
SortKey int `json:"sortKey" bson:"sortKey"` //排序
|
||||
Active bool `json:"active" bson:"active"` //激活状态
|
||||
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
|
||||
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
mdb = db.Init(table)
|
||||
initIndex()
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package userrecomod
|
||||
|
||||
import "91porn-server/models/commod"
|
||||
|
||||
// RecoListReq 推荐用户查询
|
||||
type RecoListReq struct {
|
||||
commod.Page
|
||||
}
|
||||
|
||||
// RecoListResp 推荐用户应答
|
||||
type RecoListResp struct {
|
||||
Infos []UserReco `json:"infos"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// RecoModifyReq 推荐用户修改
|
||||
type RecoModifyReq struct {
|
||||
ID string `form:"id" json:"id"`
|
||||
UID *uint64 `form:"uid" json:"uid"`
|
||||
SortKey *int `form:"sortKey" json:"sortKey"`
|
||||
Active *bool `form:"active" json:"active"`
|
||||
}
|
||||
|
||||
// RecoAddReq 推荐用户新增
|
||||
type RecoAddReq struct {
|
||||
UID uint64 `form:"uid" json:"uid"` //推荐用户id
|
||||
SortKey int `form:"sortKey" json:"sortKey"` //排序
|
||||
Active bool `form:"active" json:"active"` //激活状态
|
||||
}
|
||||
|
||||
// RecoDelReq 推荐用户删除
|
||||
type RecoDelReq struct {
|
||||
ID string `form:"id" json:"id"`
|
||||
}
|
||||
Reference in New Issue
Block a user