moto/internal/organize/department.go

113 lines
3.1 KiB
Go
Raw Normal View History

2024-12-12 17:39:04 +08:00
package organize
import (
"context"
"fmt"
"git.nobla.cn/golang/moto/common/db"
"git.nobla.cn/golang/moto/models"
"git.nobla.cn/golang/rest/types"
"gorm.io/gorm"
"strings"
)
func RecursiveDepartment(ctx context.Context, parent string, level int, departments []*models.Department) []*types.TypeValue {
var (
child []*types.TypeValue
)
values := make([]*types.TypeValue, 0)
for _, dept := range departments {
if dept.Parent == parent {
values = append(values, &types.TypeValue{
Label: "|-" + strings.Repeat("--", level) + dept.Name,
Value: dept.ID,
})
child = RecursiveDepartment(ctx, dept.ID, level+1, departments)
if len(child) > 0 {
for _, row := range child {
values = append(values, row)
}
}
}
}
return values
}
func RecursiveNestedDepartment(ctx context.Context, parent string, level int, departments []*models.Department) []*types.NestedValue {
values := make([]*types.NestedValue, 0)
for _, dept := range departments {
if dept.Parent == parent {
v := &types.NestedValue{
Label: dept.Name,
Value: dept.ID,
Children: RecursiveNestedDepartment(ctx, dept.ID, level+1, departments),
}
values = append(values, v)
}
}
return values
}
2024-12-13 10:37:09 +08:00
func DepartmentUserNested(ctx context.Context) []*types.NestedValue {
2024-12-12 17:39:04 +08:00
var (
err error
value any
users []*models.User
departments []*models.Department
)
2024-12-13 10:37:09 +08:00
if value, err = db.TryCache(ctx, fmt.Sprintf("departments"), func(tx *gorm.DB) (any, error) {
2024-12-12 17:39:04 +08:00
departments = make([]*models.Department, 0)
2024-12-13 10:37:09 +08:00
err = tx.Order("created_at ASC").Find(&departments).Error
2024-12-12 17:39:04 +08:00
return departments, err
2024-12-13 10:37:09 +08:00
}, db.WithDepend("SELECT max(`updated_at`) FROM `departments`")); err == nil {
2024-12-12 17:39:04 +08:00
departments = value.([]*models.Department)
} else {
return nil
}
2024-12-13 10:37:09 +08:00
if value, err = db.TryCache(ctx, fmt.Sprintf("users"), func(tx *gorm.DB) (any, error) {
2024-12-12 17:39:04 +08:00
users = make([]*models.User, 0)
2024-12-13 10:37:09 +08:00
err = tx.Order("uid ASC").Find(&users).Error
2024-12-12 17:39:04 +08:00
return users, err
2024-12-13 10:37:09 +08:00
}, db.WithDepend("SELECT max(`updated_at`) FROM `users`")); err == nil {
2024-12-12 17:39:04 +08:00
users = value.([]*models.User)
} else {
return nil
}
depts := RecursiveDepartment(ctx, "", 0, departments)
values := make([]*types.NestedValue, 0)
for _, dept := range depts {
v := &types.NestedValue{
Label: dept.Label,
Value: dept.Value,
Children: make([]*types.NestedValue, 0),
}
for _, user := range users {
if user.Department == v.Value {
v.Children = append(v.Children, &types.NestedValue{
Label: fmt.Sprintf("%s(%s)", user.Username, user.UID),
Value: user.UID,
})
}
}
values = append(values, v)
}
return values
}
2024-12-13 10:37:09 +08:00
func DepartmentTypes(ctx context.Context) []*types.TypeValue {
result, err := db.TryCache(ctx, fmt.Sprintf("department:types"), func(tx *gorm.DB) (any, error) {
2024-12-12 17:39:04 +08:00
values := make([]*models.Department, 0)
2024-12-13 10:37:09 +08:00
if err := db.WithContext(ctx).Find(&values).Error; err == nil {
2024-12-12 17:39:04 +08:00
return RecursiveDepartment(ctx, "", 0, values), nil
} else {
return nil, err
}
2024-12-13 10:37:09 +08:00
}, db.WithDepend("SELECT max(`updated_at`) FROM `departments`"))
2024-12-12 17:39:04 +08:00
if err == nil {
return result.([]*types.TypeValue)
}
return nil
}