This commit is contained in:
parent
0227e34269
commit
8a1497f952
1 changed files with 40 additions and 12 deletions
|
|
@ -3,32 +3,59 @@ package ru.spcex.clearing.dflt.management.service;
|
|||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.StringJoiner;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.clearing.classes.statics.data.misc.OrderCurrency;
|
||||
import ru.clearing.classes.statics.data.security.Rates;
|
||||
import ru.clearing.platform.dictionary.CurrencyPairDictionary;
|
||||
import ru.spcex.clearing.platform.messaging.domain.Consts;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.utilities.NotificationNewRequest;
|
||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
||||
import ru.spcex.platform.enumeration.ObjectType;
|
||||
import ru.spcex.platform.enumeration.OvernightType;
|
||||
import ru.spcex.platform.enumeration.Priority;
|
||||
import ru.spcex.platform.enumeration.Side;
|
||||
|
||||
@Service
|
||||
public class OrderCurrencyTriExportService {
|
||||
private static final Logger log = LoggerFactory.getLogger(OrderCurrencyTriExportService.class);
|
||||
private final ImdgQueryService imdgQueryService;
|
||||
private final KafkaSender kafkaSender;
|
||||
private static final String ACTION = "Ввод внебиржевой заявки";
|
||||
|
||||
public OrderCurrencyTriExportService(
|
||||
ImdgQueryService imdgQueryService) {
|
||||
public OrderCurrencyTriExportService(ImdgQueryService imdgQueryService,
|
||||
KafkaSender kafkaSender) {
|
||||
this.imdgQueryService = imdgQueryService;
|
||||
this.kafkaSender = kafkaSender;
|
||||
}
|
||||
|
||||
/**
|
||||
* Возвращает содержимое tri-файла в виде строки.
|
||||
*/
|
||||
public String exportToTri(Collection<OrderCurrency> orders) {
|
||||
return orders.stream()
|
||||
.map(this::toTriLine)
|
||||
boolean allSuccess = true;
|
||||
List<String> rows = new ArrayList<>();
|
||||
for (OrderCurrency orderCurrency : orders) {
|
||||
String baseCurrency = resolveBaseRate(orderCurrency);
|
||||
if (OvernightType.SWAP.equalsByKey(orderCurrency.getOvernightType()) && baseCurrency == null) {
|
||||
allSuccess = false;
|
||||
}
|
||||
rows.add(toTriLine(orderCurrency, baseCurrency));
|
||||
}
|
||||
if (!allSuccess) {
|
||||
String comment = "При формировании файла с заявками не определен Базовый курс";
|
||||
NotificationNewRequest newReq = new NotificationNewRequest();
|
||||
newReq.setObjectType(ObjectType.rgst.getKey());
|
||||
newReq.setPriority(Priority.HIGH.getKey());
|
||||
newReq.setComment(comment);
|
||||
kafkaSender.sendRequestToQueue(Consts.NOTIFICATION_NEW, newReq);
|
||||
}
|
||||
return rows.stream()
|
||||
.reduce((a, b) -> a + System.lineSeparator() + b)
|
||||
.orElse("");
|
||||
}
|
||||
|
|
@ -40,7 +67,7 @@ public class OrderCurrencyTriExportService {
|
|||
return exportToTri(orders).getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
private String toTriLine(OrderCurrency orderCurrency) {
|
||||
private String toTriLine(OrderCurrency orderCurrency, String baseCurrency) {
|
||||
StringJoiner joiner = new StringJoiner(";", "", ";");
|
||||
joiner.add("TRANS_ID=" + nullSafe(orderCurrency.getId()));
|
||||
joiner.add("CLASSCODE=" + nullSafe(orderCurrency.getMarket()));
|
||||
|
|
@ -56,7 +83,7 @@ public class OrderCurrencyTriExportService {
|
|||
joiner.add("Ссылка=");
|
||||
joiner.add("Код расчетов=" + nullSafe(orderCurrency.getSettleCode()));
|
||||
joiner.add("На заявку №=");
|
||||
joiner.add("Базовый курс=" + resolveBaseRate(orderCurrency));
|
||||
joiner.add("Базовый курс=" + nullSafe(baseCurrency));
|
||||
|
||||
return joiner.toString();
|
||||
}
|
||||
|
|
@ -68,22 +95,23 @@ public class OrderCurrencyTriExportService {
|
|||
if (Side.SELL.equalsByKey(side)) {
|
||||
return "Продажа";
|
||||
}
|
||||
throw new IllegalArgumentException("Неизвестное значение side: " + side);
|
||||
log.warn("Неизвестное значение side: {}", side);
|
||||
return "";
|
||||
}
|
||||
|
||||
private String resolveBaseRate(OrderCurrency orderCurrency) {
|
||||
public String resolveBaseRate(OrderCurrency orderCurrency) {
|
||||
if (OvernightType.SPOT.equalsByKey(orderCurrency.getOvernightType())) {
|
||||
return "";
|
||||
}
|
||||
|
||||
CurrencyPairDictionary currencyPairDictionary = imdgQueryService.getCurrencyPairDictionaryBySecurityId(orderCurrency.getSecurityId());
|
||||
String baseCurrency = currencyPairDictionary.getBaseCurrency();
|
||||
Rates rates = imdgQueryService.getRate(currencyPairDictionary.getBaseCurrency(), LocalDate.now());
|
||||
LocalDate now = LocalDate.now();
|
||||
Rates rates = imdgQueryService.getRate(currencyPairDictionary.getBaseCurrency(), now);
|
||||
|
||||
if (rates == null || rates.getValue() == null) {
|
||||
throw new IllegalStateException(
|
||||
"Не найден активный курс для валюты " + baseCurrency + " на дату " + LocalDate.now()
|
||||
);
|
||||
log.warn("Не найден активный курс для валюты {} на дату {}", baseCurrency, now);
|
||||
return null;
|
||||
}
|
||||
|
||||
return formatDecimal(rates.getValue());
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue