chore: apply task changes
This commit is contained in:
@@ -3,12 +3,14 @@ package iot_card
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/gateway"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
"github.com/break/junhong_cmp_fiber/internal/model/dto"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store"
|
||||
"github.com/break/junhong_cmp_fiber/internal/store/postgres"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/constants"
|
||||
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -18,6 +20,8 @@ type Service struct {
|
||||
shopStore *postgres.ShopStore
|
||||
assetAllocationRecordStore *postgres.AssetAllocationRecordStore
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||
gatewayClient *gateway.Client
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -26,6 +30,8 @@ func New(
|
||||
shopStore *postgres.ShopStore,
|
||||
assetAllocationRecordStore *postgres.AssetAllocationRecordStore,
|
||||
seriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||
gatewayClient *gateway.Client,
|
||||
logger *zap.Logger,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
@@ -33,6 +39,8 @@ func New(
|
||||
shopStore: shopStore,
|
||||
assetAllocationRecordStore: assetAllocationRecordStore,
|
||||
seriesAllocationStore: seriesAllocationStore,
|
||||
gatewayClient: gatewayClient,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -640,3 +648,53 @@ func (s *Service) buildCardNotFoundFailedItems(iccids []string) []dto.CardSeries
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
// SyncCardStatusFromGateway 从 Gateway 同步卡状态(示例方法)
|
||||
func (s *Service) SyncCardStatusFromGateway(ctx context.Context, iccid string) error {
|
||||
if s.gatewayClient == nil {
|
||||
return errors.New(errors.CodeGatewayError, "Gateway 客户端未配置")
|
||||
}
|
||||
|
||||
resp, err := s.gatewayClient.QueryCardStatus(ctx, &gateway.CardStatusReq{
|
||||
CardNo: iccid,
|
||||
})
|
||||
if err != nil {
|
||||
s.logger.Error("查询卡状态失败", zap.String("iccid", iccid), zap.Error(err))
|
||||
return errors.Wrap(errors.CodeGatewayError, err, "查询卡状态失败")
|
||||
}
|
||||
|
||||
card, err := s.iotCardStore.GetByICCID(ctx, iccid)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return errors.New(errors.CodeNotFound, "IoT卡不存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
var newStatus int
|
||||
switch resp.CardStatus {
|
||||
case "准备":
|
||||
newStatus = constants.IotCardStatusInStock
|
||||
case "正常":
|
||||
newStatus = constants.IotCardStatusDistributed
|
||||
case "停机":
|
||||
newStatus = constants.IotCardStatusSuspended
|
||||
default:
|
||||
s.logger.Warn("未知的卡状态", zap.String("cardStatus", resp.CardStatus))
|
||||
return nil
|
||||
}
|
||||
|
||||
if card.Status != newStatus {
|
||||
card.Status = newStatus
|
||||
if err := s.iotCardStore.Update(ctx, card); err != nil {
|
||||
return err
|
||||
}
|
||||
s.logger.Info("同步卡状态成功",
|
||||
zap.String("iccid", iccid),
|
||||
zap.Int("oldStatus", card.Status),
|
||||
zap.Int("newStatus", newStatus),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestIotCardService_BatchSetSeriesBinding(t *testing.T) {
|
||||
assetAllocationRecordStore := postgres.NewAssetAllocationRecordStore(tx, rdb)
|
||||
seriesAllocationStore := postgres.NewShopSeriesAllocationStore(tx)
|
||||
|
||||
svc := New(tx, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore)
|
||||
svc := New(tx, iotCardStore, shopStore, assetAllocationRecordStore, seriesAllocationStore, nil, nil)
|
||||
ctx := context.Background()
|
||||
|
||||
shop := &model.Shop{
|
||||
|
||||
Reference in New Issue
Block a user