duidui_fiber/internal/camp/service/task_content.go
2026-03-27 10:34:03 +08:00

51 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"encoding/json"
"fmt"
)
// ExtractPaperIDFromTaskContent 从任务 content JSON 中解析试卷 ID支持 exam_id/paper_id/examId/paperId含数字
func ExtractPaperIDFromTaskContent(content json.RawMessage) string {
if len(content) == 0 {
return ""
}
var m map[string]interface{}
if err := json.Unmarshal(content, &m); err != nil {
return ""
}
paperID := stringFromMap(m, "exam_id", "paper_id", "examId", "paperId")
if paperID != "" {
return paperID
}
if obj, ok := m["objective"].(map[string]interface{}); ok {
paperID = stringFromMap(obj, "exam_id", "paper_id", "examId", "paperId")
}
return paperID
}
func stringFromMap(m map[string]interface{}, keys ...string) string {
for _, k := range keys {
v, ok := m[k]
if !ok || v == nil {
continue
}
switch val := v.(type) {
case string:
if val != "" {
return val
}
case float64:
if val == float64(int64(val)) {
return fmt.Sprintf("%d", int64(val))
}
return fmt.Sprintf("%.0f", val)
case int:
return fmt.Sprintf("%d", val)
case int64:
return fmt.Sprintf("%d", val)
}
}
return ""
}