fix FinalMkrSession

This commit is contained in:
etreschenkov 2023-06-02 20:11:14 +03:00
parent e2a0f33012
commit f046ffb3ab
4 changed files with 289 additions and 31 deletions

View file

@ -0,0 +1,14 @@
package ru.spcex.clearing.session.stage.impl;
import ru.clearing.classes.statics.data.execution.ExecutionCommon;
import ru.clearing.classes.statics.data.registry.Registry;
import ru.spcex.platform.enumeration.RegistryDesignation;
import ru.spcex.platform.imdg.api.ImdgProvider;
public interface IRegistryBuilder {
IRegistryBuilder imdg(ImdgProvider imdgProvider);
IRegistryBuilder exec(ExecutionCommon exec);
IRegistryBuilder registryDesignation(RegistryDesignation registryDesignation);
Registry build();
}

View file

@ -25,7 +25,7 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
public class RegistryBuilder {
public class RegistryDepoBuilder implements IRegistryBuilder {
private Imdg<Company> companyImdg;
private Imdg<TradingClearingRegistry> tradingClearingRegistryImdg;
private Imdg<Account> accountImdg;
@ -38,14 +38,15 @@ public class RegistryBuilder {
private ExecutionCommon exec;
private RegistryDesignation regDsgn;
private RegistryBuilder() {
private RegistryDepoBuilder() {
}
public static RegistryBuilder builder() {
return new RegistryBuilder();
public static RegistryDepoBuilder builder() {
return new RegistryDepoBuilder();
}
public RegistryBuilder imdg(ImdgProvider imdgProvider) {
@Override
public RegistryDepoBuilder imdg(ImdgProvider imdgProvider) {
this.companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
this.tradingClearingRegistryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_TradingClearingRegistry, TradingClearingRegistry.class);
this.accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
@ -58,16 +59,19 @@ public class RegistryBuilder {
return this;
}
public RegistryBuilder exec(ExecutionCommon exec) {
@Override
public RegistryDepoBuilder exec(ExecutionCommon exec) {
this.exec = exec;
return this;
}
public RegistryBuilder registryDesignation(RegistryDesignation registryDesignation) {
@Override
public RegistryDepoBuilder registryDesignation(RegistryDesignation registryDesignation) {
this.regDsgn = registryDesignation;
return this;
}
@Override
public Registry build() {
ISide side = getSide(exec);
Registry reg = new Registry();
@ -82,8 +86,7 @@ public class RegistryBuilder {
String capacityByAccount = null;
boolean isMoney = false;
Account account = null;
if ((regDsgn.equals(RegistryDesignation.O) && side.isBuy()) || (regDsgn.equals(RegistryDesignation.T) && side.isSell())
|| exec.type().equals(ExecutionType.ExecutionDeposit)) {
if ((regDsgn.equals(RegistryDesignation.O) && side.isBuy()) || (regDsgn.equals(RegistryDesignation.T) && side.isSell())) {
isMoney = true;
reg.setAccountId(tcr.getMoneyAccountId());
account = accountImdg.getSingleObjectByID(tcr.getMoneyAccountId());
@ -92,29 +95,26 @@ public class RegistryBuilder {
Currency currency = currencyImdg.getSingleObjectByFieldValues(Map.of("currencyCode", exec.getSettlementCurrency()));
reg.setSecurityId(currency.getId());
reg.setSecuritySymbol(currency.getCurrencyCode());
if (AccountType.Clrn.equalsByKey(account.getAccountType())) {
ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByID(tcr.getMoneyAccountId());
if (clearingAccount != null) {
capacityByAccount = clearingAccount.getClearingAccountType();
}
} else if (AccountType.Info.equalsByKey(account.getAccountType())){
InformationAccount informationAccount = informationAccountImdg.getSingleObjectByID(tcr.getMoneyAccountId());
if (informationAccount != null) {
capacityByAccount = "A"; //todo сделать enum
}
}
capacityByAccount = defineCapacityByAccountType(account.getAccountType(), tcr.getMoneyAccountId());
} else if ((regDsgn.equals(RegistryDesignation.T) && side.isBuy()) || (regDsgn.equals(RegistryDesignation.O) && side.isSell())) {
isMoney = false;
reg.setAccountId(tcr.getDepoAccountId());
account = accountImdg.getSingleObjectByID(tcr.getDepoAccountId());
if (exec.type().equals(ExecutionType.ExecutionFond)) {
account = accountImdg.getSingleObjectByID(tcr.getDepoAccountId());
reg.setAccountId(tcr.getDepoAccountId());
DepoAccount depoAccount = depoAccountImdg.getSingleObjectByID(tcr.getDepoAccountId());
if (depoAccount != null) {
capacityByAccount = depoAccount.getDepoAccountType();
}
} else if (exec.type().equals(ExecutionType.ExecutionDeposit)) {
account = accountImdg.getSingleObjectByID(tcr.getMoneyAccountId());
reg.setAccountId(tcr.getMoneyAccountId());
capacityByAccount = defineCapacityByAccountType(account.getAccountType(), tcr.getMoneyAccountId());
}
reg.setRegistryInstrumentType(RegistryInstrumentType.S.getKey());
reg.setBalanceDimension(BalanceDimension.PICS.getKey()); //fixme add second leg code branch
reg.setSecurityId(exec.getSecurityId());
reg.setSecuritySymbol(exec.getSecuritySymbol());
DepoAccount depoAccount = depoAccountImdg.getSingleObjectByID(tcr.getDepoAccountId());
if (depoAccount != null) {
capacityByAccount = depoAccount.getDepoAccountType();
}
}
if (account != null) {
@ -199,4 +199,20 @@ public class RegistryBuilder {
throw new RuntimeException("Unknown Execution type: " + exec.type());
}
}
private String defineCapacityByAccountType(String accountType, Long moneyAccountId) {
String capacityByAccount = null;
if (AccountType.Clrn.equalsByKey(accountType)) {
ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByID(moneyAccountId);
if (clearingAccount != null) {
capacityByAccount = clearingAccount.getClearingAccountType();
}
} else if (AccountType.Info.equalsByKey(accountType)) {
InformationAccount informationAccount = informationAccountImdg.getSingleObjectByID(moneyAccountId);
if (informationAccount != null) {
capacityByAccount = "A"; //todo сделать enum
}
}
return capacityByAccount;
}
}

View file

@ -0,0 +1,218 @@
package ru.spcex.clearing.session.stage.impl;
import ru.clearing.classes.statics.data.account.Account;
import ru.clearing.classes.statics.data.account.ClearingAccount;
import ru.clearing.classes.statics.data.account.DepoAccount;
import ru.clearing.classes.statics.data.account.InformationAccount;
import ru.clearing.classes.statics.data.company.Company;
import ru.clearing.classes.statics.data.execution.ExecutionCommon;
import ru.clearing.classes.statics.data.execution.ExecutionDeposit;
import ru.clearing.classes.statics.data.execution.ExecutionFond;
import ru.clearing.classes.statics.data.misc.Currency;
import ru.clearing.classes.statics.data.misc.Market;
import ru.clearing.classes.statics.data.misc.Session;
import ru.clearing.classes.statics.data.registry.Registry;
import ru.clearing.classes.statics.data.registry.TradingClearingRegistry;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.session.stage.util.RegistryUtil;
import ru.spcex.platform.classes.base.interfaces.ExecutionType;
import ru.spcex.platform.enumeration.*;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Map;
public class RegistryFondBuilder implements IRegistryBuilder {
private Imdg<Company> companyImdg;
private Imdg<TradingClearingRegistry> tradingClearingRegistryImdg;
private Imdg<Account> accountImdg;
private Imdg<Market> marketImdg;
private Imdg<Session> sessionImdg;
private Imdg<ClearingAccount> clearingAccountImdg;
private Imdg<InformationAccount> informationAccountImdg;
private Imdg<DepoAccount> depoAccountImdg;
private Imdg<Currency> currencyImdg;
private ExecutionCommon exec;
private RegistryDesignation regDsgn;
private RegistryFondBuilder() {
}
public static RegistryFondBuilder builder() {
return new RegistryFondBuilder();
}
@Override
public RegistryFondBuilder imdg(ImdgProvider imdgProvider) {
this.companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
this.tradingClearingRegistryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_TradingClearingRegistry, TradingClearingRegistry.class);
this.accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
this.marketImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Market, Market.class);
this.sessionImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Session, Session.class);
this.clearingAccountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_ClearingAccount, ClearingAccount.class);
this.depoAccountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_DepoAccount, DepoAccount.class);
this.informationAccountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_InformationAccount, InformationAccount.class);
this.currencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Currency, Currency.class);
return this;
}
@Override
public RegistryFondBuilder exec(ExecutionCommon exec) {
this.exec = exec;
return this;
}
@Override
public RegistryFondBuilder registryDesignation(RegistryDesignation registryDesignation) {
this.regDsgn = registryDesignation;
return this;
}
@Override
public Registry build() {
ISide side = getSide(exec);
Registry reg = new Registry();
reg.setCompanyId(exec.getCompanyId());
reg.setCreated(Instant.now());
Company company = searchCompany();
reg.setTradingCode(company.getTradingCode());
reg.setClearingCode(company.getClearingCode());
reg.setShortName(company.getShortName());
reg.setFullName(company.getFullName());
TradingClearingRegistry tcr = searchTradingClearingRegistry();
String capacityByAccount = null;
boolean isMoney = false;
Account account = null;
if ((regDsgn.equals(RegistryDesignation.O) && side.isBuy()) || (regDsgn.equals(RegistryDesignation.T) && side.isSell())) {
isMoney = true;
reg.setAccountId(tcr.getMoneyAccountId());
account = accountImdg.getSingleObjectByID(tcr.getMoneyAccountId());
reg.setRegistryInstrumentType(RegistryInstrumentType.M.getKey());
reg.setBalanceDimension(BalanceDimension.MONY.getKey()); //fixme add second leg code branch
Currency currency = currencyImdg.getSingleObjectByFieldValues(Map.of("currencyCode", exec.getSettlementCurrency()));
reg.setSecurityId(currency.getId());
reg.setSecuritySymbol(currency.getCurrencyCode());
capacityByAccount = defineCapacityByAccountType(account.getAccountType(), tcr.getMoneyAccountId());
} else if ((regDsgn.equals(RegistryDesignation.T) && side.isBuy()) || (regDsgn.equals(RegistryDesignation.O) && side.isSell())) {
isMoney = false;
if (exec.type().equals(ExecutionType.ExecutionFond)) {
account = accountImdg.getSingleObjectByID(tcr.getDepoAccountId());
reg.setAccountId(tcr.getDepoAccountId());
DepoAccount depoAccount = depoAccountImdg.getSingleObjectByID(tcr.getDepoAccountId());
if (depoAccount != null) {
capacityByAccount = depoAccount.getDepoAccountType();
}
} else if (exec.type().equals(ExecutionType.ExecutionDeposit)) {
account = accountImdg.getSingleObjectByID(tcr.getMoneyAccountId());
reg.setAccountId(tcr.getMoneyAccountId());
capacityByAccount = defineCapacityByAccountType(account.getAccountType(), tcr.getMoneyAccountId());
}
reg.setRegistryInstrumentType(RegistryInstrumentType.S.getKey());
reg.setBalanceDimension(BalanceDimension.PICS.getKey()); //fixme add second leg code branch
reg.setSecurityId(exec.getSecurityId());
reg.setSecuritySymbol(exec.getSecuritySymbol());
}
if (account != null) {
reg.setAccountType(account.getAccountType());
reg.setAccount(account.getAccount());
}
reg.setRegistryDesignation(regDsgn.getKey());
if (capacityByAccount != null) {
reg.setRegistryCapacity(capacityByAccount);
}
reg.setRegistryUnit(RegistryUnit.T.getKey());
reg.setRegistryCode(RegistryUtil.clearingCode(reg));
reg.setTradingClearingRegistryId(exec.getTradingClearingRegistryId());
reg.setTradingClearingRegistry(tcr.getCode());
reg.setRegistryStatus(RegistryStatus.PROC.getKey());
switch (exec.type()) {
case ExecutionDeposit -> {
ExecutionDeposit execDep = (ExecutionDeposit) this.exec;
reg.setBalance((execDep).getFirstLegAmount());
if (regDsgn.equals(RegistryDesignation.T)) {
reg.setSettledCredit((execDep).getFirstLegAmount());
} else if (regDsgn.equals(RegistryDesignation.O)) {
reg.setSettledDebit((execDep).getFirstLegAmount());
}
reg.setSettlementDate(execDep.getFirstLegSettlementDate()); //fixme add second leg code branch
reg.setSettlementCode(execDep.getFirstLegSettlementCode()); //fixme add second leg code branch
reg.setRefundDate(execDep.getSecondLegSettlementDate()); //fixme add second leg code branch??
reg.setValueDate(execDep.getFirstLegSettlementDate());
reg.setContract(execDep.getContract());
}
case ExecutionFond -> {
ExecutionFond execFond = (ExecutionFond) this.exec;
if (isMoney) {
//todo ОКРУГЛЕНИЕ(fixedIncomeSecurity.nominalValue * quantity * price/100) + ОКРУГЛЕНИЕ (executionFond.interestAmount)
reg.setBalance(execFond.getSettlementAmount());
} else {
reg.setBalance(execFond.getQuantity());
}
reg.setSettlementDate(execFond.getSettlementDate());
reg.setSettlementCode(execFond.getSettlementCode());
}
}
reg.setTradingDate(exec.getTradingDate());
reg.setClearingDate(LocalDate.now());
reg.setPrice(exec.getPrice());
reg.setCounterPartyId(exec.getCounterPartyId());
reg.setGroupId(groupId());
reg.setSessionId(exec.getSessionId());
reg.setSessionType(sessionType());
return reg;
}
private static DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd");
private Long groupId() {
LocalDate now = LocalDate.now();
Market market = marketImdg.getSingleObjectBySQL("code = '" + exec.getMarket() + "'");
return Long.valueOf(now.format(yyyyMMdd) + exec.getExchangeExecutionId() + market.getId());
}
//можно передать из стейджа
private String sessionType() {
Session session = sessionImdg.getSingleObjectByID(exec.getSessionId());
return session.getSessionType();
}
private Company searchCompany() {
return companyImdg.getSingleObjectBySQL("id = " + exec.getCompanyId());
}
private TradingClearingRegistry searchTradingClearingRegistry() {
return tradingClearingRegistryImdg.getSingleObjectByID(exec.getTradingClearingRegistryId());
}
private static ISide getSide(ExecutionCommon exec) {
if (exec.type().equals(ExecutionType.ExecutionDeposit)) {
return ISide.parse(MoneyFlowSide.class, exec.getSide());
} else if (exec.type().equals(ExecutionType.ExecutionFond)) {
return ISide.parse(Side.class, exec.getSide());
} else {
throw new RuntimeException("Unknown Execution type: " + exec.type());
}
}
private String defineCapacityByAccountType(String accountType, Long moneyAccountId) {
String capacityByAccount = null;
if (AccountType.Clrn.equalsByKey(accountType)) {
ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByID(moneyAccountId);
if (clearingAccount != null) {
capacityByAccount = clearingAccount.getClearingAccountType();
}
} else if (AccountType.Info.equalsByKey(accountType)) {
InformationAccount informationAccount = informationAccountImdg.getSingleObjectByID(moneyAccountId);
if (informationAccount != null) {
capacityByAccount = "A"; //todo сделать enum
}
}
return capacityByAccount;
}
}

View file

@ -83,7 +83,8 @@ public class RequirementsAndObligationCreation implements ISessionStage {
.update();
registryImdg.update(registry);
}, () -> {
Registry newRegister = RegistryBuilder.builder()
IRegistryBuilder registryBuilder = defineBuilderByExecType(partyExec.type());
Registry newRegister = registryBuilder
.imdg(imdgProvider)
.exec(exec)
.registryDesignation(regDsgn)
@ -129,7 +130,8 @@ public class RequirementsAndObligationCreation implements ISessionStage {
} else {
throw new IllegalStateException("cannot construct for " + des + " " + side);
}
return imdgRegSearch(p, exec.getTradingClearingRegistryId(), exec.getCompanyId(), settlementDt(exec));
return imdgRegSearch(p, exec.getTradingClearingRegistryId(), exec.getCompanyId(), settlementDt(exec),
exec.getSessionId());
}
private Optional<Registry> findRegBySettlementDt(ExecutionCommon exec, RegistryDesignation des) {
@ -156,16 +158,18 @@ public class RequirementsAndObligationCreation implements ISessionStage {
} else {
throw new IllegalStateException("cannot construct by settleDt for " + des + " " + side);
}
return imdgRegSearch(p, exec.getTradingClearingRegistryId(), exec.getCompanyId(), ((ExecutionDeposit) exec).getSecondLegSettlementDate());
return imdgRegSearch(p, exec.getTradingClearingRegistryId(), exec.getCompanyId(), ((ExecutionDeposit) exec).getSecondLegSettlementDate(), exec.getSessionId());
}
public Optional<Registry> imdgRegSearch(RegistryTradingParams p, Long tcrId, Long companyId, LocalDate settlementDt) {
public Optional<Registry> imdgRegSearch(RegistryTradingParams p, Long tcrId, Long companyId, LocalDate settlementDt,
Long sessionId) {
String sql = RegistryCodeSqlBuilder.getInstance(p).build();
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
ImdgPredicate rgstrPredicate = pb.and(pb.sql(sql),
pb.equals("tradingClearingRegistryId", tcrId),
pb.equals("companyId", companyId),
pb.equals("settlementDate", settlementDt)
pb.equals("settlementDate", settlementDt),
pb.equals("sessionId", sessionId)
);
return Optional.ofNullable(registryImdg.getSingleObjectByPredicate(rgstrPredicate));
}
@ -224,4 +228,10 @@ public class RequirementsAndObligationCreation implements ISessionStage {
}
}
private IRegistryBuilder defineBuilderByExecType(ExecutionType executionType) {
return switch (executionType) {
case ExecutionFond -> RegistryFondBuilder.builder();
case ExecutionDeposit -> RegistryDepoBuilder.builder();
};
}
}