This commit is contained in:
@@ -93,8 +93,15 @@ func (s *Service) List(ctx context.Context, req *dto.ListDeviceRequest) (*dto.Li
|
||||
if req.Status != nil {
|
||||
filters["status"] = *req.Status
|
||||
}
|
||||
if req.ShopID != nil {
|
||||
filters["shop_id"] = req.ShopID
|
||||
shopIDs, hasShopIDs := normalizeShopIDs(req.ShopIDs)
|
||||
if hasShopIDs {
|
||||
filters["shop_ids"] = shopIDs
|
||||
} else if req.ShopID != nil {
|
||||
if *req.ShopID == 0 {
|
||||
filters["shop_ids"] = []uint{}
|
||||
} else {
|
||||
filters["shop_id"] = req.ShopID
|
||||
}
|
||||
}
|
||||
if req.BatchNo != "" {
|
||||
filters["batch_no"] = req.BatchNo
|
||||
@@ -141,6 +148,25 @@ func (s *Service) List(ctx context.Context, req *dto.ListDeviceRequest) (*dto.Li
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeShopIDs(ids []uint) ([]uint, bool) {
|
||||
if len(ids) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
seen := make(map[uint]struct{}, len(ids))
|
||||
result := make([]uint, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[id]; exists {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
result = append(result, id)
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
|
||||
// Get 获取设备详情
|
||||
func (s *Service) Get(ctx context.Context, id uint) (*dto.DeviceResponse, error) {
|
||||
device, err := s.deviceStore.GetByID(ctx, id)
|
||||
|
||||
@@ -156,8 +156,15 @@ func (s *Service) ListStandalone(ctx context.Context, req *dto.ListStandaloneIot
|
||||
if req.CarrierID != nil {
|
||||
filters["carrier_id"] = *req.CarrierID
|
||||
}
|
||||
if req.ShopID != nil {
|
||||
filters["shop_id"] = *req.ShopID
|
||||
shopIDs, hasShopIDs := normalizeShopIDs(req.ShopIDs)
|
||||
if hasShopIDs {
|
||||
filters["shop_ids"] = shopIDs
|
||||
} else if req.ShopID != nil {
|
||||
if *req.ShopID == 0 {
|
||||
filters["shop_ids"] = []uint{}
|
||||
} else {
|
||||
filters["shop_id"] = *req.ShopID
|
||||
}
|
||||
}
|
||||
if req.ICCID != "" {
|
||||
filters["iccid"] = req.ICCID
|
||||
@@ -200,8 +207,16 @@ func (s *Service) ListStandalone(ctx context.Context, req *dto.ListStandaloneIot
|
||||
shopID := middleware.GetShopIDFromContext(ctx)
|
||||
if shopID > 0 {
|
||||
subordinateIDs, err := s.shopStore.GetSubordinateShopIDs(ctx, shopID)
|
||||
if err == nil && len(subordinateIDs) > 1 {
|
||||
filters["subordinate_shop_ids"] = subordinateIDs
|
||||
if err == nil {
|
||||
if hasShopIDs {
|
||||
scopedShopIDs := intersectShopIDs(shopIDs, subordinateIDs)
|
||||
filters["shop_ids"] = scopedShopIDs
|
||||
if len(scopedShopIDs) > 1 {
|
||||
filters["subordinate_shop_ids"] = scopedShopIDs
|
||||
}
|
||||
} else if len(subordinateIDs) > 1 {
|
||||
filters["subordinate_shop_ids"] = subordinateIDs
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -229,6 +244,42 @@ func (s *Service) ListStandalone(ctx context.Context, req *dto.ListStandaloneIot
|
||||
}, nil
|
||||
}
|
||||
|
||||
func normalizeShopIDs(ids []uint) ([]uint, bool) {
|
||||
if len(ids) == 0 {
|
||||
return nil, false
|
||||
}
|
||||
seen := make(map[uint]struct{}, len(ids))
|
||||
result := make([]uint, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if id == 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := seen[id]; exists {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
result = append(result, id)
|
||||
}
|
||||
return result, true
|
||||
}
|
||||
|
||||
func intersectShopIDs(selectedIDs, allowedIDs []uint) []uint {
|
||||
if len(selectedIDs) == 0 || len(allowedIDs) == 0 {
|
||||
return []uint{}
|
||||
}
|
||||
allowed := make(map[uint]struct{}, len(allowedIDs))
|
||||
for _, id := range allowedIDs {
|
||||
allowed[id] = struct{}{}
|
||||
}
|
||||
result := make([]uint, 0, len(selectedIDs))
|
||||
for _, id := range selectedIDs {
|
||||
if _, ok := allowed[id]; ok {
|
||||
result = append(result, id)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// GetByICCID 通过 ICCID 获取单卡详情
|
||||
func (s *Service) GetByICCID(ctx context.Context, iccid string) (*dto.IotCardDetailResponse, error) {
|
||||
card, err := s.iotCardStore.GetByICCID(ctx, iccid)
|
||||
|
||||
Reference in New Issue
Block a user