This commit is contained in:
2026-04-30 15:49:40 +08:00
parent 5425e2ce19
commit 948243b13d
11 changed files with 703 additions and 178 deletions

View File

@@ -450,13 +450,16 @@ func (s *Service) AllocateDevices(ctx context.Context, req *dto.AllocateDevicesR
}, nil
}
beforeData := make([]map[string]any, 0, len(devices))
for _, device := range devices {
beforeData = append(beforeData, deviceSnapshot(device))
}
newStatus := constants.DeviceStatusDistributed
targetShopID := req.TargetShopID
targetShopName := s.getAuditShopName(ctx, &targetShopID)
shopMap := s.loadShopData(ctx, devices)
beforeData := make([]map[string]any, 0, len(devices))
for _, device := range devices {
snapshot := deviceSnapshot(device)
snapshot["shop_name"] = deviceShopMapValue(shopMap, device.ShopID)
beforeData = append(beforeData, snapshot)
}
err = s.db.Transaction(func(tx *gorm.DB) error {
txDeviceStore := postgres.NewDeviceStore(tx, nil)
@@ -492,8 +495,9 @@ func (s *Service) AllocateDevices(ctx context.Context, req *dto.AllocateDevicesR
nil,
map[string]any{"devices": beforeData},
map[string]any{
"target_shop_id": req.TargetShopID,
"failed_items": failedItems,
"target_shop_id": req.TargetShopID,
"target_shop_name": targetShopName,
"failed_items": failedItems,
},
len(devices),
len(deviceIDs),
@@ -511,9 +515,10 @@ func (s *Service) AllocateDevices(ctx context.Context, req *dto.AllocateDevicesR
nil,
map[string]any{"devices": beforeData},
map[string]any{
"target_shop_id": req.TargetShopID,
"device_ids": deviceIDs,
"failed_items": failedItems,
"target_shop_id": req.TargetShopID,
"target_shop_name": targetShopName,
"device_ids": deviceIDs,
"failed_items": failedItems,
},
len(devices),
len(deviceIDs),
@@ -629,11 +634,6 @@ func (s *Service) RecallDevices(ctx context.Context, req *dto.RecallDevicesReque
}, nil
}
beforeData := make([]map[string]any, 0, len(devices))
for _, device := range devices {
beforeData = append(beforeData, deviceSnapshot(device))
}
var newShopID *uint
var newStatus int
if isPlatform {
@@ -643,6 +643,14 @@ func (s *Service) RecallDevices(ctx context.Context, req *dto.RecallDevicesReque
newShopID = operatorShopID
newStatus = constants.DeviceStatusDistributed
}
targetShopName := s.getAuditRecallTargetShopName(ctx, newShopID)
shopMap := s.loadShopData(ctx, devices)
beforeData := make([]map[string]any, 0, len(devices))
for _, device := range devices {
snapshot := deviceSnapshot(device)
snapshot["shop_name"] = deviceShopMapValue(shopMap, device.ShopID)
beforeData = append(beforeData, snapshot)
}
err = s.db.Transaction(func(tx *gorm.DB) error {
txDeviceStore := postgres.NewDeviceStore(tx, nil)
@@ -684,8 +692,9 @@ func (s *Service) RecallDevices(ctx context.Context, req *dto.RecallDevicesReque
nil,
map[string]any{"devices": beforeData},
map[string]any{
"device_ids": deviceIDs,
"failed_items": failedItems,
"device_ids": deviceIDs,
"failed_items": failedItems,
"target_shop_name": targetShopName,
},
len(devices),
len(deviceIDs),
@@ -703,9 +712,10 @@ func (s *Service) RecallDevices(ctx context.Context, req *dto.RecallDevicesReque
nil,
map[string]any{"devices": beforeData},
map[string]any{
"device_ids": deviceIDs,
"failed_items": failedItems,
"target_shop": newShopID,
"device_ids": deviceIDs,
"failed_items": failedItems,
"target_shop": newShopID,
"target_shop_name": targetShopName,
},
len(devices),
len(deviceIDs),
@@ -748,6 +758,31 @@ func (s *Service) validateDirectSubordinate(ctx context.Context, operatorShopID
return nil
}
func (s *Service) getAuditShopName(ctx context.Context, shopID *uint) string {
if shopID == nil || *shopID == 0 {
return ""
}
var shop model.Shop
if err := s.db.WithContext(ctx).Unscoped().First(&shop, *shopID).Error; err != nil {
return ""
}
return shop.ShopName
}
func (s *Service) getAuditRecallTargetShopName(ctx context.Context, shopID *uint) string {
if shopID == nil {
return "平台库存"
}
return s.getAuditShopName(ctx, shopID)
}
func deviceShopMapValue(shopMap map[uint]string, shopID *uint) string {
if shopID == nil {
return ""
}
return shopMap[*shopID]
}
func (s *Service) loadShopData(ctx context.Context, devices []*model.Device) map[uint]string {
shopIDs := make([]uint, 0)
shopIDSet := make(map[uint]bool)