diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstructionDepositReturn.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstructionDepositReturn.java index 0d8939dba..f05762570 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstructionDepositReturn.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstructionDepositReturn.java @@ -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 obligations = registryImdg.getCollectionObjectsBySQL(registryCodeCondition); List obligationsByMoney = obligations.stream().filter(registry -> equalByRgs(LM_T, registry)).toList(); - for (Registry obligationByMoney : obligationsByMoney) { - Optional dmx = searchDmx(obligationByMoney); + Map> registriesByGroup = obligations + .stream(). + collect(Collectors.groupingBy(Registry::getGroupId)); + log.info("registry groups found {}", registriesByGroup.size()); + + for (Map.Entry> entry : registriesByGroup.entrySet()) { + List groupRgs = entry.getValue(); + Optional lmtO = groupRgs.stream().filter(rgs -> equalByRgs(LM_T, rgs)).findFirst(); + Optional 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 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 dmtInfo = searchDmtInfo(obligationByMoney); + Optional 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 dmtClnr = searchDmtClrn(obligationByMoney); + Optional 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 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 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 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 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 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 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 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 formedPaymentInstructions = new ArrayList<>(); - 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, 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(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> 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");