This commit is contained in:
etreshenkov 2023-05-18 17:46:28 +03:00
parent 0d296dac74
commit 715328e4c9
4 changed files with 134 additions and 56 deletions

View file

@ -1,14 +1,23 @@
package ru.spcex.clearing.models;
import ru.clearing.classes.statics.data.registry.Registry;
import ru.spcex.platform.enumeration.RegistryCapacity;
import ru.spcex.platform.enumeration.RegistryDesignation;
import ru.spcex.platform.enumeration.RegistryInstrumentType;
import ru.spcex.platform.enumeration.RegistryUnit;
import ru.spcex.platform.utils.enumeration.IEnumKey;
public record RegistryTradingParams(RegistryDesignation registryDesignation,
RegistryInstrumentType registryInstrumentType,
RegistryCapacity registryCapacity,
RegistryUnit registryUnit) {
public boolean equalByRegistry(Registry registry) {
boolean isEqualDesignation = this.registryDesignation == null || IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == this.registryDesignation;
boolean isEqualInstrumentType = this.registryInstrumentType == null || IEnumKey.getEnumByKey(RegistryInstrumentType.class, registry.getRegistryInstrumentType()) == this.registryInstrumentType;
boolean isEqualCapacity = this.registryCapacity == null || IEnumKey.getEnumByKey(RegistryCapacity.class, registry.getRegistryCapacity()) == this.registryCapacity;
boolean isEqualUnit = this.registryUnit == null || IEnumKey.getEnumByKey(RegistryUnit.class, registry.getRegistryUnit()) == this.registryUnit;
return isEqualDesignation && isEqualInstrumentType && isEqualCapacity && isEqualUnit;
}
}

View file

