All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m23s
新增 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)与参数校验中文提示共用实现。
127 lines
4.5 KiB
Go
127 lines
4.5 KiB
Go
// Package h5popup 提供 H5 运营弹窗配置的后台只读投影。
|
|
// 该投影只读,不修改任何状态;启停与版本递增由 application 层写用例完成。
|
|
package h5popup
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
|
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
)
|
|
|
|
// Query 提供 H5 运营弹窗配置的后台列表与详情查询。
|
|
type Query struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewQuery 创建 H5 运营弹窗配置查询。
|
|
func NewQuery(db *gorm.DB) *Query {
|
|
return &Query{db: db}
|
|
}
|
|
|
|
// List 按最近更新时间倒序分页查询运营弹窗配置。
|
|
func (q *Query) List(ctx context.Context, request dto.H5PopupConfigurationListRequest) (*dto.H5PopupConfigurationListResponse, error) {
|
|
page, pageSize, offset, err := normalizePagination(request.Page, request.PageSize)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
base := q.db.WithContext(ctx).Model(&model.H5PopupConfiguration{})
|
|
if request.Enabled != nil {
|
|
status := constants.H5PopupStatusDisabled
|
|
if *request.Enabled {
|
|
status = constants.H5PopupStatusEnabled
|
|
}
|
|
base = base.Where("enabled = ?", status)
|
|
}
|
|
var total int64
|
|
if err := base.Count(&total).Error; err != nil {
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置总数失败")
|
|
}
|
|
var records []model.H5PopupConfiguration
|
|
if err := base.Order("updated_at DESC, id DESC").Offset(offset).Limit(pageSize).Find(&records).Error; err != nil {
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置列表失败")
|
|
}
|
|
items := make([]dto.H5PopupConfigurationResponse, 0, len(records))
|
|
for index := range records {
|
|
items = append(items, *toConfigurationResponse(&records[index]))
|
|
}
|
|
return &dto.H5PopupConfigurationListResponse{Items: items, Total: total, Page: page, Size: pageSize}, nil
|
|
}
|
|
|
|
// Get 按 ID 查询运营弹窗配置详情。
|
|
func (q *Query) Get(ctx context.Context, id uint) (*dto.H5PopupConfigurationResponse, error) {
|
|
if id == 0 {
|
|
return nil, errors.New(errors.CodeH5PopupConfigurationNotFound)
|
|
}
|
|
var record model.H5PopupConfiguration
|
|
if err := q.db.WithContext(ctx).Where("id = ?", id).Take(&record).Error; err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, errors.New(errors.CodeH5PopupConfigurationNotFound)
|
|
}
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置失败")
|
|
}
|
|
return toConfigurationResponse(&record), nil
|
|
}
|
|
|
|
// normalizePagination 归一化页码与每页数量并返回偏移量。
|
|
func normalizePagination(page, pageSize int) (int, int, int, error) {
|
|
if page <= 0 {
|
|
page = 1
|
|
}
|
|
if page > constants.NotificationMaxPage {
|
|
return 0, 0, 0, errors.New(errors.CodeInvalidParam, "页码超出允许范围")
|
|
}
|
|
if pageSize <= 0 {
|
|
pageSize = constants.DefaultPageSize
|
|
}
|
|
if pageSize > constants.MaxPageSize {
|
|
return 0, 0, 0, errors.New(errors.CodeInvalidParam, "每页数量超出允许范围")
|
|
}
|
|
return page, pageSize, (page - 1) * pageSize, nil
|
|
}
|
|
|
|
// toConfigurationResponse 将配置投影为对外响应,范围维度空数组表示全量。
|
|
func toConfigurationResponse(record *model.H5PopupConfiguration) *dto.H5PopupConfigurationResponse {
|
|
if record == nil {
|
|
return nil
|
|
}
|
|
return &dto.H5PopupConfigurationResponse{
|
|
ID: record.ID, Title: record.Title, Content: record.Content,
|
|
Pages: append([]string{}, record.Pages...),
|
|
ShopIDs: toShopIDs(record.ShopIDs),
|
|
DeviceTypes: append([]string{}, record.DeviceTypes...),
|
|
CardTypes: append([]string{}, record.CardTypes...),
|
|
Priority: record.Priority,
|
|
Frequency: record.Frequency,
|
|
FrequencyText: constants.GetH5PopupFrequencyName(record.Frequency),
|
|
Enabled: record.Enabled == constants.H5PopupStatusEnabled,
|
|
EnabledText: constants.GetH5PopupEnabledName(record.Enabled),
|
|
ActionType: record.ActionType,
|
|
StartsAt: record.StartsAt,
|
|
EndsAt: record.EndsAt,
|
|
Version: record.Version,
|
|
CreatedAt: record.CreatedAt,
|
|
UpdatedAt: record.UpdatedAt,
|
|
Creator: record.Creator,
|
|
Updater: record.Updater,
|
|
}
|
|
}
|
|
|
|
// toShopIDs 将 JSONB 文本数组还原为店铺 ID 列表;非法项在写入侧已被拒绝,这里按跳过处理保证读取可用。
|
|
func toShopIDs(values model.StringJSONBArray) []uint {
|
|
shopIDs := make([]uint, 0, len(values))
|
|
for _, value := range values {
|
|
parsed, err := strconv.ParseUint(value, 10, 64)
|
|
if err != nil || parsed == 0 {
|
|
continue
|
|
}
|
|
shopIDs = append(shopIDs, uint(parsed))
|
|
}
|
|
return shopIDs
|
|
}
|