feat(卡列表): 新增 has_active_package 查询条件筛选生效中套餐

- DTO ListStandaloneIotCardRequest 增加 HasActivePackage *bool 字段,
  query/json 名 has_active_package,description 中文
- Service ListStandalone 将 HasActivePackage 映射为 filters["has_active_package"]
- Store applyStandaloneFilters 新增 EXISTS/NOT EXISTS 子查询,
  使用 constants.PackageUsageStatusActive,限制 master_usage_id IS NULL
  且 deleted_at IS NULL,所有查询路径(默认/两阶段/并行)均复用此函数

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-19 12:06:33 +08:00
parent 5e7e68cce7
commit 9f5c5c3680
3 changed files with 15 additions and 1 deletions

View File

@@ -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:"运营商名称(模糊查询)"`

View File

@@ -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 选择全表扫描

View File

@@ -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
}