This commit is contained in:
@@ -29,6 +29,8 @@ type PollingCallback interface {
|
||||
OnCardCreated(ctx context.Context, card *model.IotCard)
|
||||
// OnCardStatusChanged 卡状态变化时的回调
|
||||
OnCardStatusChanged(ctx context.Context, cardID uint)
|
||||
// OnBatchCardsStatusChanged 批量卡状态变化时的回调(批量分配/回收场景)
|
||||
OnBatchCardsStatusChanged(ctx context.Context, cardIDs []uint)
|
||||
// OnCardDeleted 卡删除时的回调
|
||||
OnCardDeleted(ctx context.Context, cardID uint)
|
||||
// OnCardEnabled 卡启用轮询时的回调
|
||||
@@ -597,6 +599,7 @@ func (s *Service) AllocateCards(ctx context.Context, req *dto.AllocateStandalone
|
||||
|
||||
newStatus := constants.IotCardStatusDistributed
|
||||
toShopID := req.ToShopID
|
||||
allocationNo := s.assetAllocationRecordStore.GenerateAllocationNo(ctx, constants.AssetAllocationTypeAllocate)
|
||||
|
||||
err = s.db.Transaction(func(tx *gorm.DB) error {
|
||||
txIotCardStore := postgres.NewIotCardStore(tx, nil)
|
||||
@@ -606,7 +609,6 @@ func (s *Service) AllocateCards(ctx context.Context, req *dto.AllocateStandalone
|
||||
return err
|
||||
}
|
||||
|
||||
allocationNo := s.assetAllocationRecordStore.GenerateAllocationNo(ctx, constants.AssetAllocationTypeAllocate)
|
||||
records := s.buildAllocationRecords(cards, cardIDs, operatorShopID, toShopID, operatorID, allocationNo, req.Remark)
|
||||
return txRecordStore.BatchCreate(ctx, records)
|
||||
})
|
||||
@@ -631,11 +633,14 @@ func (s *Service) AllocateCards(ctx context.Context, req *dto.AllocateStandalone
|
||||
}
|
||||
s.iotCardStore.InvalidateListCountCache(ctx)
|
||||
|
||||
// 通知轮询调度器状态变化(卡被分配后可能需要重新匹配配置)
|
||||
// 通知轮询调度器状态变化(异步执行 + 批量操作,避免 N 次单卡 DB 查询打满连接池)
|
||||
if s.pollingCallback != nil && len(cardIDs) > 0 {
|
||||
for _, cardID := range cardIDs {
|
||||
s.pollingCallback.OnCardStatusChanged(ctx, cardID)
|
||||
}
|
||||
cardIDsCopy := make([]uint, len(cardIDs))
|
||||
copy(cardIDsCopy, cardIDs)
|
||||
cb := s.pollingCallback
|
||||
go func() {
|
||||
cb.OnBatchCardsStatusChanged(context.Background(), cardIDsCopy)
|
||||
}()
|
||||
}
|
||||
|
||||
shopMap := s.loadShopNames(ctx, cards)
|
||||
@@ -672,7 +677,7 @@ func (s *Service) AllocateCards(ctx context.Context, req *dto.AllocateStandalone
|
||||
TotalCount: len(cards),
|
||||
SuccessCount: len(cardIDs),
|
||||
FailCount: len(failedItems),
|
||||
AllocationNo: s.assetAllocationRecordStore.GenerateAllocationNo(ctx, constants.AssetAllocationTypeAllocate),
|
||||
AllocationNo: allocationNo,
|
||||
FailedItems: failedItems,
|
||||
}, nil
|
||||
}
|
||||
@@ -844,11 +849,14 @@ func (s *Service) RecallCards(ctx context.Context, req *dto.RecallStandaloneCard
|
||||
}
|
||||
s.iotCardStore.InvalidateListCountCache(ctx)
|
||||
|
||||
// 通知轮询调度器状态变化(卡被回收后可能需要重新匹配配置)
|
||||
// 通知轮询调度器状态变化(异步批量执行,避免回收大批卡时打满 DB 连接池)
|
||||
if s.pollingCallback != nil && len(cardIDs) > 0 {
|
||||
for _, cardID := range cardIDs {
|
||||
s.pollingCallback.OnCardStatusChanged(ctx, cardID)
|
||||
}
|
||||
cardIDsCopy := make([]uint, len(cardIDs))
|
||||
copy(cardIDsCopy, cardIDs)
|
||||
cb := s.pollingCallback
|
||||
go func() {
|
||||
cb.OnBatchCardsStatusChanged(context.Background(), cardIDsCopy)
|
||||
}()
|
||||
}
|
||||
|
||||
shopMap := s.loadShopNames(ctx, successCards)
|
||||
|
||||
Reference in New Issue
Block a user