правка 7 шага для МКР и промежуточной сессии

This commit is contained in:
ialbert 2023-08-02 19:18:13 +03:00
parent 7174024333
commit c8fa6e052e
4 changed files with 111 additions and 76 deletions

View file

@ -43,7 +43,7 @@ public class FinalMkrSession extends AbstractSession implements InitializingBean
private final InspectionObligations inspectionObligations;
private final FormingRegistersOnOS formingRegistersOnOS;
private final FormingPaymentInstructionReturnMkr formingPaymentInstructionReturn;
private final FormingPaymentInstructionDealsFinalMkr formingPaymentInstructionDeals;
private final FormingPaymentInstructionAssets formingPaymentInstructionDeals;
private final UnlockResources unlockResources;
private final FinishingSession finishingSession;
private final EndStageNotification endStageNotification;
@ -61,7 +61,7 @@ public class FinalMkrSession extends AbstractSession implements InitializingBean
ObligationAdmission obligationsAdmission,
InclusionObligations inclusionObligations,
InspectionObligations inspectionObligations, FormingRegistersOnOS formingRegistersOnOS,
FormingPaymentInstructionReturnMkr formingPaymentInstructionReturn, FormingPaymentInstructionDealsFinalMkr formingPaymentInstructionDeals,
FormingPaymentInstructionReturnMkr formingPaymentInstructionReturn, FormingPaymentInstructionAssets formingPaymentInstructionDeals,
UnlockResources unlockResources,
FinishingSession finishingSession,
EndStageNotification endStageNotification,
@ -104,6 +104,7 @@ public class FinalMkrSession extends AbstractSession implements InitializingBean
inclusionObligations.setSessionType(sessionType());
inspectionObligationsReturn.setSessionType(sessionType());
inspectionObligations.setSessionType(sessionType());
formingPaymentInstructionDeals.setSection(section());
imdgProvider.waitAvailable();
initSessionIfPresent();
}
@ -197,7 +198,7 @@ public class FinalMkrSession extends AbstractSession implements InitializingBean
StageResult<Collection<PaymentInstruction>> paymentResult = null;
{
FormingPaymentInstructionDealsMkrPayload payload = new FormingPaymentInstructionDealsMkrPayload();
FormingPaymentInstructionPayload payload = new FormingPaymentInstructionPayload();
payload.setSessionId(currSession.getId());
payload.setPaymentInstructionReturns(returnsPayment.getStageResult());
//stage 7

View file

@ -43,7 +43,7 @@ public class IntermediateMkrSession extends AbstractSession implements Initializ
private final InspectionObligations inspectionObligations;
private final FormingRegistersOnOS formingRegistersOnOS;
private final FormingPaymentInstructionReturnMkr formingPaymentInstructionReturn;
private final FormingPaymentInstructionDealsFinalMkr formingPaymentInstructionDeals;
private final FormingPaymentInstructionAssets formingPaymentInstructionDeals;
private final UnlockResources unlockResources;
private final FinishingSession finishingSession;
private final EndStageNotification endStageNotification;
@ -61,7 +61,7 @@ public class IntermediateMkrSession extends AbstractSession implements Initializ
ObligationAdmission obligationsAdmission,
InclusionObligations inclusionObligations,
InspectionObligations inspectionObligations, FormingRegistersOnOS formingRegistersOnOS,
FormingPaymentInstructionReturnMkr formingPaymentInstructionReturn, FormingPaymentInstructionDealsFinalMkr formingPaymentInstructionDeals,
FormingPaymentInstructionReturnMkr formingPaymentInstructionReturn, FormingPaymentInstructionAssets formingPaymentInstructionDeals,
UnlockResources unlockResources,
FinishingSession finishingSession,
EndStageNotification endStageNotification,
@ -104,6 +104,7 @@ public class IntermediateMkrSession extends AbstractSession implements Initializ
inclusionObligations.setSessionType(sessionType());
inspectionObligationsReturn.setSessionType(sessionType());
inspectionObligations.setSessionType(sessionType());
formingPaymentInstructionDeals.setSection(section());
imdgProvider.waitAvailable();
initSessionIfPresent();
}
@ -184,7 +185,7 @@ public class IntermediateMkrSession extends AbstractSession implements Initializ
StageResult<Collection<PaymentInstruction>> paymentResult = null;
{
FormingPaymentInstructionDealsMkrPayload payload = new FormingPaymentInstructionDealsMkrPayload();
FormingPaymentInstructionPayload payload = new FormingPaymentInstructionPayload();
payload.setSessionId(currSession.getId());
payload.setPaymentInstructionReturns(returnsPayment.getStageResult());
//stage 7

View file

@ -43,6 +43,7 @@ import ru.spcex.platform.utils.number.BigDecimalUtil;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.*;
import java.util.stream.Stream;
import static ru.spcex.platform.enumeration.RegistryTradingParams.*;
import static ru.spcex.platform.utils.number.BigDecimalUtil.safeBD;
@ -66,6 +67,7 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
private final IMessageResolver msgResolver = new SimpleMessageResolver();
private final SecuritySelector<Security> securitySelector;
private final RegistryManager rgsMng;
private Section section;
@Autowired
public FormingPaymentInstructionAssets(ImdgProvider imdgProvider,
@ -90,7 +92,7 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
FormingPaymentInstructionPayload payload = (FormingPaymentInstructionPayload) task.getData();
switch (task.getTaskType()) {
case FormingPaymentInstruction -> {
return formingPaymentInstructions(payload.getSessionId());
return formingPaymentInstructions(payload);
}
default -> {
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
@ -98,12 +100,15 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
}
}
private Collection<Registry> selectAMBRegistries() {
private Collection<Registry> selectAMBRegistries(Long sessionId) {
ImdgPredicateBuilder rgsPb = registryImdg.predicateBuilder();
RegistryCodeSqlBuilder registryCodeSqlBuilder = RegistryCodeSqlBuilder.getInstance(
AM_B
);
ImdgPredicate rgsCodePrdct = rgsPb.sql(registryCodeSqlBuilder.build());
ImdgPredicate rgsCodePrdct = rgsPb.and(
rgsPb.sql(registryCodeSqlBuilder.build()),
rgsPb.equals("sessionId", sessionId)
);
return registryImdg.getCollectionObjectsByPredicate(rgsCodePrdct);
}
@ -116,8 +121,9 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
return registryImdg.getCollectionObjectsByPredicate(rgsCodePrdct);
}
private StageResult<?> formingPaymentInstructions(Long sessionId) {
Collection<Registry> AMBregistries = selectAMBRegistries();
private StageResult<?> formingPaymentInstructions(FormingPaymentInstructionPayload payload) {
Long sessionId = payload.getSessionId();
Collection<Registry> AMBregistries = selectAMBRegistries(sessionId);
log.debug("found AMB registries.size() = {}", AMBregistries.size());
Instant now = Instant.now();
Account tranAcc = accountImdg.getFirstObjectBySQL("accountType = '%s' and status = '%s' and processingSign = '%s'"
@ -210,75 +216,84 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
registryImdg.update(registry);
paymentInstructions.add(paymentInstruction);
}
if (section == null || !section.equals(Section.MKR)) {
Collection<Registry> ASBregistries = selectASBRegistries();
log.debug("found ASB registries.size() = {}", ASBregistries.size());
Collection<Registry> ASBregistries = selectASBRegistries();
log.debug("found ASB registries.size() = {}", ASBregistries.size());
for (Registry registry : ASBregistries) {
//по каждому AMB регистру создаем PaymentInstruction
//проверяем balance посчитанный на шаге 5
for (Registry registry : ASBregistries) {
//по каждому AMB регистру создаем PaymentInstruction
//проверяем balance посчитанный на шаге 5
if (registry.getBalance().compareTo(BigDecimal.ZERO) == 0) {
log.debug("Skip creating paymentInstruction by registry with 0 balance");
continue;
if (registry.getBalance().compareTo(BigDecimal.ZERO) == 0) {
log.debug("Skip creating paymentInstruction by registry with 0 balance");
continue;
}
Account counterAcc = accountImdg.getSingleObjectByID(registry.getAccountId());
boolean isPositiveBalance = registry.getBalance().compareTo(BigDecimal.ZERO) > 0;
Long senderId;
Long addresseeId;
Account debitLegAccount;
Account creditLegAccount;
BigDecimal amount;
if (isPositiveBalance) {
senderId = registry.getCompanyId();
addresseeId = Sender.One.getId();
debitLegAccount = dtrnAcc;
creditLegAccount = counterAcc;
amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAstO = rgsMng.findRelatedAsset(
registry.getTradingClearingRegistryId(),
registry.getCompanyId(),
registry.getSecuritySymbol(),
AS_T
);
payerAstO.ifPresent(amt -> {
amt.setSettledDebit(safeBD(amt.getSettledDebit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now);
});
} else {
senderId = Sender.One.getId();
addresseeId = registry.getCompanyId();
debitLegAccount = counterAcc;
creditLegAccount = dtrnAcc;
amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAstO = rgsMng.findRelatedAsset(
registry.getTradingClearingRegistryId(),
registry.getCompanyId(),
registry.getSecuritySymbol(),
AS_T
);
payerAstO.ifPresent(amt -> {
amt.setSettledCredit(safeBD(amt.getSettledCredit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now);
});
}
PaymentInstructionBuilderV2 paymentInstructionBuilder = PaymentInstructionBuilderV2.builder(imdgProvider)
.registry(registry)
.sender(senderId)
.addressee(addresseeId)
.debitLegAccount(debitLegAccount)
.creditLegAccount(creditLegAccount)
.amount(amount)
.sessionId(sessionId)
.purpose(String.format("Перевод по итогу клиринга по ТКР %s", registry.getTradingClearingRegistry()));
PaymentInstruction paymentInstruction = paymentInstructionBuilder.build();
log.debug("Created paymentInstruction by registry.id: {}", registry.getId());
paymentInstructionImdg.insert(paymentInstruction);
registry.setPaymentId(paymentInstruction.getId());
registry.setUpdated(now);
registryImdg.update(registry);
paymentInstructions.add(paymentInstruction);
}
Account counterAcc = accountImdg.getSingleObjectByID(registry.getAccountId());
boolean isPositiveBalance = registry.getBalance().compareTo(BigDecimal.ZERO) > 0;
Long senderId;
Long addresseeId;
Account debitLegAccount;
Account creditLegAccount;
BigDecimal amount;
if (isPositiveBalance) {
senderId = registry.getCompanyId();
addresseeId = Sender.One.getId();
debitLegAccount = dtrnAcc;
creditLegAccount = counterAcc;
amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAstO = rgsMng.findRelatedAsset(
registry.getTradingClearingRegistryId(),
registry.getCompanyId(),
registry.getSecuritySymbol(),
AS_T
);
payerAstO.ifPresent(amt -> {
amt.setSettledDebit(safeBD(amt.getSettledDebit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now);
});
} else {
senderId = Sender.One.getId();
addresseeId = registry.getCompanyId();
debitLegAccount = counterAcc;
creditLegAccount = dtrnAcc;
amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAstO = rgsMng.findRelatedAsset(
registry.getTradingClearingRegistryId(),
registry.getCompanyId(),
registry.getSecuritySymbol(),
AS_T
);
payerAstO.ifPresent(amt -> {
amt.setSettledCredit(safeBD(amt.getSettledCredit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now);
});
}
PaymentInstructionBuilderV2 paymentInstructionBuilder = PaymentInstructionBuilderV2.builder(imdgProvider)
.registry(registry)
.sender(senderId)
.addressee(addresseeId)
.debitLegAccount(debitLegAccount)
.creditLegAccount(creditLegAccount)
.amount(amount)
.sessionId(sessionId)
.purpose(String.format("Перевод по итогу клиринга по ТКР %s", registry.getTradingClearingRegistry()));
PaymentInstruction paymentInstruction = paymentInstructionBuilder.build();
log.debug("Created paymentInstruction by registry.id: {}", registry.getId());
paymentInstructionImdg.insert(paymentInstruction);
registry.setPaymentId(paymentInstruction.getId());
registry.setUpdated(now);
registryImdg.update(registry);
paymentInstructions.add(paymentInstruction);
}
if (payload.getPaymentInstructionReturns().size() > 0) {
log.debug("PaymentInstructions return size {}, PaymentInstructions deals size {}. sending SDF03",
payload.getPaymentInstructionReturns().size(),
paymentInstructions.size());
paymentInstructions = Stream.concat(payload.getPaymentInstructionReturns().stream(),
paymentInstructions.stream()).toList();
}
sendSdfs(paymentInstructions);
StageResult<Collection<PaymentInstruction>> stageResult = new StageResult<>(null, true);
@ -364,4 +379,8 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
sDf12.setGenerationTime(Instant.now());
return sDf12;
}
public void setSection(Section section) {
this.section = section;
}
}

View file

@ -1,7 +1,13 @@
package ru.spcex.clearing.session.stage.task;
import ru.clearing.classes.statics.data.payment.PaymentInstruction;
import java.util.Collection;
import java.util.Collections;
public class FormingPaymentInstructionPayload {
private Long sessionId;
private Collection<PaymentInstruction> paymentInstructionReturns;
public Long getSessionId() {
return sessionId;
@ -10,4 +16,12 @@ public class FormingPaymentInstructionPayload {
public void setSessionId(Long sessionId) {
this.sessionId = sessionId;
}
public Collection<PaymentInstruction> getPaymentInstructionReturns() {
return paymentInstructionReturns != null ? paymentInstructionReturns : Collections.emptyList();
}
public void setPaymentInstructionReturns(Collection<PaymentInstruction> paymentInstructionReturns) {
this.paymentInstructionReturns = paymentInstructionReturns;
}
}