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 返回某年月仍活跃(未取消、未删除)的场次集合,
|
||||
|
||||
Reference in new issue
Block a user