deploy-hxtesting / build-and-deploy (push) Successful in 1m18s
2.创建课程时课程默认是”线上“,发布课程时必须要有课件! 3.课程管理中的课件上传预览下载交互样式,按维护”试题“交互样式风格来!列表中增加一列”试题“,列表中”试题“、”课件“按实际情况只展示”有/无“即可!
363 lines
15 KiB
Go
363 lines
15 KiB
Go
package training
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"path"
|
|
|
|
"academy/internal/errors"
|
|
"academy/internal/filetransport"
|
|
"academy/internal/types"
|
|
"academy/internal/utils"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 课程管理(课程资产主数据,tr_course_library,000151)。
|
|
// 字段:课程名称/所属部门(org_depts)/说明/适合人员/分类(配置中心
|
|
// course_category_dict 可配,可自定义)/类型(必修课/选修课)/线上或线下/
|
|
// 讲师/时长/课件。月培训计划的「计划名称」从本表选择。
|
|
// 列表 Viewer 可读(月培训计划下拉同源),数据范围按角色收窄(000157):
|
|
// 人事全部/课长经理本部门/讲师学员仅「与我相关」;维护(新增/编辑/发布/导入/
|
|
// 导出)限人事/课长/讲师;核验/删除仅课长/经理且限本部门、删除仅限待核验行。
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// saveCourseLibraryRequest is the body of POST /training/course-library.
|
|
type saveCourseLibraryRequest struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name" binding:"required"`
|
|
OwnerDeptID string `json:"owner_dept_id"`
|
|
// OwnerDeptName 所属部门名(导入兜底:前端未匹配到部门 id 时只传名,
|
|
// 服务端按组织架构名册解析;与只读展示列 owner_dept_name 同名字段复用)。
|
|
OwnerDeptName string `json:"owner_dept_name"`
|
|
Description string `json:"description"`
|
|
SuitableMembers string `json:"suitable_members"`
|
|
Category string `json:"category"`
|
|
Type string `json:"type"`
|
|
Mode string `json:"mode"`
|
|
LecturerID string `json:"lecturer_id"`
|
|
LecturerExternal string `json:"lecturer_external"`
|
|
Hours float64 `json:"hours"`
|
|
MaterialName string `json:"material_name"`
|
|
// ClearFile 移除课件文件(前端「移除」按钮;置 true 强制清空 file_* 三列)。
|
|
ClearFile bool `json:"clear_file"`
|
|
}
|
|
|
|
// toCourseLibrary maps the request body onto the domain model.
|
|
func toCourseLibrary(req saveCourseLibraryRequest) *types.TrainingCourseLibrary {
|
|
return &types.TrainingCourseLibrary{
|
|
ID: req.ID,
|
|
Name: req.Name,
|
|
OwnerDeptID: req.OwnerDeptID,
|
|
OwnerDeptName: req.OwnerDeptName,
|
|
Description: req.Description,
|
|
SuitableMembers: req.SuitableMembers,
|
|
Category: req.Category,
|
|
Type: req.Type,
|
|
Mode: req.Mode,
|
|
LecturerID: req.LecturerID,
|
|
LecturerExternal: req.LecturerExternal,
|
|
Hours: req.Hours,
|
|
MaterialName: req.MaterialName,
|
|
ClearFile: req.ClearFile,
|
|
}
|
|
}
|
|
|
|
// ListCourseLibrary GET /training/course-library — 课程管理列表(关键字分页)。
|
|
// 数据范围按调用方角色强制注入(000157):人事全部/课长经理本部门/讲师学员仅
|
|
// 「与我相关」;月培训计划「计划名称」下拉同源,口径一致。
|
|
// scope=catalog(000160)为学员目录口径:已发布 + 适合人员覆盖调用方
|
|
// (「全部」或调用方部门名;未设置适合人员沿用原部门范围口径),不按维护
|
|
// 角色收窄 —— 目录面向全体员工,「培训中心·和信课程」同源。
|
|
func (h *Handler) ListCourseLibrary(c *gin.Context) {
|
|
q := listQueryFromRequest(c)
|
|
if q.Scope == "catalog" {
|
|
q.CatalogOnly = true
|
|
// 调用方部门(id+名)用于适合人员匹配;无培训档案的调用方两个字段
|
|
// 留空(仅见「全部」与未设置适合人员的课程),不阻断目录查询。
|
|
if member, err := h.svc.CurrentMember(c.Request.Context(), callerTenantID(c), callerUserID(c)); err == nil && member != nil {
|
|
q.CatalogDeptID = member.DeptID
|
|
}
|
|
if q.CatalogDeptID != "" {
|
|
if depts, err := h.svc.ListDepts(c.Request.Context(), callerTenantID(c)); err == nil {
|
|
for _, d := range depts {
|
|
if d.ID == q.CatalogDeptID {
|
|
q.CatalogDeptName = d.Name
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
q.DeptID, q.RelatedUserID = h.callerCourseLibraryScope(c)
|
|
}
|
|
items, total, err := h.svc.ListCourseLibraryItems(c.Request.Context(), callerTenantID(c), q)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"items": items, "total": total}})
|
|
}
|
|
|
|
// SaveCourseLibraryItem POST /training/course-library — 新增/编辑(id 空 = 新增)。
|
|
// 维护角色限人事/课长/讲师;无维护角色的用户兜底仅可编辑「与我相关」课程
|
|
// (本人创建或本人授课,不可新增,000157)。
|
|
func (h *Handler) SaveCourseLibraryItem(c *gin.Context) {
|
|
var req saveCourseLibraryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
if req.ID == "" || !h.canEditRelatedLibraryCourse(c, req.ID) {
|
|
return // requireRoles/兜底校验已写 403/404
|
|
}
|
|
}
|
|
item, err := h.svc.SaveCourseLibraryItem(c.Request.Context(), callerTenantID(c), toCourseLibrary(req), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
|
}
|
|
|
|
// canEditRelatedLibraryCourse 无维护角色用户的编辑兜底(000157):目标课程须存在
|
|
// 且创建人或讲师为本人;学员编辑同样触发服务层「编辑即回待核验」重核验流程。
|
|
func (h *Handler) canEditRelatedLibraryCourse(c *gin.Context, id string) bool {
|
|
item, err := h.svc.GetCourseLibraryItem(c.Request.Context(), callerTenantID(c), id)
|
|
if err != nil || item == nil {
|
|
_ = c.Error(errors.NewNotFoundError("课程不存在"))
|
|
return false
|
|
}
|
|
userID := callerUserID(c)
|
|
if item.CreatorID != userID && item.LecturerID != userID {
|
|
_ = c.Error(errors.NewForbiddenError("仅可编辑本人创建或本人授课的课程"))
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// DeleteCourseLibraryItem DELETE /training/course-library/:id — 删除
|
|
// (000157:仅本部门课长/经理,仅待核验状态;人事/学员/空间管理员无删除权限)。
|
|
func (h *Handler) DeleteCourseLibraryItem(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleSupervisor, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
deptID, ok := h.callerVerifyDeptScope(c)
|
|
if !ok {
|
|
_ = c.Error(errors.NewForbiddenError("仅本部门课长/经理可删除课程"))
|
|
return
|
|
}
|
|
if err := h.svc.DeleteCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id"), deptID); err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// SetCourseLibraryPublishStatus POST /training/course-library/:id/publish|offline
|
|
// — 课程条目发布/下线(000154,人事/课长/讲师)。发布后课程进入培训中心
|
|
// 「和信课程」目录;下线即从目录撤回。动作取值兼容两种路由注册:字面量
|
|
// /course-library/:id/publish|offline(现网注册,FullPath 末段)与
|
|
// /course-library/:id/:action 通配(Param),任一命中即执行。
|
|
func (h *Handler) SetCourseLibraryPublishStatus(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
action := c.Param("action")
|
|
if action == "" {
|
|
action = path.Base(c.FullPath())
|
|
}
|
|
status := ""
|
|
switch action {
|
|
case "publish":
|
|
status = "published"
|
|
case "offline":
|
|
status = "offline"
|
|
default:
|
|
_ = c.Error(errors.NewBadRequestError("不支持的操作(仅 publish/offline)"))
|
|
return
|
|
}
|
|
if err := h.svc.SetCourseLibraryPublishStatus(c.Request.Context(), callerTenantID(c), c.Param("id"), status); err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
item, err := h.svc.GetCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
|
}
|
|
|
|
// callerVerifyDeptScope 核验/删除的部门口径(000157):一律按课长身份收窄本部门,
|
|
// 不设全局旁路(人事/空间管理员均不例外)——核验权只属于「该部门课长/经理」。
|
|
// 返回所在部门 id 与是否具备身份;无课长身份(或课长未分配部门)一律拒绝。
|
|
func (h *Handler) callerVerifyDeptScope(c *gin.Context) (deptID string, ok bool) {
|
|
member, err := h.svc.CurrentMember(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
|
if err != nil || member == nil {
|
|
return "", false
|
|
}
|
|
roles := h.svc.ExpandTrainingRoles(c.Request.Context(), callerTenantID(c), member.TrainingRoleList())
|
|
// 经理继承课长权限组,与课长同口径;人事角色不构成核验身份。
|
|
if types.HasTrainingRole(roles, types.TrainingRoleSupervisor) && member.DeptID != "" {
|
|
return member.DeptID, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
// verifyCourseLibraryRequest is the body of POST /training/course-library/verify
|
|
// (单个/批量核验共用,ids 长度 1 即单条)。
|
|
type verifyCourseLibraryRequest struct {
|
|
IDs []string `json:"ids" binding:"required,min=1"`
|
|
}
|
|
|
|
// VerifyCourseLibraryItems POST /training/course-library/verify — 课程条目核验
|
|
// (000157):仅本部门课长/经理可核验(人事/讲师/学员/空间管理员均无跨部门核验权),
|
|
// 待核验 → 已确认;越权/已确认/不存在行跳过不阻断其余行。
|
|
func (h *Handler) VerifyCourseLibraryItems(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleSupervisor, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
var req verifyCourseLibraryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
deptID, ok := h.callerVerifyDeptScope(c)
|
|
if !ok {
|
|
_ = c.Error(errors.NewForbiddenError("仅本部门课长/经理可核验课程"))
|
|
return
|
|
}
|
|
verified, skipped, err := h.svc.VerifyCourseLibraryItems(c.Request.Context(), callerTenantID(c), req.IDs, callerUserID(c), deptID)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"verified": verified, "skipped": skipped}})
|
|
}
|
|
|
|
// ImportCourseLibraryRequest is the body of POST /training/course-library/import
|
|
// (前端解析 Excel 后逐行提交,与需求导入同口径)。
|
|
type ImportCourseLibraryRequest struct {
|
|
Items []saveCourseLibraryRequest `json:"items" binding:"required,min=1"`
|
|
}
|
|
|
|
// ImportCourseLibrary POST /training/course-library/import — 批量导入。
|
|
func (h *Handler) ImportCourseLibrary(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
var req ImportCourseLibraryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
items := make([]*types.TrainingCourseLibrary, 0, len(req.Items))
|
|
for _, row := range req.Items {
|
|
items = append(items, toCourseLibrary(row))
|
|
}
|
|
imported, err := h.svc.ImportCourseLibraryItems(c.Request.Context(), callerTenantID(c), items, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"imported": imported}})
|
|
}
|
|
|
|
// ExportCourseLibraryRequest is the body of POST /training/course-library/export
|
|
// (ids 空 = 全量导出;当前筛选由前端传当前列表 id)。
|
|
type ExportCourseLibraryRequest struct {
|
|
IDs []string `json:"ids"`
|
|
}
|
|
|
|
// ExportCourseLibrary POST /training/course-library/export — 课程管理 xlsx 下载。
|
|
func (h *Handler) ExportCourseLibrary(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
var req ExportCourseLibraryRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
filename, data, err := h.svc.ExportCourseLibrary(c.Request.Context(), callerTenantID(c), req.IDs)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.Header("Content-Disposition", "attachment; filename="+filename)
|
|
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", data)
|
|
}
|
|
|
|
// UploadCourseLibraryMaterial POST /training/course-library/:id/material —
|
|
// 上传课件原始文件(multipart: file)。
|
|
func (h *Handler) UploadCourseLibraryMaterial(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
fh, err := c.FormFile("file")
|
|
if err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("缺少上传文件"))
|
|
return
|
|
}
|
|
if fh.Size > utils.GetMaxFileSizeMB()*1024*1024 {
|
|
_ = c.Error(errors.NewBadRequestError(fmt.Sprintf("文件大小不能超过%dMB", utils.GetMaxFileSizeMB())))
|
|
return
|
|
}
|
|
item, err := h.svc.UploadCourseLibraryMaterial(c.Request.Context(), callerTenantID(c), c.Param("id"), fh, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
|
}
|
|
|
|
// AttachCourseLibraryMaterialFromKb POST /training/course-library/:id/material/from-kb
|
|
// — 从知识库「企业培训(课件)」选择已有文档作为课程课件(body: knowledge_id)。
|
|
func (h *Handler) AttachCourseLibraryMaterialFromKb(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
var req struct {
|
|
KnowledgeID string `json:"knowledge_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
item, err := h.svc.AttachCourseLibraryMaterialFromKnowledge(c.Request.Context(), callerTenantID(c), c.Param("id"), req.KnowledgeID, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
|
}
|
|
|
|
// RemoveCourseLibraryMaterial POST /training/course-library/:id/material/remove
|
|
// — 移除课程课件原始文件/知识库挂接(名称登记保留,课件变更即回待核验)。
|
|
func (h *Handler) RemoveCourseLibraryMaterial(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
item, err := h.svc.RemoveCourseLibraryMaterial(c.Request.Context(), callerTenantID(c), c.Param("id"), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
|
}
|
|
|
|
// CourseLibraryMaterialFile GET /training/course-library/:id/material/file —
|
|
// 课件原始文件流(下载/在线预览)。
|
|
func (h *Handler) CourseLibraryMaterialFile(c *gin.Context) {
|
|
rc, fileName, err := h.svc.GetCourseLibraryMaterialFile(c.Request.Context(), callerTenantID(c), c.Param("id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
if err := filetransport.Serve(c.Writer, c.Request, rc, filetransport.Options{Filename: fileName}); err != nil {
|
|
_ = c.Error(err)
|
|
}
|
|
}
|