обработка sdf04 и изменения balance для регистров

This commit is contained in:
etreschenkov 2023-07-31 15:38:59 +03:00
parent a5bb906eea
commit 79b65e0023
5 changed files with 65 additions and 14 deletions

View file

@ -4,6 +4,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.clearing.classes.statics.data.account.Account; import ru.clearing.classes.statics.data.account.Account;
import ru.clearing.classes.statics.data.misc.Session;
import ru.clearing.classes.statics.data.registry.Registry; import ru.clearing.classes.statics.data.registry.Registry;
import ru.clearing.classes.statics.data.sdf.SDf04; import ru.clearing.classes.statics.data.sdf.SDf04;
import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.imdg.IMDGDistributedNames;
@ -11,10 +12,7 @@ import ru.spcex.clearing.platform.messaging.domain.cud.balance.StatementRequest;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender; import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.clearing.service.AnltSearcher; import ru.spcex.clearing.service.AnltSearcher;
import ru.spcex.clearing.service.model.Result; import ru.spcex.clearing.service.model.Result;
import ru.spcex.platform.enumeration.AccountType; import ru.spcex.platform.enumeration.*;
import ru.spcex.platform.enumeration.RegistryDesignation;
import ru.spcex.platform.enumeration.RegistryInstrumentType;
import ru.spcex.platform.enumeration.RegistryUnit;
import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.imdg.api.ImdgProvider;
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate; import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
@ -26,6 +24,7 @@ import java.math.BigDecimal;
import java.time.Instant; import java.time.Instant;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map;
@Service @Service
public class Sdf04Executor extends AbstractExecutor<SDf04> { public class Sdf04Executor extends AbstractExecutor<SDf04> {
@ -36,11 +35,13 @@ public class Sdf04Executor extends AbstractExecutor<SDf04> {
private final Imdg<Account> accountImdg; private final Imdg<Account> accountImdg;
private final AnltSearcher anltSearcher; private final AnltSearcher anltSearcher;
private final IMessageResolver msgRslv; private final IMessageResolver msgRslv;
protected final Imdg<Session> sessionImdg;
public Sdf04Executor(ImdgProvider imdgProvider, AnltSearcher anltSearcher, IMessageResolver msgRslv) { public Sdf04Executor(ImdgProvider imdgProvider, AnltSearcher anltSearcher, IMessageResolver msgRslv) {
this.imdgProvider = imdgProvider; this.imdgProvider = imdgProvider;
this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class); this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
this.accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); this.accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class);
this.sessionImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Session, Session.class);
this.anltSearcher = anltSearcher; this.anltSearcher = anltSearcher;
this.msgRslv = msgRslv; this.msgRslv = msgRslv;
} }
@ -69,11 +70,20 @@ public class Sdf04Executor extends AbstractExecutor<SDf04> {
Collection<Registry> registries; Collection<Registry> registries;
Account anltAcc = accountImdg.getFirstObjectBySQL("account = '%s' and accountType = '%s'" Account anltAcc = accountImdg.getFirstObjectBySQL("account = '%s' and accountType = '%s'"
.formatted(sdf04.getC_acc_deb(), AccountType.Anlt.getKey())); .formatted(sdf04.getC_acc_deb(), AccountType.Anlt.getKey()));
//если сдф04 не по аналитическому счету, выбираем по тому c_acc_deb что пришло // Account tranAcc = accountImdg.getFirstObjectBySQL("account = '%s' and accountType = '%s'"
// .formatted(sdf04.getC_acc_cred(), AccountType.Tran.getKey()));
// //если сдф04 не по аналитическому счету ни по tran счету, выбираем по тому c_acc_deb/c_acc_cred что пришло
// boolean isAnltAcc = anltAcc != null;
// boolean isTranAcc = tranAcc != null;
// if (isAnltAcc && isTranAcc) {
// log.warn("Found ANLT and TRAN acc by sdf04.id: {}", sdf04.getId());
// }
if (anltAcc == null) { if (anltAcc == null) {
registries = selectRegistryForSDF04(sdf04.getC_acc_deb()); registries = selectRegistryForSDF04(sdf04.getC_acc_deb());
// String acc = anltAcc == null ? sdf04.getC_acc_deb() : sdf04.getC_acc_cred();
// registries = selectRegistryForSDF04(acc);
} else { } else {
//в сдф04 указан ANLT счет, ищем по контракту //в сдф04 указан ANLT или TRAN счет, ищем по контракту
AnltSearcher.AnltSearch anltSearch = anltSearcher.loadByAnlt(sdf04.getSpecif_1()); AnltSearcher.AnltSearch anltSearch = anltSearcher.loadByAnlt(sdf04.getSpecif_1());
if (!anltSearch.isFound()) { if (!anltSearch.isFound()) {
log.error("sdf04.id={} search by specif1 failed {}", sdf04.getId(), msgRslv.resolve(anltSearch.getError())); log.error("sdf04.id={} search by specif1 failed {}", sdf04.getId(), msgRslv.resolve(anltSearch.getError()));
@ -84,6 +94,18 @@ public class Sdf04Executor extends AbstractExecutor<SDf04> {
} }
registries.forEach(registry -> unlockRegistry(registry, parseString(sdf04.getSum_deb()))); registries.forEach(registry -> unlockRegistry(registry, parseString(sdf04.getSum_deb())));
} }
//todo переписать логику поиска на аналогичную по счетам(TRAN)
Session activeSession = sessionImdg.getSingleObjectByFieldValues(Map.of("workflowStatus", Status.Active.getKey()));
if (activeSession != null) {
Collection<Registry> registriesByActiveSessionId = selectABRegistryBySessionId(activeSession.getId());
registriesByActiveSessionId.stream().filter(registry -> registry.getBalance().compareTo(BigDecimal.ZERO) < 0)
.forEach(registry -> {
registry.setBalance(BigDecimal.ZERO);
registry.setUpdated(Instant.now());
registryImdg.update(registry);
});
}
return result; return result;
} }
@ -103,6 +125,21 @@ public class Sdf04Executor extends AbstractExecutor<SDf04> {
return result; return result;
} }
protected Collection<Registry> selectABRegistryBySessionId(Long sessionId) {
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
ImdgPredicate query = pb.and(
pb.and(
pb.equals("registryDesignation", RegistryDesignation.A.getKey()),
pb.equals("registryUnit", RegistryUnit.B.getKey())
),
pb.equals("sessionId", sessionId)
);
Collection<Registry> result = registryImdg.getCollectionObjectsByPredicate(query);
log.trace("Selected {} registry's by sql: {}", result.size(), query);
return result;
}
boolean unlockRegistry(Registry registry, BigDecimal value) { boolean unlockRegistry(Registry registry, BigDecimal value) {
if (registry.getBalance() == null) registry.setBalance(BigDecimal.ZERO); if (registry.getBalance() == null) registry.setBalance(BigDecimal.ZERO);
registry.setBalance(registry.getBalance().subtract(value)); registry.setBalance(registry.getBalance().subtract(value));

View file

@ -146,6 +146,7 @@ public class SecondaryAuctionT0Session extends AbstractSession implements Initia
//stage 5 //stage 5
{ {
InspectionPoolPayload companyIdPayload = new InspectionPoolPayload(); InspectionPoolPayload companyIdPayload = new InspectionPoolPayload();
companyIdPayload.setSessionId(currSession.getId());
companyIdPayload.setProcessedCompanyId(currSession.getCompanyId()); companyIdPayload.setProcessedCompanyId(currSession.getCompanyId());
runStage(TaskType.InspectionObligations, companyIdPayload, inspectionObligations); runStage(TaskType.InspectionObligations, companyIdPayload, inspectionObligations);
} }

View file

@ -165,7 +165,7 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
amount = registry.getBalance() == null ? null : registry.getBalance().abs(); amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAmtO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AM_T); Optional<Registry> payerAmtO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AM_T);
payerAmtO.ifPresent(amt -> { payerAmtO.ifPresent(amt -> {
amt.setSettledDebit(safeBD(amt.getSettledDebit()).subtract(safeBD(registry.getBalance()))); amt.setSettledDebit(safeBD(amt.getSettledDebit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now); setUpdatedStoreInImdg(amt, now);
}); });
} else { } else {
@ -173,10 +173,10 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
addresseeId = registry.getCompanyId(); addresseeId = registry.getCompanyId();
debitLegAccount = counterAcc; debitLegAccount = counterAcc;
creditLegAccount = tranAcc; creditLegAccount = tranAcc;
amount = registry.getBalance() == null ? null : registry.getBalance().abs().negate(); amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAmtO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AM_T); Optional<Registry> payerAmtO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AM_T);
payerAmtO.ifPresent(amt -> { payerAmtO.ifPresent(amt -> {
amt.setSettledCredit(safeBD(amt.getSettledCredit()).add(safeBD(registry.getBalance()))); amt.setSettledCredit(safeBD(amt.getSettledCredit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now); setUpdatedStoreInImdg(amt, now);
}); });
} }
@ -225,7 +225,7 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
amount = registry.getBalance() == null ? null : registry.getBalance().abs(); amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAstO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AS_T); Optional<Registry> payerAstO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AS_T);
payerAstO.ifPresent(amt -> { payerAstO.ifPresent(amt -> {
amt.setSettledDebit(safeBD(amt.getSettledDebit()).subtract(safeBD(registry.getBalance()))); amt.setSettledDebit(safeBD(amt.getSettledDebit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now); setUpdatedStoreInImdg(amt, now);
}); });
} else { } else {
@ -233,10 +233,10 @@ public class FormingPaymentInstructionAssets implements ISessionStage {
addresseeId = registry.getCompanyId(); addresseeId = registry.getCompanyId();
debitLegAccount = counterAcc; debitLegAccount = counterAcc;
creditLegAccount = dtrnAcc; creditLegAccount = dtrnAcc;
amount = registry.getBalance() == null ? null : registry.getBalance().abs().negate(); amount = registry.getBalance() == null ? null : registry.getBalance().abs();
Optional<Registry> payerAstO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AS_T); Optional<Registry> payerAstO = rgsMng.findRelatedAsset(registry.getTradingClearingRegistryId(), registry.getCompanyId(), AS_T);
payerAstO.ifPresent(amt -> { payerAstO.ifPresent(amt -> {
amt.setSettledCredit(safeBD(amt.getSettledCredit()).add(safeBD(registry.getBalance()))); amt.setSettledCredit(safeBD(amt.getSettledCredit()).add(safeBD(registry.getBalance().abs())));
setUpdatedStoreInImdg(amt, now); setUpdatedStoreInImdg(amt, now);
}); });
} }

