yunzer_go/server/services/oa.go
2025-11-06 15:56:29 +08:00

77 lines
1.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package services
import (
"fmt"
"server/models"
)
// OABaseData 基础数据结构
type OABaseData struct {
Departments []*models.Department `json:"departments"`
Positions []*models.Position `json:"positions"`
Roles []*models.Role `json:"roles"`
}
// GetOABaseData 获取OA基础数据部门、职位、角色
// 这是一个合并接口,用于一次性获取所有基础数据,减少网络请求次数
// 使用 goroutine 并行查询,提高性能
func GetOABaseData(tenantId int) (*OABaseData, error) {
type deptResult struct {
departments []*models.Department
err error
}
type posResult struct {
positions []*models.Position
err error
}
type roleResult struct {
roles []*models.Role
err error
}
deptChan := make(chan deptResult, 1)
posChan := make(chan posResult, 1)
roleChan := make(chan roleResult, 1)
// 并行获取部门数据
go func() {
depts, err := models.GetTenantDepartments(tenantId)
deptChan <- deptResult{departments: depts, err: err}
}()
// 并行获取职位数据
go func() {
pos, err := models.GetTenantPositions(tenantId)
posChan <- posResult{positions: pos, err: err}
}()
// 并行获取角色数据
go func() {
rols, err := models.GetRoleByTenantId(tenantId)
roleChan <- roleResult{roles: rols, err: err}
}()
// 接收所有结果
deptRes := <-deptChan
posRes := <-posChan
roleRes := <-roleChan
// 如果任何一个查询失败,返回错误
if deptRes.err != nil {
return nil, fmt.Errorf("获取部门列表失败: %v", deptRes.err)
}
if posRes.err != nil {
return nil, fmt.Errorf("获取职位列表失败: %v", posRes.err)
}
if roleRes.err != nil {
return nil, fmt.Errorf("获取角色列表失败: %v", roleRes.err)
}
return &OABaseData{
Departments: deptRes.departments,
Positions: posRes.positions,
Roles: roleRes.roles,
}, nil
}