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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user