55import io .github .timemachinelab .core .qatree .QaTree ;
66import io .github .timemachinelab .core .qatree .QaTreeNode ;
77import io .github .timemachinelab .core .serializable .JsonNode ;
8+ import io .github .timemachinelab .core .question .*;
9+ import io .github .timemachinelab .core .serializable .TempFormQuestion ;
810
911import java .util .ArrayList ;
1012import java .util .List ;
13+ import java .util .Map ;
14+ import java .util .HashMap ;
1115
1216public class QaTreeSerializeUtil {
1317
@@ -16,33 +20,210 @@ public static String serialize(QaTree t) throws JsonProcessingException {
1620 return "[]" ;
1721 }
1822
19- List <JsonNode > result = new ArrayList <>();
23+ List <Map < String , Object > > result = new ArrayList <>();
2024
21- firstOrderTraversal (t .getRoot (), null , result );
25+ firstOrderTraversalEnhanced (t .getRoot (), null , result );
2226
2327 return JSONObject .toJSONString (result );
2428 }
2529
26- private static void firstOrderTraversal (QaTreeNode node , String parentId , List <JsonNode > result ) throws JsonProcessingException {
30+ /**
31+ * 增强版遍历方法,返回SSE兼容的格式
32+ */
33+ private static void firstOrderTraversalEnhanced (QaTreeNode node , String parentId , List <Map <String , Object >> result ) throws JsonProcessingException {
2734 if (node == null ) {
2835 return ;
2936 }
3037
3138 // 获取子节点列表
3239 List <QaTreeNode > children = new ArrayList <>();
33-
3440 if (node .getChildren () != null ) {
3541 children .addAll (node .getChildren ().values ());
3642 }
3743
38- // 访问当前节点
39- JsonNode jsonNode = JsonNode .Convert2JsonNode (node , parentId );
40-
41- result .add (jsonNode );
44+ // 创建增强的节点数据
45+ Map <String , Object > enhancedNode = createEnhancedNode (node , parentId );
46+ result .add (enhancedNode );
4247
4348 // 先序遍历
4449 for (QaTreeNode child : children ) {
45- firstOrderTraversal (child , node .getId (), result );
50+ firstOrderTraversalEnhanced (child , node .getId (), result );
4651 }
4752 }
53+
54+ /**
55+ * 创建SSE兼容的增强节点数据
56+ */
57+ private static Map <String , Object > createEnhancedNode (QaTreeNode node , String parentId ) {
58+ Map <String , Object > enhancedNode = new HashMap <>();
59+ enhancedNode .put ("nodeId" , node .getId ());
60+ enhancedNode .put ("parentId" , parentId );
61+
62+ String answer = "" ;
63+ Map <String , Object > questionData = null ;
64+
65+ BaseQuestion qa = node .getQa ();
66+ if (qa != null ) {
67+ // 根据问题类型创建questionData
68+ QuestionType type = QuestionType .fromString (qa .getType ());
69+ switch (type ) {
70+ case INPUT :
71+ InputQuestion inputQA = (InputQuestion ) qa ;
72+ questionData = createInputQuestionData (inputQA );
73+ answer = inputQA .getAnswer () != null ? inputQA .getAnswer () : "" ;
74+ break ;
75+ case SINGLE :
76+ SingleChoiceQuestion singleQA = (SingleChoiceQuestion ) qa ;
77+ questionData = createSingleQuestionData (singleQA );
78+ answer = formatSingleAnswer (singleQA );
79+ break ;
80+ case MULTI :
81+ MultipleChoiceQuestion multiQA = (MultipleChoiceQuestion ) qa ;
82+ questionData = createMultiQuestionData (multiQA );
83+ answer = formatMultiAnswer (multiQA );
84+ break ;
85+ case FORM :
86+ FormQuestion formQA = (FormQuestion ) qa ;
87+ questionData = createFormQuestionData (formQA );
88+ answer = formQA .getAnswer () != null ? JSONObject .toJSONString (formQA .getAnswer ()) : "" ;
89+ break ;
90+ default :
91+ // 普通文本问题
92+ questionData = createTextQuestionData (qa .getQuestion ());
93+ break ;
94+ }
95+ }
96+
97+ enhancedNode .put ("questionData" , questionData );
98+ enhancedNode .put ("answer" , answer );
99+
100+ return enhancedNode ;
101+ }
102+
103+ /**
104+ * 创建输入问题数据
105+ */
106+ private static Map <String , Object > createInputQuestionData (InputQuestion inputQA ) {
107+ Map <String , Object > questionData = new HashMap <>();
108+ questionData .put ("type" , "input" );
109+ questionData .put ("question" , inputQA .getQuestion () != null ? inputQA .getQuestion () : "" );
110+ questionData .put ("desc" , inputQA .getDesc () != null ? inputQA .getDesc () : "" );
111+ return questionData ;
112+ }
113+
114+ /**
115+ * 创建单选问题数据
116+ */
117+ private static Map <String , Object > createSingleQuestionData (SingleChoiceQuestion singleQA ) {
118+ Map <String , Object > questionData = new HashMap <>();
119+ questionData .put ("type" , "single" );
120+ questionData .put ("question" , singleQA .getQuestion () != null ? singleQA .getQuestion () : "" );
121+ questionData .put ("desc" , singleQA .getDesc () != null ? singleQA .getDesc () : "" );
122+ questionData .put ("options" , singleQA .getOptions () != null ? singleQA .getOptions () : new ArrayList <>());
123+ return questionData ;
124+ }
125+
126+ /**
127+ * 创建多选问题数据
128+ */
129+ private static Map <String , Object > createMultiQuestionData (MultipleChoiceQuestion multiQA ) {
130+ Map <String , Object > questionData = new HashMap <>();
131+ questionData .put ("type" , "multi" );
132+ questionData .put ("question" , multiQA .getQuestion () != null ? multiQA .getQuestion () : "" );
133+ questionData .put ("desc" , multiQA .getDesc () != null ? multiQA .getDesc () : "" );
134+ questionData .put ("options" , multiQA .getOptions () != null ? multiQA .getOptions () : new ArrayList <>());
135+ return questionData ;
136+ }
137+
138+ /**
139+ * 创建表单问题数据
140+ */
141+ private static Map <String , Object > createFormQuestionData (FormQuestion formQA ) {
142+ Map <String , Object > questionData = new HashMap <>();
143+ questionData .put ("type" , "form" );
144+ questionData .put ("question" , formQA .getQuestion () != null ? formQA .getQuestion () : "" );
145+ questionData .put ("desc" , formQA .getDesc () != null ? formQA .getDesc () : "" );
146+ questionData .put ("fields" , formQA .getFields () != null ? formQA .getFields () : new ArrayList <>());
147+ return questionData ;
148+ }
149+
150+ /**
151+ * 创建普通文本问题数据
152+ */
153+ private static Map <String , Object > createTextQuestionData (String question ) {
154+ Map <String , Object > questionData = new HashMap <>();
155+ questionData .put ("type" , "text" );
156+ questionData .put ("question" , question != null ? question : "" );
157+ questionData .put ("desc" , "" );
158+ return questionData ;
159+ }
160+
161+ /**
162+ * 格式化单选答案
163+ */
164+ private static String formatSingleAnswer (SingleChoiceQuestion singleQA ) {
165+ if (singleQA .getAnswer () != null && !singleQA .getAnswer ().isEmpty ()) {
166+ List <String > answerLabels = new ArrayList <>();
167+ for (String answerId : singleQA .getAnswer ()) {
168+ String label = findOptionLabel (singleQA .getOptions (), answerId );
169+ answerLabels .add (label != null ? label : answerId );
170+ }
171+ return String .join ("," , answerLabels );
172+ }
173+ return "" ;
174+ }
175+
176+ /**
177+ * 格式化多选答案
178+ */
179+ private static String formatMultiAnswer (MultipleChoiceQuestion multiQA ) {
180+ if (multiQA .getAnswer () != null && !multiQA .getAnswer ().isEmpty ()) {
181+ List <String > answerLabels = new ArrayList <>();
182+ for (String answerId : multiQA .getAnswer ()) {
183+ String label = findOptionLabel (multiQA .getOptions (), answerId );
184+ answerLabels .add (label != null ? label : answerId );
185+ }
186+ return String .join ("," , answerLabels );
187+ }
188+ return "" ;
189+ }
190+
191+ /**
192+ * 根据选项id查找对应的标签
193+ */
194+ private static String findOptionLabel (List <Option > options , String id ) {
195+ if (options == null || id == null ) {
196+ return null ;
197+ }
198+ for (Option option : options ) {
199+ if (id .equals (option .getId ())) {
200+ return option .getLabel ();
201+ }
202+ }
203+ return null ;
204+ }
205+
206+ // 保留原有的序列化方法作为备用
207+ private static void firstOrderTraversal (QaTreeNode node , String parentId , List <JsonNode > result ) throws JsonProcessingException {
208+ if (node == null ) {
209+ return ;
210+ }
211+
212+ // 获取子节点列表
213+ List <QaTreeNode > children = new ArrayList <>();
214+
215+ if (node .getChildren () != null ) {
216+ children .addAll (node .getChildren ().values ());
217+ }
218+
219+ // 访问当前节点
220+ JsonNode jsonNode = JsonNode .Convert2JsonNode (node , parentId );
221+
222+ result .add (jsonNode );
223+
224+ // 先序遍历
225+ for (QaTreeNode child : children ) {
226+ firstOrderTraversal (child , node .getId (), result );
227+ }
228+ }
48229}
0 commit comments