diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java index afe3be8ef..4c7b635f6 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/session/stage/impl/FormingPaymentInstruction.java @@ -26,6 +26,8 @@ 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 ru.spcex.platform.utils.enumeration.IMessageResolver; @@ -33,6 +35,7 @@ import ru.spcex.platform.utils.enumeration.SimpleMessageResolver; import java.math.BigDecimal; import java.time.Instant; +import java.time.format.DateTimeFormatter; import java.util.*; import java.util.stream.Collectors; @@ -271,22 +274,26 @@ public class FormingPaymentInstruction implements ISessionStage { } private void sendSdfs(List formedPaymentInstructions) { - Long sdf03GroupId = null; - Long sdf12GroupId = null; + 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()))) { - sdf03GroupId = sdf03GroupId != null ? sdf12GroupId : imdgProvider.getImdgIdGenerator().nextId(); - newSDf03(paymentInstruction, sdf03GroupId); + sDf03Created.add(newSDf03(paymentInstruction)); } else if (!InstrumentType.CRNC.equalsByKey(security.getInstrumentType()) && AccountType.Depo.equalsByKey(account.getAccountType())) { - sdf12GroupId = sdf12GroupId != null ? sdf12GroupId : imdgProvider.getImdgIdGenerator().nextId(); - newSDf12(paymentInstruction, sdf12GroupId); + 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"); @@ -294,6 +301,25 @@ public class FormingPaymentInstruction implements ISessionStage { 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"); @@ -309,7 +335,7 @@ public class FormingPaymentInstruction implements ISessionStage { return paymentInstructionBuilder.build(); } - private void newSDf03(PaymentInstruction paymentInstruction, Long sdf03GroupId) { + private SDf03 newSDf03(PaymentInstruction paymentInstruction) { log.debug("creating sdf03"); SDf03 sDf03 = new SDf03(); sDf03.setId(idGenerator.nextId()); @@ -318,7 +344,7 @@ public class FormingPaymentInstruction implements ISessionStage { 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.getDebitLeg_account()); + sDf03.setC_acc_deb(paymentInstruction.getCreditLeg_account()); String senderSbankName = ""; if (paymentInstruction.getSenderId().equals(1L)) { @@ -335,7 +361,7 @@ public class FormingPaymentInstruction implements ISessionStage { sDf03.setSbanknam4(senderSbankName); sDf03.setSbanknam5(senderSbankName); - sDf03.setC_acc_cred(paymentInstruction.getCreditLeg_account()); + sDf03.setC_acc_cred(paymentInstruction.getDebitLeg_account()); String addresseeSbankName = ""; if (paymentInstruction.getAddresseeId().equals(1L)) { @@ -352,29 +378,35 @@ public class FormingPaymentInstruction implements ISessionStage { sDf03.setRbanknam4(addresseeSbankName); sDf03.setRbanknam5(addresseeSbankName); -// sDf03.setPay_date();// + 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.setGenerationId(sdf03GroupId); sDf03.setPaymentInstructionId(paymentInstruction.getId()); - sDf03Imdg.insert(sDf03); log.debug("successfully processed, new id {}", sDf03.getId()); + return sDf03; } - private void newSDf12(PaymentInstruction paymentInstruction, Long sdf12GroupId) { + private SDf12 newSDf12(PaymentInstruction paymentInstruction) { log.debug("creating sdf12"); SDf12 sDf12 = new SDf12(); sDf12.setId(idGenerator.nextId()); sDf12.setOutDocument(sDf12.getId().toString()); -// sDf12.setQuantity(); + 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()); - sDf12.setGenerationId(sdf12GroupId); - sDf12Imdg.insert(sDf12); + return sDf12; } private BigDecimal safeBD(BigDecimal value) { return value != null ? value : BigDecimal.ZERO; } + + DateTimeFormatter payDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yy"); + }