diff --git a/src/main/java/com/chint/application/dtos/response/LegRes.java b/src/main/java/com/chint/application/dtos/response/LegRes.java index 5895a117..da5e082b 100644 --- a/src/main/java/com/chint/application/dtos/response/LegRes.java +++ b/src/main/java/com/chint/application/dtos/response/LegRes.java @@ -8,11 +8,14 @@ import com.chint.domain.aggregates.order.Location; import com.chint.domain.aggregates.order.OrderDetail; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import org.jetbrains.annotations.NotNull; import org.springframework.data.annotation.Id; import java.time.format.DateTimeFormatter; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static com.chint.infrastructure.constant.LegConstant.LEG_TYPE_OTHER; import static com.chint.infrastructure.constant.LegConstant.LEG_TYPE_TAXI; @@ -99,26 +102,36 @@ public class LegRes { legRes.setSupplierNameList(new ArrayList<>()); } + StringBuilder sb = getStringBuilder(leg, originLocation, destinationLocation); + legRes.setLocationNameList(sb.toString()); + + return legRes; + } + + private static @NotNull StringBuilder getStringBuilder(Leg leg, + Location originLocation, + Location destinationLocation) { StringBuilder sb = new StringBuilder(); + Set locationSet = new HashSet<>(); if (leg.getLegType().equals(LEG_TYPE_TAXI) || leg.getLegType().equals(LEG_TYPE_OTHER)) { List locationList = leg.getLegExtensionField().getLocationList(); if (locationList != null && !locationList.isEmpty()) { for (Location location : locationList) { - sb.append(location.getLocationName()); - sb.append(","); + locationSet.add(location.getLocationName()); } - // Remove the last comma - if (!sb.isEmpty()) { - sb.setLength(sb.length() - 1); - } - legRes.setLocationNameList(sb.toString()); } } else { - sb.append(originLocation.getLocationName()); - sb.append(","); - sb.append(destinationLocation.getLocationName()); + locationSet.add(originLocation.getLocationName()); + locationSet.add(destinationLocation.getLocationName()); } - legRes.setLocationNameList(sb.toString()); - return legRes; + for (String locationName : locationSet) { + sb.append(locationName); + sb.append(","); + } + // Remove the last comma if StringBuilder is not empty + if (!sb.isEmpty()) { + sb.setLength(sb.length() - 1); + } + return sb; } }