chore: 初始化仓库,导入项目基线代码
Academy v0.8.3 完整代码基线:Go 后端(internal/cmd/cli/mcp-server/docreader)、 Vue 前端(frontend)、数据库迁移(migrations)、部署编排(docker/helm/deploy)与文档。 今日开发的培训推送管理改动随后单独提交。
This commit is contained in:
commit
7ec21f35b0
5767 files changed
+1436788
No files matched your search
@@ -0,0 +1,237 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
werrors "academy/internal/errors"
|
||||
"academy/internal/logger"
|
||||
"academy/internal/types"
|
||||
secutils "academy/internal/utils"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 企业培训 ↔ 知识库打通:
|
||||
// - 系统默认知识库(企业培训课件/题库)按租户惰性创建(ensure 幂等);
|
||||
// - 课件上传 / 题库 Excel 导入自动同步进对应知识库,走既有文档解析入库
|
||||
// 链路(切片/向量化),从而支持 AI 对话问答;
|
||||
// - tr_materials / tr_question_banks 回填 knowledge_id,支持建课反选。
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// 系统默认知识库命名与描述(与知识库列表页卡片展示一致)。
|
||||
const (
|
||||
trainingCoursewareKBName = "企业培训(课件)"
|
||||
trainingQuestionBankKBName = "企业培训(题库)"
|
||||
|
||||
trainingCoursewareKBDescription = "企业培训课件库:自动保存课程管理上传与登记的课件,支持对话问答。"
|
||||
trainingQuestionBankKBDescription = "企业培训题库:自动保存题库导入的题库文件,支持对话问答。"
|
||||
|
||||
// trainingKBChannel 知识入库渠道标记(文档来源审计)。
|
||||
trainingKBChannel = "training"
|
||||
)
|
||||
|
||||
// ListTrainingKnowledgeBases returns (lazily creating if missing) the two
|
||||
// system-default enterprise-training knowledge bases.
|
||||
func (s *trainingService) ListTrainingKnowledgeBases(ctx context.Context) (*types.TrainingKnowledgeBases, error) {
|
||||
courseware, questionBank, err := s.ensureTrainingKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &types.TrainingKnowledgeBases{
|
||||
Courseware: trainingKBSummary(courseware),
|
||||
QuestionBank: trainingKBSummary(questionBank),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ensureTrainingKnowledgeBases finds the two training KBs by their source
|
||||
// marker within the caller's tenant and creates any that is missing. Safe to
|
||||
// call on every request: existing KBs short-circuit to a single list query.
|
||||
func (s *trainingService) ensureTrainingKnowledgeBases(ctx context.Context) (*types.KnowledgeBase, *types.KnowledgeBase, error) {
|
||||
kbs, err := s.kbService.ListKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var courseware, questionBank *types.KnowledgeBase
|
||||
for _, kb := range kbs {
|
||||
switch kb.Source {
|
||||
case types.KnowledgeBaseSourceTrainingCourseware:
|
||||
if courseware == nil {
|
||||
courseware = kb
|
||||
}
|
||||
case types.KnowledgeBaseSourceTrainingQuestionBank:
|
||||
if questionBank == nil {
|
||||
questionBank = kb
|
||||
}
|
||||
}
|
||||
}
|
||||
if courseware == nil {
|
||||
if courseware, err = s.createTrainingKnowledgeBase(ctx,
|
||||
types.KnowledgeBaseSourceTrainingCourseware,
|
||||
trainingCoursewareKBName, trainingCoursewareKBDescription); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
if questionBank == nil {
|
||||
if questionBank, err = s.createTrainingKnowledgeBase(ctx,
|
||||
types.KnowledgeBaseSourceTrainingQuestionBank,
|
||||
trainingQuestionBankKBName, trainingQuestionBankKBDescription); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
return courseware, questionBank, nil
|
||||
}
|
||||
|
||||
// createTrainingKnowledgeBase provisions one system-default training KB.
|
||||
// Chunking/embedding/vector-store configuration follows the tenant defaults
|
||||
// via EnsureDefaults — identical to creating the KB from the UI.
|
||||
func (s *trainingService) createTrainingKnowledgeBase(ctx context.Context, source, name, description string) (*types.KnowledgeBase, error) {
|
||||
kb, err := s.kbService.CreateKnowledgeBase(ctx, &types.KnowledgeBase{
|
||||
Name: name,
|
||||
Description: description,
|
||||
Source: source,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to ensure training knowledge base, source: %s, error: %v", source, err)
|
||||
return nil, err
|
||||
}
|
||||
logger.Infof(ctx, "Training knowledge base ensured, id: %s, source: %s", kb.ID, source)
|
||||
return kb, nil
|
||||
}
|
||||
|
||||
// videoMaterialExts 视频类课件扩展名:视频不入知识库解析链路(解析白名单
|
||||
// 不含视频),只存原始文件供学习详情页播放。
|
||||
var videoMaterialExts = map[string]bool{
|
||||
".mp4": true, ".webm": true, ".mov": true, ".m4v": true, ".mkv": true, ".avi": true,
|
||||
}
|
||||
|
||||
// isVideoMaterialFile 按登记类型或扩展名判断是否视频课件。
|
||||
func isVideoMaterialFile(kind, filename string) bool {
|
||||
if strings.EqualFold(strings.TrimSpace(kind), "video") {
|
||||
return true
|
||||
}
|
||||
return videoMaterialExts[strings.ToLower(path.Ext(filename))]
|
||||
}
|
||||
|
||||
// UploadMaterial 课件文件上传:文档类自动同步进「企业培训(课件)」知识库,
|
||||
// 材料记录回填 knowledge_id;视频类只存原始文件(回填 file_key),由
|
||||
// /training/materials/:id/file 流式取回。重复上传同一文件时幂等关联已存在文档。
|
||||
func (s *trainingService) UploadMaterial(ctx context.Context, tenantID uint64, fh *multipart.FileHeader, courseID, kind, name, updaterID string) (*types.TrainingMaterial, error) {
|
||||
if fh == nil {
|
||||
return nil, werrors.NewBadRequestError("缺少上传文件")
|
||||
}
|
||||
if strings.TrimSpace(kind) == "" {
|
||||
kind = "doc"
|
||||
}
|
||||
if strings.TrimSpace(name) == "" {
|
||||
name = fh.Filename
|
||||
}
|
||||
if isVideoMaterialFile(kind, fh.Filename) {
|
||||
fileKey, err := s.fileSvc.SaveFile(ctx, fh, tenantID, "")
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to save training video material, error: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return s.CreateMaterial(ctx, tenantID, &types.TrainingMaterial{
|
||||
CourseID: courseID,
|
||||
Kind: "video",
|
||||
Name: name,
|
||||
FileKey: fileKey,
|
||||
Size: fh.Size,
|
||||
}, updaterID)
|
||||
}
|
||||
coursewareKB, _, err := s.ensureTrainingKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
knowledgeID, err := s.syncFileToKnowledgeBase(ctx, coursewareKB.ID, fh, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.CreateMaterial(ctx, tenantID, &types.TrainingMaterial{
|
||||
CourseID: courseID,
|
||||
Kind: kind,
|
||||
Name: name,
|
||||
KnowledgeID: knowledgeID,
|
||||
Size: fh.Size,
|
||||
}, updaterID)
|
||||
}
|
||||
|
||||
// syncFileToKnowledgeBase uploads one file into the given KB through the
|
||||
// standard CreateKnowledgeFromFile pipeline (save → async parse/index).
|
||||
// Duplicate uploads resolve to the existing knowledge so re-imports stay
|
||||
// idempotent. When displayName is non-empty and passes input validation it
|
||||
// replaces the original file name (e.g. question banks renamed to the bank
|
||||
// name typed in the import dialog).
|
||||
func (s *trainingService) syncFileToKnowledgeBase(ctx context.Context, kbID string, fh *multipart.FileHeader, displayName string) (string, error) {
|
||||
customName := displayName
|
||||
if customName != "" {
|
||||
customName += path.Ext(fh.Filename)
|
||||
if _, ok := secutils.ValidateInput(customName); !ok {
|
||||
customName = ""
|
||||
}
|
||||
}
|
||||
knowledge, err := s.knowledgeSvc.CreateKnowledgeFromFile(ctx, kbID, fh, nil, nil, customName, nil, trainingKBChannel, nil)
|
||||
if err != nil {
|
||||
// Same-bytes re-upload: link the already-existing document instead of failing.
|
||||
var dup *types.DuplicateKnowledgeError
|
||||
if errors.As(err, &dup) && dup.Knowledge != nil {
|
||||
return dup.Knowledge.ID, nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if knowledge == nil {
|
||||
return "", nil
|
||||
}
|
||||
return knowledge.ID, nil
|
||||
}
|
||||
|
||||
// syncExcelToQuestionBankKB 把题库 Excel 同步进「企业培训(题库)」知识库并返回
|
||||
// knowledge_id。fail-open:同步失败仅记日志返回空串,不阻断题库导入主流程。
|
||||
func (s *trainingService) syncExcelToQuestionBankKB(ctx context.Context, fh *multipart.FileHeader, bankName string) string {
|
||||
if fh == nil {
|
||||
return ""
|
||||
}
|
||||
_, bankKB, err := s.ensureTrainingKnowledgeBases(ctx)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Skip syncing question bank to knowledge base (ensure failed): %v", err)
|
||||
return ""
|
||||
}
|
||||
knowledgeID, err := s.syncFileToKnowledgeBase(ctx, bankKB.ID, fh, strings.TrimSpace(bankName))
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "Skip syncing question bank to knowledge base (upload failed): %v", err)
|
||||
return ""
|
||||
}
|
||||
return knowledgeID
|
||||
}
|
||||
|
||||
// trainingKBSummary projects a KB row into the training-facing summary DTO.
|
||||
func trainingKBSummary(kb *types.KnowledgeBase) *types.TrainingKnowledgeBaseSummary {
|
||||
if kb == nil {
|
||||
return nil
|
||||
}
|
||||
return &types.TrainingKnowledgeBaseSummary{ID: kb.ID, Name: kb.Name, Source: kb.Source}
|
||||
}
|
||||
|
||||
// GetMaterialFile streams one material's file for the learning detail page.
|
||||
// 文档类经 knowledge_id 走知识库取回流;视频类按 file_key 直接取原始文件。
|
||||
func (s *trainingService) GetMaterialFile(ctx context.Context, tenantID uint64, materialID string) (io.ReadCloser, string, error) {
|
||||
material, err := s.repo.GetMaterial(ctx, tenantID, materialID)
|
||||
if err != nil {
|
||||
return nil, "", werrors.NewNotFoundError("课件不存在")
|
||||
}
|
||||
if material.KnowledgeID != "" {
|
||||
return s.knowledgeSvc.GetKnowledgeFile(ctx, material.KnowledgeID)
|
||||
}
|
||||
if material.FileKey != "" {
|
||||
rc, err := s.fileSvc.GetFile(ctx, material.FileKey)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
return rc, material.Name, nil
|
||||
}
|
||||
return nil, "", werrors.NewNotFoundError("该课件未上传文件")
|
||||
}
|
||||
Reference in new issue
Block a user