feat(training): 企业培训迭代:核验权限收窄、课程发布状态与培训中心改版
需求域: - 需求导入修复内/外培训与外部讲师落库;按模板标题年入库(年-月一致) - 核验确认收窄为「需求归属部门+经理职称角色」,越权提示联系部门经理 - 确认转月计划后自动推送讲师/课长维护;导入后自动推送部门经理核验(站内+企微) 课程域: - 课程管理发布状态字段(迁移000154)+发布/下线;培训中心「和信课程」目录 - 需求讲师改选后回写课程管理;确认同步带出适合人员/课件/试题 - 月计划详情课件/试题更换(上传/知识库选择/题库换绑)回写课程管理, 无关联条目的存量计划自动创建课程条目 模板与表单: - 教育训练需求表:适合人员/内-外培训口径;部门联动课程下拉; 适合人员/讲师按所选课程自动带出;在线提报课程下拉(本部门)自动带出分类/讲师/适合人员 页面与权限: - 培训中心页签重组(和信课程/我的培训/我的考试),移除线上/线下页签 - 数据驾驶舱迁侧栏(职称角色之下);职称角色迁至更多工具配置中心之后 - 配置中心/职称角色对经理开放;页签「职称角色」更名「角色权限」 - 修复年度下拉空白(TDesign options 需对象数组)、月计划列表地点兜底 其他: 培训预览端口修正与 dev:training 脚本;tmp-extract-duckdb 加入 gitignore
This commit is contained in:
1 parent
aae041a0b4
commit
242dd7e1a6
47 files changed
+5882
-1134
No files matched your search
@@ -157,8 +157,9 @@ func (h *Handler) callerDataScope(c *gin.Context) (dataScope, *types.TrainingMem
|
||||
}
|
||||
}
|
||||
|
||||
// callerDeptScope 计划类列表的部门范围限定:纯课长(有课长角色、无人事/经理
|
||||
// 全局视角)只能查询本部门数据,返回其所在部门 ID;其余角色返回空串(不限定)。
|
||||
// callerDeptScope 计划类列表的部门范围限定:人事/空间管理员为全局视角(返回
|
||||
// 空串不限定);经理/课长(含继承课长的自定义角色)只能查询本部门数据,返回其
|
||||
// 所在部门 ID;其余角色(讲师/学员)不限定(本就无管理入口)。
|
||||
func (h *Handler) callerDeptScope(c *gin.Context) string {
|
||||
if callerRole := types.TenantRoleFromContext(c.Request.Context()); callerRole.IsValid() &&
|
||||
(callerRole == types.TenantRoleAdmin || callerRole == types.TenantRoleOwner) {
|
||||
@@ -169,12 +170,14 @@ func (h *Handler) callerDeptScope(c *gin.Context) string {
|
||||
return ""
|
||||
}
|
||||
roles := h.svc.ExpandTrainingRoles(c.Request.Context(), callerTenantID(c), member.TrainingRoleList())
|
||||
if types.HasTrainingRole(roles, types.TrainingRoleHR) ||
|
||||
types.HasTrainingRole(roles, types.TrainingRoleManager) ||
|
||||
!types.HasTrainingRole(roles, types.TrainingRoleSupervisor) {
|
||||
// 人事全量;经理继承课长权限组,与课长一致收窄本部门
|
||||
if types.HasTrainingRole(roles, types.TrainingRoleHR) {
|
||||
return ""
|
||||
}
|
||||
return member.DeptID
|
||||
if types.HasTrainingRole(roles, types.TrainingRoleSupervisor) {
|
||||
return member.DeptID
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// checkCourseVisibility 课程详情可见性:全局视角与已发布课程对所有人开放;
|
||||
@@ -314,7 +317,8 @@ func (h *Handler) ImportMembers(c *gin.Context) {
|
||||
// 企微 userid(培训提醒推送目标;空串=解绑)。
|
||||
func (h *Handler) UpdateMemberWecomUserid(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
// 经理与人事同权(000159):职称角色页对经理开放。
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -341,10 +345,10 @@ func rolesToJSON(roles []string) types.JSON {
|
||||
return types.JSON(data)
|
||||
}
|
||||
|
||||
// UpdateMemberRoles PUT /training/members/:id/roles
|
||||
// UpdateMemberRoles PUT /training/members/:id/roles — 经理与人事同权(000159)。
|
||||
func (h *Handler) UpdateMemberRoles(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -391,6 +395,17 @@ func (h *Handler) ListDemands(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"items": items, "total": total}})
|
||||
}
|
||||
|
||||
// ListDemandYears GET /training/demands/years — 需求库实际年份去重列表(降序),
|
||||
// 年度需求收集页年份下拉数据源(前端默认选中最新一年);纯课长收窄本部门口径。
|
||||
func (h *Handler) ListDemandYears(c *gin.Context) {
|
||||
years, err := h.svc.ListDemandYears(c.Request.Context(), callerTenantID(c), h.callerDeptScope(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": years})
|
||||
}
|
||||
|
||||
// saveDemandRequest is the body of POST /training/demands.
|
||||
// 字段口径对齐「年度教育培训收集模板」(docs/moban):厂区/主办单位(→dept_id)/课程名称/
|
||||
// 次数/参加人员(→post_scope)/地点/分类/培训讲师/月份。
|
||||
@@ -485,6 +500,8 @@ func (h *Handler) SaveDemand(c *gin.Context) {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
// 需求讲师回写课程管理(000156):在线改选讲师后课程管理同步追加(fail-open)。
|
||||
h.syncDemandLecturerToLibrary(ctx, callerTenantID(c), callerUserID(c), []*types.TrainingDemand{demand})
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": demand})
|
||||
}
|
||||
|
||||
@@ -550,6 +567,7 @@ func (h *Handler) ImportDemands(c *gin.Context) {
|
||||
Year: item.Year, DeptID: item.DeptID, CourseName: item.CourseName,
|
||||
Plant: item.Plant, Category: item.Category, SessionCount: item.SessionCount,
|
||||
Location: item.Location, PostScope: item.PostScope, LecturerID: item.LecturerID,
|
||||
LecturerExternal: trimSpace(item.LecturerExternal), TrainType: trimSpace(item.TrainType),
|
||||
TargetCount: item.TargetCount, Hours: item.Hours, ExpectMonth: item.ExpectMonth,
|
||||
PlannedTime: trimSpace(item.PlannedTime), Remark: item.Remark,
|
||||
})
|
||||
@@ -559,22 +577,57 @@ func (h *Handler) ImportDemands(c *gin.Context) {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"imported": len(result)}})
|
||||
// 需求讲师回写课程管理(000156):改选讲师后课程管理同步追加该讲师(fail-open)。
|
||||
h.syncDemandLecturerToLibrary(ctx, callerTenantID(c), callerUserID(c), demands)
|
||||
// 导入后自动推送部门经理核验(000157,站内+企微,fail-open)。
|
||||
notified := h.notifyDemandVerify(ctx, callerTenantID(c), demands)
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"imported": len(result), "notified": notified}})
|
||||
}
|
||||
|
||||
// ConfirmDemand POST /training/demands/:id/confirm — 人事复核确认。
|
||||
// demandConfirmDenied 年度需求核验确认的统一拒绝文案(000154 权限收窄):
|
||||
// 仅「需求归属部门」内、职称角色为经理的成员可确认,其余人员一律提示联系部门经理。
|
||||
const demandConfirmDenied = "暂无权限,请联系部门经理~"
|
||||
|
||||
// ConfirmDemand POST /training/demands/:id/confirm — 经理核验确认(部门+角色双校验)。
|
||||
// 仅「需求归属部门的成员且职称角色为经理」可核验(000154 收窄:人事/空间管理员/
|
||||
// 其他部门经理一律拒绝),统一提示「暂无权限,请联系部门经理~」;
|
||||
// 部门校验先于确认落库,避免越权时已改状态。
|
||||
func (h *Handler) ConfirmDemand(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
tenantID, userID := callerTenantID(c), callerUserID(c)
|
||||
member, err := h.svc.CurrentMember(ctx, tenantID, userID)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
demand, err := h.svc.ConfirmDemand(ctx, callerTenantID(c), c.Param("id"), callerUserID(c))
|
||||
if member == nil {
|
||||
_ = c.Error(errors.NewForbiddenError(demandConfirmDenied))
|
||||
return
|
||||
}
|
||||
roles := h.svc.ExpandTrainingRoles(ctx, tenantID, member.TrainingRoleList())
|
||||
if !types.HasTrainingRole(roles, types.TrainingRoleManager) {
|
||||
_ = c.Error(errors.NewForbiddenError(demandConfirmDenied))
|
||||
return
|
||||
}
|
||||
existing, err := h.svc.GetDemand(ctx, tenantID, c.Param("id"))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
// 本部门口径:仅可核验本部门需求(部门缺失的需求先由人事/提报人补全部门)。
|
||||
if existing.DeptID == "" || existing.DeptID != member.DeptID {
|
||||
_ = c.Error(errors.NewForbiddenError(demandConfirmDenied))
|
||||
return
|
||||
}
|
||||
demand, err := h.svc.ConfirmDemand(ctx, tenantID, c.Param("id"), userID)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
// 已确认需求自动生成草稿课程到课程管理(fail-open,不阻断确认)。
|
||||
h.syncDemandCourse(ctx, callerTenantID(c), demand, callerUserID(c))
|
||||
h.syncDemandCourse(ctx, tenantID, demand, userID)
|
||||
// 确认转月培训计划后自动推送讲师与部门课长维护(000157,站内+企微,fail-open)。
|
||||
h.notifyDemandConfirmed(ctx, tenantID, userID, demand)
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": demand})
|
||||
}
|
||||
|
||||
@@ -601,12 +654,25 @@ func (h *Handler) syncDemandCourse(ctx context.Context, tenantID uint64, demand
|
||||
DeptIDs: sliceToJSON([]string{demand.DeptID}),
|
||||
PostScope: demand.PostScope,
|
||||
}
|
||||
// 经理确认自动转月度计划(000153):回填来源需求 id 供列表/详情溯源;
|
||||
// 课程管理存在同名课程时直接关联(计划侧自动带出并锁定课程信息),
|
||||
// 无同名课程时课长在月度计划编辑弹窗手动选择课程管理课程。
|
||||
course.SourceDemandID = demand.ID
|
||||
if libItems, _, libErr := h.svc.ListCourseLibraryItems(ctx, tenantID, &interfaces.TrainingListQuery{Page: 1, PageSize: 500}); libErr == nil {
|
||||
for _, item := range libItems {
|
||||
if item != nil && item.Name == demand.CourseName {
|
||||
course.LibraryCourseID = item.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// 授课时间默认取需求「期望月份」(YYYY-MM,课程管理侧可修改并留版本记录)。
|
||||
if demand.Year > 0 && demand.ExpectMonth >= 1 && demand.ExpectMonth <= 12 {
|
||||
course.TeachTime = fmt.Sprintf("%04d-%02d", demand.Year, demand.ExpectMonth)
|
||||
}
|
||||
// 计划培训时间:需求已填则原样同步到课程(YYYY-MM-DD HH:mm / YYYY-MM-DD)。
|
||||
course.PlannedTime = trimSpace(demand.PlannedTime)
|
||||
// 计划培训月份:需求侧为「YYYY-M」(如 2027-1),归一为课程口径 YYYY-MM;
|
||||
// 旧日期格式(YYYY-MM-DD 等)原样同步。
|
||||
course.PlannedTime = demandPlannedMonthOf(trimSpace(demand.PlannedTime))
|
||||
// 需求录入信息全量同步(000150 补充的课程侧落点),课程详情弹窗只读回显。
|
||||
// 内/外培训、厂区、地点、培训次数、备注原样带入;外部讲师仅在无系统讲师时生效。
|
||||
externalLecturer := trimSpace(demand.LecturerExternal)
|
||||
@@ -701,6 +767,164 @@ func splitNames(raw string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
// trainingDeptRoleRecipients 取指定部门具有任一目标职称角色(按权限分配展开后)
|
||||
// 的在职成员 user_id;部门为空或查询失败返回空(fail-open 供推送场景使用)。
|
||||
func (h *Handler) trainingDeptRoleRecipients(ctx context.Context, tenantID uint64, deptID string, want ...types.TrainingRole) []string {
|
||||
if deptID == "" {
|
||||
return nil
|
||||
}
|
||||
members, _, err := h.svc.ListMembers(ctx, tenantID, &interfaces.TrainingListQuery{Page: 1, PageSize: 500})
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
out := []string{}
|
||||
for _, m := range members {
|
||||
if m == nil || m.DeptID != deptID || m.UserID == "" {
|
||||
continue
|
||||
}
|
||||
roles := h.svc.ExpandTrainingRoles(ctx, tenantID, m.TrainingRoleList())
|
||||
for _, w := range want {
|
||||
if types.HasTrainingRole(roles, w) {
|
||||
out = append(out, m.UserID)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// notifyDemandVerify 需求导入后自动推送(000157):按部门聚合,站内+企微通知
|
||||
// 该部门经理(职称角色经理)进行核实【确认】;fail-open 不阻断导入。
|
||||
// 返回成功送达的通知数(写入导入响应,前端提示)。
|
||||
func (h *Handler) notifyDemandVerify(ctx context.Context, tenantID uint64, demands []*types.TrainingDemand) int {
|
||||
byDept := map[string][]string{}
|
||||
year := 0
|
||||
for _, d := range demands {
|
||||
if d == nil || d.DeptID == "" || trimSpace(d.CourseName) == "" {
|
||||
continue
|
||||
}
|
||||
byDept[d.DeptID] = append(byDept[d.DeptID], trimSpace(d.CourseName))
|
||||
if d.Year > 0 && year == 0 {
|
||||
year = d.Year
|
||||
}
|
||||
}
|
||||
if len(byDept) == 0 {
|
||||
return 0
|
||||
}
|
||||
deptNames := map[string]string{}
|
||||
if depts, err := h.svc.ListDepts(ctx, tenantID); err == nil {
|
||||
for _, d := range depts {
|
||||
if d != nil {
|
||||
deptNames[d.ID] = d.Name
|
||||
}
|
||||
}
|
||||
}
|
||||
sent := 0
|
||||
for deptID, courses := range byDept {
|
||||
recipients := h.trainingDeptRoleRecipients(ctx, tenantID, deptID, types.TrainingRoleManager)
|
||||
if len(recipients) == 0 {
|
||||
continue
|
||||
}
|
||||
name := deptNames[deptID]
|
||||
if name == "" {
|
||||
name = deptID
|
||||
}
|
||||
title := fmt.Sprintf("【年度培训需求核验】%d年度 %s 培训需求待核验", year, name)
|
||||
content := fmt.Sprintf(
|
||||
"%s 新导入 %d 条年度培训需求(课程:%s)。请登录 AIDO → 企业培训 → 年培训计划,逐条核验并【确认】,确认后将自动转入月培训计划。",
|
||||
name, len(courses), strings.Join(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)
|
||||
continue
|
||||
}
|
||||
sent += n
|
||||
}
|
||||
return sent
|
||||
}
|
||||
|
||||
// notifyDemandConfirmed 需求确认转月培训计划后自动推送(000157):站内+企微通知
|
||||
// 该课程讲师与部门课长维护培训相关信息(讲师维护课程信息,课长安排授课计划);
|
||||
// 确认操作人自身不重复通知;fail-open 不阻断确认。
|
||||
func (h *Handler) notifyDemandConfirmed(ctx context.Context, tenantID uint64, operatorID string, demand *types.TrainingDemand) {
|
||||
if demand == nil {
|
||||
return
|
||||
}
|
||||
recipients := h.trainingDeptRoleRecipients(ctx, tenantID, demand.DeptID, types.TrainingRoleSupervisor)
|
||||
recipients = append(recipients, trimSpace(demand.LecturerID))
|
||||
seen := map[string]bool{}
|
||||
final := []string{}
|
||||
for _, uid := range recipients {
|
||||
if uid == "" || uid == operatorID || seen[uid] {
|
||||
continue
|
||||
}
|
||||
seen[uid] = true
|
||||
final = append(final, uid)
|
||||
}
|
||||
if len(final) == 0 {
|
||||
return
|
||||
}
|
||||
title := fmt.Sprintf("【培训计划维护】%s 已确认转入月培训计划", demand.CourseName)
|
||||
content := fmt.Sprintf(
|
||||
"%d年度需求「%s」已核验确认并自动转入月培训计划:请讲师维护课程信息(课件/试题),课长安排授课时间/地点与学员名单。入口:AIDO → 企业培训 → 月培训计划。",
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// syncDemandLecturerToLibrary 需求讲师回写课程管理(000156):需求(导入/在线保存)
|
||||
// 按课程名称匹配课程管理条目,讲师与条目不一致时以需求讲师为准更新(改选讲师后
|
||||
// 课程管理同步追加为该讲师);同批同名取首条。fail-open:失败仅记日志不阻断导入。
|
||||
func (h *Handler) syncDemandLecturerToLibrary(ctx context.Context, tenantID uint64, updaterID string, demands []*types.TrainingDemand) {
|
||||
byName := map[string]*types.TrainingDemand{}
|
||||
for _, d := range demands {
|
||||
if d == nil || trimSpace(d.CourseName) == "" {
|
||||
continue
|
||||
}
|
||||
if trimSpace(d.LecturerID) == "" && trimSpace(d.LecturerExternal) == "" {
|
||||
continue
|
||||
}
|
||||
if _, ok := byName[d.CourseName]; !ok {
|
||||
byName[d.CourseName] = d
|
||||
}
|
||||
}
|
||||
if len(byName) == 0 {
|
||||
return
|
||||
}
|
||||
items, _, err := h.svc.ListCourseLibraryItems(ctx, tenantID, &interfaces.TrainingListQuery{Page: 1, PageSize: 500})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "Failed to list course library for lecturer sync", err)
|
||||
return
|
||||
}
|
||||
for _, item := range items {
|
||||
if item == nil {
|
||||
continue
|
||||
}
|
||||
d, ok := byName[item.Name]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
lecturerID, lecturerExt := trimSpace(d.LecturerID), trimSpace(d.LecturerExternal)
|
||||
if item.LecturerID == lecturerID && item.LecturerExternal == lecturerExt {
|
||||
continue
|
||||
}
|
||||
updated := *item
|
||||
updated.LecturerID = lecturerID
|
||||
updated.LecturerExternal = lecturerExt
|
||||
if _, err := h.svc.SaveCourseLibraryItem(ctx, tenantID, &updated, updaterID); err != nil {
|
||||
logger.Error(ctx, "Failed to sync demand lecturer to course library", err)
|
||||
continue
|
||||
}
|
||||
// 系统员工讲师自动补齐讲师角色(讲师名册/下拉可见,fail-open)。
|
||||
if err := h.svc.EnsureLecturerRole(ctx, tenantID, lecturerID); err != nil {
|
||||
logger.Error(ctx, "Failed to ensure lecturer role on demand sync", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateAnnualPlan POST /training/annual-plans/generate — AI 汇总生成年度排期。
|
||||
func (h *Handler) GenerateAnnualPlan(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
@@ -923,6 +1147,8 @@ type saveCourseRequest struct {
|
||||
SessionCount int `json:"session_count"`
|
||||
LecturerExternal string `json:"lecturer_external"`
|
||||
Remark string `json:"remark"`
|
||||
// LibraryCourseID 课程管理条目 id(月培训计划「计划名称」选择课程后关联)。
|
||||
LibraryCourseID string `json:"library_course_id"`
|
||||
}
|
||||
|
||||
// SaveCourse POST /training/courses — 课程创建(线下/线上双向导数据源)。
|
||||
@@ -946,6 +1172,7 @@ func (h *Handler) SaveCourse(c *gin.Context) {
|
||||
PlannedTime: req.PlannedTime,
|
||||
TrainType: req.TrainType, Plant: req.Plant, Location: req.Location,
|
||||
SessionCount: req.SessionCount, LecturerExternal: req.LecturerExternal, Remark: req.Remark,
|
||||
LibraryCourseID: req.LibraryCourseID,
|
||||
}, callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
@@ -1027,15 +1254,16 @@ func (h *Handler) ImportQuestionBankFromKnowledge(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
KnowledgeID string `json:"knowledge_id" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
CourseID string `json:"course_id"`
|
||||
KnowledgeID string `json:"knowledge_id" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
CourseID string `json:"course_id"`
|
||||
LibraryCourseID string `json:"library_course_id"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
bank, err := h.svc.ImportQuestionBankFromKnowledge(ctx, callerTenantID(c), req.KnowledgeID, req.Name, req.CourseID, callerUserID(c))
|
||||
bank, err := h.svc.ImportQuestionBankFromKnowledge(ctx, callerTenantID(c), req.KnowledgeID, req.Name, req.CourseID, req.LibraryCourseID, callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
@@ -1112,15 +1340,16 @@ func (h *Handler) ImportGeneratedQuestionBank(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
CourseID string `json:"course_id"`
|
||||
Name string `json:"name"`
|
||||
Questions []*types.TrainingQuestion `json:"questions" binding:"required"`
|
||||
CourseID string `json:"course_id"`
|
||||
LibraryCourseID string `json:"library_course_id"`
|
||||
Name string `json:"name"`
|
||||
Questions []*types.TrainingQuestion `json:"questions" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
bank, err := h.svc.ImportGeneratedQuestionBank(ctx, callerTenantID(c), req.CourseID, req.Name, req.Questions, callerUserID(c))
|
||||
bank, err := h.svc.ImportGeneratedQuestionBank(ctx, callerTenantID(c), req.CourseID, req.LibraryCourseID, req.Name, req.Questions, callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,236 @@
|
||||
package training
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"academy/internal/errors"
|
||||
"academy/internal/filetransport"
|
||||
"academy/internal/types"
|
||||
"academy/internal/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 课程管理(课程资产主数据,tr_course_library,000151)。
|
||||
// 字段:课程名称/所属部门(org_depts)/说明/适合人员/分类(配置中心
|
||||
// course_category_dict 可配,可自定义)/类型(必修课/选修课)/线上或线下/
|
||||
// 讲师/时长/课件。月培训计划的「计划名称」从本表选择。
|
||||
// 列表 Viewer 可读(月培训计划下拉同源);维护动作限人事/课长/讲师。
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// saveCourseLibraryRequest is the body of POST /training/course-library.
|
||||
type saveCourseLibraryRequest struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
OwnerDeptID string `json:"owner_dept_id"`
|
||||
Description string `json:"description"`
|
||||
SuitableMembers string `json:"suitable_members"`
|
||||
Category string `json:"category"`
|
||||
Type string `json:"type"`
|
||||
Mode string `json:"mode"`
|
||||
LecturerID string `json:"lecturer_id"`
|
||||
LecturerExternal string `json:"lecturer_external"`
|
||||
Hours float64 `json:"hours"`
|
||||
MaterialName string `json:"material_name"`
|
||||
// ClearFile 移除课件文件(前端「移除」按钮;置 true 强制清空 file_* 三列)。
|
||||
ClearFile bool `json:"clear_file"`
|
||||
}
|
||||
|
||||
// toCourseLibrary maps the request body onto the domain model.
|
||||
func toCourseLibrary(req saveCourseLibraryRequest) *types.TrainingCourseLibrary {
|
||||
return &types.TrainingCourseLibrary{
|
||||
ID: req.ID,
|
||||
Name: req.Name,
|
||||
OwnerDeptID: req.OwnerDeptID,
|
||||
Description: req.Description,
|
||||
SuitableMembers: req.SuitableMembers,
|
||||
Category: req.Category,
|
||||
Type: req.Type,
|
||||
Mode: req.Mode,
|
||||
LecturerID: req.LecturerID,
|
||||
LecturerExternal: req.LecturerExternal,
|
||||
Hours: req.Hours,
|
||||
MaterialName: req.MaterialName,
|
||||
ClearFile: req.ClearFile,
|
||||
}
|
||||
}
|
||||
|
||||
// ListCourseLibrary GET /training/course-library — 课程管理列表(关键字分页)。
|
||||
func (h *Handler) ListCourseLibrary(c *gin.Context) {
|
||||
items, total, err := h.svc.ListCourseLibraryItems(c.Request.Context(), callerTenantID(c), listQueryFromRequest(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"items": items, "total": total}})
|
||||
}
|
||||
|
||||
// SaveCourseLibraryItem POST /training/course-library — 新增/编辑(id 空 = 新增)。
|
||||
func (h *Handler) SaveCourseLibraryItem(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
var req saveCourseLibraryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
item, err := h.svc.SaveCourseLibraryItem(c.Request.Context(), callerTenantID(c), toCourseLibrary(req), callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
||||
}
|
||||
|
||||
// DeleteCourseLibraryItem DELETE /training/course-library/:id — 删除(人事/课长)。
|
||||
func (h *Handler) DeleteCourseLibraryItem(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id")); err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
// SetCourseLibraryPublishStatus POST /training/course-library/:id/:action
|
||||
// — 课程条目发布/下线(000154;action=publish|offline,人事/课长/讲师)。
|
||||
// 发布后课程进入培训中心「和信课程」目录;下线即从目录撤回。
|
||||
func (h *Handler) SetCourseLibraryPublishStatus(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
status := ""
|
||||
switch c.Param("action") {
|
||||
case "publish":
|
||||
status = "published"
|
||||
case "offline":
|
||||
status = "offline"
|
||||
default:
|
||||
_ = c.Error(errors.NewBadRequestError("不支持的操作(仅 publish/offline)"))
|
||||
return
|
||||
}
|
||||
if err := h.svc.SetCourseLibraryPublishStatus(c.Request.Context(), callerTenantID(c), c.Param("id"), status); err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
item, err := h.svc.GetCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id"))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
||||
}
|
||||
|
||||
// ImportCourseLibraryRequest is the body of POST /training/course-library/import
|
||||
// (前端解析 Excel 后逐行提交,与需求导入同口径)。
|
||||
type ImportCourseLibraryRequest struct {
|
||||
Items []saveCourseLibraryRequest `json:"items" binding:"required,min=1"`
|
||||
}
|
||||
|
||||
// ImportCourseLibrary POST /training/course-library/import — 批量导入。
|
||||
func (h *Handler) ImportCourseLibrary(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
||||
return
|
||||
}
|
||||
var req ImportCourseLibraryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
items := make([]*types.TrainingCourseLibrary, 0, len(req.Items))
|
||||
for _, row := range req.Items {
|
||||
items = append(items, toCourseLibrary(row))
|
||||
}
|
||||
imported, err := h.svc.ImportCourseLibraryItems(c.Request.Context(), callerTenantID(c), items, callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"imported": imported}})
|
||||
}
|
||||
|
||||
// ExportCourseLibraryRequest is the body of POST /training/course-library/export
|
||||
// (ids 空 = 全量导出;当前筛选由前端传当前列表 id)。
|
||||
type ExportCourseLibraryRequest struct {
|
||||
IDs []string `json:"ids"`
|
||||
}
|
||||
|
||||
// ExportCourseLibrary POST /training/course-library/export — 课程管理 xlsx 下载。
|
||||
func (h *Handler) ExportCourseLibrary(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
||||
return
|
||||
}
|
||||
var req ExportCourseLibraryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
filename, data, err := h.svc.ExportCourseLibrary(c.Request.Context(), callerTenantID(c), req.IDs)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.Header("Content-Disposition", "attachment; filename="+filename)
|
||||
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", data)
|
||||
}
|
||||
|
||||
// UploadCourseLibraryMaterial POST /training/course-library/:id/material —
|
||||
// 上传课件原始文件(multipart: file)。
|
||||
func (h *Handler) UploadCourseLibraryMaterial(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
fh, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("缺少上传文件"))
|
||||
return
|
||||
}
|
||||
if fh.Size > utils.GetMaxFileSizeMB()*1024*1024 {
|
||||
_ = c.Error(errors.NewBadRequestError(fmt.Sprintf("文件大小不能超过%dMB", utils.GetMaxFileSizeMB())))
|
||||
return
|
||||
}
|
||||
item, err := h.svc.UploadCourseLibraryMaterial(c.Request.Context(), callerTenantID(c), c.Param("id"), fh, callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
||||
}
|
||||
|
||||
// AttachCourseLibraryMaterialFromKb POST /training/course-library/:id/material/from-kb
|
||||
// — 从知识库「企业培训(课件)」选择已有文档作为课程课件(body: knowledge_id)。
|
||||
func (h *Handler) AttachCourseLibraryMaterialFromKb(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
KnowledgeID string `json:"knowledge_id" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
item, err := h.svc.AttachCourseLibraryMaterialFromKnowledge(c.Request.Context(), callerTenantID(c), c.Param("id"), req.KnowledgeID, callerUserID(c))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": item})
|
||||
}
|
||||
|
||||
// CourseLibraryMaterialFile GET /training/course-library/:id/material/file —
|
||||
// 课件原始文件流(下载/在线预览)。
|
||||
func (h *Handler) CourseLibraryMaterialFile(c *gin.Context) {
|
||||
rc, fileName, err := h.svc.GetCourseLibraryMaterialFile(c.Request.Context(), callerTenantID(c), c.Param("id"))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
if err := filetransport.Serve(c.Writer, c.Request, rc, filetransport.Options{Filename: fileName}); err != nil {
|
||||
_ = c.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package training
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -36,6 +37,20 @@ func parseFlexibleTime(raw string) *time.Time {
|
||||
return nil
|
||||
}
|
||||
|
||||
// demandPlannedMonthOf 需求侧「计划培训月份」(YYYY-M,如 2027-1)归一为课程口径
|
||||
// YYYY-MM(2027-01);其余格式(旧 YYYY-MM-DD 日期等)原样返回。
|
||||
func demandPlannedMonthOf(raw string) string {
|
||||
parts := strings.SplitN(raw, "-", 2)
|
||||
if len(parts) == 2 {
|
||||
y, yErr := strconv.Atoi(strings.TrimSpace(parts[0]))
|
||||
m, mErr := strconv.Atoi(strings.TrimSpace(parts[1]))
|
||||
if yErr == nil && mErr == nil && y >= 1000 && y <= 9999 && m >= 1 && m <= 12 {
|
||||
return fmt.Sprintf("%04d-%02d", y, m)
|
||||
}
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
// sliceToJSON encodes a string slice into jsonb payload.
|
||||
func sliceToJSON(items []string) types.JSON {
|
||||
if len(items) == 0 {
|
||||
@@ -79,7 +94,7 @@ func (h *Handler) ImportQuestionBankExcel(c *gin.Context) {
|
||||
}
|
||||
bank, err := h.svc.ImportQuestionBankExcel(
|
||||
ctx, callerTenantID(c),
|
||||
trimSpace(c.PostForm("name")), c.PostForm("dept_id"), c.PostForm("course_id"),
|
||||
trimSpace(c.PostForm("name")), c.PostForm("dept_id"), c.PostForm("course_id"), c.PostForm("library_course_id"),
|
||||
file, callerUserID(c),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -209,9 +224,10 @@ func (h *Handler) SettleLecturerScores(c *gin.Context) {
|
||||
}
|
||||
|
||||
// SeedRuleConfigs POST /training/rule-configs/seed — 初始化默认规则(已存在不覆盖)。
|
||||
// 经理与人事同权(000158):配置中心对经理开放。
|
||||
func (h *Handler) SeedRuleConfigs(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
seeded, err := h.svc.SeedRuleConfigs(ctx, callerTenantID(c), callerUserID(c))
|
||||
@@ -517,7 +533,8 @@ func (h *Handler) ListRuleConfigs(c *gin.Context) {
|
||||
// SaveRuleConfig PUT /training/rule-configs/:key — 业务规则配置(人事确认后修改)。
|
||||
func (h *Handler) SaveRuleConfig(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
// 经理与人事同权(000158):配置中心对经理开放。
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -617,7 +634,8 @@ func (h *Handler) PushTestNotification(c *gin.Context) {
|
||||
// summary 带回给前端展示)。
|
||||
func (h *Handler) SyncWecomUseridsFromOA(c *gin.Context, apply bool) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
// 经理与人事同权(000159):职称角色页对经理开放。
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
summary, err := h.svc.SyncWecomUseridsFromOA(ctx, callerTenantID(c), apply)
|
||||
@@ -693,7 +711,8 @@ func (h *Handler) DownloadTrainingTemplate(c *gin.Context) {
|
||||
// UploadTrainingTemplate POST /training/templates/:key/upload — 上传自定义模板
|
||||
// (人事;校验扩展名与大小,入库后各下载入口即刻生效)。
|
||||
func (h *Handler) UploadTrainingTemplate(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
// 经理与人事同权(000158):配置中心对经理开放。
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
file, err := c.FormFile("file")
|
||||
@@ -729,7 +748,8 @@ func (h *Handler) UploadTrainingTemplate(c *gin.Context) {
|
||||
// ResetTrainingTemplate DELETE /training/templates/:key — 恢复内置默认模板
|
||||
// (人事;删除租户自定义版本,下载回退内置文件)。
|
||||
func (h *Handler) ResetTrainingTemplate(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR) {
|
||||
// 经理与人事同权(000158):配置中心对经理开放。
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
||||
return
|
||||
}
|
||||
if err := h.svc.ResetTrainingTemplate(c.Request.Context(), callerTenantID(c), c.Param("key"), callerUserID(c)); err != nil {
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
package training
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"academy/internal/errors"
|
||||
"academy/internal/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// 课程管理 · 试题维护(000153)
|
||||
// 试题随课程管理课程维护:课程条目下题库列表/Excel 导入/AI 出题,以及
|
||||
// 题库删除与单题级增删改查;月度计划选课后经 library_course_id 回退取卷。
|
||||
// 权限口径与课程管理一致:维护动作限人事/课长/讲师。
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ListLibraryCourseQuestionBanks GET /training/course-library/:id/question-banks
|
||||
// — 课程条目下的题库列表(试题维护抽屉)。
|
||||
func (h *Handler) ListLibraryCourseQuestionBanks(c *gin.Context) {
|
||||
banks, err := h.svc.ListLibraryCourseQuestionBanks(c.Request.Context(), callerTenantID(c), c.Param("id"))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": banks})
|
||||
}
|
||||
|
||||
// ImportLibraryCourseQuestionBank POST /training/course-library/:id/question-banks/import
|
||||
// — Excel 批量导入题库并挂课程管理条目(multipart: file[name][dept_id])。
|
||||
// 同名重导入即替换;原始 Excel 自动同步「企业培训(题库)」知识库(服务层 fail-open)。
|
||||
func (h *Handler) ImportLibraryCourseQuestionBank(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
file, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("缺少上传文件"))
|
||||
return
|
||||
}
|
||||
bank, err := h.svc.ImportQuestionBankExcel(
|
||||
ctx, callerTenantID(c),
|
||||
trimSpace(c.PostForm("name")), c.PostForm("dept_id"), "", c.Param("id"),
|
||||
file, callerUserID(c),
|
||||
)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": bank})
|
||||
}
|
||||
|
||||
// LinkQuestionBankToLibraryCourse PUT /training/course-library/:id/question-banks/link
|
||||
// — 更换课程条目的试题(000156):所选题库整体替换该条目当前挂接的题库,
|
||||
// 更换后自动同步到课程管理对应课程(月计划详情「试题 · 更换」入口)。
|
||||
func (h *Handler) LinkQuestionBankToLibraryCourse(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
BankID string `json:"bank_id" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
if err := h.svc.LinkQuestionBankToLibraryCourse(ctx, callerTenantID(c), c.Param("id"), req.BankID); err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
banks, err := h.svc.ListLibraryCourseQuestionBanks(ctx, callerTenantID(c), c.Param("id"))
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": banks})
|
||||
}
|
||||
|
||||
// GenerateLibraryCourseQuestions POST /training/course-library/:id/generate-questions
|
||||
// — 课程管理课件 AI 出题(课件已同步知识库;预览编辑用,不落库)。
|
||||
func (h *Handler) GenerateLibraryCourseQuestions(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Count int `json:"count"`
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
questions, err := h.svc.GenerateLibraryCourseQuestions(ctx, callerTenantID(c), callerUserID(c), c.Param("id"), req.Count)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": questions})
|
||||
}
|
||||
|
||||
// DeleteQuestionBank DELETE /training/question-banks/:id — 删除题库
|
||||
// (连带题目与缓存试卷;试题维护抽屉删除入口)。
|
||||
func (h *Handler) DeleteQuestionBank(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteQuestionBank(c.Request.Context(), callerTenantID(c), c.Param("id")); err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
|
||||
// saveQuestionRequest is the body of POST /training/question-banks/:id/questions
|
||||
// (id 空 = 新增,否则编辑;题干/选项/答案等全字段)。
|
||||
type saveQuestionRequest struct {
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Stem string `json:"stem" binding:"required"`
|
||||
Options []string `json:"options"`
|
||||
Answer string `json:"answer" binding:"required"`
|
||||
Score float64 `json:"score"`
|
||||
Difficulty int `json:"difficulty"`
|
||||
KnowledgePoint string `json:"knowledge_point"`
|
||||
Analysis string `json:"analysis"`
|
||||
}
|
||||
|
||||
// SaveQuestion POST /training/question-banks/:id/questions — 单题新增/编辑
|
||||
// (题库 question_count 同步刷新)。
|
||||
func (h *Handler) SaveQuestion(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
var req saveQuestionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
q, err := h.svc.SaveQuestion(c.Request.Context(), callerTenantID(c), &types.TrainingQuestion{
|
||||
ID: req.ID,
|
||||
BankID: c.Param("id"),
|
||||
Type: req.Type,
|
||||
Stem: req.Stem,
|
||||
Options: sliceToJSON(req.Options),
|
||||
Answer: req.Answer,
|
||||
Score: req.Score,
|
||||
Difficulty: req.Difficulty,
|
||||
KnowledgePoint: req.KnowledgePoint,
|
||||
Analysis: req.Analysis,
|
||||
})
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true, "data": q})
|
||||
}
|
||||
|
||||
// DeleteQuestion DELETE /training/question-banks/:id/questions/:qid — 删除单题。
|
||||
func (h *Handler) DeleteQuestion(c *gin.Context) {
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
||||
return
|
||||
}
|
||||
if err := h.svc.DeleteQuestion(c.Request.Context(), callerTenantID(c), c.Param("qid")); err != nil {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"success": true})
|
||||
}
|
||||
Reference in new issue
Block a user