|
30 | 30 | import org.springframework.transaction.annotation.Transactional; |
31 | 31 |
|
32 | 32 | import java.time.LocalDateTime; |
| 33 | +import java.util.Comparator; |
33 | 34 | import java.util.List; |
34 | 35 | import java.util.Map; |
35 | 36 | import java.util.Optional; |
@@ -163,6 +164,39 @@ protected Long createScenarioInTransaction( |
163 | 164 | // lastDecision 처리 (필요 시) |
164 | 165 | if (lastDecision != null) { |
165 | 166 | decisionFlowService.createDecisionNodeNext(lastDecision); |
| 167 | + List<DecisionNode> ordered = decisionNodeRepository.findByDecisionLine_IdOrderByAgeYearAscIdAsc(decisionLine.getId()); |
| 168 | + DecisionNode parent = ordered.isEmpty() ? null : ordered.get(ordered.size() - 1); |
| 169 | + |
| 170 | + // 베이스 라인의 tail BaseNode 해석(“결말” 우선, 없으면 최대 age) |
| 171 | + BaseLine baseLine = decisionLine.getBaseLine(); |
| 172 | + List<BaseNode> baseNodes = baseLine.getBaseNodes(); |
| 173 | + BaseNode tailBase = baseNodes.stream() |
| 174 | + .filter(b -> { |
| 175 | + String s = b.getSituation() == null ? "" : b.getSituation(); |
| 176 | + String d = b.getDecision() == null ? "" : b.getDecision(); |
| 177 | + return s.contains("결말") || d.contains("결말"); |
| 178 | + }) |
| 179 | + .max(Comparator.comparingInt(BaseNode::getAgeYear).thenComparingLong(BaseNode::getId)) |
| 180 | + .orElseGet(() -> baseNodes.stream() |
| 181 | + .max(Comparator.comparingInt(BaseNode::getAgeYear).thenComparingLong(BaseNode::getId)) |
| 182 | + .orElseThrow(() -> new ApiException(ErrorCode.INVALID_INPUT_VALUE, "tail base not found")) |
| 183 | + ); |
| 184 | + |
| 185 | + // 엔티티 빌더로 ‘결말’ 결정노드 저장(테일과 동일 age) |
| 186 | + DecisionNode ending = DecisionNode.builder() |
| 187 | + .user(decisionLine.getUser()) |
| 188 | + .nodeKind(NodeType.DECISION) |
| 189 | + .decisionLine(decisionLine) |
| 190 | + .baseNode(tailBase) |
| 191 | + .parent(parent) |
| 192 | + .category(tailBase.getCategory()) |
| 193 | + .situation("결말") |
| 194 | + .decision("결말") |
| 195 | + .ageYear(tailBase.getAgeYear()) |
| 196 | + .background(tailBase.getSituation() == null ? "" : tailBase.getSituation()) |
| 197 | + .build(); |
| 198 | + |
| 199 | + decisionNodeRepository.save(ending); |
166 | 200 | } |
167 | 201 |
|
168 | 202 | // DecisionLine 완료 처리 |
|
0 commit comments