目的:为和信智汇学院有重要通知时可随时向用户手动推送重要信息使用 推送内容:标题、内容、落地页(均可自定义配置),默认值标题为“和信智汇学院”、内容为“重要通知”、落地页默认系统登录页如果已登录默认登录后系统页面 推送人员:默认全员通知,可根据部门、职位、具体人员进行自定义人员 推送时间:可立即推送也可设定定时推送!
This commit is contained in:
1 parent
78e9787793
commit
61fb94744a
14 files changed
+1451
-2
No files matched your search
@@ -0,0 +1,209 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"academy/internal/types"
|
||||
)
|
||||
|
||||
// 手动推送任务数据访问([2026-11 需求] 配置中心 · 推送管理「手动推送」)。
|
||||
// 任务表 tr_push_tasks 只存推送意图,扇出到 tr_notifications 由 service 层
|
||||
// 完成;到期认领用 compare-and-set(pending→sending),多实例安全且 SQLite
|
||||
// 仓库测试可跑(不走 SKIP LOCKED);sending 超时回收由调度器按
|
||||
// sendingReclaimAfter 统一处理。
|
||||
|
||||
// CreatePushTask 落一条推送意图(立即/定时共用,立即任务落库即扇出后置 sent)。
|
||||
func (r *trainingRepository) CreatePushTask(ctx context.Context, task *types.TrainingPushTask) error {
|
||||
return r.db.WithContext(ctx).Create(task).Error
|
||||
}
|
||||
|
||||
// GetPushTask 取租户内单条任务。
|
||||
func (r *trainingRepository) GetPushTask(ctx context.Context, tenantID uint64, id string) (*types.TrainingPushTask, error) {
|
||||
var task types.TrainingPushTask
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND id = ?", tenantID, id).
|
||||
First(&task).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &task, nil
|
||||
}
|
||||
|
||||
// ListPushTasksByStatus 按状态取任务(发送时间升序,待发送定时任务展示)。
|
||||
func (r *trainingRepository) ListPushTasksByStatus(ctx context.Context, tenantID uint64, status string, limit int) ([]*types.TrainingPushTask, error) {
|
||||
if limit <= 0 || limit > 100 {
|
||||
limit = 100
|
||||
}
|
||||
var items []*types.TrainingPushTask
|
||||
err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND status = ? AND deleted_at IS NULL", tenantID, status).
|
||||
Order("send_at ASC, id ASC").Limit(limit).
|
||||
Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
// ClaimDuePushTasks 扫描到期任务(send_at<=now,跨租户)并逐条 CAS 认领:
|
||||
// UPDATE ... WHERE status='pending' 影响 0 行 = 已被其他实例抢走,跳过。
|
||||
func (r *trainingRepository) ClaimDuePushTasks(ctx context.Context, now time.Time, limit int) ([]*types.TrainingPushTask, error) {
|
||||
if limit <= 0 {
|
||||
limit = 50
|
||||
}
|
||||
var due []*types.TrainingPushTask
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("status = ? AND send_at <= ? AND deleted_at IS NULL", types.PushTaskStatusPending, now).
|
||||
Order("send_at ASC").Limit(limit).
|
||||
Find(&due).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
claimed := make([]*types.TrainingPushTask, 0, len(due))
|
||||
for _, task := range due {
|
||||
res := r.db.WithContext(ctx).Model(&types.TrainingPushTask{}).
|
||||
Where("id = ? AND status = ?", task.ID, types.PushTaskStatusPending).
|
||||
Updates(map[string]interface{}{
|
||||
"status": types.PushTaskStatusSending,
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
if res.Error != nil {
|
||||
return claimed, res.Error
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
continue // 另一实例已认领
|
||||
}
|
||||
claimed = append(claimed, task)
|
||||
}
|
||||
return claimed, nil
|
||||
}
|
||||
|
||||
// MarkPushTaskSent 记录扇出完成(仅 sending 态可落,防并发双写)。
|
||||
func (r *trainingRepository) MarkPushTaskSent(ctx context.Context, id string, sentCount int, sentAt time.Time) error {
|
||||
return r.db.WithContext(ctx).Model(&types.TrainingPushTask{}).
|
||||
Where("id = ? AND status = ?", id, types.PushTaskStatusSending).
|
||||
Updates(map[string]interface{}{
|
||||
"status": types.PushTaskStatusSent,
|
||||
"sent_count": sentCount,
|
||||
"sent_at": sentAt,
|
||||
"updated_at": time.Now(),
|
||||
}).Error
|
||||
}
|
||||
|
||||
// RequeueStalePushTasks 把 sending 超时的任务(实例扇出中途崩溃)放回 pending
|
||||
// 重投;通知行按 (target,scene,ref_id) 唯一索引幂等,重跑只补漏不重发。
|
||||
func (r *trainingRepository) RequeueStalePushTasks(ctx context.Context, staleBefore time.Time) (int64, error) {
|
||||
res := r.db.WithContext(ctx).Model(&types.TrainingPushTask{}).
|
||||
Where("status = ? AND updated_at < ?", types.PushTaskStatusSending, staleBefore).
|
||||
Updates(map[string]interface{}{
|
||||
"status": types.PushTaskStatusPending,
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
return res.RowsAffected, res.Error
|
||||
}
|
||||
|
||||
// RequeuePushTask 把单条 sending 任务放回 pending(扇出瞬时失败,调度器下轮重试)。
|
||||
func (r *trainingRepository) RequeuePushTask(ctx context.Context, id string) error {
|
||||
return r.db.WithContext(ctx).Model(&types.TrainingPushTask{}).
|
||||
Where("id = ? AND status = ?", id, types.PushTaskStatusSending).
|
||||
Updates(map[string]interface{}{
|
||||
"status": types.PushTaskStatusPending,
|
||||
"updated_at": time.Now(),
|
||||
}).Error
|
||||
}
|
||||
|
||||
// CancelPushTask 取消待发送任务;返回是否取消成功(已发送/已认领不可取消)。
|
||||
func (r *trainingRepository) CancelPushTask(ctx context.Context, tenantID uint64, id string) (bool, error) {
|
||||
res := r.db.WithContext(ctx).Model(&types.TrainingPushTask{}).
|
||||
Where("tenant_id = ? AND id = ? AND status = ? AND deleted_at IS NULL",
|
||||
tenantID, id, types.PushTaskStatusPending).
|
||||
Updates(map[string]interface{}{
|
||||
"status": types.PushTaskStatusCancelled,
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
return res.Error == nil && res.RowsAffected > 0, res.Error
|
||||
}
|
||||
|
||||
// ListManualPushAudience 解析手动推送人员名单(user_id 去重,保持稳定顺序):
|
||||
// - all:空间全部 active 成员(users JOIN tenant_members,ListTenantUsers 同口径,
|
||||
// 含未入员工名册的账号,站内通知可达;企微投递时无绑定者自然 skipped)
|
||||
// - dept / post:org_employees 按 dept_id / post 过滤(active 档案)
|
||||
// - user:直接采用指定名单(仍按空间成员口径过滤,防越租户投递)
|
||||
func (r *trainingRepository) ListManualPushAudience(
|
||||
ctx context.Context, tenantID uint64, mode string, deptIDs, posts, userIDs []string,
|
||||
) ([]string, error) {
|
||||
clean := func(list []string) []string {
|
||||
out := make([]string, 0, len(list))
|
||||
seen := map[string]bool{}
|
||||
for _, id := range list {
|
||||
if id == "" || seen[id] {
|
||||
continue
|
||||
}
|
||||
seen[id] = true
|
||||
out = append(out, id)
|
||||
}
|
||||
return out
|
||||
}
|
||||
switch mode {
|
||||
case types.PushTaskAudienceDept, types.PushTaskAudiencePost:
|
||||
ids := clean(deptIDs)
|
||||
column, values := "dept_id", ids
|
||||
if mode == types.PushTaskAudiencePost {
|
||||
column, values = "post", clean(posts)
|
||||
}
|
||||
if len(values) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var rows []types.OrgEmployee
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("tenant_id = ? AND "+column+" IN ? AND status = 'active' AND user_id <> '' AND deleted_at IS NULL",
|
||||
tenantID, values).
|
||||
Find(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]string, 0, len(rows))
|
||||
seen := map[string]bool{}
|
||||
for _, m := range rows {
|
||||
if seen[m.UserID] {
|
||||
continue
|
||||
}
|
||||
seen[m.UserID] = true
|
||||
out = append(out, m.UserID)
|
||||
}
|
||||
return out, nil
|
||||
case types.PushTaskAudienceUser:
|
||||
ids := clean(userIDs)
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
// 指定名单仍限定在本空间成员内(与全员同口径),避免误投其他空间用户。
|
||||
var rows []struct{ ID string }
|
||||
if err := r.db.WithContext(ctx).Table("users u").
|
||||
Select("u.id").
|
||||
Joins("JOIN tenant_members tm ON tm.user_id = u.id AND tm.tenant_id = ? "+
|
||||
"AND tm.status = 'active' AND tm.deleted_at IS NULL", tenantID).
|
||||
Where("u.id IN ? AND u.is_active = TRUE AND u.deleted_at IS NULL", ids).
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return clean(func() []string {
|
||||
out := make([]string, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, row.ID)
|
||||
}
|
||||
return out
|
||||
}()), nil
|
||||
default: // all
|
||||
var rows []struct{ ID string }
|
||||
if err := r.db.WithContext(ctx).Table("users u").
|
||||
Select("u.id").
|
||||
Joins("JOIN tenant_members tm ON tm.user_id = u.id AND tm.tenant_id = ? "+
|
||||
"AND tm.status = 'active' AND tm.deleted_at IS NULL", tenantID).
|
||||
Where("u.is_active = TRUE AND u.deleted_at IS NULL").
|
||||
Order("u.created_at ASC").
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]string, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, row.ID)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user