diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilderDepositReturn.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilderDepositReturn.java new file mode 100644 index 000000000..897e38acf --- /dev/null +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionBuilderDepositReturn.java @@ -0,0 +1,42 @@ +package ru.spcex.clearing.service.builder; + +import ru.clearing.classes.statics.data.payment.PaymentInstruction; +import ru.clearing.classes.statics.data.registry.Registry; +import ru.spcex.platform.imdg.api.ImdgProvider; + +import java.math.BigDecimal; + +public class PaymentInstructionBuilderDepositReturn { + + private ImdgProvider imdgProvider; + private Long sessionId; + private BigDecimal amount; + private Registry lm_tRgs; + + public static PaymentInstructionBuilderDepositReturn builder(ImdgProvider imdgProvider) { + return new PaymentInstructionBuilderDepositReturn(imdgProvider); + } + + private PaymentInstructionBuilderDepositReturn(ImdgProvider imdgProvider) { + this.imdgProvider = imdgProvider; + } + + public PaymentInstructionBuilderDepositReturn sessionId(Long sessionId) { + this.sessionId = sessionId; + return this; + } + + public PaymentInstructionBuilderDepositReturn amount(BigDecimal amount) { + this.amount = amount; + return this; + } + + public PaymentInstructionBuilderDepositReturn lm_tRgs(Registry rgs) { + this.lm_tRgs = rgs; + return this; + } + + public PaymentInstruction build() { + return null; + } +} 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 new file mode 100644 index 000000000..0d8939dba --- /dev/null +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstructionDepositReturn.java @@ -0,0 +1,532 @@ +package ru.spcex.clearing.session.stage.impl; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.ConfigurableBeanFactory; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Service; +import ru.clearing.classes.statics.data.account.Account; +import ru.clearing.classes.statics.data.company.Company; +import ru.clearing.classes.statics.data.payment.PaymentInstruction; +import ru.clearing.classes.statics.data.registry.Registry; +import ru.clearing.classes.statics.data.sdf.SDf03; +import ru.clearing.classes.statics.data.sdf.SDf12; +import ru.clearing.classes.statics.data.security.Security; +import ru.spcex.clearing.imdg.IMDGDistributedNames; +import ru.spcex.clearing.platform.messaging.domain.Consts; +import ru.spcex.clearing.platform.messaging.domain.cud.balance.ExportToFileRequest; +import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender; +import ru.spcex.clearing.service.builder.PaymentInstructionBuilder; +import ru.spcex.clearing.session.stage.ISessionStage; +import ru.spcex.clearing.session.stage.StageResult; +import ru.spcex.clearing.session.stage.Task; +import ru.spcex.clearing.session.stage.task.FormingPaymentInstructionPayload; +import ru.spcex.platform.enumeration.*; +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.imdg.api.predicate.ImdgPredicate; +import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder; +import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder; +import ru.spcex.platform.utils.enumeration.IEnumKey; + +import java.math.BigDecimal; +import java.time.Instant; +import java.time.format.DateTimeFormatter; +import java.util.*; +import java.util.stream.Collectors; + +@Service +@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) +public class FormingPaymentInstructionDepositReturn implements ISessionStage { + private final Logger log = LoggerFactory.getLogger(getClass()); + private final ImdgProvider imdgProvider; + private final ImdgId idGenerator; + private final Imdg registryImdg; + private final Imdg paymentInstructionImdg; + private final Imdg securityImdg; + private final Imdg accountImdg; + private final Imdg companyImdg; + private final Imdg sDf03Imdg; + private final Imdg sDf12Imdg; + private final KafkaSender kafkaSender; + private final static RegistryTradingParams OS_T; + private final static RegistryTradingParams OM_T; + private final static RegistryTradingParams TS_T; + private final static RegistryTradingParams TM_T; + private final static RegistryTradingParams DM_X; + private final static RegistryTradingParams DM_T; + private final static RegistryTradingParams AM_F; + private final static RegistryTradingParams CM_T; + private final static RegistryTradingParams LM_T; + private final static RegistryTradingParams CS_T; + private final static RegistryTradingParams LS_T; + static { + OS_T = new RegistryTradingParams(RegistryDesignation.O, + RegistryInstrumentType.S, + null, + RegistryUnit.T); + + OM_T = new RegistryTradingParams(RegistryDesignation.O, + RegistryInstrumentType.M, + null, + RegistryUnit.T); + + TS_T = new RegistryTradingParams(RegistryDesignation.T, + RegistryInstrumentType.S, + null, + RegistryUnit.T); + + TM_T = new RegistryTradingParams(RegistryDesignation.T, + RegistryInstrumentType.M, + null, + RegistryUnit.T); + DM_X = new RegistryTradingParams(RegistryDesignation.D, + RegistryInstrumentType.M, + null, + RegistryUnit.X); + DM_T = new RegistryTradingParams(RegistryDesignation.D, + RegistryInstrumentType.M, + null, + RegistryUnit.T); + AM_F = new RegistryTradingParams(RegistryDesignation.A, + RegistryInstrumentType.M, + null, + RegistryUnit.F); + + + + CM_T = new RegistryTradingParams(RegistryDesignation.C, + RegistryInstrumentType.M, + null, + RegistryUnit.T); + LM_T = new RegistryTradingParams(RegistryDesignation.L, + RegistryInstrumentType.M, + null, + RegistryUnit.T); + CS_T = new RegistryTradingParams(RegistryDesignation.C, + RegistryInstrumentType.S, + null, + RegistryUnit.T); + LS_T = new RegistryTradingParams(RegistryDesignation.L, + RegistryInstrumentType.S, + null, + RegistryUnit.T); + + + } + + @Autowired + public FormingPaymentInstructionDepositReturn(ImdgProvider imdgProvider, + KafkaSender kafkaSender) { + this.kafkaSender = kafkaSender; + this.imdgProvider = imdgProvider; + this.idGenerator = imdgProvider.getImdgIdGenerator(); + this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class); + this.securityImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Security, Security.class); + this.accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); + this.companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class); + this.sDf03Imdg = imdgProvider.getImdg(IMDGDistributedNames.Map_SDf03, SDf03.class); + this.sDf12Imdg = imdgProvider.getImdg(IMDGDistributedNames.Map_SDf12, SDf12.class); + this.paymentInstructionImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_PaymentInstruction, PaymentInstruction.class); + } + + @Override + public StageResult submit(Task task) { + FormingPaymentInstructionPayload payload = (FormingPaymentInstructionPayload) task.getData(); + switch (task.getTaskType()) { + case FormingPaymentInstruction -> { + return formingPaymentInstruction(payload.getSessionId()); + } + default -> { + throw new IllegalStateException("Unknown task type: " + task.getTaskType()); + } + } + } + + //CM_T LM_T CS_T LS_T + + private StageResult formingPaymentInstruction(Long sessionId) { + + RegistryCodeSqlBuilder registryCodeSqlBuilder = RegistryCodeSqlBuilder.getInstance(LM_T,CM_T, LS_T, CS_T); + String registryCodeCondition = registryCodeSqlBuilder.build(); + + Collection obligations = registryImdg.getCollectionObjectsBySQL(registryCodeCondition); + List obligationsByMoney = obligations.stream().filter(registry -> equalByRgs(LM_T, registry)).toList(); + + for (Registry obligationByMoney : obligationsByMoney) { + Optional dmx = searchDmx(obligationByMoney); + 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()); + continue; + } + + Optional dmtInfo = searchDmtInfo(obligationByMoney); + if (dmtInfo.isPresent()) { + PaymentInstruction payInstr = createPaymentInstruction(List.of(obligationByMoney), dmtInfo.get().getBalance(), sessionId); + obligationByMoney.setPaymentId(payInstr.getId()); + obligationByMoney.setUpdated(Instant.now()); + registryImdg.update(obligationByMoney); + continue; + } + + Optional dmtClnr = searchDmtClrn(obligationByMoney); + 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); + + StageResult> stageResult = new StageResult(null, true); + stageResult.setStageResult(formedPaymentInstructions); + return stageResult; + } + + /** + * fixme duplicate + * Если УК зачисляет средства на ТБС Инициатора + */ + private Optional searchDmx(Registry rgs) { + String sqlCondition = String.format("(%s) and companyId = %d", + RegistryCodeSqlBuilder.getInstance(DM_X).build(), + rgs.getCounterPartyId()); + Registry dmx = registryImdg.getSingleObjectBySQL(sqlCondition); + return Optional.ofNullable(dmx); + } + + /** + * fixme duplicate + * Если УК зачисляет средства на свой регистр на КС + */ + private Optional searchDmtInfo(Registry rgs) { + String sqlCondition = String.format("(%s) and accountType='%s' and companyId = %d and counterPartyId = %d", + RegistryCodeSqlBuilder.getInstance(DM_T).build(), + AccountType.Info.getKey(), + rgs.getCompanyId(), + rgs.getCounterPartyId()); + Registry dmt = registryImdg.getSingleObjectBySQL(sqlCondition); + return Optional.ofNullable(dmt); + } + + /** + * fixme duplicate + * Если УК зачисляет средства на свой ТБС + */ + private Optional searchDmtClrn(Registry rgs) { + String sqlCondition = String.format("(%s) and accountType='%s' and companyId = %d and counterPartyId = %d", + RegistryCodeSqlBuilder.getInstance(DM_T).build(), + AccountType.Clrn.getKey(), + rgs.getCompanyId(), + rgs.getCounterPartyId()); + Registry dmt = registryImdg.getSingleObjectBySQL(sqlCondition); + return Optional.ofNullable(dmt); + } + + + + private void sendSdfs(List formedPaymentInstructions) { + List sDf03Created = new ArrayList<>(); + List sDf12Created = new ArrayList<>(); + for (PaymentInstruction paymentInstruction : formedPaymentInstructions) { + Security security = securityImdg.getSingleObjectByID(paymentInstruction.getCreditLeg_securityId()); + Account account = accountImdg.getSingleObjectByID(paymentInstruction.getCreditLeg_accountId()); + if (InstrumentType.CRNC.equalsByKey(security.getInstrumentType()) && + List.of(AccountType.Corr, AccountType.Clrn).contains(IEnumKey.getEnumByKey(AccountType.class, account.getAccountType()))) { + sDf03Created.add(newSDf03(paymentInstruction)); + } else if (!InstrumentType.CRNC.equalsByKey(security.getInstrumentType()) && + AccountType.Depo.equalsByKey(account.getAccountType())) { + sDf12Created.add(newSDf12(paymentInstruction)); + } + } + + Long sdf03GroupId = !sDf03Created.isEmpty() ? imdgProvider.getImdgIdGenerator().nextId() : null; + for (SDf03 sDf03 : sDf03Created) { + sDf03.setGenerationId(sdf03GroupId); + sDf03Imdg.insert(sDf03); + } + + if (sdf03GroupId != null) { + ExportToFileRequest exportToFileRequest = new ExportToFileRequest(); + exportToFileRequest.setNameOfTable("DF-03"); + exportToFileRequest.setSdfGroupId(sdf03GroupId); + kafkaSender.sendRequestToQueue(Consts.EXPORT_PROCESS, exportToFileRequest); + } + + Long sdf12GroupId = null; + Long maxTxNumber = 1L; + if (!sDf12Created.isEmpty()) { + sdf12GroupId = !sDf12Created.isEmpty() ? imdgProvider.getImdgIdGenerator().nextId() : null; + ImdgPredicateBuilder predicateBuilder = sDf12Imdg.predicateBuilder(); + ImdgPredicate notEmptyTransactionNum = predicateBuilder.not(predicateBuilder.equals("transaction_number", "")); + Long maxId = sDf12Imdg.aggregateLongMax("id", notEmptyTransactionNum); + if (maxId != null) { + SDf12 sDf12 = sDf12Imdg.getSingleObjectByID(maxId); + maxTxNumber = Long.parseLong(sDf12.getTransactionNumber()) + 1; + } + } + for (SDf12 sDf12 : sDf12Created) { + sDf12.setGenerationId(sdf03GroupId); + sDf12.setTransactionNumber(maxTxNumber.toString()); + sDf12.setTransactionQuantity(String.valueOf(sDf12Created.size())); + sDf12Imdg.insert(sDf12); + } + + if (sdf12GroupId != null) { + ExportToFileRequest exportToFileRequest = new ExportToFileRequest(); + exportToFileRequest.setNameOfTable("DF-12"); + exportToFileRequest.setSdfGroupId(sdf12GroupId); + kafkaSender.sendRequestToQueue(Consts.EXPORT_PROCESS, exportToFileRequest); + } + } + + private PaymentInstruction createPaymentInstruction(List registries, BigDecimal balance, Long sessionId) { + PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registries) + .sessionId(sessionId) + .amount(balance); + return paymentInstructionBuilder.build(); + } + + private SDf03 newSDf03(PaymentInstruction paymentInstruction) { + log.debug("creating sdf03"); + SDf03 sDf03 = new SDf03(); + sDf03.setId(idGenerator.nextId()); + sDf03.setSeg_type("S"); + sDf03.setDoc_type("002"); + String strId = paymentInstruction.getId().toString(); + String strIdCut = strId.length() > 16 ? strId.substring(strId.length() - 16) : strId; + sDf03.setDocnm_ref(strIdCut); + sDf03.setC_acc_deb(paymentInstruction.getCreditLeg_account()); + + String senderSbankName = ""; + if (paymentInstruction.getSenderId().equals(1L)) { + senderSbankName = paymentInstruction.getPayeeBankName(); + } else { + Company company = companyImdg.getSingleObjectByID(paymentInstruction.getSenderId()); + if (company != null) { + senderSbankName = company.getShortName(); + } + } + sDf03.setSbanknam1(senderSbankName); + sDf03.setSbanknam2(senderSbankName); + sDf03.setSbanknam3(senderSbankName); + sDf03.setSbanknam4(senderSbankName); + sDf03.setSbanknam5(senderSbankName); + + sDf03.setC_acc_cred(paymentInstruction.getDebitLeg_account()); + + String addresseeSbankName = ""; + if (paymentInstruction.getAddresseeId().equals(1L)) { + addresseeSbankName = paymentInstruction.getAddresseeBankName(); + } else { + Company company = companyImdg.getSingleObjectByID(paymentInstruction.getAddresseeId()); + if (company != null) { + addresseeSbankName = company.getShortName(); + } + } + sDf03.setRbanknam1(addresseeSbankName); + sDf03.setRbanknam2(addresseeSbankName); + sDf03.setRbanknam3(addresseeSbankName); + sDf03.setRbanknam4(addresseeSbankName); + sDf03.setRbanknam5(addresseeSbankName); + + sDf03.setPay_date(payDateFormatter.format(paymentInstruction.getPaymentDate())); + sDf03.setPay_val("RUR"); + sDf03.setSum_deb(paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : ""); + sDf03.setSpecif_1(paymentInstruction.getPaymentPurpose()); + sDf03.setGenerationTime(Instant.now()); + sDf03.setPaymentInstructionId(paymentInstruction.getId()); + log.debug("successfully processed, new id {}", sDf03.getId()); + return sDf03; + } + + private SDf12 newSDf12(PaymentInstruction paymentInstruction) { + log.debug("creating sdf12"); + SDf12 sDf12 = new SDf12(); + sDf12.setId(idGenerator.nextId()); + sDf12.setOutDocument(sDf12.getId().toString()); + sDf12.setDirection("DELFREE"); + sDf12.setQuantity(paymentInstruction.getCreditLeg_amount().toString()); + sDf12.setSecurityCode(paymentInstruction.getCreditLeg_securityId().toString()); + sDf12.setDepoCodeSender(paymentInstruction.getCreditLeg_account()); + sDf12.setDepoCodeAdressee(paymentInstruction.getDebitLeg_account()); +// sDf12.setTransactionNumber(); + sDf12.setGenerationTime(Instant.now()); + return sDf12; + } + + private BigDecimal safeBD(BigDecimal value) { + return value != null ? value : BigDecimal.ZERO; + } + + DateTimeFormatter payDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yy"); + + + private static boolean equalByRgs(RegistryTradingParams rgsParams, Registry rgs) { + return rgsParams.equalByRegistry( + IEnumKey.getEnumByKey(RegistryDesignation.class, rgs.getRegistryDesignation()), + IEnumKey.getEnumByKey(RegistryInstrumentType.class, rgs.getRegistryInstrumentType()), + IEnumKey.getEnumByKey(RegistryCapacity.class, rgs.getRegistryCapacity()), + IEnumKey.getEnumByKey(RegistryUnit.class, rgs.getRegistryUnit()) + ); + } +}