94 lines
3.0 KiB
Go
94 lines
3.0 KiB
Go
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
|
|
}
|