fix: 修复已删除店铺名称无法显示的问题
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m6s

店铺被软删除后,GORM 默认过滤 deleted_at IS NOT NULL 的记录,
导致查询店铺名称时找不到对应店铺,shop_name 字段被 omitempty 省略。

修复方案:在加载店铺名称的查询中添加 Unscoped(),包含已删除的店铺。

影响接口:
- GET /api/admin/devices(设备列表)
- GET /api/admin/iot-cards/standalone(独立卡列表)
- GET /api/admin/asset-allocation-records(分配记录列表)
- GET /api/admin/enterprises(企业列表)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-25 16:27:58 +08:00
parent 037595c22e
commit 6dc6afece0
4 changed files with 8 additions and 4 deletions

View File

@@ -177,7 +177,8 @@ func (s *Service) loadShopNames(ctx context.Context, shopIDs []uint) map[uint]st
}
var shops []model.Shop
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
// 使用 Unscoped() 包含已删除的店铺,确保能显示店铺名称
s.db.WithContext(ctx).Unscoped().Where("id IN ?", shopIDs).Find(&shops)
for _, shop := range shops {
result[shop.ID] = shop.ShopName
}

View File

@@ -426,7 +426,8 @@ func (s *Service) loadShopData(ctx context.Context, devices []*model.Device) map
shopMap := make(map[uint]string)
if len(shopIDs) > 0 {
var shops []model.Shop
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
// 使用 Unscoped() 包含已删除的店铺,确保能显示店铺名称
s.db.WithContext(ctx).Unscoped().Where("id IN ?", shopIDs).Find(&shops)
for _, shop := range shops {
shopMap[shop.ID] = shop.ShopName
}

View File

@@ -316,7 +316,8 @@ func (s *Service) List(ctx context.Context, req *dto.EnterpriseListReq) (*dto.En
shopMap := make(map[uint]string)
if len(shopIDs) > 0 {
var shops []model.Shop
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
// 使用 Unscoped() 包含已删除的店铺,确保能显示店铺名称
s.db.WithContext(ctx).Unscoped().Where("id IN ?", shopIDs).Find(&shops)
for _, shop := range shops {
shopMap[shop.ID] = shop.ShopName
}

View File

@@ -200,7 +200,8 @@ func (s *Service) loadShopNames(ctx context.Context, cards []*model.IotCard) map
shopMap := make(map[uint]string)
if len(shopIDs) > 0 {
var shops []model.Shop
s.db.WithContext(ctx).Where("id IN ?", shopIDs).Find(&shops)
// 使用 Unscoped() 包含已删除的店铺,确保能显示店铺名称
s.db.WithContext(ctx).Unscoped().Where("id IN ?", shopIDs).Find(&shops)
for _, shop := range shops {
shopMap[shop.ID] = shop.ShopName
}