diff --git a/internal/model/dto/iot_card_dto.go b/internal/model/dto/iot_card_dto.go index a3435ec..1293a82 100644 --- a/internal/model/dto/iot_card_dto.go +++ b/internal/model/dto/iot_card_dto.go @@ -17,7 +17,8 @@ type ListStandaloneIotCardRequest struct { BatchNo string `json:"batch_no" query:"batch_no" validate:"omitempty,max=100" maxLength:"100" description:"批次号"` PackageID *uint `json:"package_id" query:"package_id" description:"套餐ID"` IsDistributed *bool `json:"is_distributed" query:"is_distributed" description:"是否已分销 (true:已分销, false:未分销)"` - IsReplaced *bool `json:"is_replaced" query:"is_replaced" description:"是否有换卡记录 (true:有换卡记录, false:无换卡记录)"` + IsReplaced *bool `json:"is_replaced" query:"is_replaced" description:"是否有换卡记录 (true:有换卡记录, false:无换卡记录)"` + HasActivePackage *bool `json:"has_active_package" query:"has_active_package" description:"是否有生效中的套餐 (true:有生效中套餐, false:无生效中套餐, 不传:不过滤)"` ICCIDStart string `json:"iccid_start" query:"iccid_start" validate:"omitempty,max=20" maxLength:"20" description:"ICCID起始号"` ICCIDEnd string `json:"iccid_end" query:"iccid_end" validate:"omitempty,max=20" maxLength:"20" description:"ICCID结束号"` CarrierName string `json:"carrier_name" query:"carrier_name" validate:"omitempty,max=100" maxLength:"100" description:"运营商名称(模糊查询)"` diff --git a/internal/service/iot_card/service.go b/internal/service/iot_card/service.go index b0dc946..64658f8 100644 --- a/internal/service/iot_card/service.go +++ b/internal/service/iot_card/service.go @@ -203,6 +203,9 @@ func (s *Service) ListStandalone(ctx context.Context, req *dto.ListStandaloneIot if req.CarrierName != "" { filters["carrier_name"] = req.CarrierName } + if req.HasActivePackage != nil { + filters["has_active_package"] = *req.HasActivePackage + } // 代理用户注入 subordinate_shop_ids,让 Store 层走并行查询路径 // 避免 PG 对 shop_id IN (...) + ORDER BY 选择全表扫描 diff --git a/internal/store/postgres/iot_card_store.go b/internal/store/postgres/iot_card_store.go index 190197e..d3b9a95 100644 --- a/internal/store/postgres/iot_card_store.go +++ b/internal/store/postgres/iot_card_store.go @@ -895,6 +895,16 @@ func (s *IotCardStore) applyStandaloneFilters(ctx context.Context, query *gorm.D if carrierName, ok := filters["carrier_name"].(string); ok && carrierName != "" { query = query.Where("carrier_name LIKE ?", "%"+carrierName+"%") } + // has_active_package 过滤:是否存在生效中的主套餐记录 + if hasActivePackage, ok := filters["has_active_package"].(bool); ok { + subQuery := s.db.WithContext(ctx).Table("tb_package_usage pu").Select("1"). + Where("pu.iot_card_id = tb_iot_card.id AND pu.status = ? AND pu.master_usage_id IS NULL AND pu.deleted_at IS NULL", constants.PackageUsageStatusActive) + if hasActivePackage { + query = query.Where("EXISTS (?)", subQuery) + } else { + query = query.Where("NOT EXISTS (?)", subQuery) + } + } return query }