176 lines
6.2 KiB
Go
176 lines
6.2 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, "查询运营弹窗配置列表失败")
|
|
}
|
|
accountNames, err := q.loadAccountNames(ctx, accountIDs(records))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items := make([]dto.H5PopupConfigurationResponse, 0, len(records))
|
|
for index := range records {
|
|
items = append(items, *toConfigurationResponse(&records[index], accountNames))
|
|
}
|
|
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, "查询运营弹窗配置失败")
|
|
}
|
|
accountNames, err := q.loadAccountNames(ctx, []uint{record.Creator, record.Updater})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toConfigurationResponse(&record, accountNames), 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
|
|
}
|
|
|
|
// accountIDs 收集配置列表中的创建人与更新人账号 ID。
|
|
func accountIDs(records []model.H5PopupConfiguration) []uint {
|
|
ids := make([]uint, 0, len(records)*2)
|
|
for index := range records {
|
|
ids = append(ids, records[index].Creator, records[index].Updater)
|
|
}
|
|
return ids
|
|
}
|
|
|
|
// loadAccountNames 批量读取配置关联账号名称,缺失账号留空。
|
|
func (q *Query) loadAccountNames(ctx context.Context, ids []uint) (map[uint]string, error) {
|
|
uniqueIDs := make([]uint, 0, len(ids))
|
|
seen := make(map[uint]struct{}, len(ids))
|
|
for _, id := range ids {
|
|
if id == 0 {
|
|
continue
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
continue
|
|
}
|
|
seen[id] = struct{}{}
|
|
uniqueIDs = append(uniqueIDs, id)
|
|
}
|
|
names := make(map[uint]string, len(uniqueIDs))
|
|
if len(uniqueIDs) == 0 {
|
|
return names, nil
|
|
}
|
|
var accounts []model.Account
|
|
if err := q.db.WithContext(ctx).Unscoped().Model(&model.Account{}).Select("id", "username").Where("id IN ?", uniqueIDs).Find(&accounts).Error; err != nil {
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询运营弹窗配置关联账号失败")
|
|
}
|
|
for index := range accounts {
|
|
names[accounts[index].ID] = accounts[index].Username
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
// toConfigurationResponse 将配置投影为对外响应,范围维度空数组表示全量。
|
|
func toConfigurationResponse(record *model.H5PopupConfiguration, accountNames map[uint]string) *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,
|
|
CreatorName: accountNames[record.Creator],
|
|
Updater: record.Updater,
|
|
UpdaterName: accountNames[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
|
|
}
|