Files
yunzerwebsiteallinone/go/controllers/backend_oa_scheduling_scope.go
T
2026-11-16 17:22:50 +08:00

189 lines
4.7 KiB
Go

package controllers
import (
"strconv"
"strings"
"server/models"
"github.com/beego/beego/v2/client/orm"
)
type schedulingEmployee struct {
ID uint `json:"id"`
Name string `json:"name"`
Account string `json:"account"`
Department string `json:"department"`
DepartmentName string `json:"department_name"`
Position string `json:"position"`
Phone string `json:"phone"`
AccountStatus int8 `json:"account_status"`
}
type scOrgIndex struct {
rows map[uint64]models.BackendOrganization
children map[uint64][]uint64
names map[uint64]string
}
func (c *BackendOaSchedulingController) scLoadOrgIndex(tid uint64) *scOrgIndex {
index := &scOrgIndex{
rows: map[uint64]models.BackendOrganization{},
children: map[uint64][]uint64{},
names: map[uint64]string{},
}
var rows []models.BackendOrganization
_, err := models.Orm.QueryTable(new(models.BackendOrganization)).
Filter("tid", tid).
Filter("delete_time__isnull", true).
All(&rows)
if err != nil {
return index
}
for _, row := range rows {
index.rows[row.ID] = row
index.names[row.ID] = row.OrgName
index.children[row.ParentID] = append(index.children[row.ParentID], row.ID)
}
return index
}
func (index *scOrgIndex) subtreeIDs(root uint64) map[uint64]bool {
result := map[uint64]bool{}
if root == 0 {
return result
}
queue := []uint64{root}
for len(queue) > 0 {
current := queue[0]
queue = queue[1:]
if result[current] {
continue
}
result[current] = true
queue = append(queue, index.children[current]...)
}
return result
}
func (index *scOrgIndex) name(id uint64) string {
if name, ok := index.names[id]; ok {
return name
}
return ""
}
func (c *BackendOaSchedulingController) scBaseEmployeeQuery(tid uint64) orm.QuerySeter {
return employeeBaseQuery(tid)
}
func employeeBaseQuery(tid uint64) orm.QuerySeter {
return models.Orm.QueryTable(new(models.BackendEmployee)).
Filter("tid", tid).
Filter("delete_time__isnull", true).
Exclude("account_status", 2)
}
func scEmployeeDepartmentID(row models.BackendEmployee) uint64 {
if row.Department == nil {
return 0
}
id, err := strconv.ParseUint(strings.TrimSpace(*row.Department), 10, 64)
if err != nil {
return 0
}
return id
}
func (c *BackendOaSchedulingController) scEmployeeDTOs(tid uint64, rows []models.BackendEmployee) []schedulingEmployee {
index := c.scLoadOrgIndex(tid)
result := make([]schedulingEmployee, 0, len(rows))
for _, row := range rows {
orgID := scEmployeeDepartmentID(row)
position := ""
if row.Position != nil {
position = *row.Position
}
phone := ""
if row.Phone != nil {
phone = *row.Phone
}
result = append(result, schedulingEmployee{
ID: row.ID,
Name: row.Name,
Account: row.Account,
Department: strconv.FormatUint(orgID, 10),
DepartmentName: index.name(orgID),
Position: position,
Phone: phone,
AccountStatus: row.AccountStatus,
})
}
return result
}
func (c *BackendOaSchedulingController) scEmployeesByOrg(tid, orgID uint64, includeChild bool) ([]models.BackendEmployee, error) {
qs := c.scBaseEmployeeQuery(tid)
if orgID > 0 {
allowed := map[uint64]bool{orgID: true}
if includeChild {
allowed = c.scLoadOrgIndex(tid).subtreeIDs(orgID)
}
values := make([]string, 0, len(allowed))
for id := range allowed {
values = append(values, strconv.FormatUint(id, 10))
}
qs = qs.Filter("department__in", values)
}
var rows []models.BackendEmployee
_, err := qs.OrderBy("department", "id").All(&rows)
if err != nil && err != orm.ErrNoRows {
return nil, err
}
return rows, nil
}
func (c *BackendOaSchedulingController) scEmployeesByIDs(tid uint64, ids []uint64) ([]models.BackendEmployee, error) {
if len(ids) == 0 {
return nil, nil
}
var rows []models.BackendEmployee
_, err := c.scBaseEmployeeQuery(tid).Filter("id__in", ids).All(&rows)
if err != nil && err != orm.ErrNoRows {
return nil, err
}
return rows, nil
}
func (c *BackendOaSchedulingController) scResolveEmployees(tid uint64, scopeType int8, orgID uint64, includeChild bool, employeeIDs []uint64) ([]models.BackendEmployee, error) {
switch scopeType {
case models.OaScopeEmployees:
return c.scEmployeesByIDs(tid, employeeIDs)
case models.OaScopeCompany:
return c.scEmployeesByOrg(tid, 0, false)
default:
return c.scEmployeesByOrg(tid, orgID, includeChild)
}
}
func scWorkdays(dates []string, weekdays map[int]bool) []string {
if len(weekdays) == 0 {
return dates
}
result := make([]string, 0, len(dates))
for _, date := range dates {
parsed, ok := scParseDate(date)
if !ok {
continue
}
weekday := int(parsed.Weekday())
if weekday == 0 {
weekday = 7
}
if weekdays[weekday] {
result = append(result, date)
}
}
return result
}