This commit is contained in:
parent
6c23cbca36
commit
a7e2929cdb
10 changed files with 223 additions and 1 deletions
|
|
@ -7,6 +7,7 @@ public enum ClearingErrorInternal implements IErrorEnumId {
|
|||
AccountNotActive(5415L),
|
||||
CompanyNotActive(5411L),
|
||||
FinancialObligationNotSatisfied(4L),
|
||||
SessionGeneralError(2L);
|
||||
;
|
||||
private final Long id;
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
package ru.spcex.clearing.session.stage;
|
||||
|
||||
public interface ISessionStage {
|
||||
StageResult submit(Task<?> task);
|
||||
// StageStatus getInitStatus(); пока не нужно
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package ru.spcex.clearing.session.stage;
|
||||
|
||||
public enum SessionStage {
|
||||
BalanceRevise,
|
||||
DealsPrepare,
|
||||
ObligationCalculate,
|
||||
ObligationAdmittance,
|
||||
//...
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package ru.spcex.clearing.session.stage;
|
||||
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
|
||||
public class StageResult {
|
||||
EnumMessage error;
|
||||
boolean success;
|
||||
|
||||
public StageResult(EnumMessage error, boolean success) {
|
||||
this.error = error;
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public EnumMessage getError() {
|
||||
return error;
|
||||
}
|
||||
|
||||
public void setError(EnumMessage error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
package ru.spcex.clearing.session.stage;
|
||||
|
||||
public enum StageStatus {
|
||||
IDLE, PROCESSING
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
package ru.spcex.clearing.session.stage;
|
||||
|
||||
public class Task<T> {
|
||||
private TaskType taskType;
|
||||
private T data;
|
||||
|
||||
public Task(TaskType taskType, T data) {
|
||||
this.taskType = taskType;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public TaskType getTaskType() {
|
||||
return taskType;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
package ru.spcex.clearing.session.stage;
|
||||
|
||||
public enum TaskType {
|
||||
StartRevise,
|
||||
ContinueRevise
|
||||
}
|
||||
|
|
@ -0,0 +1,145 @@
|
|||
package ru.spcex.clearing.session.stage.impl;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.clearing.classes.statics.data.misc.Currency;
|
||||
import ru.clearing.classes.statics.data.registry.Registry;
|
||||
import ru.clearing.classes.statics.data.statement.Statement;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.platform.messaging.domain.Consts;
|
||||
import ru.spcex.clearing.platform.messaging.domain.cud.clearing.Sdf56Request;
|
||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
||||
import ru.spcex.clearing.session.stage.ISessionStage;
|
||||
import ru.spcex.clearing.session.stage.StageResult;
|
||||
import ru.spcex.clearing.session.stage.Task;
|
||||
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.specific.StatementRevisePredicate;
|
||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||
import ru.spcex.platform.utils.enumeration.IEnumKey;
|
||||
import ru.spcex.platform.utils.time.TimeUtil;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.util.Collection;
|
||||
import java.util.function.UnaryOperator;
|
||||
|
||||
import static ru.spcex.clearing.error.ClearingErrorInternal.SessionGeneralError;
|
||||
|
||||
@Service
|
||||
public class BalanceRevise implements ISessionStage {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
//todo remove (set all in single method setImdg(provider -> setImdg1();setIdGenerator();...)
|
||||
private ImdgProvider imdgProvider;
|
||||
private ImdgId idGenerator;
|
||||
private Imdg<Statement> statementImdg;
|
||||
private Imdg<Registry> registryImdg;
|
||||
private Imdg<Currency> currencyImdg;
|
||||
private KafkaSender kafkaSender;
|
||||
|
||||
@Autowired
|
||||
public BalanceRevise(ImdgProvider imdgProvider) {
|
||||
this.imdgProvider = imdgProvider;
|
||||
this.idGenerator = imdgProvider.getImdgIdGenerator();
|
||||
this.statementImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Statement, Statement.class);
|
||||
this.currencyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Currency, Currency.class);
|
||||
this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StageResult submit(Task<?> task) {
|
||||
switch (task.getTaskType()) {
|
||||
case StartRevise -> {
|
||||
return sendSdfs();
|
||||
}
|
||||
case ContinueRevise -> {
|
||||
return revise(); //((SdfClearingRequest) task.getData()).getGroupId() if needed
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private StageResult sendSdfs() {
|
||||
Collection<Long> currencies = currencyImdg.projectSingleAttribute("id");
|
||||
Statement statement = statementImdg.aggregateByMax("created", StatementRevisePredicate.get(statementImdg, currencies));
|
||||
|
||||
Sdf56Request sdf56Request = new Sdf56Request();
|
||||
sdf56Request.setNumber(idGenerator.nextId()); //fixme day scope id generator
|
||||
sdf56Request.setsDateTime(statement != null ?
|
||||
statement.getCreated() : TimeUtil.localDateToInstant(LocalDate.of(2023, Month.JANUARY, 1))); //fixme default sDate
|
||||
sdf56Request.seteDateTime(Instant.now());
|
||||
Long msgKey = kafkaSender.sendRequestToQueue(Consts.SDF56_PROCESS, sdf56Request);
|
||||
if (msgKey == null) {
|
||||
log.error("failed to put SDF56 request to kafka queue");
|
||||
return new StageResult(new EnumMessage(SessionGeneralError), false);
|
||||
}
|
||||
return new StageResult(null, true);
|
||||
}
|
||||
|
||||
private StageResult revise() {
|
||||
Collection<Statement> stmts;
|
||||
String statementSQL = String.format("inOutSDfType = %s and operationStatus = %s and statementType = %s",
|
||||
InOutSDfType.type57, OperationStatus.Pending.getKey(), StatementType.incr.getKey());
|
||||
stmts = statementImdg.getCollectionObjectsBySQL(statementSQL);
|
||||
|
||||
for (Statement stmt : stmts) {
|
||||
String unformatted = "registry_designation = '%s' " +
|
||||
"and registry_instrument_type = '%s' " +
|
||||
"and registry_unit = '%s' " +
|
||||
"and account = '%s' " +
|
||||
"and securityId = %d";
|
||||
String registrySqlAMT = String.format(unformatted,
|
||||
RegistryDesignation.A.getKey(),
|
||||
RegistryInstrumentType.M.getKey(),
|
||||
RegistryUnit.T.getKey(),
|
||||
stmt.getAccount(),
|
||||
stmt.getSecurityId()
|
||||
);
|
||||
String registrySqlAMF = String.format(unformatted,
|
||||
RegistryDesignation.A.getKey(),
|
||||
RegistryInstrumentType.M.getKey(),
|
||||
RegistryUnit.F.getKey(),
|
||||
stmt.getAccount(),
|
||||
stmt.getSecurityId()
|
||||
);
|
||||
String registrySqlAMB = String.format(unformatted,
|
||||
RegistryDesignation.A.getKey(),
|
||||
RegistryInstrumentType.M.getKey(),
|
||||
RegistryUnit.B.getKey(),
|
||||
stmt.getAccount(),
|
||||
stmt.getSecurityId()
|
||||
);
|
||||
Registry rgsAMT = registryImdg.getSingleObjectBySQL(registrySqlAMT);
|
||||
Registry rgsAMF = registryImdg.getSingleObjectBySQL(registrySqlAMF);
|
||||
Registry rgsAMB = registryImdg.getSingleObjectBySQL(registrySqlAMB);
|
||||
InOutDirection direction = IEnumKey.getEnumByKey(InOutDirection.class, stmt.getInOutDirection());
|
||||
UnaryOperator<BigDecimal> safe = (BigDecimal value) -> value != null ? value : BigDecimal.ZERO;
|
||||
if (direction == null) throw new IllegalStateException("null direction");
|
||||
switch (direction) {
|
||||
case out -> {
|
||||
rgsAMT.setBalance(safe.apply(rgsAMT.getBalance()).subtract(safe.apply(stmt.getAmount())));
|
||||
rgsAMT.setDebit(safe.apply(rgsAMT.getDebit()).add(safe.apply(stmt.getAmount())));
|
||||
}
|
||||
case in -> {
|
||||
rgsAMT.setBalance(safe.apply(rgsAMT.getBalance()).add(safe.apply(stmt.getAmount())));
|
||||
rgsAMT.setCredit(safe.apply(rgsAMT.getCredit()).add(safe.apply(stmt.getAmount())));
|
||||
}
|
||||
default -> {throw new IllegalStateException("null direction");}
|
||||
}
|
||||
rgsAMF.setBalance(safe.apply(rgsAMT.getBalance()).subtract(safe.apply(rgsAMB.getBalance())));
|
||||
registryImdg.update(rgsAMT);
|
||||
registryImdg.update(rgsAMF);
|
||||
registryImdg.update(rgsAMB);
|
||||
//todo create if needed?
|
||||
}
|
||||
//todoSdf1
|
||||
return new StageResult(null, true);
|
||||
}
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ package ru.spcex.platform.enumeration;
|
|||
import ru.spcex.platform.utils.enumeration.IEnumKey;
|
||||
|
||||
public enum InOutSDfType implements IEnumKey {
|
||||
type1("0102"), type16("1617"), type9("0910");
|
||||
type1("0102"), type16("1617"), type9("0910"), type57("57");
|
||||
|
||||
private final String key;
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ public enum RegistryUnit implements IEnumKey {
|
|||
T("T"),
|
||||
A("A"),
|
||||
R("R"),
|
||||
F("F"),
|
||||
B("B"),
|
||||
;
|
||||
|
||||
private final String key;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue