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

@@ -835,6 +835,35 @@ const RULE_SCHEMAS: RuleSchemaDesc[] = [
type: 'text',
vars: '{year} 收集年度(推送时按下一年度替换)',
},
{
// [2026-11 需求] 需求核验提醒(批量导入/在线提报创建完成后推送部门经理核验):
// 文案原为后端硬编码,现入模板可编辑;接收人与触发节点不随模板变化。
key: 'demand_verify_title',
label: '需求核验提醒 · 标题',
type: 'text',
placeholder: '【年度培训需求核验】{year}年度 {dept} 培训需求待核验',
vars: '{year} 年度 / {dept} 部门名',
},
{
key: 'demand_verify_content',
label: '需求核验提醒 · 内容',
type: 'text',
vars: '{dept} 部门名 / {count} 导入条数 / {courses} 课程名(、连接) / {year} 年度',
},
{
// [2026-11 需求] 需求确认转月计划维护提醒(通知讲师与部门课长维护授课安排)
key: 'demand_confirmed_title',
label: '需求确认转月计划提醒 · 标题',
type: 'text',
placeholder: '【培训计划维护】{course} 已确认转入月培训计划',
vars: '{course} 课程名',
},
{
key: 'demand_confirmed_content',
label: '需求确认转月计划提醒 · 内容',
type: 'text',
vars: '{year} 年度 / {course} 课程名',
},
],
},
{
@@ -878,6 +907,14 @@ const DEFAULT_RULE_VALUES: Record<string, Record<string, any>> = {
// 年培训计划收集推送默认文案(与后端 defaultDemandCollect* 同源引用)
demand_collect_title: DEFAULT_DEMAND_COLLECT_TITLE,
demand_collect_content: DEFAULT_DEMAND_COLLECT_CONTENT,
// 需求核验提醒/需求确认转月计划提醒默认文案(与后端 defaultDemandVerify*/
// defaultDemandConfirmed* 逐字一致,2026-11 需求)
demand_verify_title: '【年度培训需求核验】{year}年度 {dept} 培训需求待核验',
demand_verify_content:
'{dept} 新导入 {count} 条年度培训需求(课程:{courses})。请登录和信智汇学院 → 企业培训 → 年培训计划,逐条核验并【确认】,确认后将自动转入月培训计划。',
demand_confirmed_title: '【培训计划维护】{course} 已确认转入月培训计划',
demand_confirmed_content:
'{year}年度需求「{course}」已核验确认并自动转入月培训计划:请讲师维护课程信息(课件/试题),课长安排授课时间/地点与学员名单。入口:和信智汇学院 → 企业培训 → 月培训计划。',
},
}
+8 -1
View File
@@ -37,8 +37,15 @@ func NewTrainingRepository(db *gorm.DB) interfaces.TrainingRepository {
// GetMember fetches one member profile by ID.
func (r *trainingRepository) GetMember(ctx context.Context, tenantID uint64, id string) (*types.TrainingMember, error) {
var m types.TrainingMember
// UserName/DeptName 为联查只读展示字段:单条查询同样 JOIN users/org_depts 取值,
// 口径与 ListEmployees 一致(display_name 优先,回退 username 工号)。此前单查
// 不挂联查,UserName 恒为空 —— 学员开课通知讲师恒显示「待定」的根因
// (2026-11 根因修复)。
if err := r.db.WithContext(ctx).
Where("tenant_id = ? AND id = ?", tenantID, id).
Joins("LEFT JOIN users u ON u.id = org_employees.user_id").
Joins("LEFT JOIN org_depts dp ON dp.id = org_employees.dept_id AND dp.tenant_id = org_employees.tenant_id AND dp.deleted_at IS NULL").
Select(`org_employees.*, COALESCE(NULLIF(u.display_name, ''), u.username, '') AS user_name, COALESCE(dp.name, '') AS dept_name`).
Where("org_employees.tenant_id = ? AND org_employees.id = ?", tenantID, id).
First(&m).Error; err != nil {
return nil, err
}
@@ -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, "培训讲师:待定"))
}
+12 -10
View File
@@ -600,6 +600,12 @@ func (h *Handler) SaveDemand(c *gin.Context) {
}
// 需求讲师回写课程管理(000156):在线改选讲师后课程管理同步追加(fail-open)。
h.syncDemandLecturerToLibrary(ctx, callerTenantID(c), callerUserID(c), []*types.TrainingDemand{demand})
// [2026-11 需求] 在线提报创建完成后同样推送部门经理核验提醒(与批量导入同
// 口径:按部门聚合并通知该部门经理核验【确认】;编辑不重复推;fail-open
// 不阻断保存)。
if req.ID == "" {
h.notifyDemandVerify(ctx, callerTenantID(c), []*types.TrainingDemand{demand})
}
c.JSON(http.StatusOK, gin.H{"success": true, "data": demand})
}
@@ -1008,11 +1014,9 @@ func (h *Handler) notifyDemandVerify(ctx context.Context, tenantID uint64, deman
if name == "" {
name = deptID
}
title := fmt.Sprintf("【年度培训需求核验】%d年度 %s 培训需求待核验", year, name)
content := fmt.Sprintf(
"%s 新导入 %d 条年度培训需求(课程:%s)。请登录和信智汇学院 → 企业培训 → 年培训计划,逐条核验并【确认】,确认后将自动转入月培训计划。",
name, len(courses), strings.Join(courses, "、"),
)
// 文案入配置中心「提醒推送文案」demand_verify_*(2026-11 需求,空字段回落默认):
// 占位符 {year}/{dept}/{count}/{courses}
title, content := h.svc.DemandVerifyNoticeText(ctx, tenantID, year, name, courses)
n, err := h.svc.BroadcastNotification(ctx, tenantID, recipients, "demand_verify", title, content, "plan", strconv.Itoa(year), "")
if err != nil {
logger.Error(ctx, "Failed to notify dept managers for demand verify", err)
@@ -1044,11 +1048,9 @@ func (h *Handler) notifyDemandConfirmed(ctx context.Context, tenantID uint64, op
if len(final) == 0 {
return
}
title := fmt.Sprintf("【培训计划维护】%s 已确认转入月培训计划", demand.CourseName)
content := fmt.Sprintf(
"%d年度需求「%s」已核验确认并自动转入月培训计划:请讲师维护课程信息(课件/试题),课长安排授课时间/地点与学员名单。入口:和信智汇学院 → 企业培训 → 月培训计划。",
demand.Year, demand.CourseName,
)
// 文案入配置中心「提醒推送文案」demand_confirmed_*(2026-11 需求,空字段回落默认):
// 占位符 {year}/{course}
title, content := h.svc.DemandConfirmedNoticeText(ctx, tenantID, demand.Year, demand.CourseName)
if _, err := h.svc.BroadcastNotification(ctx, tenantID, final, "demand_confirmed", title, content, "course", demand.ID, ""); err != nil {
logger.Error(ctx, "Failed to notify lecturer and supervisors after demand confirm", err)
}
+7
View File
@@ -580,6 +580,13 @@ type TrainingService interface {
// demand_collect 场景据此生成模板匿名下载签名 token 落库,企微消息尾部由
// 调度器拼「下载模板/查看详情」链接。
BroadcastNotification(ctx context.Context, tenantID uint64, userIDs []string, scene, title, content, refType, refID, linkURL string) (int, error)
// DemandVerifyNoticeText 需求核验提醒文案(批量导入/在线提报创建后推送部门
// 经理):配置中心「提醒推送文案」demand_verify_* 可编辑,空字段回落内置默认。
// 占位符 {year}/{dept}/{count}/{courses}。返回 (标题, 内容)。
DemandVerifyNoticeText(ctx context.Context, tenantID uint64, year int, dept string, courses []string) (string, string)
// DemandConfirmedNoticeText 需求确认转月计划维护提醒文案(通知讲师与课长):
// 配置中心 demand_confirmed_* 可编辑,占位符 {year}/{course}。返回 (标题, 内容)。
DemandConfirmedNoticeText(ctx context.Context, tenantID uint64, year int, course string) (string, string)
// ---- 推送管理(2026-11 推学员/推讲师需求,配置中心可观测) ----
// NotifySessionStudents 手动对场次名单内未通知学员入队企微开课通知