需求域: - 需求导入修复内/外培训与外部讲师落库;按模板标题年入库(年-月一致) - 核验确认收窄为「需求归属部门+经理职称角色」,越权提示联系部门经理 - 确认转月计划后自动推送讲师/课长维护;导入后自动推送部门经理核验(站内+企微) 课程域: - 课程管理发布状态字段(迁移000154)+发布/下线;培训中心「和信课程」目录 - 需求讲师改选后回写课程管理;确认同步带出适合人员/课件/试题 - 月计划详情课件/试题更换(上传/知识库选择/题库换绑)回写课程管理, 无关联条目的存量计划自动创建课程条目 模板与表单: - 教育训练需求表:适合人员/内-外培训口径;部门联动课程下拉; 适合人员/讲师按所选课程自动带出;在线提报课程下拉(本部门)自动带出分类/讲师/适合人员 页面与权限: - 培训中心页签重组(和信课程/我的培训/我的考试),移除线上/线下页签 - 数据驾驶舱迁侧栏(职称角色之下);职称角色迁至更多工具配置中心之后 - 配置中心/职称角色对经理开放;页签「职称角色」更名「角色权限」 - 修复年度下拉空白(TDesign options 需对象数组)、月计划列表地点兜底 其他: 培训预览端口修正与 dev:training 脚本;tmp-extract-duckdb 加入 gitignore
331 lines
13 KiB
Go
331 lines
13 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"strings"
|
|
"time"
|
|
|
|
excelize "github.com/xuri/excelize/v2"
|
|
|
|
werrors "academy/internal/errors"
|
|
"academy/internal/logger"
|
|
"academy/internal/types"
|
|
"academy/internal/types/interfaces"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 课程管理(课程资产主数据,tr_course_library,000151)。
|
|
// 字段口径:课程名称/所属部门(org_depts)/说明/适合人员/分类(配置中心
|
|
// course_category_dict 可配,支持自定义)/类型(必修课/选修课)/线上或线下/
|
|
// 讲师/时长/课件(名称登记+可选原始文件)。
|
|
// 月培训计划的「计划名称」从本表选择,计划创建经 library_course_id 关联。
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// courseLibraryTypeLabel 类型展示文案(导出用)。
|
|
func courseLibraryTypeLabel(t string) string {
|
|
if t == "elective" {
|
|
return "选修课"
|
|
}
|
|
return "必修课"
|
|
}
|
|
|
|
// courseLibraryModeLabel 形式展示文案(导出用)。
|
|
func courseLibraryModeLabel(m string) string {
|
|
if m == types.TrainingCourseModeOnline {
|
|
return "线上"
|
|
}
|
|
return "线下"
|
|
}
|
|
|
|
// normalizeCourseLibrary 归一保存/导入共用的字段口径(名称必填、类型/形式白名单)。
|
|
func normalizeCourseLibrary(item *types.TrainingCourseLibrary) error {
|
|
item.Name = strings.TrimSpace(item.Name)
|
|
if item.Name == "" {
|
|
return werrors.NewBadRequestError("课程名称不能为空")
|
|
}
|
|
item.OwnerDeptID = strings.TrimSpace(item.OwnerDeptID)
|
|
item.Category = strings.TrimSpace(item.Category)
|
|
item.Description = strings.TrimSpace(item.Description)
|
|
item.SuitableMembers = strings.TrimSpace(item.SuitableMembers)
|
|
item.LecturerID = strings.TrimSpace(item.LecturerID)
|
|
item.LecturerExternal = strings.TrimSpace(item.LecturerExternal)
|
|
item.MaterialName = strings.TrimSpace(item.MaterialName)
|
|
if item.Type != "elective" {
|
|
item.Type = "required"
|
|
}
|
|
if item.Mode != types.TrainingCourseModeOnline {
|
|
item.Mode = types.TrainingCourseModeOffline
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListCourseLibraryItems 课程管理列表(月培训计划「计划名称」下拉同源)。
|
|
func (s *trainingService) ListCourseLibraryItems(ctx context.Context, tenantID uint64, q *interfaces.TrainingListQuery) ([]*types.TrainingCourseLibrary, int64, error) {
|
|
return s.repo.ListCourseLibrary(ctx, tenantID, q)
|
|
}
|
|
|
|
// SaveCourseLibraryItem 新增/编辑课程管理条目(id 空 = 新增)。
|
|
func (s *trainingService) SaveCourseLibraryItem(ctx context.Context, tenantID uint64, item *types.TrainingCourseLibrary, updaterID string) (*types.TrainingCourseLibrary, error) {
|
|
if err := normalizeCourseLibrary(item); err != nil {
|
|
return nil, err
|
|
}
|
|
if item.ID == "" {
|
|
item.ID = trainingNewID()
|
|
item.TenantID = tenantID
|
|
item.CreatorID = updaterID
|
|
// 课程编号(CRS-####,创建时由服务端生成;编号列只读,不收前端值)。
|
|
item.Code = s.nextCourseLibraryCode(ctx, tenantID)
|
|
if err := s.repo.CreateCourseLibraryItem(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return item, nil
|
|
}
|
|
// 编辑:先取旧值校验存在性,再全量更新(文件字段随行透传,未动即保持原值)。
|
|
old, err := s.repo.GetCourseLibraryItem(ctx, tenantID, item.ID)
|
|
if err != nil || old == nil {
|
|
return nil, werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
// 课件文件:移除标记强制清空;未重新上传时保留原文件信息
|
|
// (表单不回传 file_key,零值会被误清)。
|
|
if item.ClearFile {
|
|
item.FileKey, item.FileName, item.FileSize = "", "", 0
|
|
item.KnowledgeID = ""
|
|
} else if item.FileKey == "" && item.FileName == "" && item.FileSize == 0 {
|
|
item.FileKey, item.FileName, item.FileSize = old.FileKey, old.FileName, old.FileSize
|
|
}
|
|
// 知识库文档 id 不随表单回传:未重新上传时保留原值(与新文件一并覆盖)。
|
|
if item.KnowledgeID == "" {
|
|
item.KnowledgeID = old.KnowledgeID
|
|
}
|
|
item.TenantID = tenantID
|
|
if err := s.repo.UpdateCourseLibraryItem(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
// DeleteCourseLibraryItem 软删课程管理条目(月培训计划已创建的历史计划不受影响)。
|
|
func (s *trainingService) DeleteCourseLibraryItem(ctx context.Context, tenantID uint64, id string) error {
|
|
if _, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id); err != nil {
|
|
return werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
return s.repo.DeleteCourseLibraryItem(ctx, tenantID, id)
|
|
}
|
|
|
|
// GetCourseLibraryItem 取单条课程管理条目(发布/下线等操作后回显)。
|
|
func (s *trainingService) GetCourseLibraryItem(ctx context.Context, tenantID uint64, id string) (*types.TrainingCourseLibrary, error) {
|
|
item, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id)
|
|
if err != nil || item == nil {
|
|
return nil, werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
return item, nil
|
|
}
|
|
|
|
// SetCourseLibraryPublishStatus 课程条目发布/下线(000154):published 已发布 /
|
|
// offline 已下线;培训中心「和信课程」目录仅展示已发布课程。
|
|
func (s *trainingService) SetCourseLibraryPublishStatus(ctx context.Context, tenantID uint64, id, status string) error {
|
|
if status != "published" && status != "offline" {
|
|
return werrors.NewBadRequestError("发布状态不合法(仅支持发布/下线)")
|
|
}
|
|
item, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id)
|
|
if err != nil || item == nil {
|
|
return werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
return s.repo.UpdateCourseLibraryPublishStatus(ctx, tenantID, id, status)
|
|
}
|
|
|
|
// ImportCourseLibraryItems 批量导入(前端解析 Excel 后逐行提交,讲师已在
|
|
// 前端按员工名册解析为 id / 外部姓名,与需求导入同口径)。
|
|
func (s *trainingService) ImportCourseLibraryItems(ctx context.Context, tenantID uint64, items []*types.TrainingCourseLibrary, updaterID string) (int, error) {
|
|
if len(items) == 0 {
|
|
return 0, werrors.NewBadRequestError("没有可导入的课程数据")
|
|
}
|
|
rows := make([]*types.TrainingCourseLibrary, 0, len(items))
|
|
for _, item := range items {
|
|
if err := normalizeCourseLibrary(item); err != nil {
|
|
continue // 缺课程名称的行跳过(前端同步提示跳过行数)
|
|
}
|
|
item.ID = trainingNewID()
|
|
item.TenantID = tenantID
|
|
item.CreatorID = updaterID
|
|
// 课程编号逐行生成(CRS-####)。
|
|
item.Code = s.nextCourseLibraryCode(ctx, tenantID)
|
|
rows = append(rows, item)
|
|
}
|
|
if len(rows) == 0 {
|
|
return 0, werrors.NewBadRequestError("没有可导入的课程数据(未填课程名称的行已跳过)")
|
|
}
|
|
if err := s.repo.BatchCreateCourseLibrary(ctx, rows); err != nil {
|
|
return 0, err
|
|
}
|
|
logger.Infof(ctx, "[Training] course library imported, tenant=%d rows=%d importer=%s", tenantID, len(rows), updaterID)
|
|
return len(rows), nil
|
|
}
|
|
|
|
// ExportCourseLibrary 导出课程管理 Excel(ids 空 = 全量;当前筛选由前端传 ids)。
|
|
func (s *trainingService) ExportCourseLibrary(ctx context.Context, tenantID uint64, ids []string) (string, []byte, error) {
|
|
items, _, err := s.repo.ListCourseLibrary(ctx, tenantID, &interfaces.TrainingListQuery{Page: 1, PageSize: 5000})
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
if len(ids) > 0 {
|
|
keep := make(map[string]bool, len(ids))
|
|
for _, id := range ids {
|
|
keep[id] = true
|
|
}
|
|
filtered := items[:0]
|
|
for _, item := range items {
|
|
if keep[item.ID] {
|
|
filtered = append(filtered, item)
|
|
}
|
|
}
|
|
items = filtered
|
|
}
|
|
f := excelize.NewFile()
|
|
sheet := "课程管理"
|
|
f.SetSheetName("Sheet1", sheet)
|
|
// 列口径对齐《教育训练需求表》:所属部门=主办单位,适合人员=参加人员,
|
|
// 课件=课件收集;导入解析端(LibraryPanel onImportExcel)两套列名均兼容。
|
|
headers := []string{"课程编号", "所属部门", "课程名称", "课程说明", "适合人员", "课程分类",
|
|
"类型", "线上/线下", "讲师", "时长(学时)", "课件收集", "创建时间"}
|
|
for i, h := range headers {
|
|
cell, _ := excelize.CoordinatesToCellName(i+1, 1)
|
|
_ = f.SetCellValue(sheet, cell, h)
|
|
}
|
|
hoursText := func(h float64) string {
|
|
if h == 0 {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("%g", h)
|
|
}
|
|
for r, item := range items {
|
|
lecturer := item.LecturerName
|
|
if lecturer == "" {
|
|
lecturer = item.LecturerExternal
|
|
}
|
|
values := []string{item.Code, item.OwnerDeptName, item.Name, item.Description, item.SuitableMembers, item.Category,
|
|
courseLibraryTypeLabel(item.Type), courseLibraryModeLabel(item.Mode),
|
|
lecturer, hoursText(item.Hours), item.MaterialName, item.CreatedAt.Format("2006-01-02 15:04")}
|
|
for i, v := range values {
|
|
cell, _ := excelize.CoordinatesToCellName(i+1, r+2)
|
|
_ = f.SetCellValue(sheet, cell, v)
|
|
}
|
|
}
|
|
buf, err := f.WriteToBuffer()
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
filename := fmt.Sprintf("course_library_%s.xlsx", time.Now().Format("20060102150405"))
|
|
logger.Infof(ctx, "[Training] course library exported, tenant=%d rows=%d", tenantID, len(items))
|
|
return filename, buf.Bytes(), nil
|
|
}
|
|
|
|
// UploadCourseLibraryMaterial 上传课件原始文件(fileSvc 落 file_key,不入知识库
|
|
// 解析链路;名称登记与文件可并存)。
|
|
func (s *trainingService) UploadCourseLibraryMaterial(ctx context.Context, tenantID uint64, id string, fh *multipart.FileHeader, updaterID string) (*types.TrainingCourseLibrary, error) {
|
|
if fh == nil {
|
|
return nil, werrors.NewBadRequestError("缺少上传文件")
|
|
}
|
|
item, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id)
|
|
if err != nil || item == nil {
|
|
return nil, werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
fileKey, err := s.fileSvc.SaveFile(ctx, fh, tenantID, "")
|
|
if err != nil {
|
|
logger.Errorf(ctx, "Failed to save course library material, error: %v", err)
|
|
return nil, err
|
|
}
|
|
item.FileKey = fileKey
|
|
item.FileName = fh.Filename
|
|
item.FileSize = fh.Size
|
|
// 文档类课件同步知识库「企业培训(课件)」(000153:AI 出题与对话问答依赖
|
|
// 知识库分块;视频类仍只存原始文件)。同步失败 fail-open,退回原始文件存取。
|
|
if !isVideoMaterialFile("", fh.Filename) {
|
|
coursewareKB, _, kbErr := s.ensureTrainingKnowledgeBases(ctx)
|
|
if kbErr != nil {
|
|
logger.Warnf(ctx, "Skip syncing course library material to knowledge base (ensure failed): %v", kbErr)
|
|
} else if knowledgeID, syncErr := s.syncFileToKnowledgeBase(ctx, coursewareKB.ID, fh, ""); syncErr != nil {
|
|
logger.Warnf(ctx, "Skip syncing course library material to knowledge base (upload failed): %v", syncErr)
|
|
} else {
|
|
item.KnowledgeID = knowledgeID
|
|
}
|
|
} else {
|
|
item.KnowledgeID = ""
|
|
}
|
|
// 名称登记为空时以文件名兜底,便于列表直接识别课件。
|
|
if strings.TrimSpace(item.MaterialName) == "" {
|
|
item.MaterialName = fh.Filename
|
|
}
|
|
if err := s.repo.UpdateCourseLibraryItem(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
logger.Infof(ctx, "[Training] course library material uploaded, tenant=%d course=%s size=%d uploader=%s", tenantID, id, fh.Size, updaterID)
|
|
return item, nil
|
|
}
|
|
|
|
// GetCourseLibraryMaterialFile 流式取回课件原始文件:文档类优先按知识库文档
|
|
// 取回流(000153 同步入库后与月计划课件同口径),视频/同步失败回退 file_key。
|
|
func (s *trainingService) GetCourseLibraryMaterialFile(ctx context.Context, tenantID uint64, id string) (io.ReadCloser, string, error) {
|
|
item, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id)
|
|
if err != nil || item == nil {
|
|
return nil, "", werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
if item.FileKey == "" && item.KnowledgeID == "" {
|
|
return nil, "", werrors.NewNotFoundError("该课程未上传课件文件")
|
|
}
|
|
if item.KnowledgeID != "" {
|
|
rc, _, err := s.knowledgeSvc.GetKnowledgeFile(ctx, item.KnowledgeID)
|
|
if err == nil {
|
|
// 下载文件名沿用登记名(知识库文档名可能与登记名不一致)。
|
|
return rc, item.FileName, nil
|
|
}
|
|
logger.Warnf(ctx, "Fallback to raw file for course library material, course: %s, error: %v", id, err)
|
|
}
|
|
if item.FileKey == "" {
|
|
return nil, "", werrors.NewNotFoundError("该课程未上传课件文件")
|
|
}
|
|
rc, err := s.fileSvc.GetFile(ctx, item.FileKey)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
return rc, item.FileName, nil
|
|
}
|
|
|
|
// AttachCourseLibraryMaterialFromKnowledge 从知识库「企业培训(课件)」选择已有
|
|
// 文档作为课程课件(与上传同步同口径:下载/月计划课件挂接/AI 出题共用 knowledge_id)。
|
|
// 原始文件字段清空,名称登记为空时以文档名兜底。
|
|
func (s *trainingService) AttachCourseLibraryMaterialFromKnowledge(ctx context.Context, tenantID uint64, id, knowledgeID, updaterID string) (*types.TrainingCourseLibrary, error) {
|
|
knowledgeID = strings.TrimSpace(knowledgeID)
|
|
if knowledgeID == "" {
|
|
return nil, werrors.NewBadRequestError("缺少知识库文档")
|
|
}
|
|
item, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id)
|
|
if err != nil || item == nil {
|
|
return nil, werrors.NewNotFoundError("课程不存在")
|
|
}
|
|
doc, docErr := s.knowledgeSvc.GetKnowledgeByID(ctx, knowledgeID)
|
|
if docErr != nil || doc == nil {
|
|
return nil, werrors.NewNotFoundError("知识库文档不存在")
|
|
}
|
|
name := strings.TrimSpace(doc.FileName)
|
|
if name == "" {
|
|
name = strings.TrimSpace(doc.Title)
|
|
}
|
|
if name != "" {
|
|
item.FileName = name
|
|
if strings.TrimSpace(item.MaterialName) == "" {
|
|
item.MaterialName = name
|
|
}
|
|
}
|
|
item.FileKey, item.FileSize = "", 0
|
|
item.KnowledgeID = knowledgeID
|
|
if err := s.repo.UpdateCourseLibraryItem(ctx, item); err != nil {
|
|
return nil, err
|
|
}
|
|
logger.Infof(ctx, "[Training] course library material attached from knowledge base, tenant=%d course=%s knowledge=%s operator=%s", tenantID, id, knowledgeID, updaterID)
|
|
return item, nil
|
|
}
|