Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Has been cancelled
- 新增六对成对迁移 000232–000237:H5 弹窗类型、退款结算标识与申请人备注、优先轮询事实字段与两个新终态、通道阈值命中留痕、手机号最近解绑人、提现资格校验留痕 - 退款:原因必填与申请人备注、来源支付与渠道流水冻结、线下处理流水号补录审计、按订单查询可选退款方式、企微审批材料补齐且新增字段缺失映射即明确失败 - 优先轮询:人工关闭、有效期到期独立周期任务、失败与过期人工重触发、事实字段与异常重试查询、资产解析端点只读投影 - 通道阈值:命中事实同事务留痕与命中记录查询;员工账单:列表筛选与详情投影;商户池:列表投影与统计周期语义;H5:弹窗类型与类别排序 - 手机号:有效关联数量与最近解绑人、短信验证码失败次数限制;导出:佣金明细十五列与报表序号列 - 时间筛选:三处新增筛选纳入统一严格解析契约,员工账单产生时间参数改名 - 同步 12 份主 Spec 需求、两端点与异步任务证据链,门禁 context-health 与 OpenSpec 校验通过
129 lines
4.6 KiB
Go
129 lines
4.6 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,
|
|
PopupType: record.PopupType,
|
|
PopupTypeText: constants.GetH5PopupTypeName(record.PopupType),
|
|
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
|
|
}
|