fix stage 7
This commit is contained in:
parent
baa61228fa
commit
d1a9462029
4 changed files with 135 additions and 68 deletions
|
|
@ -9,26 +9,31 @@ import ru.clearing.classes.statics.data.security.Security;
|
||||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
import ru.spcex.platform.enumeration.CompanySymbol;
|
import ru.spcex.platform.enumeration.CompanySymbol;
|
||||||
import ru.spcex.platform.enumeration.InOutDirection;
|
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.Imdg;
|
||||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
public class PaymentInstructionBuilder {
|
public class PaymentInstructionBuilder {
|
||||||
|
|
||||||
private Registry registry;
|
private List<Registry> registries;
|
||||||
private ImdgProvider imdgProvider;
|
private ImdgProvider imdgProvider;
|
||||||
private Long sessionId;
|
private Long sessionId;
|
||||||
|
private BigDecimal amount;
|
||||||
|
|
||||||
public static PaymentInstructionBuilder builder(ImdgProvider imdgProvider, Registry registry) {
|
public static PaymentInstructionBuilder builder(ImdgProvider imdgProvider, List<Registry> registries) {
|
||||||
return new PaymentInstructionBuilder(imdgProvider, registry);
|
return new PaymentInstructionBuilder(imdgProvider, registries);
|
||||||
}
|
}
|
||||||
|
|
||||||
private PaymentInstructionBuilder(ImdgProvider imdgProvider, Registry registry) {
|
private PaymentInstructionBuilder(ImdgProvider imdgProvider, List<Registry> registries) {
|
||||||
this.imdgProvider = imdgProvider;
|
this.imdgProvider = imdgProvider;
|
||||||
this.registry = registry;
|
this.registries = registries;
|
||||||
}
|
}
|
||||||
|
|
||||||
public PaymentInstructionBuilder sessionId(Long sessionId) {
|
public PaymentInstructionBuilder sessionId(Long sessionId) {
|
||||||
|
|
@ -36,73 +41,100 @@ public class PaymentInstructionBuilder {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PaymentInstructionBuilder amount(BigDecimal amount) {
|
||||||
|
this.amount = amount;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public PaymentInstruction build() {
|
public PaymentInstruction build() {
|
||||||
Imdg<Account> accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
Imdg<Account> accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
|
||||||
Imdg<Company> companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
Imdg<Company> companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
||||||
Imdg<CompanySymbols> companySymbolsImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);
|
Imdg<CompanySymbols> companySymbolsImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_CompanySymbols, CompanySymbols.class);
|
||||||
Imdg<Security> securityImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Security, Security.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 paymentInstruction = new PaymentInstruction();
|
||||||
paymentInstruction.setSenderId(registry.getCompanyId());
|
|
||||||
paymentInstruction.setAddresseeId(registry.getCounterPartyId());
|
|
||||||
|
|
||||||
CompanySymbols companySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
|
if (registryLM.isPresent()) {
|
||||||
"companyId", registry.getCompanyId(),
|
Registry registry = registryLM.get();
|
||||||
"companySymbol", CompanySymbol.BIC.getKey())
|
paymentInstruction.setSenderId(registry.getCompanyId());
|
||||||
);
|
|
||||||
CompanySymbols counterCompanySymbols = null;
|
CompanySymbols companySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
|
||||||
if (registry.getCounterPartyId() != null) {
|
"companyId", registry.getCompanyId(),
|
||||||
counterCompanySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
|
"companySymbol", CompanySymbol.BIC.getKey()));
|
||||||
"companyId", registry.getCounterPartyId(),
|
|
||||||
"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) {
|
if (registryCM.isPresent()) {
|
||||||
paymentInstruction.setPayeeBic(companySymbols.getCompanySymbolValue());
|
Registry registry = registryCM.get();
|
||||||
}
|
paymentInstruction.setAddresseeId(registry.getCompanyId());
|
||||||
if (counterCompanySymbols != null) {
|
|
||||||
paymentInstruction.setAdresseeBic(counterCompanySymbols.getCompanySymbolValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
|
CompanySymbols companySymbols = null;
|
||||||
if (company != null) {
|
if (registry.getCounterPartyId() != null) {
|
||||||
paymentInstruction.setPayeeBankName(company.getShortName());
|
companySymbols = companySymbolsImdg.getSingleObjectByFieldValues(Map.of(
|
||||||
paymentInstruction.setAddresseeBankName(company.getShortName());
|
"companyId", registry.getCompanyId(),
|
||||||
}
|
"companySymbol", CompanySymbol.BIC.getKey())
|
||||||
paymentInstruction.setSettlementDate(registry.getSettlementDate());
|
);
|
||||||
paymentInstruction.setCreditLeg_amount(registry.getBalance());
|
}
|
||||||
paymentInstruction.setDebitLeg_amount(registry.getBalance());
|
if (companySymbols != null) {
|
||||||
|
paymentInstruction.setAdresseeBic(companySymbols.getCompanySymbolValue());
|
||||||
|
}
|
||||||
|
|
||||||
paymentInstruction.setCreditLeg_accountId(registry.getAccountId());//fixme контрагента?
|
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
|
||||||
Account account = accountImdg.getSingleObjectByID(paymentInstruction.getCreditLeg_accountId());
|
if (company != null) {
|
||||||
if (account != null) {
|
paymentInstruction.setAddresseeBankName(company.getShortName());
|
||||||
paymentInstruction.setCreditLeg_account(account.getAccount());
|
}
|
||||||
}
|
|
||||||
|
|
||||||
paymentInstruction.setDebitLeg_accountId(registry.getAccountId()); //fixme компании?
|
paymentInstruction.setDebitLeg_accountId(registry.getAccountId());
|
||||||
account = accountImdg.getSingleObjectByID(paymentInstruction.getDebitLeg_accountId());
|
Account account = accountImdg.getSingleObjectByID(registry.getAccountId());
|
||||||
if (account != null) {
|
if (account != null) {
|
||||||
paymentInstruction.setDebitLeg_account(account.getAccount());
|
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.setCreditLeg_direction(InOutDirection.out.getKey());
|
||||||
paymentInstruction.setDebitLeg_direction(InOutDirection.in.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.setTransactionStatus("STLD");
|
||||||
paymentInstruction.setCreated(Instant.now());
|
paymentInstruction.setCreated(Instant.now());
|
||||||
paymentInstruction.setClearingDate(LocalDate.now());
|
paymentInstruction.setClearingDate(LocalDate.now());
|
||||||
|
paymentInstruction.setSessionId(sessionId);
|
||||||
return paymentInstruction;
|
return paymentInstruction;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -144,9 +144,14 @@ public class PrimaryAuctionBnSession extends AbstractSession implements Initiali
|
||||||
}
|
}
|
||||||
//stage 6
|
//stage 6
|
||||||
runStage(TaskType.FormingRegistersOnOS, formingRegistersOnOS); //returns Collection<Registry>
|
runStage(TaskType.FormingRegistersOnOS, formingRegistersOnOS); //returns Collection<Registry>
|
||||||
//stage 7
|
StageResult<Collection<PaymentInstruction>> paymentResult = null;
|
||||||
StageResult<Collection<PaymentInstruction>> paymentResult = runStage(TaskType.FormingPaymentInstruction, formingPaymentInstruction);
|
{
|
||||||
if (paymentResult.getStageResult().isEmpty()) {
|
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);
|
runStage(TaskType.FormingPaymentInstruction, balanceRevise);
|
||||||
// finishPart(req);
|
// finishPart(req);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import ru.spcex.clearing.service.builder.PaymentInstructionBuilder;
|
||||||
import ru.spcex.clearing.session.stage.ISessionStage;
|
import ru.spcex.clearing.session.stage.ISessionStage;
|
||||||
import ru.spcex.clearing.session.stage.StageResult;
|
import ru.spcex.clearing.session.stage.StageResult;
|
||||||
import ru.spcex.clearing.session.stage.Task;
|
import ru.spcex.clearing.session.stage.Task;
|
||||||
|
import ru.spcex.clearing.session.stage.task.FormingPaymentInstructionPayload;
|
||||||
import ru.spcex.platform.enumeration.*;
|
import ru.spcex.platform.enumeration.*;
|
||||||
import ru.spcex.platform.imdg.api.Imdg;
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
import ru.spcex.platform.imdg.api.ImdgId;
|
import ru.spcex.platform.imdg.api.ImdgId;
|
||||||
|
|
@ -32,9 +33,8 @@ import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Collection;
|
import java.util.stream.Collectors;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||||
|
|
@ -70,9 +70,10 @@ public class FormingPaymentInstruction implements ISessionStage {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StageResult submit(Task<?> task) {
|
public StageResult submit(Task<?> task) {
|
||||||
|
FormingPaymentInstructionPayload payload = (FormingPaymentInstructionPayload) task.getData();
|
||||||
switch (task.getTaskType()) {
|
switch (task.getTaskType()) {
|
||||||
case FormingPaymentInstruction -> {
|
case FormingPaymentInstruction -> {
|
||||||
return formingPaymentInstruction();
|
return formingPaymentInstruction(payload.getSessionId());
|
||||||
}
|
}
|
||||||
default -> {
|
default -> {
|
||||||
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
|
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,
|
RegistryTradingParams registryTradingParamsL = new RegistryTradingParams(RegistryDesignation.L,
|
||||||
RegistryInstrumentType.S, null, RegistryUnit.T);
|
RegistryInstrumentType.S, null, RegistryUnit.T);
|
||||||
RegistryTradingParams registryTradingParamsC = new RegistryTradingParams(RegistryDesignation.C,
|
RegistryTradingParams registryTradingParamsC = new RegistryTradingParams(RegistryDesignation.C,
|
||||||
RegistryInstrumentType.M, null, null);
|
RegistryInstrumentType.M, null, RegistryUnit.T);
|
||||||
RegistryCodeSqlBuilder registryCodeSqlBuilder = RegistryCodeSqlBuilder.getInstance(registryTradingParamsL, registryTradingParamsC);
|
RegistryCodeSqlBuilder registryCodeSqlBuilder = RegistryCodeSqlBuilder.getInstance(registryTradingParamsL, registryTradingParamsC);
|
||||||
String registryCodeCondition = registryCodeSqlBuilder.build();
|
String registryCodeCondition = registryCodeSqlBuilder.build();
|
||||||
|
|
||||||
|
|
@ -227,20 +228,34 @@ public class FormingPaymentInstruction implements ISessionStage {
|
||||||
}
|
}
|
||||||
|
|
||||||
List<PaymentInstruction> formedPaymentInstructions = new ArrayList<>();
|
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,
|
RegistryTradingParams registryTradingParamsLT = new RegistryTradingParams(RegistryDesignation.L,
|
||||||
null, null, RegistryUnit.T);
|
null, null, RegistryUnit.T);
|
||||||
registryCodeCondition = RegistryCodeSqlBuilder.getInstance(registryTradingParamsLT).build();
|
registryCodeCondition = RegistryCodeSqlBuilder.getInstance(registryTradingParamsLT).build();
|
||||||
String sqlCondition = String.format("(%s) and securityId = %s and tradingClearingRegistryId = %s and companyId = %s and counterPartyId = %s",
|
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);
|
Collection<Registry> registries = registryImdg.getCollectionObjectsBySQL(sqlCondition);
|
||||||
|
|
||||||
BigDecimal sumBalance = registries.stream().map(Registry::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add);
|
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);
|
formedPaymentInstructions.add(paymentInstruction);
|
||||||
paymentInstructionImdg.insert(paymentInstruction);
|
paymentInstructionImdg.insert(paymentInstruction);
|
||||||
obligation.setPaymentId(paymentInstruction.getId());
|
registriesLC.forEach(registry -> {
|
||||||
registryImdg.update(obligation);
|
registry.setPaymentId(paymentInstruction.getId());
|
||||||
|
registry.setUpdated(Instant.now());
|
||||||
|
registryImdg.update(registry);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
sendSdfs(formedPaymentInstructions);
|
sendSdfs(formedPaymentInstructions);
|
||||||
|
|
@ -282,8 +297,10 @@ public class FormingPaymentInstruction implements ISessionStage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private PaymentInstruction createPaymentInstruction(Registry registry, BigDecimal balance) {
|
private PaymentInstruction createPaymentInstruction(List<Registry> registries, BigDecimal balance, Long sessionId) {
|
||||||
PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registry);
|
PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registries)
|
||||||
|
.sessionId(sessionId)
|
||||||
|
.amount(balance);
|
||||||
return paymentInstructionBuilder.build();
|
return paymentInstructionBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue