feat(培训推送):“资讯组 新导入7条年度培训需求(课程:A(培训课程测试1、AI培训课程测试2、AI培训课程测试3、AI培训课程测试4、AI培训课程测试5、AI培训课程测试6、AI培训课程测试7)。请登录和信智汇学院 → 企业培训 一 年培训计划,逐条核验并【确认】,确认后将自动转入月培训计划。”
deploy-hxtesting / build-and-deploy (push) Successful in 1m20s

This commit is contained in:
runny committed 2026-11-30 17:04:06 +08:00
1 parent 858c352991
commit 0a4471f5e4
7 files changed
+286 -11

No files matched your search

@@ -0,0 +1,75 @@
package service
import (
"context"
"encoding/json"
"path/filepath"
"strings"
"testing"
"academy/internal/application/repository"
"academy/internal/types"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// newDemandNoticeFixture 构建 sqlite + 真实仓储的 trainingService(含规则配置表),
// 专测需求域推送文案(demand_verify/demand_confirmed)的模板配置与回落。
func newDemandNoticeFixture(t *testing.T) (*trainingService, *gorm.DB, uint64) {
t.Helper()
db, err := gorm.Open(
sqlite.Open(filepath.Join(t.TempDir(), "training_demand_notice.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.TrainingRuleConfig{}))
const tenantID = uint64(42)
svc := &trainingService{repo: repository.NewTrainingRepository(db), db: db}
return svc, db, tenantID
}
// TestDemandNoticeTextTemplates 需求核验/确认提醒文案走配置中心模板
// (notify_text_templates demand_verify_*/demand_confirmed_*,2026-11 需求:
// 原硬编码文案全部入配置中心可编辑)。
func TestDemandNoticeTextTemplates(t *testing.T) {
svc, db, tenantID := newDemandNoticeFixture(t)
ctx := context.Background()
// 场景1:未配置规则 → 回落内置默认(与历史硬编码逐字一致)
title, content := svc.DemandVerifyNoticeText(ctx, tenantID, 2027, "资讯组",
[]string{"A(培训课程测试1", "AI培训课程测试2"})
require.Equal(t, "【年度培训需求核验】2027年度 资讯组 培训需求待核验", title)
require.Equal(t,
"资讯组 新导入 2 条年度培训需求(课程:A(培训课程测试1、AI培训课程测试2)。请登录和信智汇学院 → 企业培训 → 年培训计划,逐条核验并【确认】,确认后将自动转入月培训计划。",
content)
cTitle, cContent := svc.DemandConfirmedNoticeText(ctx, tenantID, 2027, "内部讲师管理办法")
require.Equal(t, "【培训计划维护】内部讲师管理办法 已确认转入月培训计划", cTitle)
require.Contains(t, cContent, "2027年度需求「内部讲师管理办法」已核验确认并自动转入月培训计划")
// 场景2:配置中心自定义模板 → 占位符按配置渲染(空字段回落默认)
raw, err := json.Marshal(map[string]string{
"demand_verify_title": "自定义核验标题 {dept}",
"demand_confirmed_title": "",
"demand_confirmed_content": "请维护 {course}",
})
require.NoError(t, err)
require.NoError(t, db.Create(&types.TrainingRuleConfig{
ID: "rule-1", TenantID: tenantID, RuleKey: ruleKeyNotifyTextTemplates, RuleValue: raw,
}).Error)
title, vContent := svc.DemandVerifyNoticeText(ctx, tenantID, 2027, "资讯组", []string{"课程A"})
require.Equal(t, "自定义核验标题 资讯组", title)
// 未配置的 demand_verify_content 回落默认,占位符照常渲染
require.True(t, strings.HasPrefix(vContent, "资讯组 新导入 1 条年度培训需求"))
cTitle, cContent = svc.DemandConfirmedNoticeText(ctx, tenantID, 2027, "内部讲师管理办法")
// demand_confirmed_title 未配置 → 回落默认;content 已配置 → 自定义
require.Equal(t, "【培训计划维护】内部讲师管理办法 已确认转入月培训计划", cTitle)
require.Equal(t, "请维护 内部讲师管理办法", cContent)
}
@@ -122,6 +122,14 @@ type notifyTextTemplates struct {
// 年培训计划收集推送(scene=demand_collect):广播空标题/内容回落渲染
DemandCollectTitle string `json:"demand_collect_title"`
DemandCollectContent string `json:"demand_collect_content"`
// 需求导入核验提醒(scene=demand_verify):按部门聚合通知部门经理核验【确认】。
// 占位符:{year} 年度 / {dept} 部门名 / {count} 导入条数 / {courses} 课程名(、连接)
DemandVerifyTitle string `json:"demand_verify_title"`
DemandVerifyContent string `json:"demand_verify_content"`
// 需求确认转月计划提醒(scene=demand_confirmed):通知讲师与部门课长维护
// 授课安排。占位符:{year} 年度 / {course} 课程名
DemandConfirmedTitle string `json:"demand_confirmed_title"`
DemandConfirmedContent string `json:"demand_confirmed_content"`
}
// 内置默认文案(与历史硬编码逐字一致;{lecturer} 缺讲师时渲染为「待定」)。
@@ -137,6 +145,12 @@ const (
// 年培训计划收集推送默认文案(与前端收集弹窗历史硬编码逐字一致;{year} 渲染收集年度)
defaultDemandCollectTitle = "【培训需求收集】{year}年度培训计划收集"
defaultDemandCollectContent = "请各部门于 {year} 年 1 月 31 日前提报 {year} 年度培训需求:可点击消息中的「教育训练需求表模板」链接直接下载批量填写后导入,或登录和信智汇学院 → 企业培训 → 培训计划 → 年度需求收集在线逐条提报。"
// 需求导入核验提醒默认文案(与历史硬编码逐字一致;{dept}/{count}/{courses}/{year})
defaultDemandVerifyTitle = "【年度培训需求核验】{year}年度 {dept} 培训需求待核验"
defaultDemandVerifyContent = "{dept} 新导入 {count} 条年度培训需求(课程:{courses})。请登录和信智汇学院 → 企业培训 → 年培训计划,逐条核验并【确认】,确认后将自动转入月培训计划。"
// 需求确认转月计划提醒默认文案(与历史硬编码逐字一致;{year}/{course})
defaultDemandConfirmedTitle = "【培训计划维护】{course} 已确认转入月培训计划"
defaultDemandConfirmedContent = "{year}年度需求「{course}」已核验确认并自动转入月培训计划:请讲师维护课程信息(课件/试题),课长安排授课时间/地点与学员名单。入口:和信智汇学院 → 企业培训 → 月培训计划。"
)
func defaultNotifyTextTemplates() notifyTextTemplates {
@@ -151,6 +165,10 @@ func defaultNotifyTextTemplates() notifyTextTemplates {
WeeklyContent: defaultWeeklyContent,
DemandCollectTitle: defaultDemandCollectTitle,
DemandCollectContent: defaultDemandCollectContent,
DemandVerifyTitle: defaultDemandVerifyTitle,
DemandVerifyContent: defaultDemandVerifyContent,
DemandConfirmedTitle: defaultDemandConfirmedTitle,
DemandConfirmedContent: defaultDemandConfirmedContent,
}
}
@@ -177,6 +195,10 @@ func parseNotifyTextTemplates(raw []byte) notifyTextTemplates {
fill(&t.WeeklyContent, saved.WeeklyContent)
fill(&t.DemandCollectTitle, saved.DemandCollectTitle)
fill(&t.DemandCollectContent, saved.DemandCollectContent)
fill(&t.DemandVerifyTitle, saved.DemandVerifyTitle)
fill(&t.DemandVerifyContent, saved.DemandVerifyContent)
fill(&t.DemandConfirmedTitle, saved.DemandConfirmedTitle)
fill(&t.DemandConfirmedContent, saved.DemandConfirmedContent)
return t
}
@@ -206,6 +228,28 @@ func renderNotifyTemplate(tpl string, vars map[string]string) string {
return strings.NewReplacer(pairs...).Replace(tpl)
}
// DemandVerifyNoticeText 需求核验提醒文案(批量导入与在线提报创建共用,2026-11
// 需求:模板入配置中心「提醒推送文案」demand_verify_*,空字段回落内置默认)。
// 占位符:{year} 年度 / {dept} 部门名 / {count} 条数 / {courses} 课程名(、连接)。
func (s *trainingService) DemandVerifyNoticeText(ctx context.Context, tenantID uint64, year int, dept string, courses []string) (string, string) {
tpl := s.loadNotifyTextTemplates(ctx, tenantID)
vars := map[string]string{
"year": strconv.Itoa(year),
"dept": dept,
"count": strconv.Itoa(len(courses)),
"courses": strings.Join(courses, "、"),
}
return renderNotifyTemplate(tpl.DemandVerifyTitle, vars), renderNotifyTemplate(tpl.DemandVerifyContent, vars)
}
// DemandConfirmedNoticeText 需求确认转月计划维护提醒文案(demand_confirmed_* 可编辑,
// 空字段回落内置默认)。占位符:{year} 年度 / {course} 课程名。
func (s *trainingService) DemandConfirmedNoticeText(ctx context.Context, tenantID uint64, year int, course string) (string, string) {
tpl := s.loadNotifyTextTemplates(ctx, tenantID)
vars := map[string]string{"year": strconv.Itoa(year), "course": course}
return renderNotifyTemplate(tpl.DemandConfirmedTitle, vars), renderNotifyTemplate(tpl.DemandConfirmedContent, vars)
}
// ---------------------------------------------------------------------------
// 学员开课通知(immediate 立即推 / day_before 调度器推,共用 scene 幂等)
// ---------------------------------------------------------------------------
@@ -252,6 +296,10 @@ func (s *trainingService) sessionNoticeText(ctx context.Context, tenantID uint64
lecturerName := "待定"
// 讲师解析兜底(000165 复测):场次未指定讲师时回退课程管理主数据的讲师,
// 避免课程已设讲师但通知仍显示「待定」(场次先建、讲师后补的场景)。
// [2026-11 根因修复] 名册讲师解析不到姓名时再回退课程的外部讲师姓名 ——
// 发布校验允许「仅外部讲师」(lecturer_id 空、lecturer_external 有值)的线下课
// 发布,此前通知不认 external,这类课程推给学员的开课通知讲师恒显示「待定」;
// 讲师 id 在名册查无此人(员工已删等)时同样落此兜底。
lecturerID := session.LecturerID
if strings.TrimSpace(lecturerID) == "" {
if course, err := s.repo.GetCourse(ctx, tenantID, session.CourseID); err == nil && course != nil {
@@ -260,6 +308,10 @@ func (s *trainingService) sessionNoticeText(ctx context.Context, tenantID uint64
}
if lecturer, err := s.repo.GetMember(ctx, tenantID, lecturerID); err == nil && lecturer != nil && lecturer.UserName != "" {
lecturerName = lecturer.UserName
} else if course, err := s.repo.GetCourse(ctx, tenantID, session.CourseID); err == nil && course != nil {
if external := strings.TrimSpace(course.LecturerExternal); external != "" {
lecturerName = external
}
}
tpl := s.loadNotifyTextTemplates(ctx, tenantID)
title := renderNotifyTemplate(tpl.StudentNoticeTitle, map[string]string{"course": courseName})
@@ -0,0 +1,95 @@
package service
import (
"context"
"path/filepath"
"strings"
"testing"
"time"
"academy/internal/application/repository"
"academy/internal/types"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// newSessionNoticeFixture 构建 sqlite + 真实仓储的 trainingService,
// 专测学员开课通知文案(sessionNoticeText)的讲师解析兜底。
func newSessionNoticeFixture(t *testing.T) (*trainingService, *gorm.DB, uint64) {
t.Helper()
db, err := gorm.Open(
sqlite.Open(filepath.Join(t.TempDir(), "training_session_notice.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.User{}, &types.OrgDept{}, &types.OrgEmployee{}, &types.TrainingCourse{}, &types.TrainingCourseSession{},
))
const tenantID = uint64(42)
svc := &trainingService{repo: repository.NewTrainingRepository(db), db: db}
return svc, db, tenantID
}
// TestSessionNoticeTextLecturerFallback 学员开课通知讲师解析链路:
// 场次讲师 → 课程名册讲师 → 课程外部讲师姓名(2026-11 根因修复:仅外部讲师的
// 课程发布放行,通知此前不认 lecturer_external,推给学员的讲师恒显示「待定」)。
func TestSessionNoticeTextLecturerFallback(t *testing.T) {
svc, db, tenantID := newSessionNoticeFixture(t)
ctx := context.Background()
startAt := time.Date(2027, 1, 1, 9, 0, 0, 0, time.Local)
newSession := func(t *testing.T, courseID string) *types.TrainingCourseSession {
t.Helper()
return &types.TrainingCourseSession{
ID: "sess-" + courseID,
TenantID: tenantID,
CourseID: courseID,
Location: "培训室",
StartAt: &startAt,
}
}
// 场景1:仅外部讲师(lecturer_id 空、lecturer_external 有值)→ 通知用外部姓名,不再「待定」
require.NoError(t, db.Create(&types.TrainingCourse{
ID: "c-external", TenantID: tenantID, Name: "内部讲师管理办法",
Mode: "offline", LecturerExternal: "王外讲",
}).Error)
_, content := svc.sessionNoticeText(ctx, tenantID, newSession(t, "c-external"))
require.Contains(t, content, "培训讲师:王外讲")
require.NotContains(t, content, "待定")
// 场景2:课程讲师在名册查无此人(员工已删)且留有外部讲师姓名 → 同样回退外部姓名
require.NoError(t, db.Create(&types.TrainingCourse{
ID: "c-missing", TenantID: tenantID, Name: "查无此讲师",
Mode: "offline", LecturerID: "gone-lecturer", LecturerExternal: "李外讲",
}).Error)
_, content = svc.sessionNoticeText(ctx, tenantID, newSession(t, "c-missing"))
require.Contains(t, content, "培训讲师:李外讲")
// 场景3:名册讲师正常解析(既有行为不回归) → 显示员工姓名
require.NoError(t, db.Create(&types.User{
ID: "u-lect", Username: "26040006", DisplayName: "刘义亮", Email: "u-lect@x.local",
TenantID: tenantID, IsActive: true, CreatedAt: time.Now(), UpdatedAt: time.Now(),
}).Error)
require.NoError(t, db.Create(&types.OrgEmployee{
ID: "m-lect", TenantID: tenantID, UserID: "u-lect", EmployeeNo: "26040006",
}).Error)
require.NoError(t, db.Create(&types.TrainingCourse{
ID: "c-roster", TenantID: tenantID, Name: "名册讲师课",
Mode: "offline", LecturerID: "m-lect",
}).Error)
_, content = svc.sessionNoticeText(ctx, tenantID, newSession(t, "c-roster"))
require.Contains(t, content, "培训讲师:刘义亮")
// 场景4:无任何讲师信息 → 维持「待定」占位
require.NoError(t, db.Create(&types.TrainingCourse{
ID: "c-none", TenantID: tenantID, Name: "无讲师课", Mode: "offline",
}).Error)
_, content = svc.sessionNoticeText(ctx, tenantID, newSession(t, "c-none"))
require.True(t, strings.Contains(content, "培训讲师:待定"))
}