This commit is contained in:
parent
be01162125
commit
c288a5669f
3 changed files with 115 additions and 109 deletions
|
|
@ -61,7 +61,7 @@ public class PaymentInstructionBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
Imdg<Company> companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
Imdg<Company> companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class);
|
||||||
Company company = companyImdg.getSingleObjectByFieldValues(Map.of("companyId", registry.getCompanyId()));
|
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
|
||||||
if (company != null) {
|
if (company != null) {
|
||||||
paymentInstruction.setPayeeBankName(company.getShortName());
|
paymentInstruction.setPayeeBankName(company.getShortName());
|
||||||
paymentInstruction.setAddresseeBankName(company.getShortName());
|
paymentInstruction.setAddresseeBankName(company.getShortName());
|
||||||
|
|
|
||||||
|
|
@ -7,14 +7,11 @@ import org.springframework.stereotype.Service;
|
||||||
import ru.clearing.classes.statics.data.registry.Registry;
|
import ru.clearing.classes.statics.data.registry.Registry;
|
||||||
import ru.spcex.clearing.error.ClearingError;
|
import ru.spcex.clearing.error.ClearingError;
|
||||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
|
|
||||||
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.ImdgId;
|
|
||||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||||
import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
|
import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
|
||||||
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
import ru.spcex.platform.utils.enumeration.EnumMessage;
|
||||||
|
|
@ -22,36 +19,60 @@ import ru.spcex.platform.utils.enumeration.IEnumKey;
|
||||||
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
import ru.spcex.platform.utils.enumeration.IMessageResolver;
|
||||||
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
|
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.time.Instant;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class InspectionObligations implements ISessionStage {
|
public class InspectionObligations implements ISessionStage {
|
||||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
//todo remove (set all in single method setImdg(provider -> setImdg1();setIdGenerator();...)
|
|
||||||
private ImdgProvider imdgProvider;
|
private ImdgProvider imdgProvider;
|
||||||
private ImdgId idGenerator;
|
|
||||||
private Imdg<Registry> registryImdg;
|
private Imdg<Registry> registryImdg;
|
||||||
private KafkaSender kafkaSender;
|
|
||||||
private final IMessageResolver msgResolver = new SimpleMessageResolver();
|
private final IMessageResolver msgResolver = new SimpleMessageResolver();
|
||||||
|
|
||||||
|
private final static RegistryTradingParams OS_T;
|
||||||
|
private final static RegistryTradingParams OM_T;
|
||||||
|
private final static RegistryTradingParams TS_T;
|
||||||
|
private final static RegistryTradingParams TM_T;
|
||||||
|
|
||||||
|
static {
|
||||||
|
OS_T = new RegistryTradingParams(RegistryDesignation.O,
|
||||||
|
RegistryInstrumentType.S,
|
||||||
|
null,
|
||||||
|
RegistryUnit.T);
|
||||||
|
|
||||||
|
OM_T = new RegistryTradingParams(RegistryDesignation.O,
|
||||||
|
RegistryInstrumentType.M,
|
||||||
|
null,
|
||||||
|
RegistryUnit.T);
|
||||||
|
|
||||||
|
TS_T = new RegistryTradingParams(RegistryDesignation.T,
|
||||||
|
RegistryInstrumentType.S,
|
||||||
|
null,
|
||||||
|
RegistryUnit.T);
|
||||||
|
|
||||||
|
TM_T = new RegistryTradingParams(RegistryDesignation.T,
|
||||||
|
RegistryInstrumentType.M,
|
||||||
|
null,
|
||||||
|
RegistryUnit.T);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public InspectionObligations(ImdgProvider imdgProvider) {
|
public InspectionObligations(ImdgProvider imdgProvider) {
|
||||||
this.imdgProvider = imdgProvider;
|
this.imdgProvider = imdgProvider;
|
||||||
this.idGenerator = imdgProvider.getImdgIdGenerator();
|
|
||||||
this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
|
this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@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 inspectionPool(payload.getProcessedCompanyId());
|
return inspectionObligations();
|
||||||
}
|
}
|
||||||
default -> {
|
default -> {
|
||||||
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
|
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
|
||||||
|
|
@ -59,119 +80,103 @@ public class InspectionObligations implements ISessionStage {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private StageResult inspectionObligations() {
|
||||||
private StageResult inspectionPool(Long processedCompanyId) {
|
|
||||||
RegistryTradingParams registryTradingParamsOST = new RegistryTradingParams(RegistryDesignation.O,
|
|
||||||
RegistryInstrumentType.S,
|
|
||||||
null,
|
|
||||||
RegistryUnit.T);
|
|
||||||
|
|
||||||
RegistryTradingParams registryTradingParamsOMT = new RegistryTradingParams(RegistryDesignation.O,
|
|
||||||
RegistryInstrumentType.M,
|
|
||||||
null,
|
|
||||||
RegistryUnit.T);
|
|
||||||
|
|
||||||
RegistryTradingParams registryTradingParamsTST = new RegistryTradingParams(RegistryDesignation.T,
|
|
||||||
RegistryInstrumentType.S,
|
|
||||||
null,
|
|
||||||
RegistryUnit.T);
|
|
||||||
|
|
||||||
RegistryTradingParams registryTradingParamsTMT = new RegistryTradingParams(RegistryDesignation.T,
|
|
||||||
RegistryInstrumentType.M,
|
|
||||||
null,
|
|
||||||
RegistryUnit.T);
|
|
||||||
|
|
||||||
|
|
||||||
String sqlCondition = String.format("%s and registryStatus = '%s'",
|
String sqlCondition = String.format("%s and registryStatus = '%s'",
|
||||||
RegistryCodeSqlBuilder.getInstance(registryTradingParamsOST, registryTradingParamsTMT, registryTradingParamsOMT, registryTradingParamsTST).build(),
|
RegistryCodeSqlBuilder.getInstance(OS_T, OM_T, TS_T, TM_T).build(),
|
||||||
RegistryStatus.POOL.getKey());
|
RegistryStatus.POOL.getKey());
|
||||||
|
|
||||||
Collection<Registry> obligations = registryImdg.getCollectionObjectsBySQL(sqlCondition);
|
Collection<Registry> registriesToProcess = registryImdg.getCollectionObjectsBySQL(sqlCondition);
|
||||||
Map<Long, List<Registry>> registryByGroupIdAndCompanyId = obligations.stream().
|
Map<Long, List<Registry>> registriesByGroup = registriesToProcess.stream().
|
||||||
collect(Collectors.groupingBy(Registry::getGroupId));
|
collect(Collectors.groupingBy(Registry::getGroupId));
|
||||||
|
|
||||||
List<Long> insecurityGroups = new ArrayList<>();
|
for (Map.Entry<Long, List<Registry>> entry : registriesByGroup.entrySet()) {
|
||||||
List<Long> alreadyUpdatedToUncover = new ArrayList<>();
|
List<Registry> group = entry.getValue();
|
||||||
for (Registry currentRegistry : obligations) {
|
List<Registry> obligationsInGroup = group.stream().filter(registry ->
|
||||||
if (insecurityGroups.contains(currentRegistry.getGroupId())) {
|
IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.O).toList();
|
||||||
log.debug("Skip insecurity groupId: {}", currentRegistry.getGroupId());
|
log.debug("Find {} obligation with ids: {} in group: {}", obligationsInGroup.size(),
|
||||||
continue;
|
obligationsInGroup.stream()
|
||||||
}
|
.map(Registry::getId).collect(Collectors.toList()),
|
||||||
|
entry.getKey());
|
||||||
RegistryTradingParams counterRegistryTradingParams = null;
|
boolean isFail = false;
|
||||||
if (IEnumKey.getEnumByKey(RegistryDesignation.class, currentRegistry.getRegistryDesignation()) == RegistryDesignation.O
|
Optional<Registry> firstUncoveredRegistry = Optional.empty();
|
||||||
&& IEnumKey.getEnumByKey(RegistryInstrumentType.class, currentRegistry.getRegistryInstrumentType()) == RegistryInstrumentType.S) {
|
for (Registry obligation : obligationsInGroup) {
|
||||||
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
String sqlAssetRegistryCondition = searchAssetsByObligationSql(obligation);
|
||||||
RegistryInstrumentType.S,
|
log.debug("search asset by registry.id={} sql {}", sqlAssetRegistryCondition, obligation.getId());
|
||||||
null,
|
Registry counter = registryImdg.getSingleObjectBySQL(sqlAssetRegistryCondition);
|
||||||
RegistryUnit.T);
|
if (counter == null) {
|
||||||
} else if (IEnumKey.getEnumByKey(RegistryDesignation.class, currentRegistry.getRegistryDesignation()) == RegistryDesignation.O
|
log.warn("Not found asset by registry.id={}", obligation.getId());
|
||||||
&& IEnumKey.getEnumByKey(RegistryInstrumentType.class, currentRegistry.getRegistryInstrumentType()) == RegistryInstrumentType.M) {
|
isFail = true;
|
||||||
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
firstUncoveredRegistry = Optional.of(obligation);
|
||||||
RegistryInstrumentType.M,
|
break;
|
||||||
null,
|
}
|
||||||
RegistryUnit.F);
|
if (obligation.getBalance().compareTo(counter.getBalance()) > 0) {
|
||||||
}
|
log.warn("groupId: {}: {}", obligation.getGroupId(),
|
||||||
|
msgResolver.resolve(new EnumMessage(ClearingError.InsecurityObligation, obligation.getCompanyId())));
|
||||||
if (counterRegistryTradingParams == null) {
|
isFail = true;
|
||||||
continue;
|
firstUncoveredRegistry = Optional.of(obligation);
|
||||||
}
|
break;
|
||||||
String sqlCounterRegistryCondition = String.format("%s and " +
|
|
||||||
"tradingClearingRegistryId = '%s' and " +
|
|
||||||
"companyId = '%s'",
|
|
||||||
RegistryCodeSqlBuilder.getInstance(counterRegistryTradingParams).build(),
|
|
||||||
currentRegistry.getTradingClearingRegistryId(),
|
|
||||||
currentRegistry.getCompanyId());
|
|
||||||
Registry counterRegistry = registryImdg.getSingleObjectBySQL(sqlCounterRegistryCondition);
|
|
||||||
if (counterRegistry == null) {
|
|
||||||
log.warn("Not found registry by params: {}; set not uncovered", sqlCounterRegistryCondition);
|
|
||||||
setUncover(currentRegistry);
|
|
||||||
alreadyUpdatedToUncover.add(currentRegistry.getId());
|
|
||||||
insecurityGroups.add(currentRegistry.getGroupId());
|
|
||||||
registryImdg.update(currentRegistry);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (currentRegistry.getBalance().compareTo(counterRegistry.getBalance()) > 0) {
|
|
||||||
log.warn("groupId: {}: {}", currentRegistry.getGroupId(),
|
|
||||||
msgResolver.resolve(new EnumMessage(ClearingError.InsecurityObligation, currentRegistry.getCompanyId())));
|
|
||||||
setUncover(currentRegistry);
|
|
||||||
alreadyUpdatedToUncover.add(currentRegistry.getId());
|
|
||||||
insecurityGroups.add(currentRegistry.getGroupId());
|
|
||||||
registryImdg.update(currentRegistry);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
currentRegistry.setRegistryStatus("OK");
|
|
||||||
registryImdg.update(currentRegistry);
|
|
||||||
}
|
|
||||||
//todo установить ok для
|
|
||||||
|
|
||||||
if (!insecurityGroups.isEmpty()) {
|
|
||||||
for (Long insecuritiesGroupId : insecurityGroups) {
|
|
||||||
for (Registry registry : registryByGroupIdAndCompanyId.get(insecuritiesGroupId)) {
|
|
||||||
if (!alreadyUpdatedToUncover.contains(registry.getId())) {
|
|
||||||
log.warn("set fail for registry.id: {}", registry.getId());
|
|
||||||
defineUncoverOrFail(registry);
|
|
||||||
registryImdg.update(registry);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defineStatusAndUpdateRegistry(group, firstUncoveredRegistry, isFail);
|
||||||
}
|
}
|
||||||
return new StageResult(null, true);
|
return new StageResult(null, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void defineUncoverOrFail(Registry registry) {
|
private String searchAssetsByObligationSql(Registry obligation) {
|
||||||
if (IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.O) {
|
RegistryTradingParams counterRegistryTradingParams = null;
|
||||||
setUncover(registry);
|
if (IEnumKey.getEnumByKey(RegistryInstrumentType.class, obligation.getRegistryInstrumentType()) == RegistryInstrumentType.S) {
|
||||||
} else {
|
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
||||||
setFail(registry);
|
RegistryInstrumentType.S,
|
||||||
|
null,
|
||||||
|
RegistryUnit.T);
|
||||||
|
} else if (IEnumKey.getEnumByKey(RegistryInstrumentType.class, obligation.getRegistryInstrumentType()) == RegistryInstrumentType.M) {
|
||||||
|
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
||||||
|
RegistryInstrumentType.M,
|
||||||
|
null,
|
||||||
|
RegistryUnit.F);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String sqlCounterRegistryCondition = String.format("%s and " +
|
||||||
|
"tradingClearingRegistryId = '%s' and " +
|
||||||
|
"companyId = '%s'",
|
||||||
|
RegistryCodeSqlBuilder.getInstance(counterRegistryTradingParams).build(),
|
||||||
|
obligation.getTradingClearingRegistryId(),
|
||||||
|
obligation.getCompanyId());
|
||||||
|
return sqlCounterRegistryCondition;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setFail(Registry registry) {
|
|
||||||
registry.setRegistryStatus("FAIL");
|
private void defineStatusAndUpdateRegistry(List<Registry> registries, Optional<Registry> firstUncoveredRegistry, boolean isFail) {
|
||||||
|
if (isFail) {
|
||||||
|
Registry uncoveredRegistry = firstUncoveredRegistry.get();
|
||||||
|
updateRegistryStatus(uncoveredRegistry, RegistryStatus.UNCV);
|
||||||
|
|
||||||
|
Optional<Registry> sameRegistryWithOtherCompany = registries.stream().filter(registry ->
|
||||||
|
!registry.getCompanyId().equals(uncoveredRegistry.getCompanyId()) &&
|
||||||
|
registry.getRegistryDesignation().equals(uncoveredRegistry.getRegistryDesignation())
|
||||||
|
).findFirst();
|
||||||
|
sameRegistryWithOtherCompany.ifPresent(registry -> {
|
||||||
|
updateRegistryStatus(registry, RegistryStatus.UNCV);
|
||||||
|
});
|
||||||
|
|
||||||
|
Collection<Registry> otherRegistry = registries.stream().filter(registry ->
|
||||||
|
!registry.getRegistryDesignation().equals(uncoveredRegistry.getRegistryDesignation())).toList();
|
||||||
|
otherRegistry.forEach(registry -> {
|
||||||
|
updateRegistryStatus(registry, RegistryStatus.FAIL);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
registries.forEach(registry -> {
|
||||||
|
updateRegistryStatus(registry, RegistryStatus.OK);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUncover(Registry registry) {
|
private void updateRegistryStatus(Registry registry, RegistryStatus registryStatus) {
|
||||||
registry.setRegistryStatus("UNCV");
|
log.debug("Update registry.id: {} to {}", registry.getId(), registryStatus.getKey());
|
||||||
|
registry.setRegistryStatus(registryStatus.getKey());
|
||||||
|
registry.setUpdated(Instant.now());
|
||||||
|
registryImdg.update(registry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ public enum RegistryStatus implements IEnumKey {
|
||||||
PROC("PROC"),
|
PROC("PROC"),
|
||||||
NACK("NACK"),
|
NACK("NACK"),
|
||||||
FAIL("FAIL"),
|
FAIL("FAIL"),
|
||||||
|
UNCV("UNCV"),
|
||||||
POOL("POOL")
|
POOL("POOL")
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue