139 lines
3.5 KiB
Go
139 lines
3.5 KiB
Go
|
package repository
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
"code.30cm.net/digimon/app-cloudep-permission-server/pkg/domain/entity"
|
||
|
"code.30cm.net/digimon/app-cloudep-permission-server/pkg/domain/repository"
|
||
|
mgo "code.30cm.net/digimon/library-go/mongo"
|
||
|
"github.com/zeromicro/go-zero/core/stores/mon"
|
||
|
"go.mongodb.org/mongo-driver/bson"
|
||
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||
|
"go.mongodb.org/mongo-driver/mongo"
|
||
|
)
|
||
|
|
||
|
// UserRoleRepositoryParam 定義 MongoDB 配置參數
|
||
|
type UserRoleRepositoryParam struct {
|
||
|
Conf *mgo.Conf
|
||
|
DBOpts []mon.Option
|
||
|
}
|
||
|
|
||
|
// userRoleRepository 實作 repository.UserRoleRepository 介面
|
||
|
type userRoleRepository struct {
|
||
|
DB mgo.DocumentDBUseCase
|
||
|
}
|
||
|
|
||
|
// NewUserRoleRepository 初始化 `UserRoleRepository`
|
||
|
func NewUserRoleRepository(param UserRoleRepositoryParam) repository.UserRoleRepository {
|
||
|
e := entity.UserRole{}
|
||
|
db, err := mgo.NewDocumentDB(param.Conf, e.Collection(), param.DBOpts...)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
return &userRoleRepository{
|
||
|
DB: db,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GetAll 取得所有 UserRole
|
||
|
func (repo *userRoleRepository) GetAll(ctx context.Context) ([]*entity.UserRole, error) {
|
||
|
var result []*entity.UserRole
|
||
|
err := repo.DB.GetClient().Find(ctx, &result, bson.M{})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// GetByUserID 透過 UID 查詢 UserRole
|
||
|
func (repo *userRoleRepository) GetByUserID(ctx context.Context, uid string) (entity.UserRole, error) {
|
||
|
var result entity.UserRole
|
||
|
err := repo.DB.GetClient().FindOne(ctx, &result, bson.M{"uid": uid})
|
||
|
if err != nil {
|
||
|
return entity.UserRole{}, err
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// GetUsersByRoleID 透過 RoleID 查詢所有使用此角色的使用者
|
||
|
func (repo *userRoleRepository) GetUsersByRoleID(ctx context.Context, roleID string) ([]entity.UserRole, error) {
|
||
|
var result []entity.UserRole
|
||
|
err := repo.DB.GetClient().Find(ctx, &result, bson.M{"role_id": roleID})
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// CountUsersByRole 統計每個角色的使用者數量
|
||
|
func (repo *userRoleRepository) CountUsersByRole(ctx context.Context) ([]repository.RoleUserCount, error) {
|
||
|
pipeline := []bson.M{
|
||
|
{"$group": bson.M{
|
||
|
"_id": "$role_id",
|
||
|
"count": bson.M{"$sum": 1},
|
||
|
}},
|
||
|
}
|
||
|
|
||
|
var result []repository.RoleUserCount
|
||
|
err := repo.DB.GetClient().Aggregate(ctx, &result, pipeline)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
// CreateUserRole 新增使用者角色
|
||
|
func (repo *userRoleRepository) CreateUserRole(ctx context.Context, param entity.UserRole) error {
|
||
|
if param.UID == "" {
|
||
|
return fmt.Errorf("uid can't be empty")
|
||
|
}
|
||
|
|
||
|
if param.RoleID == "" {
|
||
|
return fmt.Errorf("role_id can't be empty")
|
||
|
}
|
||
|
|
||
|
if param.ID.IsZero() {
|
||
|
now := time.Now().UTC().UnixNano()
|
||
|
param.ID = primitive.NewObjectID()
|
||
|
param.CreateAt = now
|
||
|
param.UpdateAt = now
|
||
|
}
|
||
|
|
||
|
_, err := repo.DB.GetClient().InsertOne(ctx, param)
|
||
|
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// UpdateUserRole 更新使用者角色
|
||
|
func (repo *userRoleRepository) UpdateUserRole(ctx context.Context, uid, roleID string) (entity.UserRole, error) {
|
||
|
filter := bson.M{"uid": uid}
|
||
|
update := bson.M{
|
||
|
"$set": bson.M{
|
||
|
"role_id": roleID,
|
||
|
"update_at": time.Now().UTC().UnixNano(),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
var updated entity.UserRole
|
||
|
err := repo.DB.GetClient().FindOneAndUpdate(ctx, &updated, filter, update)
|
||
|
if err != nil {
|
||
|
return entity.UserRole{}, err
|
||
|
}
|
||
|
|
||
|
return updated, nil
|
||
|
}
|
||
|
|
||
|
func (repo *userRoleRepository) Index20250225UP(ctx context.Context) (*mongo.Cursor, error) {
|
||
|
repo.DB.PopulateIndex(ctx, "role_id", 1, false)
|
||
|
repo.DB.PopulateIndex(ctx, "uid", 1, true)
|
||
|
|
||
|
return repo.DB.GetClient().Indexes().List(ctx)
|
||
|
}
|