feat(导出时间筛选): AUG26-014 统一时间筛选与临期导出,归档并同步主 Spec 与证据链
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 13m40s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 13m40s
统一时间筛选:新增共享严格解析器 pkg/utils/time_range.go,只接受带显式时区的 RFC3339 秒级时间(拒绝小数秒、无时区、date-only、空格分隔、±hhmm、未补零、非法日期与越界偏移),闭区间含两端、归一为 UTC 瞬时,创建期与执行期共用同一份实现。 端点改造(13 个入口):IoT 卡导入任务、设备导入任务、导出任务列表、订单列表参数名不变仅收紧解析;换货、分配记录、代理充值、临期列表改名 start_time/end_time(旧参数名显式拒绝);提现记录两处删除解析失败静默跳过,非法参数一律 1001;授权记录由起始闭结束开改为闭区间含两端;临期列表改按当前生效主套餐最终到期时刻比较,保留剩余天数上下限与既有粗放窗口。 临期导出新建:新场景 expiring_asset 与受控入口 POST /api/admin/expiring-assets/export,复用列表候选预筛与最终到期推算,一行一资产、加油包不单独成行,列序与 111 §18.1 逐列一致,店铺/业务员/用户组按执行时当前归属补充且不超出创建时冻结范围。 佣金明细导出新增按创建时间闭区间筛选(原佣金与回溯两条分支各自创建时间列),记录粒度、列定义与余额口径不变。 冻结与遗留任务:创建期把筛选与时间边界规范化为 UTC RFC3339 秒级串写入既有 query_json,无新列无迁移;执行期只按冻结值严格解析,非法冻结值在任何分片与文件动作前落任务失败并写安全摘要,不放行全量;重试沿用原快照。达量预警导出执行期同样纳入严格解析(其入口契约、列定义与触发快照口径不变)。 归档 add-export-time-filter-standards 并新建主 Spec openspec/specs/export-time-filter/spec.md,同步 requirement-evidence.json 与入口能力矩阵,README 导出场景清单更新为 11 个场景。 验证:junhong_cmp_test + 本地隔离 Redis(DB7,测试部署共享队列 DB6 未被占用)实跑 85 PASS / 0 FAIL(接受/拒绝集合、区间与顺序语义、列表与导出同筛选行集一致、代理 HTTP 全链路与范围冻结、遗留旧格式任务安全失败、列与余额口径回归、表头逐字),门禁 gofmt/go build/gendocs 两次一致/openspec validate/doctor/context-health 全绿;无 Schema 变更、无迁移、无运行时开关。
This commit is contained in:
229
internal/exporter/expiring_asset_scene.go
Normal file
229
internal/exporter/expiring_asset_scene.go
Normal file
@@ -0,0 +1,229 @@
|
||||
package exporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
packageexpiryquery "github.com/break/junhong_cmp_fiber/internal/query/packageexpiry"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
)
|
||||
|
||||
// ExpiringAssetDataSource 临期资产导出数据源。
|
||||
//
|
||||
// 粒度为一行对应一项资产,取该资产当前生效主套餐的最终到期时间与剩余天数,加油包不单独成行。
|
||||
// 候选预筛、最终到期推算与行序全部复用 internal/query/packageexpiry 的列表实现,不另写第二套到期口径。
|
||||
// 店铺、业务员与用户组按导出执行时的当前归属补充,结果仍受任务创建时冻结的可见店铺范围约束。
|
||||
type ExpiringAssetDataSource struct {
|
||||
db *gorm.DB
|
||||
expiryQ *packageexpiryquery.Query
|
||||
}
|
||||
|
||||
// NewExpiringAssetDataSource 创建临期资产导出数据源。
|
||||
func NewExpiringAssetDataSource(db *gorm.DB) *ExpiringAssetDataSource {
|
||||
return &ExpiringAssetDataSource{db: db, expiryQ: packageexpiryquery.NewQuery(db)}
|
||||
}
|
||||
|
||||
// Scene 返回导出场景编码。
|
||||
func (s *ExpiringAssetDataSource) Scene() string {
|
||||
return constants.ExportTaskSceneExpiringAsset
|
||||
}
|
||||
|
||||
// Count 统计临期资产导出行数,与列表同一筛选下的行集合一致。
|
||||
func (s *ExpiringAssetDataSource) Count(ctx context.Context, params ExportParams) (int, error) {
|
||||
items, err := s.items(ctx, params)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return len(items), nil
|
||||
}
|
||||
|
||||
// Headers 返回临期资产导出表头,列序与 111.md §18.1 逐列一致。
|
||||
func (s *ExpiringAssetDataSource) Headers(context.Context, ExportParams) ([]string, error) {
|
||||
return []string{
|
||||
"店铺", "业务员", "用户组", "资产类型", "设备类型", "设备型号", "资产标识", "当前套餐", "到期时间", "剩余天数",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Fetch 按 offset/limit 查询临期资产导出数据。
|
||||
func (s *ExpiringAssetDataSource) Fetch(ctx context.Context, params ExportParams, offset, limit int) ([][]string, error) {
|
||||
if limit <= 0 {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
items, err := s.items(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if offset >= len(items) {
|
||||
return [][]string{}, nil
|
||||
}
|
||||
end := offset + limit
|
||||
if end > len(items) {
|
||||
end = len(items)
|
||||
}
|
||||
page := items[offset:end]
|
||||
|
||||
shopIDs := make([]uint, 0, len(page))
|
||||
deviceIDs := make([]uint, 0, len(page))
|
||||
for _, item := range page {
|
||||
if item.ShopID != nil {
|
||||
shopIDs = append(shopIDs, *item.ShopID)
|
||||
}
|
||||
if item.AssetType == constants.AssetTypeDevice {
|
||||
deviceIDs = append(deviceIDs, item.AssetID)
|
||||
}
|
||||
}
|
||||
shops, err := s.loadShopOwners(ctx, normalizeUintSlice(shopIDs))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ownerIDs := make([]uint, 0, len(shops))
|
||||
for _, shop := range shops {
|
||||
if shop.BusinessOwnerAccountID != nil {
|
||||
ownerIDs = append(ownerIDs, *shop.BusinessOwnerAccountID)
|
||||
}
|
||||
}
|
||||
ownerIDs = normalizeUintSlice(ownerIDs)
|
||||
ownerNames, err := loadAccountNames(ctx, s.db, ownerIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
groupNames, err := businessUserGroupNames(ctx, s.db, ownerIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
devices, err := s.loadDeviceAttributes(ctx, normalizeUintSlice(deviceIDs))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows := make([][]string, 0, len(page))
|
||||
for _, item := range page {
|
||||
var shopName, ownerName, groupName string
|
||||
if item.ShopID != nil {
|
||||
shop := shops[*item.ShopID]
|
||||
shopName = shop.ShopName
|
||||
if shop.BusinessOwnerAccountID != nil {
|
||||
ownerName = ownerNames[*shop.BusinessOwnerAccountID]
|
||||
groupName = currentOwnerGroupName(groupNames, shop.BusinessOwnerAccountID)
|
||||
}
|
||||
}
|
||||
var deviceType, deviceModel string
|
||||
if item.AssetType == constants.AssetTypeDevice {
|
||||
device := devices[item.AssetID]
|
||||
deviceType, deviceModel = device.DeviceType, device.DeviceModel
|
||||
}
|
||||
rows = append(rows, []string{
|
||||
shopName,
|
||||
ownerName,
|
||||
groupName,
|
||||
assetTypeName(item.AssetType),
|
||||
deviceType,
|
||||
deviceModel,
|
||||
item.Identifier,
|
||||
item.PackageName,
|
||||
formatOptionalTime(item.EstimatedFinalExpiresAt),
|
||||
formatRemainingDaysValue(item.DaysUntilFinalExpiry),
|
||||
})
|
||||
}
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// items 复用列表同一候选预筛与最终到期推算,数据范围来自任务创建时冻结的可见店铺范围。
|
||||
func (s *ExpiringAssetDataSource) items(ctx context.Context, params ExportParams) ([]dto.ExpiringAssetItem, error) {
|
||||
filter, err := s.listFilter(params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
scope := func(query *gorm.DB) *gorm.DB {
|
||||
return applyExportShopScope(query, params, "shop_id")
|
||||
}
|
||||
return s.expiryQ.ListAllWithScope(ctx, scope, filter)
|
||||
}
|
||||
|
||||
// listFilter 把任务冻结的筛选快照转换为列表同一口径的筛选条件。
|
||||
func (s *ExpiringAssetDataSource) listFilter(params ExportParams) (packageexpiryquery.ListFilter, error) {
|
||||
filter := packageexpiryquery.ListFilter{
|
||||
AssetType: filterValue(params.Filters, "asset_type"),
|
||||
Keyword: filterValue(params.Filters, "keyword"),
|
||||
}
|
||||
if shopID, ok := filterUint(params.Filters, "shop_id"); ok {
|
||||
filter.ShopID = &shopID
|
||||
}
|
||||
if packageID, ok := filterUint(params.Filters, "package_id"); ok {
|
||||
filter.PackageID = &packageID
|
||||
}
|
||||
if daysMin, ok := filterInt(params.Filters, "days_min"); ok {
|
||||
filter.DaysMin = &daysMin
|
||||
}
|
||||
if daysMax, ok := filterInt(params.Filters, "days_max"); ok {
|
||||
filter.DaysMax = &daysMax
|
||||
}
|
||||
start, end, err := strictTimeRange(params.Filters)
|
||||
if err != nil {
|
||||
return packageexpiryquery.ListFilter{}, err
|
||||
}
|
||||
filter.StartTime, filter.EndTime = start, end
|
||||
return filter, nil
|
||||
}
|
||||
|
||||
func (s *ExpiringAssetDataSource) loadShopOwners(ctx context.Context, shopIDs []uint) (map[uint]expiringAssetShop, error) {
|
||||
result := make(map[uint]expiringAssetShop, len(shopIDs))
|
||||
if len(shopIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
var rows []expiringAssetShop
|
||||
if err := s.db.WithContext(ctx).Table("tb_shop").
|
||||
Select("id, shop_name, business_owner_account_id").
|
||||
Where("id IN ? AND deleted_at IS NULL", shopIDs).
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询临期导出店铺当前归属失败")
|
||||
}
|
||||
for _, row := range rows {
|
||||
result[row.ID] = row
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *ExpiringAssetDataSource) loadDeviceAttributes(ctx context.Context, deviceIDs []uint) (map[uint]expiringAssetDevice, error) {
|
||||
result := make(map[uint]expiringAssetDevice, len(deviceIDs))
|
||||
if len(deviceIDs) == 0 {
|
||||
return result, nil
|
||||
}
|
||||
var rows []expiringAssetDevice
|
||||
if err := s.db.WithContext(ctx).Table("tb_device").
|
||||
Select("id, device_type, device_model").
|
||||
Where("id IN ?", deviceIDs).
|
||||
Scan(&rows).Error; err != nil {
|
||||
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询临期导出设备类型与型号失败")
|
||||
}
|
||||
for _, row := range rows {
|
||||
result[row.ID] = row
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// expiringAssetShop 是执行时当前店铺归属投影。
|
||||
type expiringAssetShop struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
ShopName string `gorm:"column:shop_name"`
|
||||
BusinessOwnerAccountID *uint `gorm:"column:business_owner_account_id"`
|
||||
}
|
||||
|
||||
// expiringAssetDevice 是执行时当前设备类型与型号投影。
|
||||
type expiringAssetDevice struct {
|
||||
ID uint `gorm:"column:id"`
|
||||
DeviceType string `gorm:"column:device_type"`
|
||||
DeviceModel string `gorm:"column:device_model"`
|
||||
}
|
||||
|
||||
// formatRemainingDaysValue 输出列表同一最终到期推算给出的剩余上海自然日天数;无法精确推算时为空。
|
||||
func formatRemainingDaysValue(days *int) string {
|
||||
if days == nil {
|
||||
return ""
|
||||
}
|
||||
return strconv.Itoa(*days)
|
||||
}
|
||||
Reference in New Issue
Block a user