From 6dc6afece0f0870297cd28bc8465f4512a803677 Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 25 Feb 2026 16:27:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=B7=B2=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=BA=97=E9=93=BA=E5=90=8D=E7=A7=B0=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 店铺被软删除后,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 --- internal/service/asset_allocation_record/service.go | 3 ++- internal/service/device/service.go | 3 ++- internal/service/enterprise/service.go | 3 ++- internal/service/iot_card/service.go | 3 ++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/service/asset_allocation_record/service.go b/internal/service/asset_allocation_record/service.go index 05300e9..cd8dc08 100644 --- a/internal/service/asset_allocation_record/service.go +++ b/internal/service/asset_allocation_record/service.go @@ -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 } diff --git a/internal/service/device/service.go b/internal/service/device/service.go index 26c1215..00429f2 100644 --- a/internal/service/device/service.go +++ b/internal/service/device/service.go @@ -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 } diff --git a/internal/service/enterprise/service.go b/internal/service/enterprise/service.go index 505fcaf..6d39908 100644 --- a/internal/service/enterprise/service.go +++ b/internal/service/enterprise/service.go @@ -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 } diff --git a/internal/service/iot_card/service.go b/internal/service/iot_card/service.go index 4df8eea..8a3cee2 100644 --- a/internal/service/iot_card/service.go +++ b/internal/service/iot_card/service.go @@ -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 }