261 lines
8.2 KiB
Go
261 lines
8.2 KiB
Go
package orgcenter
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"academy/internal/errors"
|
|
"academy/internal/types"
|
|
"academy/internal/types/interfaces"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Handler 组织中心 HTTP 处理器。权限模型:
|
|
// - 组织架构为租户级共享数据,读写由空间 RBAC 把关(路由层 Viewer 读 / Contributor 写);
|
|
// - 各业务模块的角色绑定(module_key)由模块自己的接口/handler 细控,
|
|
// 本层不感知培训等模块的业务角色语义。
|
|
type Handler struct {
|
|
svc interfaces.OrgCenterService
|
|
}
|
|
|
|
// NewHandler constructs the org center handler.
|
|
func NewHandler(svc interfaces.OrgCenterService) *Handler {
|
|
return &Handler{svc: svc}
|
|
}
|
|
|
|
// callerTenantID reads the tenant id injected by the auth middleware.
|
|
func callerTenantID(c *gin.Context) uint64 {
|
|
return c.GetUint64(types.TenantIDContextKey.String())
|
|
}
|
|
|
|
// orgListQueryFromRequest parses the common list filters from the request.
|
|
func orgListQueryFromRequest(c *gin.Context) *types.OrgListQuery {
|
|
q := &types.OrgListQuery{
|
|
DeptID: c.Query("dept_id"),
|
|
Keyword: c.Query("keyword"),
|
|
}
|
|
if v, err := strconv.Atoi(c.Query("page")); err == nil {
|
|
q.Page = v
|
|
}
|
|
if v, err := strconv.Atoi(c.Query("page_size")); err == nil {
|
|
q.PageSize = v
|
|
}
|
|
return q
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 部门
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// ListDepts GET /org/depts
|
|
func (h *Handler) ListDepts(c *gin.Context) {
|
|
depts, err := h.svc.ListDepts(c.Request.Context(), callerTenantID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": depts})
|
|
}
|
|
|
|
// saveDeptRequest is the body of POST /org/depts.
|
|
type saveDeptRequest struct {
|
|
ID string `json:"id"`
|
|
ParentID string `json:"parent_id"`
|
|
Name string `json:"name" binding:"required"`
|
|
Code string `json:"code"`
|
|
Sort int `json:"sort"`
|
|
}
|
|
|
|
// SaveDept POST /org/depts (id 非空走更新)
|
|
func (h *Handler) SaveDept(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req saveDeptRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
dept, err := h.svc.SaveDept(ctx, callerTenantID(c), &types.OrgDept{
|
|
ID: req.ID, ParentID: req.ParentID, Name: req.Name, Code: req.Code, Sort: req.Sort,
|
|
})
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": dept})
|
|
}
|
|
|
|
// DeleteDept DELETE /org/depts/:id
|
|
func (h *Handler) DeleteDept(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if err := h.svc.DeleteDept(ctx, callerTenantID(c), c.Param("id")); err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 员工档案
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// ListEmployees GET /org/employees?module_key=&dept_id=&keyword=&page=&page_size=
|
|
// module_key 可选:传入时附加该模块的绑定角色展示(如 training)。
|
|
func (h *Handler) ListEmployees(c *gin.Context) {
|
|
employees, total, err := h.svc.ListEmployees(
|
|
c.Request.Context(), callerTenantID(c), c.Query("module_key"), orgListQueryFromRequest(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"items": employees, "total": total}})
|
|
}
|
|
|
|
// importEmployeeItem is one row of the employee import payload.
|
|
type importEmployeeItem struct {
|
|
UserID string `json:"user_id" binding:"required"`
|
|
EmployeeNo string `json:"employee_no"`
|
|
DeptID string `json:"dept_id" binding:"required"`
|
|
Post string `json:"post"`
|
|
WecomUserid string `json:"wecom_userid"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// ImportEmployeesRequest is the body of POST /org/employees/import.
|
|
type ImportEmployeesRequest struct {
|
|
Employees []importEmployeeItem `json:"employees" binding:"required,min=1"`
|
|
}
|
|
|
|
// ImportEmployees POST /org/employees/import (部门必填,服务层校验命中)
|
|
func (h *Handler) ImportEmployees(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req ImportEmployeesRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
employees := make([]*types.OrgEmployee, 0, len(req.Employees))
|
|
for _, item := range req.Employees {
|
|
employees = append(employees, &types.OrgEmployee{
|
|
UserID: item.UserID,
|
|
EmployeeNo: item.EmployeeNo,
|
|
DeptID: item.DeptID,
|
|
Post: item.Post,
|
|
WecomUserid: item.WecomUserid,
|
|
Status: item.Status,
|
|
})
|
|
}
|
|
count, err := h.svc.ImportEmployees(ctx, callerTenantID(c), employees)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"imported": count}})
|
|
}
|
|
|
|
// updateEmployeeRequest is the body of PUT /org/employees/:id.
|
|
// 姓名/工号随平台账号维护,不在可编辑范围;部门必填(与导入同规)。
|
|
type updateEmployeeRequest struct {
|
|
DeptID string `json:"dept_id" binding:"required"`
|
|
Post string `json:"post"`
|
|
Status string `json:"status"`
|
|
}
|
|
|
|
// UpdateEmployee PUT /org/employees/:id — 修改员工档案。
|
|
// Read-modify-write:先取现有档案再覆盖可编辑字段,wecom_userid/lecturer_rate
|
|
// 等由 OA 同步/业务模块维护的字段保持不动;状态留空表示不变更。
|
|
func (h *Handler) UpdateEmployee(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req updateEmployeeRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
employee, err := h.svc.GetEmployee(ctx, callerTenantID(c), c.Param("id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
employee.DeptID = req.DeptID
|
|
employee.Post = req.Post
|
|
if req.Status != "" {
|
|
employee.Status = req.Status
|
|
}
|
|
updated, err := h.svc.UpdateEmployee(ctx, callerTenantID(c), employee)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": updated})
|
|
}
|
|
|
|
// ListPlatformUsers GET /org/platform-users — 员工导入的用户选择器数据源。
|
|
func (h *Handler) ListPlatformUsers(c *gin.Context) {
|
|
users, err := h.svc.ListTenantUsers(c.Request.Context(), callerTenantID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": users})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 模块角色绑定
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// ListModuleMembers GET /org/modules/:module_key/members
|
|
func (h *Handler) ListModuleMembers(c *gin.Context) {
|
|
members, err := h.svc.ListModuleMembers(
|
|
c.Request.Context(), callerTenantID(c), c.Param("module_key"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": members})
|
|
}
|
|
|
|
// putModuleMemberRequest is the body of PUT /org/modules/:module_key/members/:employee_id.
|
|
type putModuleMemberRequest struct {
|
|
Roles []string `json:"roles"`
|
|
Extra map[string]any `json:"extra"`
|
|
}
|
|
|
|
// PutModuleMember PUT /org/modules/:module_key/members/:employee_id
|
|
func (h *Handler) PutModuleMember(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req putModuleMemberRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
member, err := h.svc.PutModuleMember(ctx, callerTenantID(c), &types.OrgModuleMember{
|
|
ModuleKey: c.Param("module_key"),
|
|
EmployeeID: c.Param("employee_id"),
|
|
Roles: orgJSONFromRoles(req.Roles),
|
|
Extra: orgJSONFromAny(req.Extra),
|
|
})
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": member})
|
|
}
|
|
|
|
// orgJSONFromRoles encodes a role list as the roles jsonb payload.
|
|
func orgJSONFromRoles(roles []string) types.JSON {
|
|
if roles == nil {
|
|
roles = []string{}
|
|
}
|
|
data, _ := json.Marshal(roles)
|
|
return types.JSON(data)
|
|
}
|
|
|
|
// orgJSONFromAny encodes an arbitrary map as the extra jsonb payload.
|
|
func orgJSONFromAny(extra map[string]any) types.JSON {
|
|
if extra == nil {
|
|
extra = map[string]any{}
|
|
}
|
|
data, _ := json.Marshal(extra)
|
|
return types.JSON(data)
|
|
}
|