1.增加按“时间”、“标题”、“企微状态”查询 2.增加数据统计在列表顶部 3.增加分页功能
This commit is contained in:
1 parent
0c471efb9f
commit
9d4092bc7f
14 files changed
+447
-108
No files matched your search
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"academy/internal/types"
|
||||
"academy/internal/types/interfaces"
|
||||
@@ -10,6 +11,13 @@ import (
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// 推送记录分页口径:默认每页 15 条(与前端 TrainingPagination 默认一致),
|
||||
// 单页上限 200。
|
||||
const (
|
||||
pushLogPageSizeDefault = 15
|
||||
pushLogPageSizeMax = 200
|
||||
)
|
||||
|
||||
// trainingRepository 培训业务域数据访问。实现按域拆分在
|
||||
// training.go / training_plan_course.go / training_exam_dash.go 三个文件。
|
||||
// 所有查询都按 tenant_id 过滤,与框架租户隔离惯例保持一致。
|
||||
@@ -351,23 +359,91 @@ func (r *trainingRepository) ListNotificationsByRef(ctx context.Context, tenantI
|
||||
return items, err
|
||||
}
|
||||
|
||||
// ListPushLogs 管理端逐人推送明细(推送管理台"推送记录"):按场景集合可选过滤,
|
||||
// 时间倒序;push_status 空 = 仅站内不入企微外箱。
|
||||
func (r *trainingRepository) ListPushLogs(ctx context.Context, tenantID uint64, scenes []string, refType string, limit int) ([]*types.TrainingNotification, error) {
|
||||
if limit <= 0 || limit > 500 {
|
||||
limit = 200
|
||||
// ListPushLogs 管理端逐人推送明细(推送管理台"推送记录"):按场景/引用/标题
|
||||
// 模糊/企微状态/时间范围可选过滤,时间倒序;push_status 空 = 仅站内不入企微外箱。
|
||||
// 返回当前页行、过滤条件命中总数与全量状态统计(统计与分页无关),
|
||||
// 供配置中心「统计下列查询结果」口径。
|
||||
func (r *trainingRepository) ListPushLogs(ctx context.Context, tenantID uint64, q *types.TrainingPushLogQuery) ([]*types.TrainingNotification, int64, *types.TrainingPushLogStats, error) {
|
||||
if q == nil {
|
||||
q = &types.TrainingPushLogQuery{}
|
||||
}
|
||||
tx := r.db.WithContext(ctx).Model(&types.TrainingNotification{}).
|
||||
Where("tenant_id = ? AND deleted_at IS NULL", tenantID)
|
||||
if len(scenes) > 0 {
|
||||
tx = tx.Where("scene IN ?", scenes)
|
||||
// scoped 每次返回新鲜会话(Count/统计/取页三次独立执行,避免链式污染)。
|
||||
scoped := func() *gorm.DB {
|
||||
tx := r.db.WithContext(ctx).Model(&types.TrainingNotification{}).
|
||||
Where("tenant_id = ? AND deleted_at IS NULL", tenantID)
|
||||
if len(q.Scenes) > 0 {
|
||||
tx = tx.Where("scene IN ?", q.Scenes)
|
||||
}
|
||||
if q.RefType != "" {
|
||||
tx = tx.Where("ref_type = ?", q.RefType)
|
||||
}
|
||||
if title := strings.TrimSpace(q.Title); title != "" {
|
||||
tx = tx.Where("title LIKE ? ESCAPE '"+likeEscapeChar+"'", "%"+escapeLikeKeyword(title)+"%")
|
||||
}
|
||||
switch q.PushStatus {
|
||||
case "":
|
||||
// 全部状态
|
||||
case "pending":
|
||||
// 排队与投递中合并为一个筛选项,与统计口径一致。
|
||||
tx = tx.Where("push_status IN ?", []string{"pending", "sending"})
|
||||
case "in_site":
|
||||
tx = tx.Where("push_status = ''")
|
||||
default:
|
||||
tx = tx.Where("push_status = ?", q.PushStatus)
|
||||
}
|
||||
if q.StartTime != nil {
|
||||
tx = tx.Where("created_at >= ?", *q.StartTime)
|
||||
}
|
||||
if q.EndTime != nil {
|
||||
tx = tx.Where("created_at <= ?", *q.EndTime)
|
||||
}
|
||||
return tx
|
||||
}
|
||||
if refType != "" {
|
||||
tx = tx.Where("ref_type = ?", refType)
|
||||
|
||||
var total int64
|
||||
if err := scoped().Count(&total).Error; err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
|
||||
// 全量状态统计:一条 GROUP BY 聚合出各状态计数,再归并到六项口径。
|
||||
var statusRows []struct {
|
||||
PushStatus string
|
||||
Cnt int64
|
||||
}
|
||||
if err := scoped().Select("push_status, COUNT(*) AS cnt").Group("push_status").Scan(&statusRows).Error; err != nil {
|
||||
return nil, 0, nil, err
|
||||
}
|
||||
stats := &types.TrainingPushLogStats{}
|
||||
for _, row := range statusRows {
|
||||
switch row.PushStatus {
|
||||
case "":
|
||||
stats.InSite += row.Cnt
|
||||
case "sent":
|
||||
stats.Sent += row.Cnt
|
||||
case "dead":
|
||||
stats.Dead += row.Cnt
|
||||
case "skipped":
|
||||
stats.Skipped += row.Cnt
|
||||
default: // pending / sending
|
||||
stats.Pending += row.Cnt
|
||||
}
|
||||
stats.Total += row.Cnt
|
||||
}
|
||||
|
||||
page, pageSize := q.Page, q.PageSize
|
||||
if page <= 0 {
|
||||
page = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = pushLogPageSizeDefault
|
||||
}
|
||||
if pageSize > pushLogPageSizeMax {
|
||||
pageSize = pushLogPageSizeMax
|
||||
}
|
||||
var items []*types.TrainingNotification
|
||||
err := tx.Order("created_at DESC, id DESC").Limit(limit).Find(&items).Error
|
||||
return items, err
|
||||
err := scoped().Order("created_at DESC, id DESC").
|
||||
Offset((page - 1) * pageSize).Limit(pageSize).Find(&items).Error
|
||||
return items, total, stats, err
|
||||
}
|
||||
|
||||
// ListMonthActiveSessions 返回某年月仍活跃(未取消、未删除)的场次集合,
|
||||
|
||||
@@ -646,7 +646,10 @@ func (r *trainingRepository) EvidenceRows(ctx context.Context, tenantID uint64,
|
||||
COALESCE(lu.username, '') AS lecturer_name,
|
||||
COALESCE(c.name, '') AS course_name,
|
||||
TO_CHAR(s.start_at, 'YYYY-MM-DD HH24:MI') AS session_time,
|
||||
COALESCE(TO_CHAR(er.score, 'FM999'), '') AS exam_score`).
|
||||
COALESCE(TO_CHAR(er.score, 'FM999'), '') AS exam_score,
|
||||
EXISTS (SELECT 1 FROM tr_enrollments en
|
||||
WHERE en.tenant_id = sr.tenant_id AND en.session_id = sr.session_id
|
||||
AND en.member_id = sr.member_id AND en.deleted_at IS NULL) AS in_roster`).
|
||||
Joins("JOIN tr_course_sessions s ON s.id = sr.session_id AND s.tenant_id = sr.tenant_id").
|
||||
Joins("LEFT JOIN tr_courses c ON c.id = s.course_id AND c.tenant_id = sr.tenant_id").
|
||||
Joins("LEFT JOIN org_employees m ON m.id = sr.member_id AND m.tenant_id = sr.tenant_id").
|
||||
|
||||
@@ -881,6 +881,8 @@ func (r *trainingRepository) GetSignRecordByID(ctx context.Context, tenantID uin
|
||||
// ListSignRecords returns all records of a session, earliest first.
|
||||
// 联查员工档案/账号/部门补齐学员展示字段(姓名/工号/部门):签到明细学员列
|
||||
// 不能依赖前端名册全量反查(名册分页截断或未入名册时曾回退显示 member_id)。
|
||||
// in_roster 实时联查 tr_enrollments 标注是否「参加人员」(2026-11-28 规则调整:
|
||||
// 员工档案全员可签到,明细按此标注要求/非要求培训人员)。
|
||||
func (r *trainingRepository) ListSignRecords(ctx context.Context, tenantID uint64, sessionID string) ([]*types.TrainingSignRecord, error) {
|
||||
var items []*types.TrainingSignRecord
|
||||
err := r.db.WithContext(ctx).
|
||||
@@ -892,7 +894,10 @@ func (r *trainingRepository) ListSignRecords(ctx context.Context, tenantID uint6
|
||||
// user_name 取真实姓名(display_name)优先:员工编号型 IdP(Casdoor)的
|
||||
// username 存工号,真实姓名在 display_name(与组织中心列表口径一致)。
|
||||
Select(`sr.*, COALESCE(NULLIF(u.display_name, ''), u.username, '') AS member_name,
|
||||
COALESCE(e.employee_no, '') AS employee_no, COALESCE(dp.name, '') AS dept_name`).
|
||||
COALESCE(e.employee_no, '') AS employee_no, COALESCE(dp.name, '') AS dept_name,
|
||||
EXISTS (SELECT 1 FROM tr_enrollments en
|
||||
WHERE en.tenant_id = sr.tenant_id AND en.session_id = sr.session_id
|
||||
AND en.member_id = sr.member_id AND en.deleted_at IS NULL) AS in_roster`).
|
||||
Order("sr.signed_at ASC").Scan(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
@@ -883,17 +883,17 @@ func (s *trainingService) rotateSignCode(ctx context.Context, tenantID uint64, c
|
||||
}
|
||||
|
||||
// Checkin performs the scan sign-in with rule-based anti-fraud checks:
|
||||
// token/expiry, same-day, roster membership, employee-no match, nonce replay,
|
||||
// token/expiry, same-day, employee-no match, nonce replay,
|
||||
// duplicate scan, late arrival, geo fence.
|
||||
// 一期为确定性规则引擎,不调 LLM;规则参数来自 sign_policy 配置。
|
||||
// 2026-11 企微扫码需求增强:
|
||||
// - 培训当天校验:非当天扫码签到直接友好提示(提前扫/过期补扫分文案);
|
||||
// - 工号严格校验(in.EmployeeNo 非空时):须与登录账号档案工号一致,且必须在
|
||||
// 该场次「参加人员」名单中 —— 名单外直接拒绝(不再仅标记待复核);
|
||||
// - 工号严格校验(in.EmployeeNo 非空时):须与登录账号档案工号一致;
|
||||
// - nonce 防重放 + client_ts 时钟偏差校验;
|
||||
// - 轮换宽限:token 未命中当前码时接受 prev_token 在宽限期内签到。
|
||||
// 既有登录态签到(不带 employee_no/nonce)行为不变:名单外仍标记 not_in_roster
|
||||
// 进待复核,不硬拒。
|
||||
// 2026-11-28 签到规则调整:签到不再限定发布培训计划时所选「参加人员」——
|
||||
// 员工档案(org_employees)全员均可凭工号签到;是否要求培训人员不再拦截/进
|
||||
// 异常待复核,由签到明细按名册实时标注(ListSignRecords 联查 in_roster)。
|
||||
func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID string, in *types.CheckinInput) (*types.TrainingSignRecord, error) {
|
||||
if in == nil || in.Token == "" {
|
||||
return nil, errors.NewBadRequestError("签到码无效")
|
||||
@@ -922,23 +922,10 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID s
|
||||
return nil, errors.NewForbiddenError("未建立培训档案,无法签到")
|
||||
}
|
||||
|
||||
// 名单校验(名单外人员自动标记;严格模式下直接拒绝,见下)。
|
||||
roster, err := s.repo.ListEnrollments(ctx, tenantID, session.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inRoster := false
|
||||
for _, e := range roster {
|
||||
if e.MemberID == member.ID {
|
||||
inRoster = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
|
||||
// 严格扫码链路(employee_no 非空,即 2026-11 新签到页):当天校验/工号+名单
|
||||
// 硬拒/nonce 防重放/时钟偏差仅作用于此链路。既有路径(登录态直签、代录补签)
|
||||
// 严格扫码链路(employee_no 非空,即 2026-11 新签到页):当天校验/工号一致
|
||||
// 校验/nonce 防重放/时钟偏差仅作用于此链路。既有路径(登录态直签、代录补签)
|
||||
// 行为不变 —— 尤其代录补签常发生在非当天,当天校验不得拦截。
|
||||
strictScan := strings.TrimSpace(in.EmployeeNo) != ""
|
||||
|
||||
@@ -953,15 +940,12 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID s
|
||||
return nil, errors.NewBadRequestError("本课程签到已结束,无法补签")
|
||||
}
|
||||
|
||||
// 工号严格校验(2026-11 需求规则 2 + 补充规则):工号须与档案一致,且必须在
|
||||
// 「参加人员」名单中,名单外直接拒绝。
|
||||
// 工号严格校验(2026-11 需求规则 2):工号须与档案一致。2026-11-28 规则调整
|
||||
// 后不再要求在「参加人员」名单中,名单外人员照常签到,由明细标注区分。
|
||||
if strictScan {
|
||||
if !strings.EqualFold(strings.TrimSpace(member.EmployeeNo), strings.TrimSpace(in.EmployeeNo)) {
|
||||
return nil, errors.NewForbiddenError("录入工号与当前账号的档案工号不一致,请确认后重试")
|
||||
}
|
||||
if !inRoster {
|
||||
return nil, errors.NewForbiddenError("您不在该课程的「参加人员」名单中,请联系讲师或人事确认")
|
||||
}
|
||||
}
|
||||
|
||||
// nonce 防重放 + 时钟偏差(仅扫码链路携带;同 nonce 二次提交直接拒绝)。
|
||||
@@ -998,12 +982,6 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID s
|
||||
}
|
||||
}
|
||||
|
||||
// 名单外人员标记(工号严格链路已在上方硬拒,此处仅既有登录态路径可达)。
|
||||
if !inRoster {
|
||||
flags = append(flags, "not_in_roster")
|
||||
status = types.TrainingSignAbnormal
|
||||
}
|
||||
|
||||
// 重复扫码直接拒绝(唯一约束兜底)。
|
||||
if _, err := s.repo.GetSignRecord(ctx, tenantID, session.ID, member.ID); err == nil {
|
||||
return nil, errors.NewBadRequestError("您已签到,请勿重复扫码")
|
||||
@@ -1031,11 +1009,6 @@ func (s *trainingService) Checkin(ctx context.Context, tenantID uint64, userID s
|
||||
if err := s.repo.UpsertSignRecord(ctx, record); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 软性提示:名单外人员进入待复核队列。
|
||||
if !inRoster {
|
||||
_ = s.Notify(ctx, tenantID, userID, "signin_review",
|
||||
"签到待复核", "您的签到因名单校验未通过已进入待复核队列。", "session", session.ID)
|
||||
}
|
||||
return record, nil
|
||||
}
|
||||
|
||||
@@ -1114,14 +1087,19 @@ func (s *trainingService) ExportEvidence(ctx context.Context, tenantID uint64, s
|
||||
f := excelize.NewFile()
|
||||
sheet := "佐证包"
|
||||
f.SetSheetName("Sheet1", sheet)
|
||||
headers := []string{"部门", "工号", "姓名", "签到时间", "讲师姓名", "课程名称", "上课时间", "学员考试成绩"}
|
||||
// 2026-11-28 签到规则调整配套:全员可签到,佐证包标注要求/非要求培训人员。
|
||||
headers := []string{"部门", "工号", "姓名", "签到时间", "讲师姓名", "课程名称", "上课时间", "学员考试成绩", "人员类型"}
|
||||
for i, h := range headers {
|
||||
cell, _ := excelize.CoordinatesToCellName(i+1, 1)
|
||||
_ = f.SetCellValue(sheet, cell, h)
|
||||
}
|
||||
for r, row := range rows {
|
||||
rosterLabel := "非要求培训人员"
|
||||
if row.InRoster {
|
||||
rosterLabel = "要求培训人员"
|
||||
}
|
||||
values := []string{row.DeptName, row.EmployeeNo, row.MemberName, row.SignedAt,
|
||||
row.LecturerName, row.CourseName, row.SessionTime, row.ExamScore}
|
||||
row.LecturerName, row.CourseName, row.SessionTime, row.ExamScore, rosterLabel}
|
||||
for i, v := range values {
|
||||
cell, _ := excelize.CoordinatesToCellName(i+1, r+2)
|
||||
_ = f.SetCellValue(sheet, cell, v)
|
||||
|
||||
@@ -31,11 +31,8 @@ import (
|
||||
|
||||
// 周一讲师汇总提醒的规则键(ruleKeyNotifyWeeklyLecturer* 定义在
|
||||
// training_scheduler.go,同包共享);defaultWeeklyRemindHour 与需求"每周一 09:00"
|
||||
// 一致(北京时间)。pushLogDefaultLimit 推送记录默认拉取条数。
|
||||
const (
|
||||
defaultWeeklyRemindHour = 9
|
||||
pushLogDefaultLimit = 200
|
||||
)
|
||||
// 一致(北京时间)。
|
||||
const defaultWeeklyRemindHour = 9
|
||||
|
||||
// beijingZone 需求口径统一 Asia/Shanghai(周次/发送时点不随服务器时区漂移)。
|
||||
var beijingZone = time.FixedZone("Asia/Shanghai", 8*3600)
|
||||
@@ -642,15 +639,13 @@ func wecomDispatchContent(row *claimedNotification) string {
|
||||
// 推送记录(管理台逐人明细)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ListPushLogs 管理端逐人推送明细:tr_notifications 按场景/引用过滤后映射
|
||||
// 收件人姓名与工号(org_employees 联 users 展示名)。
|
||||
// ListPushLogs 管理端逐人推送明细:tr_notifications 按时间/标题/企微状态/
|
||||
// 场景过滤后分页取数,映射收件人姓名与工号(org_employees 联 users 展示名),
|
||||
// 连同命中总数与全量状态统计一起组装分页结果。
|
||||
func (s *trainingService) ListPushLogs(
|
||||
ctx context.Context, tenantID uint64, scenes []string, refType string, limit int,
|
||||
) ([]*types.TrainingPushLog, error) {
|
||||
if limit <= 0 {
|
||||
limit = pushLogDefaultLimit
|
||||
}
|
||||
rows, err := s.repo.ListPushLogs(ctx, tenantID, scenes, refType, limit)
|
||||
ctx context.Context, tenantID uint64, q *types.TrainingPushLogQuery,
|
||||
) (*types.TrainingPushLogPage, error) {
|
||||
rows, total, stats, err := s.repo.ListPushLogs(ctx, tenantID, q)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -685,5 +680,5 @@ func (s *trainingService) ListPushLogs(
|
||||
}
|
||||
out = append(out, item)
|
||||
}
|
||||
return out, nil
|
||||
return &types.TrainingPushLogPage{Items: out, Total: total, Stats: stats}, nil
|
||||
}
|
||||
@@ -29,7 +29,9 @@ func (s *trainingService) CheckinFor(ctx context.Context, tenantID uint64, opera
|
||||
return s.Checkin(ctx, tenantID, operatorUserID, &types.CheckinInput{Token: token, Lat: lat, Lng: lng, Channel: channel})
|
||||
}
|
||||
// 复用 Checkin 的规则引擎,以目标成员的 user_id 走完整校验。
|
||||
// 代录不携工号/nonce:工号严格名单硬拒与防重放仅约束学员扫码链路。
|
||||
// 代录不携工号/nonce:工号严格校验与防重放仅约束学员扫码链路。
|
||||
// 2026-11-28 规则调整:名单外成员补录照常签到(明细标注非要求培训人员),
|
||||
// 不再进异常待复核。
|
||||
record, err := s.Checkin(ctx, tenantID, target.UserID, &types.CheckinInput{Token: token, Lat: lat, Lng: lng, Channel: "proxy"})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -262,8 +262,9 @@ func (s *trainingService) buildWecomCheckinPage(ctx context.Context, code *types
|
||||
// 当天性判定与 Checkin 的 same-day 规则同一口径(北京时间)。
|
||||
CanSignToday: session.StartAt != nil && sameBeijingDay(time.Now(), *session.StartAt),
|
||||
}
|
||||
// 名单/重复标记:前端提前渲染按钮态(置灰/已签到),提交时后端仍强校验兜底。
|
||||
// 匿名模式(member 为 nil,工号未输入)无从判定,交给提交时强校验。
|
||||
// 名单/重复标记:前端提前渲染提示(2026-11-28 规则调整后名单外照常可签到,
|
||||
// InRoster 仅作「非要求培训人员」提示,不再置灰拦截);提交时后端仍兜底校验。
|
||||
// 匿名模式(member 为 nil,工号未输入)无从判定,交给提交时校验。
|
||||
if member != nil {
|
||||
if roster, rerr := s.repo.ListEnrollments(ctx, code.TenantID, session.ID); rerr == nil {
|
||||
for _, e := range roster {
|
||||
@@ -308,8 +309,9 @@ func (s *trainingService) WecomCheckinPageAnon(ctx context.Context, token, emplo
|
||||
|
||||
// WecomCheckinByEmployee implements interfaces.TrainingService.
|
||||
// POST /training/wecom/checkin-by-employee — 工号扫码签到:
|
||||
// 签到码(token/轮换宽限)→ 工号反查档案 → 复用 Checkin 规则引擎(当天性/
|
||||
// 名单硬拒/围栏/迟到/重复/nonce 防重放)→ 按规则开关签发会话(自动登录)。
|
||||
// 签到码(token/轮换宽限)→ 工号反查档案(员工档案全员可签,2026-11-28 规则
|
||||
// 调整后不再限定「参加人员」名单)→ 复用 Checkin 规则引擎(当天性/工号一致/
|
||||
// 围栏/迟到/重复/nonce 防重放)→ 按规则开关签发会话(自动登录)。
|
||||
func (s *trainingService) WecomCheckinByEmployee(ctx context.Context, token, employeeNo string, lat, lng float64, nonce string, clientTS int64) (*types.WecomCheckinByEmployee, error) {
|
||||
token = strings.TrimSpace(token)
|
||||
employeeNo = strings.TrimSpace(employeeNo)
|
||||
@@ -327,7 +329,7 @@ func (s *trainingService) WecomCheckinByEmployee(ctx context.Context, token, emp
|
||||
if err != nil || member == nil {
|
||||
return nil, errors.NewNotFoundError("工号 " + employeeNo + " 不在培训档案中,请核对后重试或联系人事")
|
||||
}
|
||||
// 复用 Checkin 规则引擎:工号严格链路(employee_no 非空)当天性/名单硬拒/
|
||||
// 复用 Checkin 规则引擎:工号严格链路(employee_no 非空)当天性/工号一致/
|
||||
// nonce/时钟偏差全部生效;档案由工号反查而来,工号一致性天然成立。
|
||||
record, err := s.Checkin(ctx, code.TenantID, member.UserID, &types.CheckinInput{
|
||||
Token: token,
|
||||
|
||||
Reference in new issue
Block a user