View file

@ -13,6 +13,7 @@ import ru.spcex.clearing.service.RegistryManager;
import ru.spcex.clearing.session.stage.ISessionStage; import ru.spcex.clearing.session.stage.ISessionStage;
import ru.spcex.clearing.session.stage.StageResult; import ru.spcex.clearing.session.stage.StageResult;
import ru.spcex.clearing.session.stage.Task; import ru.spcex.clearing.session.stage.Task;
import ru.spcex.clearing.session.stage.task.InspectionPoolPayload;
import ru.spcex.platform.enumeration.*; import ru.spcex.platform.enumeration.*;
import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.imdg.api.ImdgProvider;
@ -58,15 +59,16 @@ public class InspectionObligations implements ISessionStage {
@Override @Override
public StageResult submit(Task<?> task) { public StageResult submit(Task<?> task) {
InspectionPoolPayload payload = (InspectionPoolPayload) task.getData();
switch (task.getTaskType()) { switch (task.getTaskType()) {
case InspectionObligations -> { case InspectionObligations -> {
return inspectionObligations(); return inspectionObligations(payload.getSessionId());
} }
default -> throw new IllegalStateException("Unknown task type: " + task.getTaskType()); default -> throw new IllegalStateException("Unknown task type: " + task.getTaskType());
} }
} }
private StageResult inspectionObligations() { private StageResult inspectionObligations(Long sessionId) {
String sqlCondition = String.format("(%s) and registryStatus = '%s'", String sqlCondition = String.format("(%s) and registryStatus = '%s'",
RegistryCodeSqlBuilder.getInstance(OS_T, OM_T, TS_T, TM_T).build(), RegistryCodeSqlBuilder.getInstance(OS_T, OM_T, TS_T, TM_T).build(),
RegistryStatus.POOL.getKey()); RegistryStatus.POOL.getKey());
@ -124,6 +126,7 @@ public class InspectionObligations implements ISessionStage {
} else if (IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.T) { } else if (IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.T) {
blockedRegistry.setBalance(blockedRegistry.getBalance().subtract(registry.getBalance())); blockedRegistry.setBalance(blockedRegistry.getBalance().subtract(registry.getBalance()));
} }
blockedRegistry.setSessionId(sessionId);
blockedRegistry.setUpdated(now); blockedRegistry.setUpdated(now);
registryImdg.update(blockedRegistry); registryImdg.update(blockedRegistry);
} else if (section != null && section == Section.FOND && } else if (section != null && section == Section.FOND &&
@ -136,6 +139,7 @@ public class InspectionObligations implements ISessionStage {
} else if (IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.T) { } else if (IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.T) {
blockedRegistry.setBalance(blockedRegistry.getBalance().subtract(registry.getBalance())); blockedRegistry.setBalance(blockedRegistry.getBalance().subtract(registry.getBalance()));
} }
blockedRegistry.setSessionId(sessionId);
blockedRegistry.setUpdated(now); blockedRegistry.setUpdated(now);
registryImdg.update(blockedRegistry); registryImdg.update(blockedRegistry);
} }

View file

@ -2,6 +2,15 @@ package ru.spcex.clearing.session.stage.task;
public class InspectionPoolPayload { public class InspectionPoolPayload {
private Long processedCompanyId; private Long processedCompanyId;
private Long sessionId;
public Long getSessionId() {
return sessionId;
}
public void setSessionId(Long sessionId) {
this.sessionId = sessionId;
}
public Long getProcessedCompanyId() { public Long getProcessedCompanyId() {
return processedCompanyId; return processedCompanyId;