|
|
|
@ -17,6 +17,7 @@ package mgo
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"github.com/OpenIMSDK/protocol/user"
|
|
|
|
|
"github.com/OpenIMSDK/tools/errs"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/OpenIMSDK/tools/mgoutil"
|
|
|
|
@ -100,7 +101,7 @@ func (u *UserMgo) CountTotal(ctx context.Context, before *time.Time) (count int6
|
|
|
|
|
return mgoutil.Count(ctx, u.coll, bson.M{"create_time": bson.M{"$lt": before}})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
|
|
|
|
|
func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string, ex string) error {
|
|
|
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
|
|
|
|
|
|
// Create a new document instead of updating an existing one
|
|
|
|
@ -110,28 +111,48 @@ func (u *UserMgo) AddUserCommand(ctx context.Context, userID string, Type int32,
|
|
|
|
|
"uuid": UUID,
|
|
|
|
|
"createTime": time.Now().Unix(), // assuming you want the creation time in Unix timestamp
|
|
|
|
|
"value": value,
|
|
|
|
|
"ex": ex,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := collection.InsertOne(ctx, doc)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *UserMgo) DeleteUserCommand(ctx context.Context, userID string, Type int32, UUID string) error {
|
|
|
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
|
|
|
|
|
|
filter := bson.M{"userID": userID, "type": Type, "uuid": UUID}
|
|
|
|
|
|
|
|
|
|
_, err := collection.DeleteOne(ctx, filter)
|
|
|
|
|
result, err := collection.DeleteOne(ctx, filter)
|
|
|
|
|
if result.DeletedCount == 0 {
|
|
|
|
|
// No records found to update
|
|
|
|
|
return errs.Wrap(errs.ErrRecordNotFound)
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, value string) error {
|
|
|
|
|
func (u *UserMgo) UpdateUserCommand(ctx context.Context, userID string, Type int32, UUID string, val map[string]any) error {
|
|
|
|
|
if len(val) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
|
|
|
|
|
|
filter := bson.M{"userID": userID, "type": Type, "uuid": UUID}
|
|
|
|
|
update := bson.M{"$set": bson.M{"value": value}}
|
|
|
|
|
update := bson.M{"$set": val}
|
|
|
|
|
|
|
|
|
|
_, err := collection.UpdateOne(ctx, filter, update)
|
|
|
|
|
return err
|
|
|
|
|
result, err := collection.UpdateOne(ctx, filter, update)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if result.MatchedCount == 0 {
|
|
|
|
|
// No records found to update
|
|
|
|
|
return errs.Wrap(errs.ErrRecordNotFound)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (u *UserMgo) GetUserCommand(ctx context.Context, userID string, Type int32) ([]*user.CommandInfoResp, error) {
|
|
|
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
|
filter := bson.M{"userID": userID, "type": Type}
|
|
|
|
@ -147,19 +168,23 @@ func (u *UserMgo) GetUserCommand(ctx context.Context, userID string, Type int32)
|
|
|
|
|
|
|
|
|
|
for cursor.Next(ctx) {
|
|
|
|
|
var document struct {
|
|
|
|
|
Type int32 `bson:"type"`
|
|
|
|
|
UUID string `bson:"uuid"`
|
|
|
|
|
Value string `bson:"value"`
|
|
|
|
|
CreateTime int64 `bson:"createTime"`
|
|
|
|
|
Ex string `bson:"ex"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := cursor.Decode(&document); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commandInfo := &user.CommandInfoResp{ // Change here: use a pointer to the struct
|
|
|
|
|
commandInfo := &user.CommandInfoResp{
|
|
|
|
|
Type: document.Type,
|
|
|
|
|
Uuid: document.UUID,
|
|
|
|
|
Value: document.Value,
|
|
|
|
|
CreateTime: document.CreateTime,
|
|
|
|
|
Ex: document.Ex,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commands = append(commands, commandInfo)
|
|
|
|
@ -171,7 +196,48 @@ func (u *UserMgo) GetUserCommand(ctx context.Context, userID string, Type int32)
|
|
|
|
|
|
|
|
|
|
return commands, nil
|
|
|
|
|
}
|
|
|
|
|
func (u *UserMgo) GetAllUserCommand(ctx context.Context, userID string) ([]*user.AllCommandInfoResp, error) {
|
|
|
|
|
collection := u.coll.Database().Collection("userCommands")
|
|
|
|
|
filter := bson.M{"userID": userID}
|
|
|
|
|
|
|
|
|
|
cursor, err := collection.Find(ctx, filter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer cursor.Close(ctx)
|
|
|
|
|
|
|
|
|
|
// Initialize commands as a slice of pointers
|
|
|
|
|
commands := []*user.AllCommandInfoResp{}
|
|
|
|
|
|
|
|
|
|
for cursor.Next(ctx) {
|
|
|
|
|
var document struct {
|
|
|
|
|
Type int32 `bson:"type"`
|
|
|
|
|
UUID string `bson:"uuid"`
|
|
|
|
|
Value string `bson:"value"`
|
|
|
|
|
CreateTime int64 `bson:"createTime"`
|
|
|
|
|
Ex string `bson:"ex"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := cursor.Decode(&document); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commandInfo := &user.AllCommandInfoResp{
|
|
|
|
|
Type: document.Type,
|
|
|
|
|
Uuid: document.UUID,
|
|
|
|
|
Value: document.Value,
|
|
|
|
|
CreateTime: document.CreateTime,
|
|
|
|
|
Ex: document.Ex,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commands = append(commands, commandInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := cursor.Err(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return commands, nil
|
|
|
|
|
}
|
|
|
|
|
func (u *UserMgo) CountRangeEverydayTotal(ctx context.Context, start time.Time, end time.Time) (map[string]int64, error) {
|
|
|
|
|
pipeline := bson.A{
|
|
|
|
|
bson.M{
|
|
|
|
|