package asset import ( "context" "gorm.io/gorm" "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" ) // ResolveIdentifier 按资产标识定位资产:(资产类型, 资产ID)。 // 先查全局标识注册表,再按设备与卡的既有标识回退,与资产详情解析口径一致; // 未命中返回空类型,由调用方按「不可区分」处理。 func ResolveIdentifier( ctx context.Context, registry *postgres.AssetIdentifierStore, cardStore *postgres.IotCardStore, deviceStore *postgres.DeviceStore, identifier string, ) (string, uint, error) { if registry != nil { record, err := registry.FindByIdentifier(ctx, identifier) if err != nil { return "", 0, errors.Wrap(errors.CodeDatabaseError, err, "查询资产标识失败") } if record != nil { return record.AssetType, record.AssetID, nil } } if deviceStore != nil { device, err := deviceStore.GetByIdentifier(ctx, identifier) if err == nil && device != nil { return constants.AssetTypeDevice, device.ID, nil } if err != nil && err != gorm.ErrRecordNotFound { return "", 0, errors.Wrap(errors.CodeDatabaseError, err, "查询设备失败") } } if cardStore != nil { card, err := cardStore.GetByIdentifier(ctx, identifier) if err == nil && card != nil { return constants.AssetTypeIotCard, card.ID, nil } if err != nil && err != gorm.ErrRecordNotFound { return "", 0, errors.Wrap(errors.CodeDatabaseError, err, "查询卡失败") } } return "", 0, nil }