|
|
@@ -13,13 +13,13 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Repository;
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
-import java.util.Set;
|
|
|
+import java.time.Instant;
|
|
|
+import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
import static java.time.LocalDateTime.now;
|
|
|
+import static java.util.stream.Collectors.toList;
|
|
|
|
|
|
@Slf4j
|
|
|
@Repository
|
|
|
@@ -48,11 +48,32 @@ public class StockRepository {
|
|
|
return quote.orElseGet(() -> repository.save(getStockQuote(code)));
|
|
|
}
|
|
|
|
|
|
- public List<Quote> update(List<Quote> quote) {
|
|
|
- return repository.saveAll(quote.stream().peek(i -> {
|
|
|
- Quote current = repository.findByCode(i.getCode()).orElse(i);
|
|
|
- i.setId(current.getId());
|
|
|
- }).toList());
|
|
|
+ public List<Quote> update(List<Quote> quotes) {
|
|
|
+ log.info("Updating [{}] quotes [{}]", quotes.size(), Instant.now());
|
|
|
+ List<String> codes = quotes.stream()
|
|
|
+ .map(Quote::getCode)
|
|
|
+ .collect(toList());
|
|
|
+ Map<String, Quote> existingQuotesMap = repository.findByCodeIn(codes)
|
|
|
+ .stream()
|
|
|
+ .collect(Collectors.toMap(Quote::getCode, Function.identity()));
|
|
|
+
|
|
|
+ log.info("Found [{}] quotes by codes [{}]", existingQuotesMap.size(), Instant.now());
|
|
|
+
|
|
|
+ List<Quote> updatedQuotes = quotes.stream()
|
|
|
+ .map(quote -> {
|
|
|
+ Quote existingQuote = existingQuotesMap.get(quote.getCode());
|
|
|
+ if (existingQuote != null) {
|
|
|
+ quote.setId(existingQuote.getId());
|
|
|
+ }
|
|
|
+ return quote;
|
|
|
+ })
|
|
|
+ .collect(toList());
|
|
|
+
|
|
|
+ log.info("Updating [{}] quotes [{}]", updatedQuotes.size(), Instant.now());
|
|
|
+ List<Quote> result = repository.saveAll(updatedQuotes);
|
|
|
+ log.info("Update complete for [{}] quotes [{}]", quotes.size(), Instant.now());
|
|
|
+
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
public boolean isValid(String code) {
|
|
|
@@ -70,7 +91,7 @@ public class StockRepository {
|
|
|
}
|
|
|
|
|
|
public Quote getStockQuote(String code) {
|
|
|
- Quote quote = repository.findByCode(code).stream().findFirst().orElse(new Quote(code, null, null, now()));
|
|
|
+ Quote quote = repository.findByCode(code).stream().findFirst().orElse(new Quote(code, null, null, null, now()));
|
|
|
quote.setPrice(getLastPrice(quote));
|
|
|
|
|
|
if (quote.getName() == null || quote.getPrice() == null) {
|