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);
|
||||
Company company = companyImdg.getSingleObjectByFieldValues(Map.of("companyId", registry.getCompanyId()));
|
||||
Company company = companyImdg.getSingleObjectByID(registry.getCompanyId());
|
||||
if (company != null) {
|
||||
paymentInstruction.setPayeeBankName(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.spcex.clearing.error.ClearingError;
|
||||
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.StageResult;
|
||||
import ru.spcex.clearing.session.stage.Task;
|
||||
import ru.spcex.clearing.session.stage.task.InspectionPoolPayload;
|
||||
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.RegistryCodeSqlBuilder;
|
||||
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.SimpleMessageResolver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Service
|
||||
public class InspectionObligations 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<Registry> registryImdg;
|
||||
private KafkaSender kafkaSender;
|
||||
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
|
||||
public InspectionObligations(ImdgProvider imdgProvider) {
|
||||
this.imdgProvider = imdgProvider;
|
||||
this.idGenerator = imdgProvider.getImdgIdGenerator();
|
||||
this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StageResult submit(Task<?> task) {
|
||||
InspectionPoolPayload payload = (InspectionPoolPayload) task.getData();
|
||||
switch (task.getTaskType()) {
|
||||
case InspectionObligations -> {
|
||||
return inspectionPool(payload.getProcessedCompanyId());
|
||||
return inspectionObligations();
|
||||
}
|
||||
default -> {
|
||||
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
|
||||
|
|
@ -59,119 +80,103 @@ public class InspectionObligations implements ISessionStage {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
|
||||
|
||||
private StageResult inspectionObligations() {
|
||||
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());
|
||||
|
||||
Collection<Registry> obligations = registryImdg.getCollectionObjectsBySQL(sqlCondition);
|
||||
Map<Long, List<Registry>> registryByGroupIdAndCompanyId = obligations.stream().
|
||||
Collection<Registry> registriesToProcess = registryImdg.getCollectionObjectsBySQL(sqlCondition);
|
||||
Map<Long, List<Registry>> registriesByGroup = registriesToProcess.stream().
|
||||
collect(Collectors.groupingBy(Registry::getGroupId));
|
||||
|
||||
List<Long> insecurityGroups = new ArrayList<>();
|
||||
List<Long> alreadyUpdatedToUncover = new ArrayList<>();
|
||||
for (Registry currentRegistry : obligations) {
|
||||
if (insecurityGroups.contains(currentRegistry.getGroupId())) {
|
||||
log.debug("Skip insecurity groupId: {}", currentRegistry.getGroupId());
|
||||
continue;
|
||||
}
|
||||
|
||||
RegistryTradingParams counterRegistryTradingParams = null;
|
||||
if (IEnumKey.getEnumByKey(RegistryDesignation.class, currentRegistry.getRegistryDesignation()) == RegistryDesignation.O
|
||||
&& IEnumKey.getEnumByKey(RegistryInstrumentType.class, currentRegistry.getRegistryInstrumentType()) == RegistryInstrumentType.S) {
|
||||
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
||||
RegistryInstrumentType.S,
|
||||
null,
|
||||
RegistryUnit.T);
|
||||
} else if (IEnumKey.getEnumByKey(RegistryDesignation.class, currentRegistry.getRegistryDesignation()) == RegistryDesignation.O
|
||||
&& IEnumKey.getEnumByKey(RegistryInstrumentType.class, currentRegistry.getRegistryInstrumentType()) == RegistryInstrumentType.M) {
|
||||
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
||||
RegistryInstrumentType.M,
|
||||
null,
|
||||
RegistryUnit.F);
|
||||
}
|
||||
|
||||
if (counterRegistryTradingParams == null) {
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
}
|
||||
for (Map.Entry<Long, List<Registry>> entry : registriesByGroup.entrySet()) {
|
||||
List<Registry> group = entry.getValue();
|
||||
List<Registry> obligationsInGroup = group.stream().filter(registry ->
|
||||
IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.O).toList();
|
||||
log.debug("Find {} obligation with ids: {} in group: {}", obligationsInGroup.size(),
|
||||
obligationsInGroup.stream()
|
||||
.map(Registry::getId).collect(Collectors.toList()),
|
||||
entry.getKey());
|
||||
boolean isFail = false;
|
||||
Optional<Registry> firstUncoveredRegistry = Optional.empty();
|
||||
for (Registry obligation : obligationsInGroup) {
|
||||
String sqlAssetRegistryCondition = searchAssetsByObligationSql(obligation);
|
||||
log.debug("search asset by registry.id={} sql {}", sqlAssetRegistryCondition, obligation.getId());
|
||||
Registry counter = registryImdg.getSingleObjectBySQL(sqlAssetRegistryCondition);
|
||||
if (counter == null) {
|
||||
log.warn("Not found asset by registry.id={}", obligation.getId());
|
||||
isFail = true;
|
||||
firstUncoveredRegistry = Optional.of(obligation);
|
||||
break;
|
||||
}
|
||||
if (obligation.getBalance().compareTo(counter.getBalance()) > 0) {
|
||||
log.warn("groupId: {}: {}", obligation.getGroupId(),
|
||||
msgResolver.resolve(new EnumMessage(ClearingError.InsecurityObligation, obligation.getCompanyId())));
|
||||
isFail = true;
|
||||
firstUncoveredRegistry = Optional.of(obligation);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
defineStatusAndUpdateRegistry(group, firstUncoveredRegistry, isFail);
|
||||
}
|
||||
return new StageResult(null, true);
|
||||
}
|
||||
|
||||
private void defineUncoverOrFail(Registry registry) {
|
||||
if (IEnumKey.getEnumByKey(RegistryDesignation.class, registry.getRegistryDesignation()) == RegistryDesignation.O) {
|
||||
setUncover(registry);
|
||||
} else {
|
||||
setFail(registry);
|
||||
private String searchAssetsByObligationSql(Registry obligation) {
|
||||
RegistryTradingParams counterRegistryTradingParams = null;
|
||||
if (IEnumKey.getEnumByKey(RegistryInstrumentType.class, obligation.getRegistryInstrumentType()) == RegistryInstrumentType.S) {
|
||||
counterRegistryTradingParams = new RegistryTradingParams(RegistryDesignation.A,
|
||||
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) {
|
||||
registry.setRegistryStatus("UNCV");
|
||||
private void updateRegistryStatus(Registry registry, RegistryStatus registryStatus) {
|
||||
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"),
|
||||
NACK("NACK"),
|
||||
FAIL("FAIL"),
|
||||
UNCV("UNCV"),
|
||||
POOL("POOL")
|
||||
;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue