fix stage 5

This commit is contained in:
etreshenkov 2023-05-30 11:14:23 +03:00
parent 5daab77c0a
commit 79637d1bd9

View file

@ -20,10 +20,7 @@ import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
@ -74,9 +71,7 @@ public class InspectionObligations implements ISessionStage {
case InspectionObligations -> {
return inspectionObligations();
}
default -> {
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
}
default -> throw new IllegalStateException("Unknown task type: " + task.getTaskType());
}
}
@ -97,29 +92,27 @@ public class InspectionObligations implements ISessionStage {
obligationsInGroup.stream()
.map(Registry::getId).collect(Collectors.toList()),
entry.getKey());
boolean isFail = false;
Optional<Registry> firstUncoveredRegistry = Optional.empty();
boolean isUncovered = false;
List<CheckResult> checkResults = new ArrayList<>();
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) {
Registry asset = registryImdg.getSingleObjectBySQL(sqlAssetRegistryCondition);
if (asset == 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;
isUncovered = true;
} else if (obligation.getBalance().compareTo(asset.getBalance()) > 0) {
if (obligation.getBalance().compareTo(asset.getBalance()) > 0) {
log.warn("groupId: {}: {}", obligation.getGroupId(),
msgResolver.resolve(new EnumMessage(ClearingError.InsecurityObligation, obligation.getCompanyId())));
isUncovered = true;
}
}
checkResults.add(new CheckResult(obligation, isUncovered));
}
defineStatusAndUpdateRegistry(group, firstUncoveredRegistry, isFail);
defineStatusAndUpdateRegistry(checkResults, group);
}
return new StageResult(null, true);
}
@ -146,31 +139,25 @@ public class InspectionObligations implements ISessionStage {
return sqlCounterRegistryCondition;
}
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);
});
private void defineStatusAndUpdateRegistry(List<CheckResult> checkResults, List<Registry> registries) {
boolean isOneUncovered = checkResults.stream().anyMatch(checkResult -> checkResult.isUncovered);
if (isOneUncovered) {
for (CheckResult checkResult : checkResults){
Optional<Registry> tRegistryWithSameCompany = registries.stream().filter(registry ->
registry.getCompanyId().equals(checkResult.registry.getCompanyId()) &&
!registry.getRegistryDesignation().equals(checkResult.registry.getRegistryDesignation())
).findFirst();
if (checkResult.isUncovered){
updateRegistryStatus(checkResult.registry, RegistryStatus.UNCV);
tRegistryWithSameCompany.ifPresent(registry -> updateRegistryStatus(registry, RegistryStatus.FAIL));
} else {
updateRegistryStatus(checkResult.registry, RegistryStatus.FAIL);
tRegistryWithSameCompany.ifPresent(registry -> updateRegistryStatus(registry, RegistryStatus.FAIL));
}
}
} else {
registries.forEach(registry -> {
updateRegistryStatus(registry, RegistryStatus.OK);
});
registries.forEach(registry -> updateRegistryStatus(registry, RegistryStatus.OK));
}
}
private void updateRegistryStatus(Registry registry, RegistryStatus registryStatus) {
@ -179,4 +166,7 @@ public class InspectionObligations implements ISessionStage {
registry.setUpdated(Instant.now());
registryImdg.update(registry);
}
private record CheckResult(Registry registry, boolean isUncovered) {
}
}