fix stage 7

This commit is contained in:
etreshenkov 2023-06-01 13:06:07 +03:00
parent baa61228fa
commit d1a9462029
4 changed files with 135 additions and 68 deletions

View file

@ -9,26 +9,31 @@ import ru.clearing.classes.statics.data.security.Security;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.platform.enumeration.CompanySymbol;
import ru.spcex.platform.enumeration.InOutDirection;
import ru.spcex.platform.enumeration.RegistryDesignation;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider;
import java.math.BigDecimal;
import java.time.Instant;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.Optional;
public class PaymentInstructionBuilder {
private Registry registry;
private List<Registry> registries;
private ImdgProvider imdgProvider;
private Long sessionId;
private BigDecimal amount;
public static PaymentInstructionBuilder builder(ImdgProvider imdgProvider, Registry registry) {
return new PaymentInstructionBuilder(imdgProvider, registry);
public static PaymentInstructionBuilder builder(ImdgProvider imdgProvider, List<Registry> registries) {
return new PaymentInstructionBuilder(imdgProvider, registries);
}
private PaymentInstructionBuilder(ImdgProvider imdgProvider, Registry registry) {
private PaymentInstructionBuilder(ImdgProvider imdgProvider, List<Registry> registries) {
this.imdgProvider = imdgProvider;
this.registry = registry;
this.registries = registries;
}
public PaymentInstructionBuilder sessionId(Long sessionId) {
@ -36,73 +41,100 @@ public class PaymentInstructionBuilder {
return this;
}
public PaymentInstructionBuilder amount(BigDecimal amount) {
this.amount = amount;
return this;
}
public PaymentInstruction build() {
Imdg<Account> accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
Imdg<Company> companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
Imdg<CompanySymbols> companySymbolsImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);
Imdg<Security> securityImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Security, Security.class);
Optional<Registry> registryLM = registries.stream()
.filter(rgst -> RegistryDesignation.L.equalsByKey(rgst.getRegistryDesignation()))
.findFirst();
Optional<Registry> registryCM = registries.stream()
.filter(rgst -> RegistryDesignation.C.equalsByKey(rgst.getRegistryDesignation()))
.findFirst();
PaymentInstruction paymentInstruction = new PaymentInstruction();
paymentInstruction.setSenderId(registry.getCompanyId());
paymentInstruction.setAddresseeId(registry.getCounterPartyId());
CompanySymbols companySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
"companyId", registry.getCompanyId(),
"companySymbol", CompanySymbol.BIC.getKey())
);
CompanySymbols counterCompanySymbols = null;
if (registry.getCounterPartyId() != null) {
counterCompanySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
"companyId", registry.getCounterPartyId(),
"companySymbol", CompanySymbol.BIC.getKey())
);
if (registryLM.isPresent()) {
Registry registry = registryLM.get();
paymentInstruction.setSenderId(registry.getCompanyId());
CompanySymbols companySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
"companyId", registry.getCompanyId(),
"companySymbol", CompanySymbol.BIC.getKey()));
if (companySymbols != null) {
paymentInstruction.setPayeeBic(companySymbols.getCompanySymbolValue());
}
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
if (company != null) {
paymentInstruction.setPayeeBankName(company.getShortName());
}
paymentInstruction.setPaymentDate(Instant.now());
paymentInstruction.setCreditLeg_accountId(registry.getAccountId());
paymentInstruction.setCreditLeg_amount(amount);
Account account = accountImdg.getSingleObjectByID(paymentInstruction.getCreditLeg_accountId());
if (account != null) {
paymentInstruction.setCreditLeg_account(account.getAccount());
}
Security security = securityImdg.getSingleObjectByID(registry.getSecurityId());
if (security != null) {
paymentInstruction.setCreditLeg_currencyCode(security.getSecuritySymbol());
paymentInstruction.setCreditLeg_securityId(security.getId());
}
paymentInstruction.setSettlementDate(registry.getSettlementDate());
}
if (companySymbols != null) {
paymentInstruction.setPayeeBic(companySymbols.getCompanySymbolValue());
}
if (counterCompanySymbols != null) {
paymentInstruction.setAdresseeBic(counterCompanySymbols.getCompanySymbolValue());
}
if (registryCM.isPresent()) {
Registry registry = registryCM.get();
paymentInstruction.setAddresseeId(registry.getCompanyId());
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
if (company != null) {
paymentInstruction.setPayeeBankName(company.getShortName());
paymentInstruction.setAddresseeBankName(company.getShortName());
}
paymentInstruction.setSettlementDate(registry.getSettlementDate());
paymentInstruction.setCreditLeg_amount(registry.getBalance());
paymentInstruction.setDebitLeg_amount(registry.getBalance());
CompanySymbols companySymbols = null;
if (registry.getCounterPartyId() != null) {
companySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
"companyId", registry.getCompanyId(),
"companySymbol", CompanySymbol.BIC.getKey())
);
}
if (companySymbols != null) {
paymentInstruction.setAdresseeBic(companySymbols.getCompanySymbolValue());
}
paymentInstruction.setCreditLeg_accountId(registry.getAccountId());//fixme контрагента?
Account account = accountImdg.getSingleObjectByID(paymentInstruction.getCreditLeg_accountId());
if (account != null) {
paymentInstruction.setCreditLeg_account(account.getAccount());
}
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
if (company != null) {
paymentInstruction.setAddresseeBankName(company.getShortName());
}
paymentInstruction.setDebitLeg_accountId(registry.getAccountId()); //fixme компании?
account = accountImdg.getSingleObjectByID(paymentInstruction.getDebitLeg_accountId());
if (account != null) {
paymentInstruction.setDebitLeg_account(account.getAccount());
paymentInstruction.setDebitLeg_accountId(registry.getAccountId());
Account account = accountImdg.getSingleObjectByID(registry.getAccountId());
if (account != null) {
paymentInstruction.setDebitLeg_account(account.getAccount());
}
paymentInstruction.setDebitLeg_amount(amount);
Security security = securityImdg.getSingleObjectByID(registry.getSecurityId());
if (security != null) {
paymentInstruction.setDebitLeg_currencyCode(security.getSecuritySymbol());
paymentInstruction.setDebitLeg_securityId(security.getId());
}
}
paymentInstruction.setCreditLeg_direction(InOutDirection.out.getKey());
paymentInstruction.setDebitLeg_direction(InOutDirection.in.getKey());
Security security = securityImdg.getSingleObjectByID(registry.getSecurityId());
if (security != null) {
//fixme тут точно что-то не то
// paymentInstruction.setCreditLeg_currencyCode(security.getSecuritySymbol());
paymentInstruction.setCreditLeg_securityId(security.getId());
// paymentInstruction.setDebitLeg_currencyCode(security.getSecuritySymbol());
paymentInstruction.setDebitLeg_securityId(security.getId());
}
paymentInstruction.setTransactionStatus("STLD");
paymentInstruction.setCreated(Instant.now());
paymentInstruction.setClearingDate(LocalDate.now());
paymentInstruction.setSessionId(sessionId);
return paymentInstruction;
}
}

View file

@ -144,9 +144,14 @@ public class PrimaryAuctionBnSession extends AbstractSession implements Initiali
}
//stage 6
runStage(TaskType.FormingRegistersOnOS, formingRegistersOnOS); //returns Collection<Registry>
//stage 7
StageResult<Collection<PaymentInstruction>> paymentResult = runStage(TaskType.FormingPaymentInstruction, formingPaymentInstruction);
if (paymentResult.getStageResult().isEmpty()) {
StageResult<Collection<PaymentInstruction>> paymentResult = null;
{
FormingPaymentInstructionPayload payload = new FormingPaymentInstructionPayload();
payload.setSessionId(currSession.getId());
//stage 7
paymentResult = runStage(TaskType.FormingPaymentInstruction, payload, formingPaymentInstruction);
}
if (paymentResult != null && paymentResult.getStageResult().isEmpty()) {
runStage(TaskType.FormingPaymentInstruction, balanceRevise);
// finishPart(req);
}

View file

@ -21,6 +21,7 @@ import ru.spcex.clearing.service.builder.PaymentInstructionBuilder;
import ru.spcex.clearing.session.stage.ISessionStage;
import ru.spcex.clearing.session.stage.StageResult;
import ru.spcex.clearing.session.stage.Task;
import ru.spcex.clearing.session.stage.task.FormingPaymentInstructionPayload;
import ru.spcex.platform.enumeration.*;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgId;
@ -32,9 +33,8 @@ import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@ -70,9 +70,10 @@ public class FormingPaymentInstruction implements ISessionStage {
@Override
public StageResult submit(Task<?> task) {
FormingPaymentInstructionPayload payload = (FormingPaymentInstructionPayload) task.getData();
switch (task.getTaskType()) {
case FormingPaymentInstruction -> {
return formingPaymentInstruction();
return formingPaymentInstruction(payload.getSessionId());
}
default -> {
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
@ -81,11 +82,11 @@ public class FormingPaymentInstruction implements ISessionStage {
}
private StageResult formingPaymentInstruction() {
private StageResult formingPaymentInstruction(Long sessionId) {
RegistryTradingParams registryTradingParamsL = new RegistryTradingParams(RegistryDesignation.L,
RegistryInstrumentType.S, null, RegistryUnit.T);
RegistryTradingParams registryTradingParamsC = new RegistryTradingParams(RegistryDesignation.C,
RegistryInstrumentType.M, null, null);
RegistryInstrumentType.M, null, RegistryUnit.T);
RegistryCodeSqlBuilder registryCodeSqlBuilder = RegistryCodeSqlBuilder.getInstance(registryTradingParamsL, registryTradingParamsC);
String registryCodeCondition = registryCodeSqlBuilder.build();
@ -227,20 +228,34 @@ public class FormingPaymentInstruction implements ISessionStage {
}
List<PaymentInstruction> formedPaymentInstructions = new ArrayList<>();
for (Registry obligation : obligations) {
Map<RegistryInstrumentType, List<Registry>> registryByInstrumentType = obligations.stream()
.collect(Collectors.groupingBy(registry -> RegistryInstrumentType.valueOf(registry.getRegistryInstrumentType())));
for (Map.Entry<RegistryInstrumentType, List<Registry>> entry : registryByInstrumentType.entrySet()) {
List<Registry> registriesLC = entry.getValue();
Optional<Registry> registryLOptional = registriesLC.stream()
.filter(rgst -> RegistryDesignation.L.equalsByKey(rgst.getRegistryDesignation()))
.findFirst();
if (registryLOptional.isEmpty()) {
log.warn("Not found registryL; registriesLC {}", registriesLC);
continue;
}
Registry registryL = registryLOptional.get();
RegistryTradingParams registryTradingParamsLT = new RegistryTradingParams(RegistryDesignation.L,
null, null, RegistryUnit.T);
registryCodeCondition = RegistryCodeSqlBuilder.getInstance(registryTradingParamsLT).build();
String sqlCondition = String.format("(%s) and securityId = %s and tradingClearingRegistryId = %s and companyId = %s and counterPartyId = %s",
registryCodeCondition, obligation.getSecurityId(), obligation.getTradingClearingRegistryId(), obligation.getCompanyId(), obligation.getCounterPartyId());
registryCodeCondition, registryL.getSecurityId(), registryL.getTradingClearingRegistryId(), registryL.getCompanyId(), registryL.getCounterPartyId());
Collection<Registry> registries = registryImdg.getCollectionObjectsBySQL(sqlCondition);
BigDecimal sumBalance = registries.stream().map(Registry::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add);
PaymentInstruction paymentInstruction = createPaymentInstruction(obligation, sumBalance);
PaymentInstruction paymentInstruction = createPaymentInstruction(registriesLC, sumBalance, sessionId);
formedPaymentInstructions.add(paymentInstruction);
paymentInstructionImdg.insert(paymentInstruction);
obligation.setPaymentId(paymentInstruction.getId());
registryImdg.update(obligation);
registriesLC.forEach(registry -> {
registry.setPaymentId(paymentInstruction.getId());
registry.setUpdated(Instant.now());
registryImdg.update(registry);
});
}
sendSdfs(formedPaymentInstructions);
@ -282,8 +297,10 @@ public class FormingPaymentInstruction implements ISessionStage {
}
}
private PaymentInstruction createPaymentInstruction(Registry registry, BigDecimal balance) {
PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registry);
private PaymentInstruction createPaymentInstruction(List<Registry> registries, BigDecimal balance, Long sessionId) {
PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registries)
.sessionId(sessionId)
.amount(balance);
return paymentInstructionBuilder.build();
}

View file

@ -0,0 +1,13 @@
package ru.spcex.clearing.session.stage.task;
public class FormingPaymentInstructionPayload {
private Long sessionId;
public Long getSessionId() {
return sessionId;
}
public void setSessionId(Long sessionId) {
this.sessionId = sessionId;
}
}