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

@@ -2,6 +2,7 @@ package device
import (
"context"
"fmt"
"strings"
"github.com/break/junhong_cmp_fiber/internal/model"
@@ -62,6 +63,8 @@ func (s *Service) logDeviceOperation(
snapshot := deviceSnapshot(device)
beforeContent := stripDeviceOperationMeta(beforeData)
afterContent := stripDeviceOperationMeta(afterData)
enrichDeviceOperationContent(beforeContent, beforeData)
enrichDeviceOperationContent(afterContent, afterData)
params.BeforeData, params.AfterData = assetAuditSvc.WrapOperationContent(beforeContent, afterContent, snapshot)
if device != nil {
@@ -81,7 +84,7 @@ func stripDeviceOperationMeta(data map[string]any) map[string]any {
}
out := make(map[string]any, len(data))
for k, v := range data {
if k == "device" || k == "card" || k == "cards" || k == "asset_snapshot" || k == "operation_content" {
if k == "device" || k == "devices" || k == "card" || k == "cards" || k == "asset_snapshot" || k == "operation_content" {
continue
}
out[k] = v
@@ -92,6 +95,116 @@ func stripDeviceOperationMeta(data map[string]any) map[string]any {
return out
}
func enrichDeviceOperationContent(content map[string]any, raw map[string]any) {
if len(raw) == 0 {
return
}
if deviceRaw, ok := raw["device"].(map[string]any); ok {
mergeReadableDeviceFields(content, deviceRaw)
}
if cardRaw, ok := raw["card"].(map[string]any); ok {
mergeReadableCardFields(content, cardRaw)
}
switch devicesRaw := raw["devices"].(type) {
case []map[string]any:
deviceIDs, virtualNos := collectDeviceReadableLists(devicesRaw)
if len(deviceIDs) > 0 && content["device_ids"] == nil {
content["device_ids"] = deviceIDs
}
if len(virtualNos) > 0 && content["device_virtual_nos"] == nil {
content["device_virtual_nos"] = virtualNos
}
case []any:
devices := make([]map[string]any, 0, len(devicesRaw))
for _, item := range devicesRaw {
deviceMap, ok := item.(map[string]any)
if !ok {
continue
}
devices = append(devices, deviceMap)
}
deviceIDs, virtualNos := collectDeviceReadableLists(devices)
if len(deviceIDs) > 0 && content["device_ids"] == nil {
content["device_ids"] = deviceIDs
}
if len(virtualNos) > 0 && content["device_virtual_nos"] == nil {
content["device_virtual_nos"] = virtualNos
}
}
}
func mergeReadableDeviceFields(content map[string]any, device map[string]any) {
if len(device) == 0 {
return
}
if _, ok := content["device_id"]; !ok {
if v, exists := device["id"]; exists {
content["device_id"] = v
}
}
if _, ok := content["device_virtual_no"]; !ok {
if v := stringifyDeviceAuditValue(device["virtual_no"]); v != "" {
content["device_virtual_no"] = v
}
}
if _, ok := content["device_imei"]; !ok {
if v := stringifyDeviceAuditValue(device["imei"]); v != "" {
content["device_imei"] = v
}
}
if _, ok := content["device_sn"]; !ok {
if v := stringifyDeviceAuditValue(device["sn"]); v != "" {
content["device_sn"] = v
}
}
}
func mergeReadableCardFields(content map[string]any, card map[string]any) {
if len(card) == 0 {
return
}
if _, ok := content["iot_card_id"]; !ok {
if v, exists := card["id"]; exists {
content["iot_card_id"] = v
}
}
if _, ok := content["iccid"]; !ok {
if v := stringifyDeviceAuditValue(card["iccid"]); v != "" {
content["iccid"] = v
}
}
}
func collectDeviceReadableLists(devices []map[string]any) ([]any, []string) {
deviceIDs := make([]any, 0, len(devices))
virtualNos := make([]string, 0, len(devices))
for _, device := range devices {
if id, ok := device["id"]; ok {
deviceIDs = append(deviceIDs, id)
}
if virtualNo := stringifyDeviceAuditValue(device["virtual_no"]); virtualNo != "" {
virtualNos = append(virtualNos, virtualNo)
}
}
return deviceIDs, virtualNos
}
func stringifyDeviceAuditValue(v any) string {
switch vv := v.(type) {
case string:
return vv
case fmt.Stringer:
return vv.String()
default:
if vv == nil {
return ""
}
return fmt.Sprint(vv)
}
}
func deviceSnapshot(device *model.Device) map[string]any {
if device == nil {
return nil
@@ -99,6 +212,8 @@ func deviceSnapshot(device *model.Device) map[string]any {
return map[string]any{
"id": device.ID,
"virtual_no": device.VirtualNo,
"imei": device.IMEI,
"sn": device.SN,
"shop_id": device.ShopID,
"status": device.Status,
"series_id": device.SeriesID,

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)