Files
junhong_cmp_fiber/internal/infrastructure/notification/repository.go
break 333ba4b647
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m23s
feat(H5弹窗): AUG26-007 风险换卡与运营弹窗投放通知
新增 000225 迁移:运营弹窗配置表 tb_h5_popup_configuration(页面/范围/优先级/频率/受控动作/启停/有效期/版本)
与 tb_notification 可空 JSONB 列 popup_snapshot。

新增通知直建窄接口 DirectWriter.CreateOrGetPersonal:与 Outbox 消费共用 prepareDelivery 的渲染、
展示期与 CreateIdempotent 规则,冲突时回查返回既有行;同步扩展个人通知查询与已读两处类型白名单,
并按个人客户入口补齐投递审计来源。

新增 H5 候选与风险换卡:GET /api/c/v1/popup-candidates 先判风险资格(广电卡 + 风险停机 +
无活动物流换货单),命中只返回风险候选;未命中再按时间/启停/页面/店铺/设备类型/卡类型范围/频率
匹配运营配置。POST /api/c/v1/risk-exchanges/:asset_id/address 锁资产行后幂等创建待发货物流换货单,
首次地址锁定,不沿用资产级群发通知。

新增后台运营弹窗配置 CRUD 与启停(仅超级管理员与平台账号),更新递增版本并刷新最近更新时间,
标题与正文统一拒绝 URL 与前端路由,全部写操作记录操作者、前后值、版本与时间。

同步 OpenAPI(cmd/gendocs、cmd/api/docs.go、pkg/openapi/handlers.go)与参数校验中文提示共用实现。
2026-09-15 15:23:52 +08:00

66 lines
2.4 KiB
Go

package notification
import (
"context"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"github.com/break/junhong_cmp_fiber/internal/model"
"github.com/break/junhong_cmp_fiber/pkg/constants"
)
// Repository 提供站内通知幂等写入能力。
type Repository struct {
db *gorm.DB
}
// NewRepository 创建站内通知持久化 Adapter。
func NewRepository(db *gorm.DB) *Repository {
return &Repository{db: db}
}
// DB 返回通知 Repository 使用的数据库连接。
func (r *Repository) DB() *gorm.DB { return r.db }
// WithTx 返回绑定指定事务的通知 Repository。
func (r *Repository) WithTx(tx *gorm.DB) *Repository { return &Repository{db: tx} }
// CreateIdempotent 以事件、接收人类型和接收人 ID 唯一键幂等写入通知。
func (r *Repository) CreateIdempotent(ctx context.Context, notification *model.Notification) (bool, error) {
result := r.db.WithContext(ctx).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "event_id"}, {Name: "recipient_kind"}, {Name: "recipient_id"}},
DoNothing: true,
}).Create(notification)
return result.RowsAffected == 1, result.Error
}
// FindByEventRecipient 按事件键与接收人回查既有通知,用于幂等冲突时返回既有行而不是重复投放。
func (r *Repository) FindByEventRecipient(ctx context.Context, eventID, recipientKind string, recipientID uint) (*model.Notification, error) {
var notification model.Notification
if err := r.db.WithContext(ctx).
Where("event_id = ? AND recipient_kind = ? AND recipient_id = ?", eventID, recipientKind, recipientID).
Take(&notification).Error; err != nil {
return nil, err
}
return &notification, nil
}
// IsActiveAccount 判断明确后台账号是否仍启用且未软删除。
func (r *Repository) IsActiveAccount(ctx context.Context, accountID uint) (bool, error) {
var count int64
err := r.db.WithContext(ctx).Model(&model.Account{}).
Where("id = ? AND status = ?", accountID, constants.StatusEnabled).
Count(&count).Error
return count == 1, err
}
// IsActivePersonalCustomer 判断明确个人客户是否仍启用且未软删除。
func (r *Repository) IsActivePersonalCustomer(ctx context.Context, customerID uint) (bool, error) {
var count int64
err := r.db.WithContext(ctx).Model(&model.PersonalCustomer{}).
Where("id = ? AND status = ?", customerID, constants.StatusEnabled).
Count(&count).Error
return count == 1, err
}