From 11450bdb96b68f36fb10ac561044d4e53045c07e Mon Sep 17 00:00:00 2001 From: "DESKTOP-JKIE562\\PC" <1013397054@qq.com> Date: Tue, 24 Nov 2026 21:22:27 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=88=91=E7=9A=84=E5=9F=B9=E8=AE=AD)?= =?UTF-8?q?=EF=BC=9A=E3=80=8C=E6=88=91=E7=9A=84=E5=9F=B9=E8=AE=AD=E3=80=8D?= =?UTF-8?q?=E7=9A=84=E2=80=9C=E4=B8=8E=E6=88=91=E7=9B=B8=E5=85=B3=E2=80=9D?= =?UTF-8?q?=E5=88=A4=E5=AE=9A=EF=BC=88=E5=AD=A6=E4=B9=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?/=E8=AE=B2=E5=B8=88/=E9=83=A8=E9=97=A8=E8=8C=83=E5=9B=B4?= =?UTF-8?q?=EF=BC=89=E4=B8=8D=E5=90=AB=E3=80=8C=E5=8F=82=E5=8A=A0=E4=BA=BA?= =?UTF-8?q?=E5=91=98=E3=80=8D=E2=80=94=E2=80=94=E7=BA=BF=E4=B8=8B=E8=AF=BE?= =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=88=90=E5=AD=A6=E4=B9=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=EF=BC=8C=E5=8F=82=E5=8A=A0=E4=BA=BA=E5=91=98=E5=8F=AA=E8=90=BD?= =?UTF-8?q?=E5=9C=BA=E6=AC=A1=E5=90=8D=E5=8D=95=E4=B8=8E=E6=96=87=E6=A1=88?= =?UTF-8?q?=EF=BC=8C=E5=88=98=E4=B9=89=E4=BA=AE=EF=BC=88=E8=B5=84=E8=AE=AF?= =?UTF-8?q?=E7=BB=84=EF=BC=89=E8=A2=AB=E8=AF=BE=E7=A8=8B=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E8=8C=83=E5=9B=B4=EF=BC=88=E4=BA=BA=E4=BA=8B=E8=AF=BE=EF=BC=89?= =?UTF-8?q?=E6=8B=A6=E5=9C=A8=E9=A1=B5=E7=AD=BE=E5=A4=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/training/index.ts | 4 + .../training/components/CourseCatalog.vue | 13 ++- .../repository/training_plan_course.go | 13 +++ internal/application/service/training_exam.go | 12 ++ .../service/training_mine_enrolled_test.go | 107 ++++++++++++++++++ internal/handler/training/training_misc.go | 12 ++ internal/router/routes_training.go | 2 + internal/types/interfaces/training.go | 6 + 8 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 internal/application/service/training_mine_enrolled_test.go diff --git a/frontend/src/api/training/index.ts b/frontend/src/api/training/index.ts index 19e5d83..1e5b874 100644 --- a/frontend/src/api/training/index.ts +++ b/frontend/src/api/training/index.ts @@ -970,6 +970,10 @@ export const saveLearningProgress = (taskId: string, materialId: string, positio export const listMyLearningTasks = () => get<{ success: boolean; data: LearningTask[] }>(`${base}/learning-tasks`) +/** 我经场次学员名单报名的课程 id 去重集合(我的培训「我参加」判定) */ +export const listMyEnrolledCourseIds = () => + get<{ success: boolean; data: string[] }>(`${base}/my/enrolled-course-ids`) + /** 管理端课程学习情况行(全员学习任务 + 员工展示字段) */ export interface CourseLearningTask extends LearningTask { employee_no?: string diff --git a/frontend/src/views/training/components/CourseCatalog.vue b/frontend/src/views/training/components/CourseCatalog.vue index 00955f4..3d06962 100644 --- a/frontend/src/views/training/components/CourseCatalog.vue +++ b/frontend/src/views/training/components/CourseCatalog.vue @@ -152,6 +152,7 @@ import { getMyProfile, listCourses, listCourseLibrary, + listMyEnrolledCourseIds, listMyExamRecords, listMyLearningTasks, listSessions, @@ -241,6 +242,8 @@ const lecturerCourses = ref([]) const myProfile = ref(null) const myTasks = ref([]) const sessions = ref([]) +// 我经场次学员名单报名的课程 id 集合(「我参加」判定;失败回退空集不阻断) +const myEnrolledCourseIds = ref>(new Set()) // 课程管理资产(000154):「和信课程」目录 = 课程管理已发布且部门范围覆盖查看人的课程 const libraryItems = ref([]) // 我的考试记录(000154):「我的考试」页签数据源 @@ -311,13 +314,16 @@ const myTaskByCourse = computed(() => { // 「我的课程」相关性口径(与后端推送口径一致): // ① 我有该课学习任务(线上必修按部门推送);② 我是该课讲师; -// ③ 课程部门范围覆盖我(dept_ids 为空 = 全员可见,非空需包含我的部门)。 +// ③ 课程部门范围覆盖我(dept_ids 为空 = 全员可见,非空需包含我的部门); +// ④ 我是该课场次参加人员(学员名单接口,线下课参加人员由此可见——此前参加 +// 人员只落名单与文案,不进任何"与我相关"判定,被设为参加人员却看不到课)。 // 讲师匹配用平台用户 id(user_id)——与后端 course.LecturerID == callerUserID 同口径。 const myUserId = computed(() => myProfile.value?.user_id || '') const isMyTeaching = (c: TrainingCourse) => !!myUserId.value && c.lecturer_id === myUserId.value const isMyCourse = (c: TrainingCourse) => { if (myTaskByCourse.value.has(c.id)) return true if (isMyTeaching(c)) return true + if (myEnrolledCourseIds.value.has(c.id)) return true const scope = c.dept_ids || [] if (!scope.length) return true return !!myProfile.value && scope.includes(myProfile.value.dept_id) @@ -546,7 +552,7 @@ const loadAll = async () => { // 讲师/管理角色额外拉维护视角课程(纯讲师=自己名下含草稿;管理=全部,下方按讲师 id 过滤), // 供「我的培训」展示我主讲(含未发布)课程与「待维护」统计;纯学员无此视角,跳过。 const needMaintainView = props.admin || props.roles.some((r) => ['hr', 'supervisor', 'exec', 'lecturer'].includes(r)) - const [c, t, s, me, lc, lib, exams] = await Promise.all([ + const [c, t, s, me, lc, lib, exams, enrolled] = await Promise.all([ // 学员目录视图:scope=student 统一走已发布课程口径(讲师账号的学习视角不受课程维护范围影响) listCourses({ page_size: 100, scope: 'student' }), listMyLearningTasks().catch(() => null), @@ -558,6 +564,8 @@ const loadAll = async () => { listCourseLibrary({ page_size: 500, scope: 'catalog' }).catch(() => null), // 我的考试记录(000154) listMyExamRecords().catch(() => null), + // 我经场次学员名单报名的课程 id(「我参加」判定;旧后端无此接口时回退空集) + listMyEnrolledCourseIds().catch(() => null), ]) courses.value = unwrap(c)?.items || [] myTasks.value = (t && unwrap(t)) || [] @@ -566,6 +574,7 @@ const loadAll = async () => { lecturerCourses.value = (lc && unwrap(lc)?.items) || [] libraryItems.value = (lib && unwrap(lib)?.items) || [] myExamRecords.value = (exams && unwrap(exams)) || [] + myEnrolledCourseIds.value = new Set((enrolled && unwrap(enrolled)) || []) } finally { loading.value = false } diff --git a/internal/application/repository/training_plan_course.go b/internal/application/repository/training_plan_course.go index 7fc0d6a..73b5b7a 100644 --- a/internal/application/repository/training_plan_course.go +++ b/internal/application/repository/training_plan_course.go @@ -540,6 +540,19 @@ func (r *trainingRepository) CountEnrolledMembersByCourseID(ctx context.Context, return int(cnt), err } +// ListMyEnrolledCourseIDs 当前成员经场次学员名单报名的课程 id 去重集合 +// (我的培训「我参加」判定数据源)。名单为软删(ReplaceEnrollments 移除即置 +// deleted_at),故须过滤 e.deleted_at;场次软删行同样不计。 +func (r *trainingRepository) ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, memberID string) ([]string, error) { + ids := []string{} + err := r.db.WithContext(ctx). + Table("tr_enrollments e"). + Joins("JOIN tr_course_sessions s ON s.id = e.session_id AND s.tenant_id = e.tenant_id AND s.deleted_at IS NULL"). + Where("e.tenant_id = ? AND e.member_id = ? AND e.deleted_at IS NULL", tenantID, memberID). + Distinct().Pluck("s.course_id", &ids).Error + return ids, err +} + // CourseStudyStats 按课程聚合签到/线上学习/考试参与成员(去重成员集合): // 签到走场次联查;学习任务直接带课程;考试支持场次卷与线上自学卷 // (session 为空时经 paper→bank→course 兜底)两条路径。 diff --git a/internal/application/service/training_exam.go b/internal/application/service/training_exam.go index 7d25f0e..dca3a1e 100644 --- a/internal/application/service/training_exam.go +++ b/internal/application/service/training_exam.go @@ -1135,6 +1135,18 @@ func (s *trainingService) ListMyLearningTasks(ctx context.Context, tenantID uint return s.repo.ListLearningTasks(ctx, tenantID, member.ID) } +// ListMyEnrolledCourseIDs 我经场次学员名单报名的课程 id 去重集合(我的培训 +// 「我参加」判定数据源):参加人员此前只落场次名单与 post_scope 文案,不进 +// 任何"与我相关"判定,线下课参加人员在「我的培训」看不到课。未建培训档案 +// 返回空集(与 ListMyLearningTasks 同口径,不报错)。 +func (s *trainingService) ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, userID string) ([]string, error) { + member, err := s.repo.GetMemberByUserID(ctx, tenantID, userID) + if err != nil { + return []string{}, nil + } + return s.repo.ListMyEnrolledCourseIDs(ctx, tenantID, member.ID) +} + // ListCourseLearningTasks returns all members' learning tasks of one course // (管理端「学习考试」课程学习情况;任务不存在但已发布课程的名单成员由前端按待学习渲染口径,此处按实际任务行返回)。 func (s *trainingService) ListCourseLearningTasks(ctx context.Context, tenantID uint64, courseID string) ([]*types.TrainingLearningTask, error) { diff --git a/internal/application/service/training_mine_enrolled_test.go b/internal/application/service/training_mine_enrolled_test.go new file mode 100644 index 0000000..8564ce8 --- /dev/null +++ b/internal/application/service/training_mine_enrolled_test.go @@ -0,0 +1,107 @@ +package service + +import ( + "context" + "path/filepath" + "testing" + + "academy/internal/application/repository" + "academy/internal/types" + "github.com/stretchr/testify/require" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +// newMineEnrolledFixture 构建 sqlite + 真实仓储的 trainingService,专测 +// ListMyEnrolledCourseIDs(我的培训「我参加」判定数据源)。 +func newMineEnrolledFixture(t *testing.T) (*trainingService, *gorm.DB, uint64) { + t.Helper() + db, err := gorm.Open( + sqlite.Open(filepath.Join(t.TempDir(), "training_mine_enrolled.db")+"?_busy_timeout=5000&_journal_mode=WAL"), + &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}, + ) + require.NoError(t, err) + raw, err := db.DB() + require.NoError(t, err) + t.Cleanup(func() { _ = raw.Close() }) + require.NoError(t, db.AutoMigrate( + &types.OrgEmployee{}, &types.TrainingCourseSession{}, &types.TrainingEnrollment{}, + )) + const tenantID = uint64(42) + svc := &trainingService{repo: repository.NewTrainingRepository(db), db: db} + return svc, db, tenantID +} + +// seedMember 建 org_employees 档案(同空间同用户唯一,复用场景自行多次挂名单)。 +func seedMember(t *testing.T, db *gorm.DB, tenantID uint64, userID string) (memberID string) { + t.Helper() + member := types.OrgEmployee{ID: trainingNewID(), TenantID: tenantID, UserID: userID, Status: "active"} + require.NoError(t, db.Create(&member).Error) + return member.ID +} + +// seedEnrollment 为指定成员落一条 场次+名单 链路,返回名单行(供软删用例复用)。 +func seedEnrollment(t *testing.T, db *gorm.DB, tenantID uint64, memberID, courseID string) *types.TrainingEnrollment { + t.Helper() + sess := types.TrainingCourseSession{ID: trainingNewID(), TenantID: tenantID, CourseID: courseID, Status: "scheduled"} + require.NoError(t, db.Create(&sess).Error) + row := &types.TrainingEnrollment{ID: trainingNewID(), TenantID: tenantID, SessionID: sess.ID, MemberID: memberID} + require.NoError(t, db.Create(row).Error) + return row +} + +// 我的培训「我参加」:名单命中返回课程 id,跨场次/跨课程去重正确。 +func TestListMyEnrolledCourseIDs_HitsAndDistinct(t *testing.T) { + svc, db, tenantID := newMineEnrolledFixture(t) + ctx := context.Background() + + // 课程A两条场次名单(去重为一个 id)+ 课程B一条名单 + memberID := seedMember(t, db, tenantID, "user-liu") + seedEnrollment(t, db, tenantID, memberID, "course-A") + seedEnrollment(t, db, tenantID, memberID, "course-A") + seedEnrollment(t, db, tenantID, memberID, "course-B") + + ids, err := svc.ListMyEnrolledCourseIDs(ctx, tenantID, "user-liu") + require.NoError(t, err) + require.ElementsMatch(t, []string{"course-A", "course-B"}, ids) +} + +// 排除口径:软删名单/软删场次/他人名单/其他空间名单均不进「我参加」。 +func TestListMyEnrolledCourseIDs_ExcludesDeletedAndForeign(t *testing.T) { + svc, db, tenantID := newMineEnrolledFixture(t) + ctx := context.Background() + + // 命中一条基线(course-keep) + memberID := seedMember(t, db, tenantID, "user-liu") + seedEnrollment(t, db, tenantID, memberID, "course-keep") + + // 软删名单(course-soft-del) + rowDel := seedEnrollment(t, db, tenantID, memberID, "course-soft-del") + require.NoError(t, db.Delete(rowDel).Error) + + // 场次软删(course-sess-del) + rowGone := seedEnrollment(t, db, tenantID, memberID, "course-sess-del") + require.NoError(t, db.Model(&types.TrainingCourseSession{}). + Where("id = ?", rowGone.SessionID).Delete(&types.TrainingCourseSession{}).Error) + + // 他人名单(course-other-member):同会话名单换人不影响本人判定 + otherID := seedMember(t, db, tenantID, "user-other") + seedEnrollment(t, db, tenantID, otherID, "course-other-member") + + // 其他空间(tenant 43)同名用户名单 + otherTenantID := seedMember(t, db, tenantID+1, "user-liu") + seedEnrollment(t, db, tenantID+1, otherTenantID, "course-other-tenant") + + ids, err := svc.ListMyEnrolledCourseIDs(ctx, tenantID, "user-liu") + require.NoError(t, err) + require.Equal(t, []string{"course-keep"}, ids) +} + +// 无培训档案用户返回空集不报错(与 ListMyLearningTasks 同口径)。 +func TestListMyEnrolledCourseIDs_NoMemberReturnsEmpty(t *testing.T) { + svc, _, tenantID := newMineEnrolledFixture(t) + ids, err := svc.ListMyEnrolledCourseIDs(context.Background(), tenantID, "user-without-profile") + require.NoError(t, err) + require.Empty(t, ids) +} diff --git a/internal/handler/training/training_misc.go b/internal/handler/training/training_misc.go index 2087ee5..37a6f44 100644 --- a/internal/handler/training/training_misc.go +++ b/internal/handler/training/training_misc.go @@ -367,6 +367,18 @@ func (h *Handler) ListMyLearningTasks(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": true, "data": items}) } +// ListMyEnrolledCourseIds GET /training/my/enrolled-course-ids — 我经场次学员 +// 名单报名的课程 id 去重集合(我的培训「我参加」判定数据源,前端与学习任务/ +// 讲师/部门范围口径互补;登录即可查,自身名单本身即个人数据)。 +func (h *Handler) ListMyEnrolledCourseIds(c *gin.Context) { + ids, err := h.svc.ListMyEnrolledCourseIDs(c.Request.Context(), callerTenantID(c), callerUserID(c)) + if err != nil { + _ = c.Error(err) + return + } + c.JSON(http.StatusOK, gin.H{"success": true, "data": ids}) +} + // ListCourseLearningTasks GET /training/learning-tasks/all?course_id= — // 按课程返回全员学习任务(人事/课长/讲师的「学习考试」课程学习情况视图)。 func (h *Handler) ListCourseLearningTasks(c *gin.Context) { diff --git a/internal/router/routes_training.go b/internal/router/routes_training.go index 8c76168..30a2947 100644 --- a/internal/router/routes_training.go +++ b/internal/router/routes_training.go @@ -168,6 +168,8 @@ func RegisterTrainingRoutes(r *gin.RouterGroup, h *training.Handler, g *rbacGuar trainingGroup.GET("/learning-progress", h.ListMyCourseProgress) trainingGroup.GET("/learning-tasks", h.ListMyLearningTasks) trainingGroup.GET("/learning-tasks/all", h.ListCourseLearningTasks) + // my/enrolled-course-ids 我经场次学员名单报名的课程 id 集合(我的培训「我参加」判定)。 + trainingGroup.GET("/my/enrolled-course-ids", h.ListMyEnrolledCourseIds) trainingGroup.POST("/course-feedbacks", h.SaveFeedback) trainingGroup.POST("/course-feedbacks/:id/reply", g.Contributor(), h.ReplyFeedback) trainingGroup.GET("/course-feedbacks/pending", h.ListPendingFeedbacks) diff --git a/internal/types/interfaces/training.go b/internal/types/interfaces/training.go index 8abd421..02eb8bb 100644 --- a/internal/types/interfaces/training.go +++ b/internal/types/interfaces/training.go @@ -116,6 +116,9 @@ type TrainingRepository interface { CountEnrolledMembersByCourse(ctx context.Context, tenantID uint64) (map[string]int, error) // CountEnrolledMembersByCourseID 单课程场次学员名单人数(发布必填校验,000161)。 CountEnrolledMembersByCourseID(ctx context.Context, tenantID uint64, courseID string) (int, error) + // ListMyEnrolledCourseIDs 当前成员经场次学员名单报名的课程 id 去重集合 + // (我的培训「我参加」判定数据源;软删名单/软删场次不计)。 + ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, memberID string) ([]string, error) // CourseStudyStats 按课程聚合签到/线上学习/考试参与成员(课程管理列表学习/考试状态列)。 CourseStudyStats(ctx context.Context, tenantID uint64) (map[string]*types.TrainingCourseStudyStats, error) // CourseReporterDeptNames 各课程上报部门名(课程创建人所属部门,联查 org_employees/org_depts)。 @@ -444,6 +447,9 @@ type TrainingService interface { // ListMyCourseProgress 当前用户在某课程下各课件的学习进度(学习详情页回读)。 ListMyCourseProgress(ctx context.Context, tenantID uint64, userID, courseID string) ([]*types.TrainingLearningProgress, error) ListMyLearningTasks(ctx context.Context, tenantID uint64, userID string) ([]*types.TrainingLearningTask, error) + // ListMyEnrolledCourseIDs 我经场次学员名单报名的课程 id 去重集合(我的培训 + // 「我参加」判定,与学习任务/讲师/部门范围口径互补;线下课参加人员由此可见)。 + ListMyEnrolledCourseIDs(ctx context.Context, tenantID uint64, userID string) ([]string, error) // ListCourseLearningTasks 按课程返回全员学习任务(人事/讲师课程学习情况)。 ListCourseLearningTasks(ctx context.Context, tenantID uint64, courseID string) ([]*types.TrainingLearningTask, error) SaveFeedback(ctx context.Context, tenantID uint64, userID, sessionID string, rating int, comment string) (*types.TrainingCourseFeedback, error)