FormingPaymentInstructionDepositReturn.java
This commit is contained in:
parent
21d959740f
commit
3310559df6
1 changed files with 28 additions and 171 deletions
|
|
@ -37,6 +37,8 @@ import java.time.format.DateTimeFormatter;
|
|||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static ru.spcex.platform.utils.number.BigDecimalUtil.safeBD;
|
||||
|
||||
@Service
|
||||
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
||||
public class FormingPaymentInstructionDepositReturn implements ISessionStage {
|
||||
|
|
@ -155,190 +157,49 @@ public class FormingPaymentInstructionDepositReturn implements ISessionStage {
|
|||
Collection<Registry> obligations = registryImdg.getCollectionObjectsBySQL(registryCodeCondition);
|
||||
List<Registry> obligationsByMoney = obligations.stream().filter(registry -> equalByRgs(LM_T, registry)).toList();
|
||||
|
||||
for (Registry obligationByMoney : obligationsByMoney) {
|
||||
Optional<Registry> dmx = searchDmx(obligationByMoney);
|
||||
Map<Long, List<Registry>> registriesByGroup = obligations
|
||||
.stream().
|
||||
collect(Collectors.groupingBy(Registry::getGroupId));
|
||||
log.info("registry groups found {}", registriesByGroup.size());
|
||||
|
||||
for (Map.Entry<Long, List<Registry>> entry : registriesByGroup.entrySet()) {
|
||||
List<Registry> groupRgs = entry.getValue();
|
||||
Optional<Registry> lmtO = groupRgs.stream().filter(rgs -> equalByRgs(LM_T, rgs)).findFirst();
|
||||
Optional<Registry> cmtO = groupRgs.stream().filter(rgs -> equalByRgs(CM_T, rgs)).findFirst();
|
||||
if (lmtO.isEmpty() || cmtO.isEmpty()) {
|
||||
log.error("LM*T or CM*T not found for group {}", entry.getKey());
|
||||
continue;
|
||||
}
|
||||
Registry lmt = lmtO.get(); //obligation by money
|
||||
Registry cmt = cmtO.get();
|
||||
Optional<Registry> dmx = searchDmx(lmt);
|
||||
if (dmx.isPresent()) {
|
||||
log.debug("LM*T#id={}, DM*X#id={} found, no action needed for group {}, skipping liability",
|
||||
obligationByMoney.getId(), dmx.get().getId(), obligationByMoney.getGroupId());
|
||||
lmt.getId(), dmx.get().getId(), lmt.getGroupId());
|
||||
continue;
|
||||
}
|
||||
|
||||
Optional<Registry> dmtInfo = searchDmtInfo(obligationByMoney);
|
||||
Optional<Registry> dmtInfo = searchDmtInfo(lmt);
|
||||
if (dmtInfo.isPresent()) {
|
||||
PaymentInstruction payInstr = createPaymentInstruction(List.of(obligationByMoney), dmtInfo.get().getBalance(), sessionId);
|
||||
obligationByMoney.setPaymentId(payInstr.getId());
|
||||
obligationByMoney.setUpdated(Instant.now());
|
||||
registryImdg.update(obligationByMoney);
|
||||
BigDecimal balance = safeBD(lmt.getBalance()).add(safeBD(cmt.getBalance())); //fixme????
|
||||
PaymentInstruction payInstr = createPaymentInstruction(List.of(lmt, cmt), balance, sessionId);
|
||||
lmt.setPaymentId(payInstr.getId());
|
||||
lmt.setUpdated(Instant.now());
|
||||
registryImdg.update(lmt);
|
||||
continue;
|
||||
}
|
||||
|
||||
Optional<Registry> dmtClnr = searchDmtClrn(obligationByMoney);
|
||||
Optional<Registry> dmtClnr = searchDmtClrn(lmt);
|
||||
if (dmtClnr.isPresent()) {
|
||||
//todo 1.1; 1.3; 2; 3; 4
|
||||
}
|
||||
|
||||
|
||||
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(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setBalance(safeBD(relatedRegistry.getBalance()).subtract(safeBD(obligationByMoney.getBalance())));
|
||||
} else if (registryTradingParamsT.equalByRegistry(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setSettledDebit(safeBD(relatedRegistry.getSettledDebit()).add(safeBD(obligationByMoney.getBalance())));
|
||||
} else if (registryTradingParamsB.equalByRegistry(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setBalance(safeBD(relatedRegistry.getBalance()).add(safeBD(obligationByMoney.getBalance())));
|
||||
}
|
||||
registryImdg.update(relatedRegistry);
|
||||
}
|
||||
}
|
||||
|
||||
List<Registry> requirementsByIssue = obligations.stream().filter(registry ->
|
||||
new RegistryTradingParams(RegistryDesignation.C, RegistryInstrumentType.S, null, RegistryUnit.T).equalByRegistry(IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, registry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, registry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, registry.getRegistryUnit())))
|
||||
.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(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setSettledDebit(safeBD(relatedRegistry.getSettledDebit()).add(safeBD(requirementByIssue.getBalance())));
|
||||
}
|
||||
registryImdg.update(relatedRegistry);
|
||||
}
|
||||
}
|
||||
|
||||
List<Registry> requirementsByMoney = obligations.stream().filter(registry ->
|
||||
new RegistryTradingParams(RegistryDesignation.C, RegistryInstrumentType.M, null, RegistryUnit.T).equalByRegistry(
|
||||
IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, registry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, registry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, registry.getRegistryUnit())))
|
||||
.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(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setSettledDebit(safeBD(relatedRegistry.getSettledDebit()).add(safeBD(requirementByMoney.getBalance())));
|
||||
}
|
||||
registryImdg.update(relatedRegistry);
|
||||
}
|
||||
}
|
||||
|
||||
List<Registry> obligationsByIssue = obligations.stream().filter(registry ->
|
||||
new RegistryTradingParams(RegistryDesignation.L, RegistryInstrumentType.S, null, RegistryUnit.T).equalByRegistry(
|
||||
IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, registry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, registry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, registry.getRegistryUnit())))
|
||||
.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(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setBalance(safeBD(relatedRegistry.getBalance()).subtract(safeBD(obligationByIssue.getBalance())));
|
||||
} else if (registryTradingParamsT.equalByRegistry(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setSettledDebit(safeBD(relatedRegistry.getSettledDebit()).add(safeBD(obligationByIssue.getBalance())));
|
||||
} else if (registryTradingParamsB.equalByRegistry(IEnumKey.getEnumByKey(RegistryDesignation.class, relatedRegistry.getRegistryDesignation()),
|
||||
IEnumKey.getEnumByKey(RegistryInstrumentType.class, relatedRegistry.getRegistryInstrumentType()),
|
||||
IEnumKey.getEnumByKey(RegistryCapacity.class, relatedRegistry.getRegistryCapacity()),
|
||||
IEnumKey.getEnumByKey(RegistryUnit.class, relatedRegistry.getRegistryUnit()))) {
|
||||
relatedRegistry.setBalance(safeBD(relatedRegistry.getBalance()).add(safeBD(obligationByIssue.getBalance())));
|
||||
}
|
||||
registryImdg.update(relatedRegistry);
|
||||
}
|
||||
}
|
||||
|
||||
List<PaymentInstruction> formedPaymentInstructions = new ArrayList<>();
|
||||
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, 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(registriesLC, sumBalance, sessionId);
|
||||
formedPaymentInstructions.add(paymentInstruction);
|
||||
paymentInstructionImdg.insert(paymentInstruction);
|
||||
registriesLC.forEach(registry -> {
|
||||
registry.setPaymentId(paymentInstruction.getId());
|
||||
registry.setUpdated(Instant.now());
|
||||
registryImdg.update(registry);
|
||||
});
|
||||
}
|
||||
|
||||
sendSdfs(formedPaymentInstructions);
|
||||
|
||||
//todo fill result
|
||||
StageResult<Collection<PaymentInstruction>> stageResult = new StageResult(null, true);
|
||||
stageResult.setStageResult(formedPaymentInstructions);
|
||||
stageResult.setStageResult(null);
|
||||
return stageResult;
|
||||
}
|
||||
|
||||
|
|
@ -514,10 +375,6 @@ public class FormingPaymentInstructionDepositReturn implements ISessionStage {
|
|||
return sDf12;
|
||||
}
|
||||
|
||||
private BigDecimal safeBD(BigDecimal value) {
|
||||
return value != null ? value : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
DateTimeFormatter payDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yy");
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue