From d1a94620292534b064419456d6d95d4cb3d5a70a Mon Sep 17 00:00:00 2001 From: etreshenkov Date: Thu, 1 Jun 2023 13:06:07 +0300 Subject: [PATCH] fix stage 7 --- .../builder/PaymentInstructionBuilder.java | 136 +++++++++++------- .../stage/PrimaryAuctionBnSession.java | 11 +- .../stage/impl/FormingPaymentInstruction.java | 43 ++++-- .../FormingPaymentInstructionPayload.java | 13 ++ 4 files changed, 135 insertions(+), 68 deletions(-) create mode 100644 clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/task/FormingPaymentInstructionPayload.java diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilder.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilder.java index c902fe6e0..5487fdf99 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilder.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilder.java @@ -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 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 registries) { + return new PaymentInstructionBuilder(imdgProvider, registries); } - private PaymentInstructionBuilder(ImdgProvider imdgProvider, Registry registry) { + private PaymentInstructionBuilder(ImdgProvider imdgProvider, List 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 accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); Imdg companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class); Imdg companySymbolsImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class); Imdg securityImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Security, Security.class); + Optional registryLM = registries.stream() + .filter(rgst -> RegistryDesignation.L.equalsByKey(rgst.getRegistryDesignation())) + .findFirst(); + + Optional 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; } } diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/PrimaryAuctionBnSession.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/PrimaryAuctionBnSession.java index 194361762..c3904fc86 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/PrimaryAuctionBnSession.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/PrimaryAuctionBnSession.java @@ -144,9 +144,14 @@ public class PrimaryAuctionBnSession extends AbstractSession implements Initiali } //stage 6 runStage(TaskType.FormingRegistersOnOS, formingRegistersOnOS); //returns Collection - //stage 7 - StageResult> paymentResult = runStage(TaskType.FormingPaymentInstruction, formingPaymentInstruction); - if (paymentResult.getStageResult().isEmpty()) { + StageResult> 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); } diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java index 91df4f43a..b94b6bd5a 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java @@ -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 formedPaymentInstructions = new ArrayList<>(); - for (Registry obligation : obligations) { + Map> registryByInstrumentType = obligations.stream() + .collect(Collectors.groupingBy(registry -> RegistryInstrumentType.valueOf(registry.getRegistryInstrumentType()))); + for (Map.Entry> entry : registryByInstrumentType.entrySet()) { + List registriesLC = entry.getValue(); + Optional 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 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 registries, BigDecimal balance, Long sessionId) { + PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registries) + .sessionId(sessionId) + .amount(balance); return paymentInstructionBuilder.build(); } diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/task/FormingPaymentInstructionPayload.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/task/FormingPaymentInstructionPayload.java new file mode 100644 index 000000000..cb67d9adb --- /dev/null +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/task/FormingPaymentInstructionPayload.java @@ -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; + } +}