@ -9,7 +9,7 @@ import java.util.stream.Collectors;
public class RegistryCodeSqlBuilder {
private List<RegistryTradingParams> registryTradingParams = new ArrayList<>();
private List<RegistryTradingParams> registryTradingParams;
public static RegistryCodeSqlBuilder getInstance(RegistryTradingParams... tradingParams) {
return new RegistryCodeSqlBuilder(Arrays.asList(tradingParams));

View file

@ -6,9 +6,10 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ru.clearing.classes.statics.data.payment.PaymentInstruction;
import ru.clearing.classes.statics.data.registry.Registry;
import ru.spcex.clearing.error.ClearingError;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.models.RegistryTradingParams;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.clearing.service.builder.sql.RegistryCodeSqlBuilder;
import ru.spcex.clearing.session.stage.ISessionStage;
import ru.spcex.clearing.session.stage.StageResult;
import ru.spcex.clearing.session.stage.Task;
@ -19,16 +20,12 @@ import ru.spcex.platform.enumeration.RegistryUnit;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgId;
import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.utils.enumeration.EnumMessage;
import ru.spcex.platform.utils.enumeration.IEnumKey;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
import java.util.ArrayList;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Service
public class FormingPaymentInstruction implements ISessionStage {
@ -64,64 +61,137 @@ public class FormingPaymentInstruction implements ISessionStage {
private StageResult inspectionPool(Long processedCompanyId) {
String sqlCondition = String.format("registryDesignation in ('%s', '%s') and " +
"registryInstrumentType in ('%s', '%s') and " +
"registryUnit = '%s' and " +
"registryStatus = '%s' and " +
"settlementDate = '%s'",
RegistryDesignation.O.getKey(), RegistryDesignation.T.getKey(),
RegistryInstrumentType.S.getKey(), RegistryInstrumentType.M.getKey(),
RegistryUnit.T.getKey(),
"POOL");
RegistryTradingParams registryTradingParamsL = new RegistryTradingParams(RegistryDesignation.L,
RegistryInstrumentType.S, null, RegistryUnit.T);
RegistryTradingParams registryTradingParamsC = new RegistryTradingParams(RegistryDesignation.C,
RegistryInstrumentType.M, null, null);
RegistryCodeSqlBuilder registryCodeSqlBuilder = RegistryCodeSqlBuilder.getInstance(registryTradingParamsL, registryTradingParamsC);
String registryCodeCondition = registryCodeSqlBuilder.build();
Collection<Registry> obligations = registryImdg.getCollectionObjectsBySQL(sqlCondition);
Map<Long, Map<Long, List<Registry>>> registryByGroupIdAndCompanyId = obligations.stream().
collect(Collectors.groupingBy(Registry::getGroupId, Collectors.groupingBy(Registry::getCompanyId)));
Collection<Registry> obligations = registryImdg.getCollectionObjectsBySQL(registryCodeCondition);
List<Long> insecurityGroups = new ArrayList<>();
for (Registry currentRegistry : obligations) {
if (insecurityGroups.contains(currentRegistry.getGroupId())) {
log.debug("Skip insecurity groupId: {}", currentRegistry.getGroupId());
continue;
}
if (IEnumKey.getEnumByKey(RegistryDesignation.class, currentRegistry.getRegistryDesignation()) == RegistryDesignation.A) {
String sqlCounterRegistryCondition = String.format("registryDesignation = '%s' and " +
"registryInstrumentType in ('%s', '%s') and " +
"registryUnit = '%s' and " +
"tradingClearingRegistryId = '%s' and " +
"companyId = '%s' and ",
RegistryDesignation.A,
RegistryInstrumentType.S.getKey(), RegistryInstrumentType.M.getKey(),
RegistryUnit.F.getKey(),
currentRegistry.getTradingClearingRegistryId(),
currentRegistry.getCompanyId());
Registry counterRegistry = registryImdg.getSingleObjectBySQL(sqlCounterRegistryCondition);
if (currentRegistry.getBalance().compareTo(counterRegistry.getBalance()) > 0) {
log.warn("{}", msgResolver.resolve(new EnumMessage(ClearingError.InsecurityObligation, currentRegistry.getCompanyId())));
insecurityGroups.add(currentRegistry.getGroupId());
break;
List<Registry> obligationsByMoney = obligations.stream().filter(registry ->
new RegistryTradingParams(RegistryDesignation.L, RegistryInstrumentType.M, null, RegistryUnit.T).equalByRegistry(registry))
.toList();
for (Registry obligationByMoney : obligationsByMoney) {
String sql = String.format("tradingClearingRegistryId = %s and companyId = %s",
obligationByMoney.getTradingClearingRegistryId(), obligationByMoney.getCompanyId());
Collection<Registry> relatedRegistries = registryImdg.getCollectionObjectsBySQL(sql);
RegistryTradingParams registryTradingParamsF = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.M, null, RegistryUnit.F);
RegistryTradingParams registryTradingParamsT = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.M, null, RegistryUnit.T);
RegistryTradingParams registryTradingParamsB = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.M, null, RegistryUnit.B);
for (Registry relatedRegistry : relatedRegistries) {
if (registryTradingParamsF.equalByRegistry(relatedRegistry)) {
relatedRegistry.setBalance(relatedRegistry.getBalance().subtract(obligationByMoney.getBalance()));
} else if (registryTradingParamsT.equalByRegistry(relatedRegistry)) {
relatedRegistry.setSettledDebit(relatedRegistry.getSettledDebit().add(obligationByMoney.getBalance()));
} else if (registryTradingParamsB.equalByRegistry(relatedRegistry)) {
relatedRegistry.setBalance(relatedRegistry.getBalance().add(obligationByMoney.getBalance()));
}
currentRegistry.setRegistryStatus("OK");
registryImdg.update(counterRegistry);
registryImdg.update(relatedRegistry);
}
}
for (Long insecuritiesGroupId : insecurityGroups) {
for (Map.Entry<Long, List<Registry>> entrySet : registryByGroupIdAndCompanyId.get(insecuritiesGroupId).entrySet()) {
if (entrySet.getKey().equals(processedCompanyId)) {
for (Registry registry : entrySet.getValue()) {
registry.setRegistryStatus("UNCV");
registryImdg.update(registry);
}
} else {
for (Registry registry : entrySet.getValue()) {
registry.setRegistryStatus("FAIL");
registryImdg.update(registry);
}
List<Registry> requirementsByIssue = obligations.stream().filter(registry ->
new RegistryTradingParams(RegistryDesignation.C, RegistryInstrumentType.S, null, RegistryUnit.T).equalByRegistry(registry))
.toList();
for (Registry requirementByIssue : requirementsByIssue) {
String sql = String.format("tradingClearingRegistryId = %s and companyId = %s",
requirementByIssue.getTradingClearingRegistryId(), requirementByIssue.getCompanyId());
Collection<Registry> relatedRegistries = registryImdg.getCollectionObjectsBySQL(sql);
RegistryTradingParams registryTradingParamsA = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.S, null, RegistryUnit.T);
for (Registry relatedRegistry : relatedRegistries) {
if (registryTradingParamsA.equalByRegistry(relatedRegistry)) {
relatedRegistry.setSettledDebit(relatedRegistry.getSettledDebit().add(requirementByIssue.getBalance()));
}
registryImdg.update(relatedRegistry);
}
}
List<Registry> requirementsByMoney = obligations.stream().filter(registry ->
new RegistryTradingParams(RegistryDesignation.C, RegistryInstrumentType.M, null, RegistryUnit.T).equalByRegistry(registry))
.toList();
for (Registry requirementByMoney : requirementsByMoney) {
String sql = String.format("tradingClearingRegistryId = %s and companyId = %s",
requirementByMoney.getTradingClearingRegistryId(), requirementByMoney.getCompanyId());
Collection<Registry> relatedRegistries = registryImdg.getCollectionObjectsBySQL(sql);
RegistryTradingParams registryTradingParamsA = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.M, null, RegistryUnit.T);
for (Registry relatedRegistry : relatedRegistries) {
if (registryTradingParamsA.equalByRegistry(relatedRegistry)) {
relatedRegistry.setSettledDebit(relatedRegistry.getSettledDebit().add(requirementByMoney.getBalance()));
}
registryImdg.update(relatedRegistry);
}
}
List<Registry> obligationsByIssue = obligations.stream().filter(registry ->
new RegistryTradingParams(RegistryDesignation.L, RegistryInstrumentType.S, null, RegistryUnit.T).equalByRegistry(registry))
.toList();
for (Registry obligationByIssue : obligationsByIssue) {
String sql = String.format("tradingClearingRegistryId = %s and companyId = %s",
obligationByIssue.getTradingClearingRegistryId(), obligationByIssue.getCompanyId());
Collection<Registry> relatedRegistries = registryImdg.getCollectionObjectsBySQL(sql);
RegistryTradingParams registryTradingParamsF = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.S, null, RegistryUnit.F);
RegistryTradingParams registryTradingParamsT = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.S, null, RegistryUnit.T);
RegistryTradingParams registryTradingParamsB = new RegistryTradingParams(RegistryDesignation.A,
RegistryInstrumentType.S, null, RegistryUnit.B);
for (Registry relatedRegistry : relatedRegistries) {
if (registryTradingParamsF.equalByRegistry(relatedRegistry)) {
relatedRegistry.setBalance(relatedRegistry.getBalance().subtract(obligationByIssue.getBalance()));
} else if (registryTradingParamsT.equalByRegistry(relatedRegistry)) {
relatedRegistry.setSettledDebit(relatedRegistry.getSettledDebit().add(obligationByIssue.getBalance()));
} else if (registryTradingParamsB.equalByRegistry(relatedRegistry)) {
relatedRegistry.setBalance(relatedRegistry.getBalance().add(obligationByIssue.getBalance()));
}
registryImdg.update(relatedRegistry);
}
}
for (Registry obligation : obligations) {
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());
Collection<Registry> registries = registryImdg.getCollectionObjectsBySQL(registryCodeCondition);
BigDecimal sumBalance = registries.stream().map(Registry::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add);
PaymentInstruction paymentInstruction = createPaymentInstruction(obligation, sumBalance);
paymentInstructionImdg.insert(paymentInstruction);
obligation.setPaymentId(paymentInstruction.getId());
registryImdg.update(obligation);
}
//todo send message to queue for forming sDf03 and sDf12?
return new StageResult(null, true);
}
private PaymentInstruction createPaymentInstruction(Registry registry, BigDecimal balance){
PaymentInstruction paymentInstruction = new PaymentInstruction();
paymentInstruction.setSenderId(registry.getCompanyId());
paymentInstruction.setAddresseeId(registry.getCounterPartyId());
//todo add builder for paymentInstruction
return paymentInstruction;
}
}

View file

@ -67,7 +67,6 @@ public class InclusionObligations implements ISessionStage {
sessionType);
Collection<Registry> obligations = registryImdg.getCollectionObjectsBySQL(sqlCondition);
Map<Long, List<Registry>> registryByGroupId = obligations.stream().
filter(registry -> registry.getCompanyId().equals(counterPartyId)).
collect(Collectors.groupingBy(Registry::getGroupId));
for (Map.Entry<Long, List<Registry>> entrySet : registryByGroupId.entrySet()) {