优化结构
This commit is contained in:
@@ -3,18 +3,17 @@ package system
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"kra/internal/biz/system"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type departmentRepo struct{ data Provider }
|
||||
|
||||
func NewDepartmentRepo(data Provider) biz.DepartmentRepo { return &departmentRepo{data: data} }
|
||||
func NewDepartmentRepo(data Provider) system.DepartmentRepo { return &departmentRepo{data: data} }
|
||||
|
||||
type departmentPO struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
@@ -47,11 +46,11 @@ type authorityDepartmentPO struct {
|
||||
|
||||
func (authorityDepartmentPO) TableName() string { return "sys_authority_departments" }
|
||||
|
||||
func deptFromPO(po departmentPO) *biz.Department {
|
||||
return &biz.Department{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, ParentID: po.ParentID, Ancestors: po.Ancestors, Sort: po.Sort, LeaderID: po.LeaderID, Status: po.Status}
|
||||
func deptFromPO(po departmentPO) *system.Department {
|
||||
return &system.Department{ID: po.ID, CreatedAt: po.CreatedAt, UpdatedAt: po.UpdatedAt, Name: po.Name, ParentID: po.ParentID, Ancestors: po.Ancestors, Sort: po.Sort, LeaderID: po.LeaderID, Status: po.Status}
|
||||
}
|
||||
|
||||
func (r *departmentRepo) attachDepartmentLeaders(ctx context.Context, departments []*biz.Department) error {
|
||||
func (r *departmentRepo) attachDepartmentLeaders(ctx context.Context, departments []*system.Department) error {
|
||||
leaderIDs := make([]uint, 0, len(departments))
|
||||
for _, department := range departments {
|
||||
if department.LeaderID != 0 {
|
||||
@@ -65,7 +64,7 @@ func (r *departmentRepo) attachDepartmentLeaders(ctx context.Context, department
|
||||
if err := r.data.DB().WithContext(ctx).Where("id IN ?", leaderIDs).Find(&leaders).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
leaderByID := make(map[uint]*biz.User, len(leaders))
|
||||
leaderByID := make(map[uint]*system.User, len(leaders))
|
||||
for i := range leaders {
|
||||
leaderByID[leaders[i].ID] = baseBizUser(&leaders[i])
|
||||
}
|
||||
@@ -74,7 +73,7 @@ func (r *departmentRepo) attachDepartmentLeaders(ctx context.Context, department
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (r *departmentRepo) CreateDepartment(ctx context.Context, v *biz.Department) error {
|
||||
func (r *departmentRepo) CreateDepartment(ctx context.Context, v *system.Department) error {
|
||||
v.Ancestors = "0"
|
||||
if v.ParentID != 0 {
|
||||
var parent departmentPO
|
||||
@@ -88,7 +87,7 @@ func (r *departmentRepo) CreateDepartment(ctx context.Context, v *biz.Department
|
||||
}
|
||||
return r.data.DB().WithContext(ctx).Create(&departmentPO{Name: v.Name, ParentID: v.ParentID, Ancestors: v.Ancestors, Sort: v.Sort, LeaderID: v.LeaderID, Status: v.Status}).Error
|
||||
}
|
||||
func (r *departmentRepo) UpdateDepartment(ctx context.Context, v *biz.Department) error {
|
||||
func (r *departmentRepo) UpdateDepartment(ctx context.Context, v *system.Department) error {
|
||||
if v.ParentID == v.ID {
|
||||
return errors.New("父部门不能是自己")
|
||||
}
|
||||
@@ -130,18 +129,18 @@ func (r *departmentRepo) DeleteDepartment(ctx context.Context, id uint) error {
|
||||
}
|
||||
return r.data.DB().WithContext(ctx).Delete(&departmentPO{}, id).Error
|
||||
}
|
||||
func (r *departmentRepo) FindDepartment(ctx context.Context, id uint) (*biz.Department, error) {
|
||||
func (r *departmentRepo) FindDepartment(ctx context.Context, id uint) (*system.Department, error) {
|
||||
var po departmentPO
|
||||
if err := r.data.DB().WithContext(ctx).First(&po, id).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
department := deptFromPO(po)
|
||||
if err := r.attachDepartmentLeaders(ctx, []*biz.Department{department}); err != nil {
|
||||
if err := r.attachDepartmentLeaders(ctx, []*system.Department{department}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return department, nil
|
||||
}
|
||||
func (r *departmentRepo) ListDepartments(ctx context.Context, name string) ([]*biz.Department, error) {
|
||||
func (r *departmentRepo) ListDepartments(ctx context.Context, name string) ([]*system.Department, error) {
|
||||
var pos []departmentPO
|
||||
db := r.data.DB().WithContext(ctx).Order("sort")
|
||||
if name != "" {
|
||||
@@ -150,15 +149,15 @@ func (r *departmentRepo) ListDepartments(ctx context.Context, name string) ([]*b
|
||||
if err := db.Find(&pos).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nodes := map[uint]*biz.Department{}
|
||||
items := make([]*biz.Department, 0, len(pos))
|
||||
nodes := map[uint]*system.Department{}
|
||||
items := make([]*system.Department, 0, len(pos))
|
||||
for _, po := range pos {
|
||||
item := deptFromPO(po)
|
||||
if name == "" {
|
||||
// Recursive GORM child queries initialize leaf collections to an empty
|
||||
// slice. Preserve that tree response shape while search and single-item
|
||||
// queries keep the model's nil children value.
|
||||
item.Children = []*biz.Department{}
|
||||
item.Children = []*system.Department{}
|
||||
}
|
||||
nodes[po.ID] = item
|
||||
items = append(items, item)
|
||||
@@ -169,7 +168,7 @@ func (r *departmentRepo) ListDepartments(ctx context.Context, name string) ([]*b
|
||||
if name != "" {
|
||||
return items, nil
|
||||
}
|
||||
roots := make([]*biz.Department, 0)
|
||||
roots := make([]*system.Department, 0)
|
||||
for _, po := range pos {
|
||||
n := nodes[po.ID]
|
||||
if p := nodes[po.ParentID]; p != nil {
|
||||
|
||||
Reference in New Issue
Block a user