1+ package io .github .timemachinelab .core .serializable ;
2+
3+ import io .github .timemachinelab .core .qatree .*;
4+ import io .github .timemachinelab .core .question .*;
5+ import lombok .Builder ;
6+ import lombok .Data ;
7+
8+ import com .fasterxml .jackson .databind .ObjectMapper ;
9+
10+ import com .alibaba .fastjson2 .JSONObject ;
11+ import com .fasterxml .jackson .core .JsonProcessingException ;
12+ import lombok .Getter ;
13+
14+ import java .util .*;
15+
16+ @ Getter
17+ @ Builder
18+ public class JsonNode {
19+ String nodeId ;
20+ String parentId ;
21+ String question ;
22+ String answer ;
23+
24+ public static JsonNode Convert2JsonNode (QaTreeNode node , String parentId ) {
25+ String question = "" ;
26+ String answer = "" ;
27+
28+ BaseQuestion qa = node .getQa ();
29+ if (qa != null ) {
30+ question = qa .getQuestion () != null ? qa .getQuestion () : "" ;
31+
32+ // 根据问题类型获取答案
33+ QuestionType type = QuestionType .fromString (qa .getType ());
34+ switch (type ) {
35+ case INPUT :
36+ InputQuestion inputQA = (InputQuestion ) qa ;
37+ answer = inputQA .getAnswer () != null ? inputQA .getAnswer () : "" ;
38+ break ;
39+ case SINGLE :
40+ SingleChoiceQuestion singleQA = (SingleChoiceQuestion ) qa ;
41+ if (singleQA .getAnswer () != null && !singleQA .getAnswer ().isEmpty ()) {
42+ List <String > answerLabels = new ArrayList <>();
43+ for (String answerId : singleQA .getAnswer ()) {
44+ String label = findOptionLabel (singleQA .getOptions (), answerId );
45+ answerLabels .add (label != null ? label : answerId );
46+ }
47+ answer = String .join ("," , answerLabels );
48+ }
49+ break ;
50+ case MULTI :
51+ MultipleChoiceQuestion multiQA = (MultipleChoiceQuestion ) qa ;
52+ if (multiQA .getAnswer () != null && !multiQA .getAnswer ().isEmpty ()) {
53+ List <String > answerLabels = new ArrayList <>();
54+ for (String answerId : multiQA .getAnswer ()) {
55+ String label = findOptionLabel (multiQA .getOptions (), answerId );
56+ answerLabels .add (label != null ? label : answerId );
57+ }
58+ answer = String .join ("," , answerLabels );
59+ }
60+ break ;
61+ case FORM :
62+ FormQuestion formQA = (FormQuestion ) qa ;
63+ // 将FormField转换为TempFormQuestion的JSON格式拼接到question后面
64+ if (formQA .getFields () != null && !formQA .getFields ().isEmpty ()) {
65+ List <TempFormQuestion > tempQuestions = formQA .getFields ().stream ()
66+ .map (field -> {
67+ return TempFormQuestion .builder ()
68+ .id (field .getId ())
69+ .question (field .getQuestion ())
70+ .type (field .getType ())
71+ .options (field .getOptions ())
72+ .desc (field .getDesc ())
73+ .build ();
74+ })
75+ .collect (java .util .stream .Collectors .toList ());
76+ String fieldsJson = JSONObject .toJSONString (tempQuestions );
77+ question = question + ":" + fieldsJson ;
78+ }
79+ // 将AnswerItem转换为JSON格式作为answer
80+ if (formQA .getAnswer () != null && !formQA .getAnswer ().isEmpty ()) {
81+ answer = JSONObject .toJSONString (formQA .getAnswer ());
82+ }
83+ break ;
84+ default :
85+ break ;
86+ }
87+ }
88+
89+ return JsonNode .builder ()
90+ .nodeId (node .getId ())
91+ .parentId (parentId )
92+ .question (question )
93+ .answer (answer )
94+ .build ();
95+ }
96+
97+ /**
98+ * 根据选项id查找对应的标签
99+ * @param options 选项列表
100+ * @param id 选项id
101+ * @return 选项标签,如果未找到则返回null
102+ */
103+ private static String findOptionLabel (List <Option > options , String id ) {
104+ if (options == null || id == null ) {
105+ return null ;
106+ }
107+ for (Option option : options ) {
108+ if (id .equals (option .getId ())) {
109+ return option .getLabel ();
110+ }
111+ }
112+ return null ;
113+ }
114+ }
0 commit comments