Files
ai_academy/internal/handler/training/training_library.go
T
runny 4e25fce9aa feat(training): 企业培训迭代:推送深链接与签名下载、课程管理修复与收集模板调整
推送域(000155):
- 广播通知支持 link_url:demand_collect 场景自动签发教育训练需求表模板
  匿名下载直链(HMAC token,20 天有效,免登录路由),企微消息尾部拼
  「下载模板 + 查看更多详情」链接;站内文本保持纯文本不受影响
- 配置中心新增「推送链接基址」(notify_link_base_url,多入口/内外网部署固定
  权威地址,留空跟随推送人访问地址)与「年培训计划收集推送文案」
  (demand_collect_*,{year} 渲染收集年度);空标题/内容按模板回落渲染
- 迁移 000155:tr_training_notifications 增 link_url/link_token 列

课程管理:
- 修复点【发布/下线】报「不支持的操作」:动作取值兼容字面量路由(FullPath
  末段)与 :action 通配两种注册形态
- 列表新增「创建人」列:creator_id 联查 users 带出姓名,导出 Excel 同步;
  新增课程「所属部门」默认当前登录用户所在部门
- 导入加固:表头归一化 + 别名模糊匹配(兼容内置模板/旧口径/自定义模板变体);
  导入与新增行支持按部门名兜底解析为 id(前端名册未命中时传 owner_dept_name)

年培训计划:
- 需求列表新增「创建人/创建时间」列(submitter_id 联查 users 带出姓名)
- 收集模板调整:下载时动态删除「培训费用(RMB)」列(旧版自定义模板同步归一,
  右侧列/合并/下拉随删列平移);备注右侧追加「调研维度/培训策划要求」说明列
  (合并至数据区末行,自动换行带边框,已带说明的自定义模板不覆盖)

开发工具:
- air 热重载适配 Windows:构建收敛到 scripts/air_build.sh(规避 air 经
  PowerShell 执行不支持 bash 命令替换),产物改 .exe 后缀;CGO_CFLAGS 取
  持久化值并集追加,不覆盖 go env -w 的头文件包含路径
2026-11-19 17:10:12 +08:00

248 lines
9.3 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 可读(月培训计划下拉同源);维护动作限人事/课长/讲师。
// ---------------------------------------------------------------------------
// 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 — 课程管理列表(关键字分页)。
func (h *Handler) ListCourseLibrary(c *gin.Context) {
items, total, err := h.svc.ListCourseLibraryItems(c.Request.Context(), callerTenantID(c), listQueryFromRequest(c))
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 空 = 新增)。
func (h *Handler) SaveCourseLibraryItem(c *gin.Context) {
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
return
}
var req saveCourseLibraryRequest
if err := c.ShouldBindJSON(&req); err != nil {
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
return
}
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})
}
// DeleteCourseLibraryItem DELETE /training/course-library/:id — 删除(人事/课长)。
func (h *Handler) DeleteCourseLibraryItem(c *gin.Context) {
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
return
}
if err := h.svc.DeleteCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id")); 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})
}
// 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})
}
// 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)
}
}