修改
This commit is contained in:
@@ -2,6 +2,7 @@ package iot_card
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/break/junhong_cmp_fiber/internal/model"
|
||||
assetAuditSvc "github.com/break/junhong_cmp_fiber/internal/service/asset_audit"
|
||||
@@ -55,7 +56,11 @@ func normalizeCardAuditPayload(beforeData, afterData map[string]any) (map[string
|
||||
}
|
||||
}
|
||||
}
|
||||
return assetAuditSvc.WrapOperationContent(stripCardOperationMeta(beforeData), stripCardOperationMeta(afterData), snapshot)
|
||||
beforeContent := stripCardOperationMeta(beforeData)
|
||||
afterContent := stripCardOperationMeta(afterData)
|
||||
enrichCardOperationContent(beforeContent, beforeData)
|
||||
enrichCardOperationContent(afterContent, afterData)
|
||||
return assetAuditSvc.WrapOperationContent(beforeContent, afterContent, snapshot)
|
||||
}
|
||||
|
||||
func stripCardOperationMeta(data map[string]any) map[string]any {
|
||||
@@ -75,20 +80,136 @@ func stripCardOperationMeta(data map[string]any) map[string]any {
|
||||
return out
|
||||
}
|
||||
|
||||
func enrichCardOperationContent(content map[string]any, raw map[string]any) {
|
||||
if len(raw) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if cardRaw, ok := raw["card"].(map[string]any); ok {
|
||||
mergeReadableCardFields(content, cardRaw)
|
||||
}
|
||||
if deviceRaw, ok := raw["device"].(map[string]any); ok {
|
||||
mergeReadableDeviceFields(content, deviceRaw)
|
||||
}
|
||||
|
||||
switch cardsRaw := raw["cards"].(type) {
|
||||
case []map[string]any:
|
||||
cardIDs, iccids := collectCardReadableLists(cardsRaw)
|
||||
if len(cardIDs) > 0 && content["card_ids"] == nil {
|
||||
content["card_ids"] = cardIDs
|
||||
}
|
||||
if len(iccids) > 0 && content["iccids"] == nil {
|
||||
content["iccids"] = iccids
|
||||
}
|
||||
case []any:
|
||||
cards := make([]map[string]any, 0, len(cardsRaw))
|
||||
for _, item := range cardsRaw {
|
||||
cardMap, ok := item.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
cards = append(cards, cardMap)
|
||||
}
|
||||
cardIDs, iccids := collectCardReadableLists(cards)
|
||||
if len(cardIDs) > 0 && content["card_ids"] == nil {
|
||||
content["card_ids"] = cardIDs
|
||||
}
|
||||
if len(iccids) > 0 && content["iccids"] == nil {
|
||||
content["iccids"] = iccids
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mergeReadableCardFields(content map[string]any, card map[string]any) {
|
||||
if len(card) == 0 {
|
||||
return
|
||||
}
|
||||
if _, ok := content["card_id"]; !ok {
|
||||
if v, exists := card["id"]; exists {
|
||||
content["card_id"] = v
|
||||
}
|
||||
}
|
||||
if _, ok := content["iccid"]; !ok {
|
||||
if v := stringifyCardAuditValue(card["iccid"]); v != "" {
|
||||
content["iccid"] = v
|
||||
}
|
||||
}
|
||||
if _, ok := content["device_virtual_no"]; !ok {
|
||||
if v := stringifyCardAuditValue(card["device_virtual_no"]); v != "" {
|
||||
content["device_virtual_no"] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 := stringifyCardAuditValue(device["virtual_no"]); v != "" {
|
||||
content["device_virtual_no"] = v
|
||||
}
|
||||
}
|
||||
if _, ok := content["device_imei"]; !ok {
|
||||
if v := stringifyCardAuditValue(device["imei"]); v != "" {
|
||||
content["device_imei"] = v
|
||||
}
|
||||
}
|
||||
if _, ok := content["device_sn"]; !ok {
|
||||
if v := stringifyCardAuditValue(device["sn"]); v != "" {
|
||||
content["device_sn"] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func collectCardReadableLists(cards []map[string]any) ([]any, []string) {
|
||||
cardIDs := make([]any, 0, len(cards))
|
||||
iccids := make([]string, 0, len(cards))
|
||||
for _, card := range cards {
|
||||
if id, ok := card["id"]; ok {
|
||||
cardIDs = append(cardIDs, id)
|
||||
}
|
||||
if iccid := stringifyCardAuditValue(card["iccid"]); iccid != "" {
|
||||
iccids = append(iccids, iccid)
|
||||
}
|
||||
}
|
||||
return cardIDs, iccids
|
||||
}
|
||||
|
||||
func stringifyCardAuditValue(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 cardSnapshot(card *model.IotCard) map[string]any {
|
||||
if card == nil {
|
||||
return nil
|
||||
}
|
||||
return map[string]any{
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"shop_id": card.ShopID,
|
||||
"status": card.Status,
|
||||
"series_id": card.SeriesID,
|
||||
"enable_polling": card.EnablePolling,
|
||||
"realname_policy": card.RealnamePolicy,
|
||||
"real_name_status": card.RealNameStatus,
|
||||
"network_status": card.NetworkStatus,
|
||||
"stop_reason": card.StopReason,
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"device_virtual_no": card.DeviceVirtualNo,
|
||||
"shop_id": card.ShopID,
|
||||
"status": card.Status,
|
||||
"series_id": card.SeriesID,
|
||||
"enable_polling": card.EnablePolling,
|
||||
"realname_policy": card.RealnamePolicy,
|
||||
"real_name_status": card.RealNameStatus,
|
||||
"network_status": card.NetworkStatus,
|
||||
"stop_reason": card.StopReason,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,13 +494,16 @@ func (s *Service) AllocateCards(ctx context.Context, req *dto.AllocateStandalone
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := s.loadShopNames(ctx, cards)
|
||||
targetShopName := s.getAuditShopName(ctx, &req.ToShopID)
|
||||
beforeData := make([]map[string]any, 0, len(cards))
|
||||
for _, card := range cards {
|
||||
beforeData = append(beforeData, map[string]any{
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"shop_id": card.ShopID,
|
||||
"status": card.Status,
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"shop_id": card.ShopID,
|
||||
"shop_name": shopMapValue(shopMap, card.ShopID),
|
||||
"status": card.Status,
|
||||
})
|
||||
}
|
||||
s.logCardAudit(ctx, assetAuditSvc.BuildLogParams{
|
||||
@@ -515,6 +518,7 @@ func (s *Service) AllocateCards(ctx context.Context, req *dto.AllocateStandalone
|
||||
},
|
||||
AfterData: map[string]any{
|
||||
"to_shop_id": req.ToShopID,
|
||||
"to_shop_name": targetShopName,
|
||||
"new_status": newStatus,
|
||||
"failed_items": failedItems,
|
||||
},
|
||||
@@ -702,13 +706,16 @@ func (s *Service) RecallCards(ctx context.Context, req *dto.RecallStandaloneCard
|
||||
}
|
||||
}
|
||||
|
||||
shopMap := s.loadShopNames(ctx, successCards)
|
||||
targetShopName := s.getAuditRecallTargetShopName(ctx, newShopID)
|
||||
beforeData := make([]map[string]any, 0, len(successCards))
|
||||
for _, card := range successCards {
|
||||
beforeData = append(beforeData, map[string]any{
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"shop_id": card.ShopID,
|
||||
"status": card.Status,
|
||||
"id": card.ID,
|
||||
"iccid": card.ICCID,
|
||||
"shop_id": card.ShopID,
|
||||
"shop_name": shopMapValue(shopMap, card.ShopID),
|
||||
"status": card.Status,
|
||||
})
|
||||
}
|
||||
s.logCardAudit(ctx, assetAuditSvc.BuildLogParams{
|
||||
@@ -722,9 +729,10 @@ func (s *Service) RecallCards(ctx context.Context, req *dto.RecallStandaloneCard
|
||||
"cards": beforeData,
|
||||
},
|
||||
AfterData: map[string]any{
|
||||
"to_shop_id": newShopID,
|
||||
"new_status": newStatus,
|
||||
"failed_items": failedItems,
|
||||
"to_shop_id": newShopID,
|
||||
"target_shop_name": targetShopName,
|
||||
"new_status": newStatus,
|
||||
"failed_items": failedItems,
|
||||
},
|
||||
})
|
||||
|
||||
@@ -774,6 +782,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 shopMapValue(shopMap map[uint]string, shopID *uint) string {
|
||||
if shopID == nil {
|
||||
return ""
|
||||
}
|
||||
return shopMap[*shopID]
|
||||
}
|
||||
|
||||
func (s *Service) getCardsForAllocation(ctx context.Context, req *dto.AllocateStandaloneCardsRequest, operatorShopID *uint) ([]*model.IotCard, error) {
|
||||
switch req.SelectionType {
|
||||
case dto.SelectionTypeList:
|
||||
@@ -1443,9 +1476,9 @@ func (s *Service) BatchUpdatePollingStatus(ctx context.Context, cardIDs []uint,
|
||||
BatchTotal: len(cardIDs),
|
||||
FailCount: len(cardIDs),
|
||||
AfterData: map[string]any{
|
||||
"card_ids": cardIDs,
|
||||
"enable_polling": enablePolling,
|
||||
"trigger_source": "batch",
|
||||
"card_ids": cardIDs,
|
||||
"enable_polling": enablePolling,
|
||||
"trigger_source": "batch",
|
||||
},
|
||||
})
|
||||
return err
|
||||
@@ -1857,11 +1890,11 @@ func (s *Service) ManualUpdateRealnameStatus(ctx context.Context, cardID uint, r
|
||||
AssetID: freshCard.ID,
|
||||
AssetIdentifier: freshCard.ICCID,
|
||||
BeforeData: map[string]any{
|
||||
"real_name_status": oldStatus,
|
||||
"real_name_status": oldStatus,
|
||||
"first_realname_at": card.FirstRealnameAt,
|
||||
},
|
||||
AfterData: map[string]any{
|
||||
"real_name_status": freshCard.RealNameStatus,
|
||||
"real_name_status": freshCard.RealNameStatus,
|
||||
"first_realname_at": freshCard.FirstRealnameAt,
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user