This commit is contained in:
@@ -3,6 +3,7 @@ package device
|
||||
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"
|
||||
@@ -22,6 +23,7 @@ type Service struct {
|
||||
shopPackageAllocationStore *postgres.ShopPackageAllocationStore
|
||||
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore
|
||||
packageSeriesStore *postgres.PackageSeriesStore
|
||||
gatewayClient *gateway.Client
|
||||
}
|
||||
|
||||
func New(
|
||||
@@ -34,6 +36,7 @@ func New(
|
||||
shopPackageAllocationStore *postgres.ShopPackageAllocationStore,
|
||||
shopSeriesAllocationStore *postgres.ShopSeriesAllocationStore,
|
||||
packageSeriesStore *postgres.PackageSeriesStore,
|
||||
gatewayClient *gateway.Client,
|
||||
) *Service {
|
||||
return &Service{
|
||||
db: db,
|
||||
@@ -45,6 +48,7 @@ func New(
|
||||
shopPackageAllocationStore: shopPackageAllocationStore,
|
||||
shopSeriesAllocationStore: shopSeriesAllocationStore,
|
||||
packageSeriesStore: packageSeriesStore,
|
||||
gatewayClient: gatewayClient,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,6 +172,40 @@ func (s *Service) GetByDeviceNo(ctx context.Context, deviceNo string) (*dto.Devi
|
||||
return s.toDeviceResponse(device, shopMap, seriesMap, bindingCounts), nil
|
||||
}
|
||||
|
||||
// GetByIdentifier 通过任意标识符获取设备详情
|
||||
// 支持 device_no(虚拟号)、imei、sn 三个字段的自动匹配
|
||||
func (s *Service) GetByIdentifier(ctx context.Context, identifier string) (*dto.DeviceResponse, error) {
|
||||
device, err := s.deviceStore.GetByIdentifier(ctx, identifier)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "设备不存在")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
shopMap := s.loadShopData(ctx, []*model.Device{device})
|
||||
seriesMap := s.loadSeriesNames(ctx, []*model.Device{device})
|
||||
bindingCounts, err := s.getBindingCounts(ctx, []uint{device.ID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.toDeviceResponse(device, shopMap, seriesMap, bindingCounts), nil
|
||||
}
|
||||
|
||||
// GetDeviceByIdentifier 通过任意标识符获取设备模型(内部使用,不转为 DTO)
|
||||
// 用于 Handler 层获取设备后提取 IMEI 调用 Gateway API
|
||||
func (s *Service) GetDeviceByIdentifier(ctx context.Context, identifier string) (*model.Device, error) {
|
||||
device, err := s.deviceStore.GetByIdentifier(ctx, identifier)
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, errors.New(errors.CodeNotFound, "设备不存在或无权限访问")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return device, nil
|
||||
}
|
||||
|
||||
func (s *Service) Delete(ctx context.Context, id uint) error {
|
||||
device, err := s.deviceStore.GetByID(ctx, id)
|
||||
if err != nil {
|
||||
@@ -491,6 +529,8 @@ func (s *Service) toDeviceResponse(device *model.Device, shopMap map[uint]string
|
||||
resp := &dto.DeviceResponse{
|
||||
ID: device.ID,
|
||||
DeviceNo: device.DeviceNo,
|
||||
IMEI: device.IMEI,
|
||||
SN: device.SN,
|
||||
DeviceName: device.DeviceName,
|
||||
DeviceModel: device.DeviceModel,
|
||||
DeviceType: device.DeviceType,
|
||||
|
||||
Reference in New Issue
Block a user