复制项目

This commit is contained in:
kim.dev.6789
2026-01-14 22:35:45 +08:00
parent 305d526110
commit b7f8db7d08
297 changed files with 81784 additions and 0 deletions

View File

@@ -0,0 +1,151 @@
// Copyright © 2023 OpenIM open source community. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package chat
import (
"context"
"time"
"github.com/openimsdk/tools/db/mongoutil"
"github.com/openimsdk/tools/db/pagination"
"github.com/openimsdk/tools/errs"
"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"
"git.imall.cloud/openim/chat/pkg/common/db/table/chat"
)
func NewFavorite(db *mongo.Database) (chat.FavoriteInterface, error) {
coll := db.Collection("favorite")
_, err := coll.Indexes().CreateMany(context.Background(), []mongo.IndexModel{
{
Keys: bson.D{
{Key: "user_id", Value: 1},
{Key: "create_time", Value: -1},
},
},
{
Keys: bson.D{
{Key: "user_id", Value: 1},
{Key: "type", Value: 1},
{Key: "create_time", Value: -1},
},
},
{
Keys: bson.D{
{Key: "user_id", Value: 1},
{Key: "status", Value: 1},
},
},
})
if err != nil {
return nil, errs.Wrap(err)
}
return &Favorite{coll: coll}, nil
}
type Favorite struct {
coll *mongo.Collection
}
func (o *Favorite) Create(ctx context.Context, favorites ...*chat.Favorite) error {
for _, favorite := range favorites {
if favorite.ID == "" {
favorite.ID = primitive.NewObjectID().Hex()
}
if favorite.CreateTime.IsZero() {
favorite.CreateTime = time.Now()
}
if favorite.UpdateTime.IsZero() {
favorite.UpdateTime = time.Now()
}
if favorite.Status == 0 {
favorite.Status = 1 // 默认为正常状态
}
}
return mongoutil.InsertMany(ctx, o.coll, favorites)
}
func (o *Favorite) Take(ctx context.Context, favoriteID string) (*chat.Favorite, error) {
return mongoutil.FindOne[*chat.Favorite](ctx, o.coll, bson.M{"_id": favoriteID, "status": 1})
}
func (o *Favorite) FindByUserID(ctx context.Context, userID string, pagination pagination.Pagination) (int64, []*chat.Favorite, error) {
filter := bson.M{
"user_id": userID,
"status": 1,
}
return mongoutil.FindPage[*chat.Favorite](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}}))
}
func (o *Favorite) FindByUserIDAndType(ctx context.Context, userID string, favoriteType int32, pagination pagination.Pagination) (int64, []*chat.Favorite, error) {
filter := bson.M{
"user_id": userID,
"type": favoriteType,
"status": 1,
}
return mongoutil.FindPage[*chat.Favorite](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}}))
}
func (o *Favorite) SearchByKeyword(ctx context.Context, userID string, keyword string, pagination pagination.Pagination) (int64, []*chat.Favorite, error) {
filter := bson.M{
"user_id": userID,
"status": 1,
"$or": []bson.M{
{"title": bson.M{"$regex": keyword, "$options": "i"}},
{"content": bson.M{"$regex": keyword, "$options": "i"}},
{"description": bson.M{"$regex": keyword, "$options": "i"}},
},
}
return mongoutil.FindPage[*chat.Favorite](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}}))
}
func (o *Favorite) Update(ctx context.Context, favoriteID string, data map[string]any) error {
if len(data) == 0 {
return nil
}
data["update_time"] = time.Now()
return mongoutil.UpdateOne(ctx, o.coll, bson.M{"_id": favoriteID}, bson.M{"$set": data}, false)
}
func (o *Favorite) Delete(ctx context.Context, favoriteIDs []string) error {
if len(favoriteIDs) == 0 {
return nil
}
// 软删除将状态设置为0
_, err := o.coll.UpdateMany(ctx, bson.M{"_id": bson.M{"$in": favoriteIDs}}, bson.M{"$set": bson.M{"status": 0, "update_time": time.Now()}})
return errs.Wrap(err)
}
func (o *Favorite) DeleteByUserID(ctx context.Context, userID string) error {
// 软删除将状态设置为0
_, err := o.coll.UpdateMany(ctx, bson.M{"user_id": userID}, bson.M{"$set": bson.M{"status": 0, "update_time": time.Now()}})
return errs.Wrap(err)
}
func (o *Favorite) CountByUserID(ctx context.Context, userID string) (int64, error) {
return mongoutil.Count(ctx, o.coll, bson.M{"user_id": userID, "status": 1})
}
func (o *Favorite) FindByTags(ctx context.Context, userID string, tags []string, pagination pagination.Pagination) (int64, []*chat.Favorite, error) {
filter := bson.M{
"user_id": userID,
"status": 1,
"tags": bson.M{"$in": tags},
}
return mongoutil.FindPage[*chat.Favorite](ctx, o.coll, filter, pagination, options.Find().SetSort(bson.D{{Key: "create_time", Value: -1}}))
}