From 8b119cc145fb4f24030685817db119b4b7193051 Mon Sep 17 00:00:00 2001 From: AKurakin Date: Thu, 9 Feb 2023 12:15:53 +0300 Subject: [PATCH] =?UTF-8?q?http://jira.mfd.msk:8088/browse/CLS-189=20Payme?= =?UTF-8?q?ntInstructionCreator=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?PaymentInstruction=20(=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B0=20?= =?UTF-8?q?=D1=85=D0=B0=D0=BA=D0=B0=20650583bd7e52f5f)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/spcex/clearing/service/Clearing.java | 30 ++-- .../builder/PaymentInstructionCreator.java | 132 ++++++++++++------ 2 files changed, 109 insertions(+), 53 deletions(-) diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/Clearing.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/Clearing.java index 218f621f2..c7c78d5fa 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/Clearing.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/Clearing.java @@ -253,18 +253,26 @@ public class Clearing { liabilitiesClaimsAssetsImdg.update(lcaSecondLeg); log.trace("creating paymentInstructions"); - boolean isCreatedFirstLegI = ClearingCategory.I.equals(category) && !lcaFirstLegFound.isPresent(); - if (isCreatedFirstLegI) { - log.debug("ignore create paymentInstruction for I firstLeg new: execDeposit {}, lcaFirstLeg {}", - execDeposit.getId(), lcaFirstLeg.getId()); + PaymentInstruction firstInstructionIExist = null; + if (ClearingCategory.I.equals(category)) { + log.trace("For category I search paymentInstruction by lca[{}]", lcaFirstLeg.getId()); + firstInstructionIExist = paymentInstructionCreator.searchExistPaymentInstructionsFirstI(lcaFirstLeg); + } + + List paymentInstructions; + if (firstInstructionIExist != null) { + log.debug("ignore create paymentInstruction for I firstLeg: execDeposit {}, lcaFirstLeg {}, updating paymentInstruction {}", + execDeposit.getId(), lcaFirstLeg.getId(), firstInstructionIExist.getId()); + paymentInstructionCreator.updatePaymentInstructionsFirstI(firstInstructionIExist, lcaFirstLeg, category); + paymentInstructions = Collections.singletonList(firstInstructionIExist); } else { - List paymentInstructions = paymentInstructionCreator.createAndSavePaymentInstructions(execDeposit, lcaFirstLeg, Collections.singleton(category)); - if (paymentInstructions.isEmpty()) { - log.trace("For lcaFirstLeg[{}] did not creaded paymentInstruction's", lcaFirstLeg.getId()); - } else { - paymentInstructionCreator.updateFieldLiabilitiesClaimsAssetsByPayment(lcaFirstLeg, lcmFirstLeg, paymentInstructions, Collections.singleton(category)); - liabilitiesClaimsAssetsImdg.update(lcaFirstLeg); - } + paymentInstructions = paymentInstructionCreator.createAndSavePaymentInstructions(execDeposit, lcaFirstLeg, Collections.singleton(category)); + } + if (paymentInstructions.isEmpty()) { + log.trace("For lcaFirstLeg[{}] did not created paymentInstruction's", lcaFirstLeg.getId()); + } else { + paymentInstructionCreator.updateFieldLiabilitiesClaimsAssetsByPayment(lcaFirstLeg, lcmFirstLeg, paymentInstructions, Collections.singleton(category)); + liabilitiesClaimsAssetsImdg.update(lcaFirstLeg); } log.trace("lcaFirstLeg {}, lcaSecondLeg {}", lcaFirstLeg, lcaSecondLeg); diff --git a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionCreator.java b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionCreator.java index 110f6d025..4ab020059 100644 --- a/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionCreator.java +++ b/clearing-parent/clearing-service/src/main/java/ru/spcex/clearing/service/builder/PaymentInstructionCreator.java @@ -22,21 +22,11 @@ import java.math.BigDecimal; import java.time.Instant; import java.time.LocalDate; import java.time.format.DateTimeFormatter; -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.concurrent.atomic.AtomicLong; import static ru.spcex.platform.utils.number.BigDecimalUtil.safeBD; -/* -todo использовать так: - paymentInstructionCreator.createAndSavePaymentInstructions() - paymentInstructionCreator.updateLiabilitiesClaimsAssetsByPaymentInstruction(); - paymentInstructionCreator.updateLiabilitiesClaimsMoneyByPaymentInstruction(); - - */ @Service public class PaymentInstructionCreator { protected static final Long SPVB_ID = 1L; // СПВБ @@ -85,35 +75,13 @@ public class PaymentInstructionCreator { return newPaymentInstructions; } - /** - * Создаёт и сохраняет - * - * - * @param executionDeposit - * @param liabilitiesClaimsAssets - * @param clearingCategory - * @return - */ - protected List createPaymentInstructions(ExecutionDeposit executionDeposit, LiabilitiesClaimsAssets liabilitiesClaimsAssets, - Collection clearingCategory) { - List newPaymentInstructions = new ArrayList<>(); - Instant now = Instant.now(); - LocalDate today = LocalDate.now(); - if (!today.equals(liabilitiesClaimsAssets.getSettlementDate())) { - log.debug("For liabilitiesClaimsAssets.SettlementDate={} is not today.", liabilitiesClaimsAssets.getSettlementDate()); - return newPaymentInstructions; - } - // I - if (clearingCategory.contains(ClearingCategory.I)) { - log.debug("By liabilitiesClaimsAssets[{}] create for category I 1 PaymentInstruction record", liabilitiesClaimsAssets.getId()); - PaymentInstruction payment1 = new PaymentInstruction(); - payment1.setId(idGenerator.nextId()); - payment1.setCreated(now); + protected void updateOrNewFieldPaymentInstructionsFirstI(PaymentInstruction payment1, + LiabilitiesClaimsAssets liabilitiesClaimsAssets, + Instant now) { + payment1.setClearingDate(TimeUtil.toLocalDate(now)); - payment1.setSenderId(liabilitiesClaimsAssets.getCompanyId()); - payment1.setAddresseeId(SPVB_ID); // СПВБ String symbol1 = selectSymbolValue(payment1.getAddresseeId(), CompanySymbol.BIC); @@ -136,13 +104,11 @@ public class PaymentInstructionCreator { payment1.setPayeeBic(symbolPRC); payment1.setAddresseeBankName(companyPRCName); - payment1.setPaymentDate(TimeUtil.localDateToInstant(liabilitiesClaimsAssets.getSettlementDate())); - payment1.setPaymentPurpose("Размещение депозита " + liabilitiesClaimsAssets.getContract()); + payment1.setPaymentPurpose(paymentPurpose(liabilitiesClaimsAssets.getContract())); payment1.setSettlementDate(liabilitiesClaimsAssets.getSettlementDate()); - Long amount = liabilitiesClaimsAssets.getLiabilitiesQuantity() == null ? null : liabilitiesClaimsAssets.getLiabilitiesQuantity().abs().longValue(); payment1.setCreditLegAmount(amount); payment1.setDebitLegAmount(amount); @@ -167,7 +133,6 @@ public class PaymentInstructionCreator { } payment1.setDebitCsAccount(null); - // payment1.setCreditLegDirection(InOutDirection.out.getKey()); // payment1.setDebitLegDirection(InOutDirection.in.getKey()); @@ -177,6 +142,39 @@ public class PaymentInstructionCreator { payment1.setTransactionStatus(TransactionStatus.stld.getKey()); +// payment1.setDocumentNumber(nextDocumentNumber(liabilitiesClaimsAssets, payment1)); + } + + /** + * Создаёт и сохраняет + * + * + * @param executionDeposit + * @param liabilitiesClaimsAssets + * @param clearingCategory + * @return + */ + protected List createPaymentInstructions(ExecutionDeposit executionDeposit, LiabilitiesClaimsAssets liabilitiesClaimsAssets, + Collection clearingCategory) { + List newPaymentInstructions = new ArrayList<>(); + Instant now = Instant.now(); + LocalDate today = LocalDate.now(); + if (!today.equals(liabilitiesClaimsAssets.getSettlementDate())) { + log.debug("For liabilitiesClaimsAssets.SettlementDate={} is not today.", liabilitiesClaimsAssets.getSettlementDate()); + return newPaymentInstructions; + } + // I + if (clearingCategory.contains(ClearingCategory.I)) { + // см. updatePaymentInstructionsFirstI() - код одинаковый + log.debug("By liabilitiesClaimsAssets[{}] create for category I 1 PaymentInstruction record", liabilitiesClaimsAssets.getId()); + PaymentInstruction payment1 = new PaymentInstruction(); + + payment1.setId(idGenerator.nextId()); + payment1.setCreated(now); + payment1.setClearingDate(TimeUtil.toLocalDate(now)); + + updateOrNewFieldPaymentInstructionsFirstI(payment1, liabilitiesClaimsAssets, now); + payment1.setDocumentNumber(nextDocumentNumber(liabilitiesClaimsAssets, payment1)); newPaymentInstructions.add(payment1); @@ -529,6 +527,56 @@ public class PaymentInstructionCreator { return newPaymentInstructions; } + /** + * + * @param contract liabilitiesClaimsAssets.contract + * @return + */ + protected String paymentPurpose(String contract) { + return "Размещение депозита " + (contract == null ? "" : contract); + } + + public PaymentInstruction searchExistPaymentInstructionsFirstI(LiabilitiesClaimsAssets lca) { + HashMap> criteria = new HashMap<>(); + criteria.put("creditLegAccountId", lca.getAccountId()); + criteria.put("senderId", lca.getCompanyId()); + criteria.put("paymentPurpose", paymentPurpose(lca.getContract())); + criteria.put("settlementDate", lca.getSettlementDate()); + PaymentInstruction payment1 = paymentInstructionImdg.getSingleObjectByFieldValues(criteria); + return payment1; + } + + /** + * Обновление п оаналогии с createPaymentInstructions + * @param payment1 + * @param liabilitiesClaimsAssets + * @param clearingCategory + */ + public void updatePaymentInstructionsFirstI(PaymentInstruction payment1, + LiabilitiesClaimsAssets liabilitiesClaimsAssets, + ClearingCategory clearingCategory) { + Instant now = Instant.now(); + LocalDate today = TimeUtil.toLocalDate(now); + if (!today.equals(liabilitiesClaimsAssets.getSettlementDate())) { + log.debug("For liabilitiesClaimsAssets.SettlementDate={} is not today.", liabilitiesClaimsAssets.getSettlementDate()); + return; + } + + log.debug("By liabilitiesClaimsAssets[{}] update for category I 1 PaymentInstruction {} record", + liabilitiesClaimsAssets.getId(), payment1.getId()); + + // I check + if (!ClearingCategory.I.equals(clearingCategory)) { + throw new IllegalArgumentException("Clearing category is not I. Method not implemented for " + clearingCategory); + } + + payment1.setUpdated(now); + updateOrNewFieldPaymentInstructionsFirstI(payment1, liabilitiesClaimsAssets, now); + + paymentInstructionImdg.update(payment1); + log.trace("PaymentInstruction {} was updated", payment1.getId()); + } + protected String nextDocumentNumber(LiabilitiesClaimsAssets liabilitiesClaimsAssets, PaymentInstruction paymentInstruction) { LocalDate nowD = LocalDate.now(); if (documentNumberResetAt == null || documentNumberResetAt.isBefore(nowD)) synchronized (this) { @@ -595,7 +643,7 @@ public class PaymentInstructionCreator { Collection clearingCategory) { // final boolean isFirstLeg = true; // final boolean isSecondLeg = !isFirstLeg; - PaymentInstruction paymentIAny = paymentInstructions.iterator().next(); + PaymentInstruction paymentIAny = paymentInstructions.get(paymentInstructions.size() - 1); // брать последнюю запись для ID // if (clearingCategory.contains(ClearingCategory.I) || clearingCategory.contains(ClearingCategory.V)) { // BigDecimal amount = bdFromLongSafe(paymentIAny.getCreditLegAmount());