feat: 优化web
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeptRepository 部门数据访问
|
||||
type DeptRepository interface {
|
||||
Create(ctx context.Context, d *entity.Dept) error
|
||||
Update(ctx context.Context, d *entity.Dept) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
GetByID(ctx context.Context, id string) (*entity.Dept, error)
|
||||
ListByTenant(ctx context.Context, tenantID string) ([]entity.Dept, error)
|
||||
CountChildren(ctx context.Context, id string) (int64, error)
|
||||
ExistsSiblingName(ctx context.Context, tenantID, parentID, name string, excludeID string) (bool, error)
|
||||
FindRoot(ctx context.Context, tenantID string) (*entity.Dept, error)
|
||||
UpdatePath(ctx context.Context, id string, path string) error
|
||||
}
|
||||
|
||||
type deptRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewDeptRepository(db *gorm.DB) DeptRepository {
|
||||
return &deptRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *deptRepository) Create(ctx context.Context, d *entity.Dept) error {
|
||||
return r.db.WithContext(ctx).Create(d).Error
|
||||
}
|
||||
|
||||
func (r *deptRepository) Update(ctx context.Context, d *entity.Dept) error {
|
||||
return r.db.WithContext(ctx).Save(d).Error
|
||||
}
|
||||
|
||||
func (r *deptRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Delete(&entity.Dept{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *deptRepository) GetByID(ctx context.Context, id string) (*entity.Dept, error) {
|
||||
var out entity.Dept
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *deptRepository) ListByTenant(ctx context.Context, tenantID string) ([]entity.Dept, error) {
|
||||
var rows []entity.Dept
|
||||
err := r.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Order("sort_order ASC, created_at ASC").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *deptRepository) CountChildren(ctx context.Context, id string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.Dept{}).Where("parent_id = ?", id).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *deptRepository) ExistsSiblingName(ctx context.Context, tenantID, parentID, name string, excludeID string) (bool, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Dept{}).Where("tenant_id = ? AND parent_id = ? AND dept_name = ?", tenantID, parentID, name)
|
||||
if excludeID != "" {
|
||||
q = q.Where("id <> ?", excludeID)
|
||||
}
|
||||
var n int64
|
||||
err := q.Count(&n).Error
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (r *deptRepository) FindRoot(ctx context.Context, tenantID string) (*entity.Dept, error) {
|
||||
var out entity.Dept
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND (parent_id = '' OR parent_id = '0')", tenantID).
|
||||
First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *deptRepository) UpdatePath(ctx context.Context, id string, path string) error {
|
||||
return r.db.WithContext(ctx).Model(&entity.Dept{}).Where("id = ?", id).Update("dept_path", path).Error
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package repository
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrInvalidState = errors.New("invalid state")
|
||||
ErrForbidden = errors.New("forbidden")
|
||||
)
|
||||
@@ -0,0 +1,111 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// MenuRepository 菜单
|
||||
type MenuRepository interface {
|
||||
Create(ctx context.Context, m *entity.Menu) error
|
||||
Update(ctx context.Context, m *entity.Menu) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
GetByID(ctx context.Context, id string) (*entity.Menu, error)
|
||||
ListAll(ctx context.Context) ([]entity.Menu, error)
|
||||
ListByType(ctx context.Context, menuType *int16) ([]entity.Menu, error)
|
||||
ExistsPerms(ctx context.Context, perms string, excludeID string) (bool, error)
|
||||
CountChildren(ctx context.Context, parentID string) (int64, error)
|
||||
CountRoleRefs(ctx context.Context, menuID string) (int64, error)
|
||||
ListByPerms(ctx context.Context, perms string) ([]entity.Menu, error)
|
||||
ListIDsByPermsIn(ctx context.Context, perms []string) ([]string, error)
|
||||
}
|
||||
|
||||
type menuRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewMenuRepository(db *gorm.DB) MenuRepository {
|
||||
return &menuRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *menuRepository) Create(ctx context.Context, m *entity.Menu) error {
|
||||
return r.db.WithContext(ctx).Create(m).Error
|
||||
}
|
||||
|
||||
func (r *menuRepository) Update(ctx context.Context, m *entity.Menu) error {
|
||||
return r.db.WithContext(ctx).Save(m).Error
|
||||
}
|
||||
|
||||
func (r *menuRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Delete(&entity.Menu{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *menuRepository) GetByID(ctx context.Context, id string) (*entity.Menu, error) {
|
||||
var out entity.Menu
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListAll(ctx context.Context) ([]entity.Menu, error) {
|
||||
var rows []entity.Menu
|
||||
err := r.db.WithContext(ctx).Order("sort_order ASC, created_at ASC").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListByType(ctx context.Context, menuType *int16) ([]entity.Menu, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Menu{})
|
||||
if menuType != nil {
|
||||
q = q.Where("menu_type = ?", *menuType)
|
||||
}
|
||||
var rows []entity.Menu
|
||||
err := q.Order("sort_order ASC, created_at ASC").Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ExistsPerms(ctx context.Context, perms string, excludeID string) (bool, error) {
|
||||
if perms == "" {
|
||||
return false, nil
|
||||
}
|
||||
q := r.db.WithContext(ctx).Model(&entity.Menu{}).Where("perms = ?", perms)
|
||||
if excludeID != "" {
|
||||
q = q.Where("id <> ?", excludeID)
|
||||
}
|
||||
var n int64
|
||||
err := q.Count(&n).Error
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) CountChildren(ctx context.Context, parentID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.Menu{}).Where("parent_id = ?", parentID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) CountRoleRefs(ctx context.Context, menuID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.RoleMenu{}).Where("menu_id = ?", menuID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListByPerms(ctx context.Context, perms string) ([]entity.Menu, error) {
|
||||
var rows []entity.Menu
|
||||
err := r.db.WithContext(ctx).Where("perms = ?", perms).Find(&rows).Error
|
||||
return rows, err
|
||||
}
|
||||
|
||||
func (r *menuRepository) ListIDsByPermsIn(ctx context.Context, perms []string) ([]string, error) {
|
||||
if len(perms) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&entity.Menu{}).Where("perms IN ?", perms).Pluck("id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"giter.top/smart/pkg/utils/id"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// RoleRepository 角色与角色菜单
|
||||
type RoleRepository interface {
|
||||
Create(ctx context.Context, r *entity.Role) error
|
||||
Update(ctx context.Context, r *entity.Role) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
GetByID(ctx context.Context, id string) (*entity.Role, error)
|
||||
List(ctx context.Context, tenantID string, name, code string, page, pageSize int) ([]entity.Role, int64, error)
|
||||
ExistsCode(ctx context.Context, tenantID string, code string, excludeID string) (bool, error)
|
||||
CountUsers(ctx context.Context, roleID string) (int64, error)
|
||||
ReplaceRoleMenus(ctx context.Context, roleID string, menuIDs []string) error
|
||||
ListMenuIDsByRole(ctx context.Context, roleID string) ([]string, error)
|
||||
ListMenuIDsByRoles(ctx context.Context, roleIDs []string) ([]string, error)
|
||||
ListRolesByUser(ctx context.Context, userID string) ([]entity.Role, error)
|
||||
}
|
||||
|
||||
type roleRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewRoleRepository(db *gorm.DB) RoleRepository {
|
||||
return &roleRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *roleRepository) Create(ctx context.Context, row *entity.Role) error {
|
||||
return r.db.WithContext(ctx).Create(row).Error
|
||||
}
|
||||
|
||||
func (r *roleRepository) Update(ctx context.Context, row *entity.Role) error {
|
||||
return r.db.WithContext(ctx).Save(row).Error
|
||||
}
|
||||
|
||||
func (r *roleRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Delete(&entity.Role{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *roleRepository) GetByID(ctx context.Context, id string) (*entity.Role, error) {
|
||||
var out entity.Role
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *roleRepository) List(ctx context.Context, tenantID string, name, code string, page, pageSize int) ([]entity.Role, int64, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Role{}).Where("tenant_id = ?", tenantID)
|
||||
if name != "" {
|
||||
q = q.Where("role_name LIKE ?", "%"+name+"%")
|
||||
}
|
||||
if code != "" {
|
||||
q = q.Where("role_code LIKE ?", "%"+code+"%")
|
||||
}
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []entity.Role
|
||||
err := q.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&rows).Error
|
||||
return rows, total, err
|
||||
}
|
||||
|
||||
func (r *roleRepository) ExistsCode(ctx context.Context, tenantID string, code string, excludeID string) (bool, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Role{}).Where("tenant_id = ? AND role_code = ?", tenantID, code)
|
||||
if excludeID != "" {
|
||||
q = q.Where("id <> ?", excludeID)
|
||||
}
|
||||
var n int64
|
||||
err := q.Count(&n).Error
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (r *roleRepository) CountUsers(ctx context.Context, roleID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.UserRole{}).Where("role_id = ?", roleID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *roleRepository) ReplaceRoleMenus(ctx context.Context, roleID string, menuIDs []string) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("role_id = ?", roleID).Delete(&entity.RoleMenu{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, mid := range menuIDs {
|
||||
rm := entity.RoleMenu{ID: id.New(), RoleID: roleID, MenuID: mid}
|
||||
if err := tx.Create(&rm).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *roleRepository) ListMenuIDsByRole(ctx context.Context, roleID string) ([]string, error) {
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&entity.RoleMenu{}).Where("role_id = ?", roleID).Pluck("menu_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
func (r *roleRepository) ListMenuIDsByRoles(ctx context.Context, roleIDs []string) ([]string, error) {
|
||||
if len(roleIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var raw []string
|
||||
err := r.db.WithContext(ctx).Model(&entity.RoleMenu{}).Where("role_id IN ?", roleIDs).Pluck("menu_id", &raw).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen := make(map[string]struct{}, len(raw))
|
||||
var ids []string
|
||||
for _, menuID := range raw {
|
||||
if _, ok := seen[menuID]; ok {
|
||||
continue
|
||||
}
|
||||
seen[menuID] = struct{}{}
|
||||
ids = append(ids, menuID)
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func (r *roleRepository) ListRolesByUser(ctx context.Context, userID string) ([]entity.Role, error) {
|
||||
var roles []entity.Role
|
||||
err := r.db.WithContext(ctx).Table("iam_role").
|
||||
Joins("JOIN iam_user_role ur ON ur.role_id = iam_role.id").
|
||||
Where("ur.user_id = ?", userID).
|
||||
Find(&roles).Error
|
||||
return roles, err
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// TenantRepository 租户数据访问
|
||||
type TenantRepository interface {
|
||||
Create(ctx context.Context, t *entity.Tenant) error
|
||||
Update(ctx context.Context, t *entity.Tenant) error
|
||||
GetByID(ctx context.Context, id string) (*entity.Tenant, error)
|
||||
GetByCode(ctx context.Context, code string) (*entity.Tenant, error)
|
||||
List(ctx context.Context, name, code string, status *int16, page, pageSize int) ([]entity.Tenant, int64, error)
|
||||
CountUsers(ctx context.Context, tenantID string) (int64, error)
|
||||
CountDepts(ctx context.Context, tenantID string) (int64, error)
|
||||
ExistsCode(ctx context.Context, code string, excludeID string) (bool, error)
|
||||
}
|
||||
|
||||
type tenantRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewTenantRepository(db *gorm.DB) TenantRepository {
|
||||
return &tenantRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *tenantRepository) Create(ctx context.Context, t *entity.Tenant) error {
|
||||
return r.db.WithContext(ctx).Create(t).Error
|
||||
}
|
||||
|
||||
func (r *tenantRepository) Update(ctx context.Context, t *entity.Tenant) error {
|
||||
return r.db.WithContext(ctx).Save(t).Error
|
||||
}
|
||||
|
||||
func (r *tenantRepository) GetByID(ctx context.Context, id string) (*entity.Tenant, error) {
|
||||
var out entity.Tenant
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *tenantRepository) GetByCode(ctx context.Context, code string) (*entity.Tenant, error) {
|
||||
var out entity.Tenant
|
||||
err := r.db.WithContext(ctx).Where("tenant_code = ?", code).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *tenantRepository) List(ctx context.Context, name, code string, status *int16, page, pageSize int) ([]entity.Tenant, int64, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Tenant{})
|
||||
if name != "" {
|
||||
q = q.Where("tenant_name LIKE ?", "%"+name+"%")
|
||||
}
|
||||
if code != "" {
|
||||
q = q.Where("tenant_code LIKE ?", "%"+code+"%")
|
||||
}
|
||||
if status != nil {
|
||||
q = q.Where("status = ?", *status)
|
||||
}
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []entity.Tenant
|
||||
err := q.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&rows).Error
|
||||
return rows, total, err
|
||||
}
|
||||
|
||||
func (r *tenantRepository) CountUsers(ctx context.Context, tenantID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.User{}).Where("tenant_id = ?", tenantID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *tenantRepository) CountDepts(ctx context.Context, tenantID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Model(&entity.Dept{}).Where("tenant_id = ?", tenantID).Count(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *tenantRepository) ExistsCode(ctx context.Context, code string, excludeID string) (bool, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.Tenant{}).Where("tenant_code = ?", code)
|
||||
if excludeID != "" {
|
||||
q = q.Where("id <> ?", excludeID)
|
||||
}
|
||||
var n int64
|
||||
err := q.Count(&n).Error
|
||||
return n > 0, err
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"giter.top/smart/internal/iam/entity"
|
||||
"giter.top/smart/pkg/utils/id"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UserRepository 用户数据访问
|
||||
type UserRepository interface {
|
||||
Create(ctx context.Context, u *entity.User) error
|
||||
Update(ctx context.Context, u *entity.User) error
|
||||
Delete(ctx context.Context, id string) error
|
||||
GetByID(ctx context.Context, id string) (*entity.User, error)
|
||||
GetByUserName(ctx context.Context, tenantID string, userName string) (*entity.User, error)
|
||||
ExistsUserName(ctx context.Context, tenantID string, userName string, excludeID string) (bool, error)
|
||||
CountByDept(ctx context.Context, deptID string) (int64, error)
|
||||
List(ctx context.Context, tenantID string, deptID *string, roleID *string, keyword string, status *int16, page, pageSize int) ([]entity.User, int64, error)
|
||||
ReplaceUserDepts(ctx context.Context, userID string, primaryDept string, deptIDs []string) error
|
||||
ReplaceUserRoles(ctx context.Context, userID string, roleIDs []string) error
|
||||
ListRoleIDs(ctx context.Context, userID string) ([]string, error)
|
||||
ListDeptIDs(ctx context.Context, userID string) ([]string, error)
|
||||
}
|
||||
|
||||
type userRepository struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *gorm.DB) UserRepository {
|
||||
return &userRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *userRepository) Create(ctx context.Context, u *entity.User) error {
|
||||
return r.db.WithContext(ctx).Create(u).Error
|
||||
}
|
||||
|
||||
func (r *userRepository) Update(ctx context.Context, u *entity.User) error {
|
||||
return r.db.WithContext(ctx).Save(u).Error
|
||||
}
|
||||
|
||||
func (r *userRepository) Delete(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Delete(&entity.User{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByID(ctx context.Context, id string) (*entity.User, error) {
|
||||
var out entity.User
|
||||
err := r.db.WithContext(ctx).Where("id = ?", id).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByUserName(ctx context.Context, tenantID string, userName string) (*entity.User, error) {
|
||||
var out entity.User
|
||||
err := r.db.WithContext(ctx).Where("tenant_id = ? AND user_name = ?", tenantID, userName).First(&out).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) ExistsUserName(ctx context.Context, tenantID string, userName string, excludeID string) (bool, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.User{}).Where("tenant_id = ? AND user_name = ?", tenantID, userName)
|
||||
if excludeID != "" {
|
||||
q = q.Where("id <> ?", excludeID)
|
||||
}
|
||||
var n int64
|
||||
err := q.Count(&n).Error
|
||||
return n > 0, err
|
||||
}
|
||||
|
||||
func (r *userRepository) CountByDept(ctx context.Context, deptID string) (int64, error) {
|
||||
var n int64
|
||||
err := r.db.WithContext(ctx).Raw(`
|
||||
SELECT COUNT(*) FROM (
|
||||
SELECT id FROM iam_user WHERE dept_id = ? AND deleted_at IS NULL
|
||||
UNION
|
||||
SELECT user_id FROM iam_user_dept WHERE dept_id = ?
|
||||
) t`, deptID, deptID).Scan(&n).Error
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *userRepository) List(ctx context.Context, tenantID string, deptID *string, roleID *string, keyword string, status *int16, page, pageSize int) ([]entity.User, int64, error) {
|
||||
q := r.db.WithContext(ctx).Model(&entity.User{}).Where("tenant_id = ?", tenantID)
|
||||
if deptID != nil {
|
||||
d := *deptID
|
||||
q = q.Where("dept_id = ? OR id IN (SELECT user_id FROM iam_user_dept WHERE dept_id = ?)", d, d)
|
||||
}
|
||||
if roleID != nil {
|
||||
sub := r.db.WithContext(ctx).Model(&entity.UserRole{}).Select("user_id").Where("role_id = ?", *roleID)
|
||||
q = q.Where("id IN (?)", sub)
|
||||
}
|
||||
if keyword != "" {
|
||||
kw := "%" + keyword + "%"
|
||||
q = q.Where("user_name LIKE ? OR real_name LIKE ? OR phone LIKE ? OR email LIKE ?", kw, kw, kw, kw)
|
||||
}
|
||||
if status != nil {
|
||||
q = q.Where("status = ?", *status)
|
||||
}
|
||||
var total int64
|
||||
if err := q.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
offset := (page - 1) * pageSize
|
||||
var rows []entity.User
|
||||
err := q.Order("created_at DESC").Offset(offset).Limit(pageSize).Find(&rows).Error
|
||||
return rows, total, err
|
||||
}
|
||||
|
||||
func (r *userRepository) ReplaceUserDepts(ctx context.Context, userID string, primaryDept string, deptIDs []string) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("user_id = ?", userID).Delete(&entity.UserDept{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
seen := map[string]struct{}{}
|
||||
for _, did := range deptIDs {
|
||||
if _, ok := seen[did]; ok {
|
||||
continue
|
||||
}
|
||||
seen[did] = struct{}{}
|
||||
ud := entity.UserDept{
|
||||
ID: id.New(),
|
||||
UserID: userID,
|
||||
DeptID: did,
|
||||
IsPrimary: did == primaryDept,
|
||||
}
|
||||
if err := tx.Create(&ud).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if len(deptIDs) == 0 && primaryDept != "" {
|
||||
ud := entity.UserDept{ID: id.New(), UserID: userID, DeptID: primaryDept, IsPrimary: true}
|
||||
return tx.Create(&ud).Error
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *userRepository) ReplaceUserRoles(ctx context.Context, userID string, roleIDs []string) error {
|
||||
return r.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("user_id = ?", userID).Delete(&entity.UserRole{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
for _, rid := range roleIDs {
|
||||
ur := entity.UserRole{ID: id.New(), UserID: userID, RoleID: rid}
|
||||
if err := tx.Create(&ur).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *userRepository) ListRoleIDs(ctx context.Context, userID string) ([]string, error) {
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&entity.UserRole{}).Where("user_id = ?", userID).Pluck("role_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
|
||||
func (r *userRepository) ListDeptIDs(ctx context.Context, userID string) ([]string, error) {
|
||||
var ids []string
|
||||
err := r.db.WithContext(ctx).Model(&entity.UserDept{}).Where("user_id = ?", userID).Pluck("dept_id", &ids).Error
|
||||
return ids, err
|
||||
}
|
||||
Reference in New Issue
Block a user