feat: 不轮询清单与批量开关轮询
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m35s
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 9m35s
- 新增不轮询清单 tb_card_polling_pause(迁移 000256 补写存量),所有改 enable_polling 的入口同事务写清单 - 卡、设备勾选批量开关轮询(单次 500,逐项结果,开启跳过风险停机与已销户)与 CSV 文件导入 - 订单套餐生效、改有效期后自动恢复轮询并按规则评估复机 - 不轮询清单查询与导出;卡、设备列表与导出增加是否轮询 - 失败告警停机被拒视图、导出与批量关闭轮询;开关轮询影响说明 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -73,6 +73,8 @@ type Service struct {
|
||||
queueClient *queue.Client
|
||||
logger *zap.Logger
|
||||
resumeCallback packagepkg.ResumeCallback
|
||||
// pollingRestorer 订单套餐生效后自动恢复关闭轮询的卡或设备;未注入时不恢复。
|
||||
pollingRestorer PollingRestorer
|
||||
assetIdentifierStore *postgres.AssetIdentifierStore
|
||||
personalCustomerStore *postgres.PersonalCustomerStore
|
||||
personalCustomerPhoneStore *postgres.PersonalCustomerPhoneStore
|
||||
@@ -167,6 +169,45 @@ func (s *Service) SetResumeCallback(callback packagepkg.ResumeCallback) {
|
||||
s.resumeCallback = callback
|
||||
}
|
||||
|
||||
// PollingRestorer 是套餐生效类入口自动恢复轮询的能力。
|
||||
type PollingRestorer interface {
|
||||
RestoreAssetPolling(ctx context.Context, assetType string, assetID uint, source string) bool
|
||||
}
|
||||
|
||||
// SetPollingRestorer 注入订单套餐生效后自动恢复轮询的能力。
|
||||
func (s *Service) SetPollingRestorer(restorer PollingRestorer) {
|
||||
s.pollingRestorer = restorer
|
||||
}
|
||||
|
||||
// afterOrderActivated 订单事务提交后:本单直接激活了主套餐时,先把关闭轮询的卡或设备恢复轮询(来源"支付或下单生效",
|
||||
// 风险停机或已销户不恢复),再按规则评估复机。resume=true 时(支付确认)总是评估;否则只在恢复了轮询时评估。
|
||||
func (s *Service) afterOrderActivated(ctx context.Context, order *model.Order, resume bool) {
|
||||
if order == nil {
|
||||
return
|
||||
}
|
||||
assetType, assetID := constants.AssetTypeIotCard, uint(0)
|
||||
if order.OrderType == model.OrderTypeSingleCard && order.IotCardID != nil {
|
||||
assetID = *order.IotCardID
|
||||
} else if order.OrderType == model.OrderTypeDevice && order.DeviceID != nil {
|
||||
assetType, assetID = constants.AssetTypeDevice, *order.DeviceID
|
||||
}
|
||||
if assetID == 0 {
|
||||
return
|
||||
}
|
||||
restored := false
|
||||
if s.pollingRestorer != nil {
|
||||
var activated int64
|
||||
if err := s.db.WithContext(ctx).Model(&model.PackageUsage{}).
|
||||
Where("order_id = ? AND status = ? AND master_usage_id IS NULL", order.ID, constants.PackageUsageStatusActive).
|
||||
Count(&activated).Error; err == nil && activated > 0 {
|
||||
restored = s.pollingRestorer.RestoreAssetPolling(ctx, assetType, assetID, constants.PollingToggleSourceOrder)
|
||||
}
|
||||
}
|
||||
if resume || restored {
|
||||
s.tryResumeAfterPayment(ctx, order)
|
||||
}
|
||||
}
|
||||
|
||||
// CreateAdminOrder 后台订单创建(仅支持 wallet/offline,立即扣款或激活)
|
||||
// 与 CreateH5Order 的核心区别:后台订单不创建待支付状态,wallet 立即扣款,offline 立即激活
|
||||
// POST /api/admin/orders
|
||||
@@ -1233,6 +1274,7 @@ func (s *Service) createOrderWithWalletPayment(ctx context.Context, order *model
|
||||
if existingOrderID > 0 {
|
||||
return existingOrderID, nil
|
||||
}
|
||||
s.afterOrderActivated(ctx, order, false)
|
||||
|
||||
// 3. 事务外:所有已支付且适用差价佣金的订单都进入佣金计算
|
||||
|
||||
@@ -1245,7 +1287,7 @@ func (s *Service) createOrderWithActivation(ctx context.Context, order *model.Or
|
||||
}
|
||||
release := s.prepareActivation(ctx, order)
|
||||
defer release()
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(order).Error; err != nil {
|
||||
return errors.Wrap(errors.CodeDatabaseError, err, "创建代购订单失败")
|
||||
}
|
||||
@@ -1270,6 +1312,10 @@ func (s *Service) createOrderWithActivation(ctx context.Context, order *model.Or
|
||||
}
|
||||
return s.appendOrderAudit(ctx, tx, constants.AuditActionOrderCreated, "创建并完成订单", order, nil, orderStateData(order))
|
||||
})
|
||||
if err == nil {
|
||||
s.afterOrderActivated(ctx, order, false)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// CreatePendingOrder 在同一事务中创建待支付订单、冻结个人资产钱包、创建明细和审计事件。
|
||||
@@ -1928,6 +1974,7 @@ func (s *Service) WalletPay(ctx context.Context, orderID uint, buyerType string,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
s.afterOrderActivated(ctx, order, false)
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -2043,7 +2090,7 @@ func (s *Service) HandlePaymentCallback(ctx context.Context, orderNo string, pay
|
||||
}
|
||||
|
||||
if shouldResumeAfterPayment {
|
||||
s.tryResumeAfterPayment(ctx, order)
|
||||
s.afterOrderActivated(ctx, order, true)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -2167,7 +2214,7 @@ func (s *Service) HandlePaymentRecordCallback(ctx context.Context, paymentNo str
|
||||
}
|
||||
|
||||
if shouldResumeAfterPayment {
|
||||
s.tryResumeAfterPayment(ctx, order)
|
||||
s.afterOrderActivated(ctx, order, true)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user