refactor: 出库时间更名为首次购买套餐时间,换货新资产不再按换货完成时间记录
Some checks failed
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Failing after 38s

- 字段 outbound_at → first_purchase_at,记录表 tb_asset_outbound_record → tb_asset_first_purchase_record,接口 /outbound-records → /first-purchase-records,筛选参数与导出列同步更名
- 换货后的新资产不因换货写入,在当前世代真正支付套餐订单时才记录;记录来源只保留 order / backfill,去掉换货单字段
- 迁移 000266 重命名列、表、索引与约束;同步 Spec、设计、任务与前端接口变更说明

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
邹进
2026-10-27 16:47:02 +09:00
parent 909f4d0ec2
commit 62ec94adec
46 changed files with 570 additions and 560 deletions

View File

@@ -201,7 +201,7 @@ func (s *Service) buildDeviceResolveResponse(ctx context.Context, device *model.
AssetType: "device",
AssetID: device.ID,
VirtualNo: device.VirtualNo,
OutboundAt: device.OutboundAt,
FirstPurchaseAt: device.FirstPurchaseAt,
Status: device.Status,
StatusName: constants.GetDeviceStatusName(device.Status),
BatchNo: device.BatchNo,
@@ -346,7 +346,7 @@ func (s *Service) buildCardResolveResponse(ctx context.Context, card *model.IotC
AssetType: "card",
AssetID: card.ID,
VirtualNo: card.VirtualNo,
OutboundAt: card.OutboundAt,
FirstPurchaseAt: card.FirstPurchaseAt,
Status: card.Status,
StatusName: constants.GetIotCardStatusName(card.Status),
BatchNo: card.BatchNo,

View File

@@ -262,7 +262,7 @@ func (s *Service) List(ctx context.Context, req *dto.ListDeviceRequest) (*dto.Li
if req.IsAuthorizedToEnterprise != nil {
filters["is_authorized_to_enterprise"] = *req.IsAuthorizedToEnterprise
}
if err := applyOutboundFilters(filters, req.OutboundStart, req.OutboundEnd); err != nil {
if err := applyFirstPurchaseFilters(filters, req.FirstPurchaseStart, req.FirstPurchaseEnd); err != nil {
return nil, err
}
@@ -895,7 +895,7 @@ func (s *Service) toDeviceResponse(device *model.Device, shopMap map[uint]string
activationStatus := activationStatuses[device.ID]
resp := &dto.DeviceResponse{
EnablePolling: device.EnablePolling,
OutboundAt: device.OutboundAt,
FirstPurchaseAt: device.FirstPurchaseAt,
ID: device.ID,
VirtualNo: device.VirtualNo,
IMEI: device.IMEI,
@@ -1774,21 +1774,21 @@ func (s *Service) recordCardCommand(ctx context.Context, card *model.IotCard, ac
})
}
// applyOutboundFilters 解析出库时间范围筛选(RFC3339)。
func applyOutboundFilters(filters map[string]any, start, end string) error {
// applyFirstPurchaseFilters 解析首次购买套餐时间范围筛选(RFC3339)。
func applyFirstPurchaseFilters(filters map[string]any, start, end string) error {
if start != "" {
value, err := time.Parse(time.RFC3339, start)
if err != nil {
return errors.New(errors.CodeInvalidParam, "出库时间起格式应为 RFC3339")
return errors.New(errors.CodeInvalidParam, "首次购买套餐时间起格式应为 RFC3339")
}
filters["outbound_start"] = value
filters["first_purchase_start"] = value
}
if end != "" {
value, err := time.Parse(time.RFC3339, end)
if err != nil {
return errors.New(errors.CodeInvalidParam, "出库时间止格式应为 RFC3339")
return errors.New(errors.CodeInvalidParam, "首次购买套餐时间止格式应为 RFC3339")
}
filters["outbound_end"] = value
filters["first_purchase_end"] = value
}
return nil
}

View File

@@ -39,7 +39,7 @@ type Service struct {
silenceHook *packagesilenceapp.ExchangeHook
}
// SetSilenceHook 注入沉默期与出库时间换货接缝。
// SetSilenceHook 注入沉默期与首次购买套餐时间换货接缝。
func (s *Service) SetSilenceHook(hook *packagesilenceapp.ExchangeHook) {
s.silenceHook = hook
}
@@ -402,7 +402,7 @@ func (s *Service) Renew(ctx context.Context, id uint) error {
}).Error; updateErr != nil {
return errors.Wrap(errors.CodeDatabaseError, updateErr, "重置旧卡转新状态失败")
}
// 出库时间:旧卡回库、世代 +1,清空出库时间并结束旧世代出库记录
// 首次购买套餐时间:旧卡回库、世代 +1,清空首次购买套餐时间并结束旧世代首次购买套餐记录
if hookErr := s.silenceHook.OnOldAssetRenewedInTx(ctx, tx, constants.ExchangeAssetTypeIotCard, card.ID, card.Generation, time.Now()); hookErr != nil {
return hookErr
}
@@ -479,7 +479,7 @@ func (s *Service) Renew(ctx context.Context, id uint) error {
}).Error; err != nil {
return errors.Wrap(errors.CodeDatabaseError, err, "重置旧设备转新状态失败")
}
// 出库时间:旧设备回库、世代 +1,清空出库时间并结束旧世代出库记录
// 首次购买套餐时间:旧设备回库、世代 +1,清空首次购买套餐时间并结束旧世代首次购买套餐记录
if err = s.silenceHook.OnOldAssetRenewedInTx(ctx, tx, constants.ExchangeAssetTypeDevice, device.ID, device.Generation, time.Now()); err != nil {
return err
}
@@ -897,10 +897,6 @@ func (s *Service) completeExchangeWithTx(ctx context.Context, tx *gorm.DB, order
if result.RowsAffected == 0 {
return errors.New(errors.CodeExchangeStatusInvalid)
}
// 出库时间:换货后的新资产取换货完成时间(当前世代已有出库时间的不覆盖)
if err = s.silenceHook.OnNewAssetInTx(ctx, tx, order.NewAssetType, *order.NewAssetID, order, now); err != nil {
return err
}
orderBefore := map[string]any{"status": fromStatus, "migration_completed": order.MigrationCompleted, "migration_balance": order.MigrationBalance, "migration_status": beforeMigrationStatus}
order.Status = constants.ExchangeStatusCompleted
order.CompletedAt = &now

View File

@@ -275,7 +275,7 @@ func (s *Service) ListStandalone(ctx context.Context, req *dto.ListStandaloneIot
if req.IsAuthorizedToEnterprise != nil {
filters["is_authorized_to_enterprise"] = *req.IsAuthorizedToEnterprise
}
if err := applyOutboundFilters(filters, req.OutboundStart, req.OutboundEnd); err != nil {
if err := applyFirstPurchaseFilters(filters, req.FirstPurchaseStart, req.FirstPurchaseEnd); err != nil {
return nil, err
}
@@ -484,7 +484,7 @@ func (s *Service) toStandaloneResponse(card *model.IotCard, shopMap map[uint]str
ID: card.ID,
ICCID: card.ICCID,
VirtualNo: card.VirtualNo,
OutboundAt: card.OutboundAt,
FirstPurchaseAt: card.FirstPurchaseAt,
CardCategory: card.CardCategory,
CarrierID: card.CarrierID,
CarrierType: card.CarrierType,
@@ -2078,21 +2078,21 @@ func (s *Service) clearRealnameReversalCounter(ctx context.Context, cardID uint)
}
}
// applyOutboundFilters 解析出库时间范围筛选(RFC3339)。
func applyOutboundFilters(filters map[string]any, start, end string) error {
// applyFirstPurchaseFilters 解析首次购买套餐时间范围筛选(RFC3339)。
func applyFirstPurchaseFilters(filters map[string]any, start, end string) error {
if start != "" {
value, err := time.Parse(time.RFC3339, start)
if err != nil {
return errors.New(errors.CodeInvalidParam, "出库时间起格式应为 RFC3339")
return errors.New(errors.CodeInvalidParam, "首次购买套餐时间起格式应为 RFC3339")
}
filters["outbound_start"] = value
filters["first_purchase_start"] = value
}
if end != "" {
value, err := time.Parse(time.RFC3339, end)
if err != nil {
return errors.New(errors.CodeInvalidParam, "出库时间止格式应为 RFC3339")
return errors.New(errors.CodeInvalidParam, "首次购买套餐时间止格式应为 RFC3339")
}
filters["outbound_end"] = value
filters["first_purchase_end"] = value
}
return nil
}

View File

@@ -87,7 +87,7 @@ type Service struct {
paymentIntegration *integrationlog.Repository
billCreation *employeecollectionapp.BillCreationService
silenceSetter *packagesilenceapp.Setter
outboundRecorder *packagesilenceapp.OutboundRecorder
firstPurchaseRecorder *packagesilenceapp.FirstPurchaseRecorder
}
// SetSilenceSetter 注入沉默期设置用例(支付成功创建套餐记录时设置沉默期)。
@@ -95,9 +95,9 @@ func (s *Service) SetSilenceSetter(setter *packagesilenceapp.Setter) {
s.silenceSetter = setter
}
// SetOutboundRecorder 注入资产出库时间写入用例。
func (s *Service) SetOutboundRecorder(recorder *packagesilenceapp.OutboundRecorder) {
s.outboundRecorder = recorder
// SetFirstPurchaseRecorder 注入资产首次购买套餐时间写入用例。
func (s *Service) SetFirstPurchaseRecorder(recorder *packagesilenceapp.FirstPurchaseRecorder) {
s.firstPurchaseRecorder = recorder
}
// SetEmployeeCollectionBillCreation 注入员工代收款账单建账用例。
@@ -2359,9 +2359,9 @@ func (s *Service) activatePackage(ctx context.Context, tx *gorm.DB, order *model
createdUsage = true
}
}
if createdUsage && s.outboundRecorder != nil {
// 出库时间:资产当前世代第 1 次购买套餐的支付时间(只记录展示,不受沉默期总开关影响)
if err := s.outboundRecorder.RecordForOrderInTx(ctx, tx, order); err != nil {
if createdUsage && s.firstPurchaseRecorder != nil {
// 首次购买套餐时间:资产当前世代第 1 次购买套餐的支付时间(只记录展示,不受沉默期总开关影响)
if err := s.firstPurchaseRecorder.RecordForOrderInTx(ctx, tx, order); err != nil {
return err
}
}