fix up stage 7; session BN

This commit is contained in:
etreschenkov 2023-05-27 14:47:25 +03:00
parent 786e4af3eb
commit 25b64c3c09
3 changed files with 152 additions and 8 deletions

View file

@ -8,6 +8,7 @@ import org.springframework.stereotype.Service;
import ru.clearing.classes.statics.data.execution.ExecutionCommon;
import ru.clearing.classes.statics.data.execution.ExecutionFond;
import ru.clearing.classes.statics.data.misc.Session;
import ru.clearing.classes.statics.data.payment.PaymentInstruction;
import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.BaseRequest;
import ru.spcex.clearing.session.stage.impl.*;
@ -22,6 +23,7 @@ import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;
@ -144,7 +146,11 @@ public class PrimaryAuctionBnSession extends AbstractSession implements Initiali
//stage 6
runStage(TaskType.FormingRegistersOnOS, formingRegistersOnOS); //returns Collection<Registry>
//stage 7
runStage(TaskType.FormingPaymentInstruction, formingPaymentInstruction);
StageResult<Collection<PaymentInstruction>> paymentResult = runStage(TaskType.FormingPaymentInstruction, formingPaymentInstruction);
if (paymentResult.getStageResult().isEmpty()) {
//todo где сверка
firstPart(req);
}
} catch (StageException e) {
//already logged
}

View file

@ -6,10 +6,18 @@ 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;
@ -23,6 +31,8 @@ import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
import java.math.BigDecimal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@ -35,6 +45,11 @@ public class FormingPaymentInstruction implements ISessionStage {
private ImdgId idGenerator;
private Imdg<Registry> registryImdg;
private Imdg<PaymentInstruction> paymentInstructionImdg;
private Imdg<Security> securityImdg;
private Imdg<Account> accountImdg;
private Imdg<Company> companyImdg;
private Imdg<SDf03> sDf03Imdg;
private Imdg<SDf12> sDf12Imdg;
private KafkaSender kafkaSender;
private final IMessageResolver msgResolver = new SimpleMessageResolver();
@ -43,6 +58,11 @@ public class FormingPaymentInstruction implements ISessionStage {
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);
}
@ -204,6 +224,7 @@ public class FormingPaymentInstruction implements ISessionStage {
}
}
List<PaymentInstruction> formedPaymentInstructions = new ArrayList<>();
for (Registry obligation : obligations) {
RegistryTradingParams registryTradingParamsLT = new RegistryTradingParams(RegistryDesignation.L,
null, null, RegistryUnit.T);
@ -214,22 +235,120 @@ public class FormingPaymentInstruction implements ISessionStage {
BigDecimal sumBalance = registries.stream().map(Registry::getBalance).reduce(BigDecimal.ZERO, BigDecimal::add);
PaymentInstruction paymentInstruction = createPaymentInstruction(obligation, sumBalance);
formedPaymentInstructions.add(paymentInstruction);
paymentInstructionImdg.insert(paymentInstruction);
obligation.setPaymentId(paymentInstruction.getId());
registryImdg.update(obligation);
}
sendSdfs(formedPaymentInstructions);
//todo send message to queue for forming sDf03 and sDf12?
StageResult<Collection<PaymentInstruction>> stageResult = new StageResult(null, true);
stageResult.setStageResult(formedPaymentInstructions);
return stageResult;
}
return new StageResult(null, true);
private void sendSdfs(List<PaymentInstruction> formedPaymentInstructions) {
Long sdf03GroupId = null;
Long sdf12GroupId = null;
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);
} else if (!InstrumentType.CRNC.equalsByKey(security.getInstrumentType()) &&
AccountType.Depo.equalsByKey(account.getAccountType())) {
sdf12GroupId = sdf12GroupId != null ? sdf12GroupId : imdgProvider.getImdgIdGenerator().nextId();
newSDf12(paymentInstruction, sdf12GroupId);
}
}
if (sdf03GroupId != null) {
ExportToFileRequest exportToFileRequest = new ExportToFileRequest();
exportToFileRequest.setNameOfTable("DF-03");
exportToFileRequest.setSdfGroupId(sdf03GroupId);
kafkaSender.sendRequestToQueue(Consts.EXPORT_PROCESS, exportToFileRequest);
}
if (sdf12GroupId != null) {
ExportToFileRequest exportToFileRequest = new ExportToFileRequest();
exportToFileRequest.setNameOfTable("DF-12");
exportToFileRequest.setSdfGroupId(sdf12GroupId);
kafkaSender.sendRequestToQueue(Consts.EXPORT_PROCESS, exportToFileRequest);
}
}
private PaymentInstruction createPaymentInstruction(Registry registry, BigDecimal balance) {
PaymentInstruction paymentInstruction = new PaymentInstruction();
paymentInstruction.setSenderId(registry.getCompanyId());
paymentInstruction.setAddresseeId(registry.getCounterPartyId());
//todo add builder for paymentInstruction
return paymentInstruction;
PaymentInstructionBuilder paymentInstructionBuilder = PaymentInstructionBuilder.builder(imdgProvider, registry);
return paymentInstructionBuilder.build();
}
private void newSDf03(PaymentInstruction paymentInstruction, Long sdf03GroupId) {
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.getDebitLeg_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.getCreditLeg_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();//
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());
}
private void newSDf12(PaymentInstruction paymentInstruction, Long sdf12GroupId) {
log.debug("creating sdf12");
SDf12 sDf12 = new SDf12();
sDf12.setId(idGenerator.nextId());
sDf12.setOutDocument(sDf12.getId().toString());
// sDf12.setQuantity();
sDf12.setGenerationTime(Instant.now());
sDf12.setGenerationId(sdf12GroupId);
sDf12Imdg.insert(sDf12);
}
// private String defineCompanyName(Function function, )
}

View file

@ -0,0 +1,19 @@
package ru.spcex.platform.enumeration;
import ru.spcex.platform.utils.enumeration.IEnumKey;
public enum InstrumentType implements IEnumKey {
CRNC ("CRNC")
;
private final String key;
InstrumentType(String key) {
this.key = key;
}
@Override
public String getKey() {
return key;
}
}