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 }