推送域(000155):
- 广播通知支持 link_url:demand_collect 场景自动签发教育训练需求表模板
匿名下载直链(HMAC token,20 天有效,免登录路由),企微消息尾部拼
「下载模板 + 查看更多详情」链接;站内文本保持纯文本不受影响
- 配置中心新增「推送链接基址」(notify_link_base_url,多入口/内外网部署固定
权威地址,留空跟随推送人访问地址)与「年培训计划收集推送文案」
(demand_collect_*,{year} 渲染收集年度);空标题/内容按模板回落渲染
- 迁移 000155:tr_training_notifications 增 link_url/link_token 列
课程管理:
- 修复点【发布/下线】报「不支持的操作」:动作取值兼容字面量路由(FullPath
末段)与 :action 通配两种注册形态
- 列表新增「创建人」列:creator_id 联查 users 带出姓名,导出 Excel 同步;
新增课程「所属部门」默认当前登录用户所在部门
- 导入加固:表头归一化 + 别名模糊匹配(兼容内置模板/旧口径/自定义模板变体);
导入与新增行支持按部门名兜底解析为 id(前端名册未命中时传 owner_dept_name)
年培训计划:
- 需求列表新增「创建人/创建时间」列(submitter_id 联查 users 带出姓名)
- 收集模板调整:下载时动态删除「培训费用(RMB)」列(旧版自定义模板同步归一,
右侧列/合并/下拉随删列平移);备注右侧追加「调研维度/培训策划要求」说明列
(合并至数据区末行,自动换行带边框,已带说明的自定义模板不覆盖)
开发工具:
- air 热重载适配 Windows:构建收敛到 scripts/air_build.sh(规避 air 经
PowerShell 执行不支持 bash 命令替换),产物改 .exe 后缀;CGO_CFLAGS 取
持久化值并集追加,不覆盖 go env -w 的头文件包含路径
849 lines
30 KiB
Go
849 lines
30 KiB
Go
package training
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"academy/internal/errors"
|
|
"academy/internal/types"
|
|
"academy/internal/utils"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 通用辅助
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// trimSpace is a local strings.TrimSpace alias keeping call sites short.
|
|
func trimSpace(raw string) string { return strings.TrimSpace(raw) }
|
|
|
|
// parseFlexibleTime accepts RFC3339 or "2006-01-02 15:04" / "2006-01-02".
|
|
func parseFlexibleTime(raw string) *time.Time {
|
|
raw = trimSpace(raw)
|
|
if raw == "" {
|
|
return nil
|
|
}
|
|
layouts := []string{time.RFC3339, "2006-01-02 15:04", "2006-01-02T15:04", "2006-01-02"}
|
|
for _, layout := range layouts {
|
|
if t, err := time.ParseInLocation(layout, raw, time.Local); err == nil {
|
|
return &t
|
|
}
|
|
}
|
|
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 {
|
|
items = []string{}
|
|
}
|
|
data, _ := json.Marshal(items)
|
|
return types.JSON(data)
|
|
}
|
|
|
|
// jsonMarshal is a thin json.Marshal wrapper returning raw bytes.
|
|
func jsonMarshal(v interface{}) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 考试域
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// ListQuestionBanks GET /training/question-banks
|
|
func (h *Handler) ListQuestionBanks(c *gin.Context) {
|
|
items, total, err := h.svc.ListQuestionBanks(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}})
|
|
}
|
|
|
|
// ImportQuestionBankExcel POST /training/question-banks/import — Excel 批量导入题库。
|
|
// 导入成功后原始 Excel 自动同步进系统默认「企业培训(题库)」知识库(服务层 fail-open),
|
|
// 支持后续 AI 对话问答;响应中的 knowledge_id 为同步生成的知识文档 ID。
|
|
func (h *Handler) ImportQuestionBankExcel(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR, 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.PostForm("course_id"), c.PostForm("library_course_id"),
|
|
file, callerUserID(c),
|
|
)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": bank})
|
|
}
|
|
|
|
// ListQuestions GET /training/question-banks/:id/questions
|
|
func (h *Handler) ListQuestions(c *gin.Context) {
|
|
items, err := h.svc.ListQuestions(c.Request.Context(), callerTenantID(c), c.Param("id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// ListPapers GET /training/papers?session_id= — 场次试卷列表。
|
|
func (h *Handler) ListPapers(c *gin.Context) {
|
|
items, err := h.svc.ListPapersForSession(c.Request.Context(), callerTenantID(c), c.Query("session_id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// GetPaperForExam GET /training/papers/:id/questions — 学员答题取卷(答案脱敏)。
|
|
func (h *Handler) GetPaperForExam(c *gin.Context) {
|
|
paper, questions, err := h.svc.GetPaperForExam(c.Request.Context(), callerTenantID(c), callerUserID(c), c.Param("id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"paper": paper, "questions": questions}})
|
|
}
|
|
|
|
// CourseExamPaper POST /training/courses/:id/exam-paper — 线上自学考试取卷:
|
|
// 确保课程线上卷存在并返回学员视角题目(卷不存在时按题库全量题数现场组卷)。
|
|
func (h *Handler) CourseExamPaper(c *gin.Context) {
|
|
paper, questions, err := h.svc.GetCourseExamPaper(c.Request.Context(), callerTenantID(c), callerUserID(c), c.Param("id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"paper": paper, "questions": questions}})
|
|
}
|
|
|
|
// MyCourseExamRecord GET /training/exam-records/my?course_id= — 学员课程线上
|
|
// 卷成绩回读(record 为 null 表示未考;passed=是否达标)。
|
|
func (h *Handler) MyCourseExamRecord(c *gin.Context) {
|
|
courseID := c.Query("course_id")
|
|
if courseID == "" {
|
|
_ = c.Error(errors.NewBadRequestError("缺少 course_id"))
|
|
return
|
|
}
|
|
record, passed, err := h.svc.GetMyCourseExamRecord(c.Request.Context(), callerTenantID(c), callerUserID(c), courseID)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"record": record, "passed": passed}})
|
|
}
|
|
|
|
// MyExamRecords GET /training/exam-records/my-list — 学员本人全部交卷记录
|
|
// (「学习考试」我的成绩列表;含试卷名/课程名/总分/及格线)。
|
|
func (h *Handler) MyExamRecords(c *gin.Context) {
|
|
items, err := h.svc.ListMyExamRecords(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// SubmitExamFor POST /training/exam-records/submit-for — 监考代交卷。
|
|
func (h *Handler) SubmitExamFor(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
var req struct {
|
|
PaperID string `json:"paper_id" binding:"required"`
|
|
MemberID string `json:"member_id" binding:"required"`
|
|
Answers map[string]string `json:"answers"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
record, err := h.svc.SubmitExamFor(ctx, callerTenantID(c), callerUserID(c), req.MemberID, req.PaperID, req.Answers)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": record})
|
|
}
|
|
|
|
// SettleLecturerScoresRequest is the body of POST /training/lecturer-scores/settle.
|
|
type SettleLecturerScoresRequest struct {
|
|
Year int `json:"year"`
|
|
Month int `json:"month" binding:"required,min=1,max=12"`
|
|
}
|
|
|
|
// SettleLecturerScores POST /training/lecturer-scores/settle — 讲师月度综合分结算。
|
|
func (h *Handler) SettleLecturerScores(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
var req SettleLecturerScoresRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
if req.Year <= 0 {
|
|
req.Year = time.Now().Year()
|
|
}
|
|
results, err := h.svc.SettleLecturerScores(ctx, callerTenantID(c), req.Year, req.Month, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"settled": len(results), "items": results}})
|
|
}
|
|
|
|
// SeedRuleConfigs POST /training/rule-configs/seed — 初始化默认规则(已存在不覆盖)。
|
|
// 经理与人事同权(000158):配置中心对经理开放。
|
|
func (h *Handler) SeedRuleConfigs(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
seeded, err := h.svc.SeedRuleConfigs(ctx, callerTenantID(c), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"seeded": len(seeded), "items": seeded}})
|
|
}
|
|
|
|
// ListPlatformUsers GET /training/platform-users — 成员导入的用户选择器数据源。
|
|
func (h *Handler) ListPlatformUsers(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR) {
|
|
return
|
|
}
|
|
users, err := h.svc.ListPlatformUsers(ctx, callerTenantID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": users})
|
|
}
|
|
|
|
// GeneratePaperRequest is the body of POST /training/papers/generate.
|
|
type GeneratePaperRequest struct {
|
|
BankID string `json:"bank_id" binding:"required"`
|
|
SessionID string `json:"session_id"`
|
|
Count int `json:"count"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
// GeneratePaper POST /training/papers/generate — 按课程/难度/知识点自动组卷。
|
|
func (h *Handler) GeneratePaper(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
var req GeneratePaperRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
paper, err := h.svc.GeneratePaper(ctx, callerTenantID(c), req.BankID, req.SessionID, req.Count, req.Version, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": paper})
|
|
}
|
|
|
|
// SubmitExam POST /training/exam-records/submit — 在线答题即时判分。
|
|
func (h *Handler) SubmitExam(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req struct {
|
|
PaperID string `json:"paper_id" binding:"required"`
|
|
Answers map[string]string `json:"answers"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
record, err := h.svc.SubmitExam(ctx, callerTenantID(c), callerUserID(c), req.PaperID, req.Answers)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": record})
|
|
}
|
|
|
|
// ListExamRecords GET /training/exam-records?session_id= — 成绩统计。
|
|
func (h *Handler) ListExamRecords(c *gin.Context) {
|
|
items, err := h.svc.ListExamRecords(c.Request.Context(), callerTenantID(c), c.Query("session_id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// ExamAnalysis GET /training/exams/:session_id/analysis — 班级薄弱点视图。
|
|
func (h *Handler) ExamAnalysis(c *gin.Context) {
|
|
items, err := h.svc.ExamAnalysis(c.Request.Context(), callerTenantID(c), c.Param("session_id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// ListWrongBook GET /training/wrong-book — 个人错题本。
|
|
func (h *Handler) ListWrongBook(c *gin.Context) {
|
|
memberID := c.Query("member_id")
|
|
if memberID == "" {
|
|
memberID = callerUserID(c)
|
|
}
|
|
items, err := h.svc.ListWrongBook(c.Request.Context(), callerTenantID(c), memberID)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 学习/评分/画像
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// SaveLearningProgress POST /training/learning-progress — 视频学习进度上报。
|
|
func (h *Handler) SaveLearningProgress(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req struct {
|
|
TaskID string `json:"task_id" binding:"required"`
|
|
MaterialID string `json:"material_id"`
|
|
PositionSec int `json:"position_sec"`
|
|
Percent int `json:"percent"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
if err := h.svc.SaveLearningProgress(ctx, callerTenantID(c), callerUserID(c), req.TaskID, req.MaterialID, req.PositionSec, req.Percent); err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// ListMyLearningTasks GET /training/learning-tasks — 我的线上学习任务。
|
|
func (h *Handler) ListMyLearningTasks(c *gin.Context) {
|
|
items, err := h.svc.ListMyLearningTasks(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// ListCourseLearningTasks GET /training/learning-tasks/all?course_id= —
|
|
// 按课程返回全员学习任务(人事/课长/讲师的「学习考试」课程学习情况视图)。
|
|
func (h *Handler) ListCourseLearningTasks(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleLecturer) {
|
|
return
|
|
}
|
|
courseID := c.Query("course_id")
|
|
if courseID == "" {
|
|
_ = c.Error(errors.NewBadRequestError("缺少 course_id"))
|
|
return
|
|
}
|
|
items, err := h.svc.ListCourseLearningTasks(c.Request.Context(), callerTenantID(c), courseID)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// ListMyCourseProgress GET /training/learning-progress?course_id= — 当前用户在
|
|
// 某课程下各课件的学习进度(学习详情页续播回读)。
|
|
func (h *Handler) ListMyCourseProgress(c *gin.Context) {
|
|
courseID := c.Query("course_id")
|
|
if courseID == "" {
|
|
_ = c.Error(errors.NewBadRequestError("缺少 course_id"))
|
|
return
|
|
}
|
|
items, err := h.svc.ListMyCourseProgress(c.Request.Context(), callerTenantID(c), callerUserID(c), courseID)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// SaveFeedback POST /training/course-feedbacks — 学员评分/随堂留言。
|
|
func (h *Handler) SaveFeedback(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
var req struct {
|
|
SessionID string `json:"session_id" binding:"required"`
|
|
Rating int `json:"rating"`
|
|
Comment string `json:"comment"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
fb, err := h.svc.SaveFeedback(ctx, callerTenantID(c), callerUserID(c), req.SessionID, req.Rating, req.Comment)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": fb})
|
|
}
|
|
|
|
// ReplyFeedback POST /training/course-feedbacks/:id/reply — 讲师答疑回复。
|
|
func (h *Handler) ReplyFeedback(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleLecturer, types.TrainingRoleHR) {
|
|
return
|
|
}
|
|
var req struct {
|
|
Reply string `json:"reply" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
fb, err := h.svc.ReplyFeedback(ctx, callerTenantID(c), c.Param("id"), req.Reply, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": fb})
|
|
}
|
|
|
|
// ListPendingFeedbacks GET /training/course-feedbacks/pending — 讲师待答疑。
|
|
func (h *Handler) ListPendingFeedbacks(c *gin.Context) {
|
|
items, err := h.svc.ListPendingFeedbacks(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// MyProfile GET /training/profile/me — 个人能力画像。
|
|
func (h *Handler) MyProfile(c *gin.Context) {
|
|
profile, err := h.svc.MyProfile(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": profile})
|
|
}
|
|
|
|
// CourseAIAnalysis GET /training/ai-analysis?course_id= — AI课程分析。
|
|
// 管理角色为全员维度(可按课程聚焦),学员为个人维度;LLM 不可用时
|
|
// service 层回退规则引擎摘要(model_name=rule-fallback)。
|
|
func (h *Handler) CourseAIAnalysis(c *gin.Context) {
|
|
out, err := h.svc.CourseAIAnalysis(c.Request.Context(), callerTenantID(c), callerUserID(c), c.Query("course_id"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 看板
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// HRDashboard GET /training/dashboards/hr — 人事工作台。
|
|
func (h *Handler) HRDashboard(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
year, _ := strconv.Atoi(c.Query("year"))
|
|
month, _ := strconv.Atoi(c.Query("month"))
|
|
out, err := h.svc.HRDashboard(c.Request.Context(), callerTenantID(c), year, month)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
|
|
}
|
|
|
|
// DeptDashboard GET /training/dashboards/department — 部门主管工作台。
|
|
func (h *Handler) DeptDashboard(c *gin.Context) {
|
|
year, _ := strconv.Atoi(c.Query("year"))
|
|
month, _ := strconv.Atoi(c.Query("month"))
|
|
out, err := h.svc.DeptDashboard(c.Request.Context(), callerTenantID(c), callerUserID(c), year, month)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
|
|
}
|
|
|
|
// ExecDashboard GET /training/dashboards/exec — 管理驾驶舱。
|
|
func (h *Handler) ExecDashboard(c *gin.Context) {
|
|
year, _ := strconv.Atoi(c.Query("year"))
|
|
out, err := h.svc.ExecDashboard(c.Request.Context(), callerTenantID(c), year)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": out})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 规则配置 / 通知
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// ListRuleConfigs GET /training/rule-configs
|
|
func (h *Handler) ListRuleConfigs(c *gin.Context) {
|
|
items, err := h.svc.ListRuleConfigs(c.Request.Context(), callerTenantID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// SaveRuleConfig PUT /training/rule-configs/:key — 业务规则配置(人事确认后修改)。
|
|
func (h *Handler) SaveRuleConfig(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
// 经理与人事同权(000158):配置中心对经理开放。
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
var req struct {
|
|
RuleValue json.RawMessage `json:"rule_value" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
cfg, err := h.svc.SaveRuleConfig(ctx, callerTenantID(c), c.Param("key"), req.RuleValue, callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": cfg})
|
|
}
|
|
|
|
// ListNotifications GET /training/notifications — 站内通知/待办。
|
|
func (h *Handler) ListNotifications(c *gin.Context) {
|
|
unreadOnly := c.Query("unread") == "true"
|
|
limit, _ := strconv.Atoi(c.Query("limit"))
|
|
items, err := h.svc.ListNotifications(c.Request.Context(), callerTenantID(c), callerUserID(c), unreadOnly, limit)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// MarkNotificationsRead POST /training/notifications/read — 全部已读。
|
|
func (h *Handler) MarkNotificationsRead(c *gin.Context) {
|
|
if err := h.svc.MarkNotificationsRead(c.Request.Context(), callerTenantID(c), callerUserID(c)); err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// BroadcastNotification POST /training/notifications/broadcast — 培训运营向指定人员批量推送通知
|
|
// (收集下年度计划等场景);通知 ref_type 用于前端铃铛点击跳转定位。
|
|
// link_url 传前端 origin:demand_collect 场景企微消息尾部拼模板下载与详情链接。
|
|
func (h *Handler) BroadcastNotification(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
var req struct {
|
|
UserIDs []string `json:"user_ids" binding:"required"`
|
|
Scene string `json:"scene"`
|
|
Title string `json:"title" binding:"required"`
|
|
Content string `json:"content"`
|
|
RefType string `json:"ref_type"`
|
|
RefID string `json:"ref_id"`
|
|
LinkURL string `json:"link_url"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
sent, err := h.svc.BroadcastNotification(ctx, callerTenantID(c), req.UserIDs, req.Scene, req.Title, req.Content, req.RefType, req.RefID, req.LinkURL)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"sent": sent}})
|
|
}
|
|
|
|
// PushTestNotification POST /training/notifications/push-test — 企微推送链路
|
|
// 验收:直接向目标用户(org_employees.wecom_userid)发送一条 markdown 消息,
|
|
// 走租户 webhook 模式企微渠道凭据,不产生通知流水。
|
|
func (h *Handler) PushTestNotification(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
if h.pusher == nil {
|
|
_ = c.Error(errors.NewBadRequestError("企微推送能力未启用"))
|
|
return
|
|
}
|
|
var req struct {
|
|
TargetUserID string `json:"target_user_id" binding:"required"`
|
|
Content string `json:"content"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
err := h.pusher.PushTestNotification(c.Request.Context(), callerTenantID(c),
|
|
callerUserID(c), req.TargetUserID, req.Content)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// SyncWecomUseridsFromOA POST /training/members/wecom-userid-sync/{preview,apply}
|
|
// 从 OA 目录按工号拉取企微 userid。preview 只读返回冲突/待更新清单;apply
|
|
// 在无冲突时写入 org_employees.wecom_userid(有冲突时服务层不写,直接把
|
|
// summary 带回给前端展示)。
|
|
func (h *Handler) SyncWecomUseridsFromOA(c *gin.Context, apply bool) {
|
|
ctx := c.Request.Context()
|
|
// 经理与人事同权(000159):职称角色页对经理开放。
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
summary, err := h.svc.SyncWecomUseridsFromOA(ctx, callerTenantID(c), apply)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": summary})
|
|
}
|
|
|
|
// SyncWecomUseridsPreview POST /training/members/wecom-userid-sync/preview
|
|
func (h *Handler) SyncWecomUseridsPreview(c *gin.Context) {
|
|
h.SyncWecomUseridsFromOA(c, false)
|
|
}
|
|
|
|
// SyncWecomUseridsApply POST /training/members/wecom-userid-sync/apply
|
|
func (h *Handler) SyncWecomUseridsApply(c *gin.Context) {
|
|
h.SyncWecomUseridsFromOA(c, true)
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 配置中心 · 导入模板管理
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// templateContentTypes 按扩展名映射下载响应的 MIME 类型。
|
|
var templateContentTypes = map[string]string{
|
|
".xls": "application/vnd.ms-excel",
|
|
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
}
|
|
|
|
// templateExtOf 提取小写扩展名(含点),无扩展名返回空串。
|
|
func templateExtOf(name string) string {
|
|
dot := strings.LastIndex(name, ".")
|
|
if dot < 0 {
|
|
return ""
|
|
}
|
|
return strings.ToLower(name[dot:])
|
|
}
|
|
|
|
// templateExtValid 校验上传模板的扩展名(.xls/.xlsx)。
|
|
func templateExtValid(name string) bool {
|
|
_, ok := templateContentTypes[templateExtOf(name)]
|
|
return ok
|
|
}
|
|
|
|
// ListTrainingTemplates GET /training/templates — 模板元数据列表(自定义优先,默认兜底回显)。
|
|
func (h *Handler) ListTrainingTemplates(c *gin.Context) {
|
|
items, err := h.svc.ListTrainingTemplates(c.Request.Context(), callerTenantID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|
|
|
|
// DownloadTrainingTemplate GET /training/templates/:key/file — 下载模板文件
|
|
// (租户自定义版本优先,否则回退二进制内置默认;带登录态即可下载)。
|
|
func (h *Handler) DownloadTrainingTemplate(c *gin.Context) {
|
|
data, fileName, err := h.svc.TrainingTemplateFile(c.Request.Context(), callerTenantID(c), c.Param("key"))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
serveTemplateFile(c, data, fileName)
|
|
}
|
|
|
|
// DownloadTrainingTemplateSigned GET /training/templates/signed-file?token=…
|
|
// — 免登录签名下载(2026-11 需求:企微推送消息内点击即下载教育训练需求表
|
|
// 模板)。token 为 broadcast 入箱时签发的 HMAC(tenant+模板+过期,20 天),
|
|
// 租户与模板键均取自 token 载荷,不接受调用方指定;匿名路由(见 noAuthAPI)。
|
|
func (h *Handler) DownloadTrainingTemplateSigned(c *gin.Context) {
|
|
payload, err := utils.VerifyTrainingTemplateToken(c.Query("token"))
|
|
if err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("下载链接无效或已过期").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
data, fileName, err := h.svc.TrainingTemplateFile(c.Request.Context(), payload.TenantID, payload.TemplateKey)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
serveTemplateFile(c, data, fileName)
|
|
}
|
|
|
|
// serveTemplateFile 模板文件响应(附件下载,中文文件名 RFC 5987 透传)。
|
|
func serveTemplateFile(c *gin.Context, data []byte, fileName string) {
|
|
contentType := "application/octet-stream"
|
|
if ct, ok := templateContentTypes[templateExtOf(fileName)]; ok {
|
|
contentType = ct
|
|
}
|
|
// RFC 5987:中文文件名用 filename* 透传,下载保留原名
|
|
c.Header("Content-Disposition", "attachment; filename*=UTF-8''"+url.PathEscape(fileName))
|
|
c.Data(http.StatusOK, contentType, data)
|
|
}
|
|
|
|
// UploadTrainingTemplate POST /training/templates/:key/upload — 上传自定义模板
|
|
// (人事;校验扩展名与大小,入库后各下载入口即刻生效)。
|
|
func (h *Handler) UploadTrainingTemplate(c *gin.Context) {
|
|
// 经理与人事同权(000158):配置中心对经理开放。
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleManager) {
|
|
return
|
|
}
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("缺少上传文件"))
|
|
return
|
|
}
|
|
if !templateExtValid(file.Filename) {
|
|
_ = c.Error(errors.NewBadRequestError("仅支持 .xls / .xlsx 模板文件"))
|
|
return
|
|
}
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
_ = c.Error(errors.NewBadRequestError("文件读取失败").WithDetails(err.Error()))
|
|
return
|
|
}
|
|
data, readErr := io.ReadAll(src)
|
|
_ = src.Close()
|
|
if readErr != nil {
|
|
_ = c.Error(errors.NewBadRequestError("文件读取失败").WithDetails(readErr.Error()))
|
|
return
|
|
}
|
|
meta, err := h.svc.SaveTrainingTemplate(
|
|
c.Request.Context(), callerTenantID(c), c.Param("key"), file.Filename, data, callerUserID(c),
|
|
)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": meta})
|
|
}
|
|
|
|
// ResetTrainingTemplate DELETE /training/templates/:key — 恢复内置默认模板
|
|
// (人事;删除租户自定义版本,下载回退内置文件)。
|
|
func (h *Handler) ResetTrainingTemplate(c *gin.Context) {
|
|
// 经理与人事同权(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 {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// 推送管理(2026-11 推学员/推讲师需求,配置中心展示)
|
|
// ---------------------------------------------------------------------------
|
|
|
|
// NotifySessionStudents POST /training/sessions/:id/notify — 手动"立即推送学员"
|
|
// 开课通知(讲师/人事;课件门控+scene 幂等,返回本轮新入队与已通知跳过数)。
|
|
func (h *Handler) NotifySessionStudents(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
queued, skipped, err := h.svc.NotifySessionStudents(
|
|
c.Request.Context(), callerTenantID(c), c.Param("id"), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{
|
|
"queued": queued,
|
|
"skipped": skipped,
|
|
}})
|
|
}
|
|
|
|
// RunWeeklyLecturerReminders POST /training/notifications/run-weekly — 手动触发
|
|
// 一轮"周一讲师未完成汇总提醒"(人事/课长;轮次键按当月第 N 周幂等,同周重复
|
|
// 触发不重发),返回新入队条数。
|
|
func (h *Handler) RunWeeklyLecturerReminders(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
queued, err := h.svc.RunWeeklyLecturerReminders(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": gin.H{"queued": queued}})
|
|
}
|
|
|
|
// ListPushLogs GET /training/notifications/push-logs — 推送逐人明细(人事/课长):
|
|
// 按场景集合(逗号分隔 scene 参数)与引用类型过滤,含收件人姓名/工号与企微
|
|
// push_status(pending/sending/sent/skipped/dead;空=仅站内)。
|
|
func (h *Handler) ListPushLogs(c *gin.Context) {
|
|
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
|
return
|
|
}
|
|
var scenes []string
|
|
if raw := strings.TrimSpace(c.Query("scenes")); raw != "" {
|
|
for _, part := range strings.Split(raw, ",") {
|
|
if part = strings.TrimSpace(part); part != "" {
|
|
scenes = append(scenes, part)
|
|
}
|
|
}
|
|
}
|
|
limit, _ := strconv.Atoi(c.Query("limit"))
|
|
items, err := h.svc.ListPushLogs(
|
|
c.Request.Context(), callerTenantID(c), scenes, c.Query("ref_type"), limit)
|
|
if err != nil {
|
|
_ = c.Error(err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "data": items})
|
|
}
|