PaymentInstruction grouping for securities
This commit is contained in:
parent
494bafbe18
commit
0c00c7fdb3
2 changed files with 48 additions and 21 deletions
|
|
@ -154,7 +154,7 @@ public class FormingPaymentInstructionSecurities implements ISessionStage {
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.groupingBy(Registry::getGroupId));
|
.collect(Collectors.groupingBy(Registry::getGroupId));
|
||||||
log.debug("groups.size = {}", groups.size());
|
log.debug("groups.size = {}", groups.size());
|
||||||
Map<Long, PaymentGroup> lstGroups = RegistryLiabilitiesGroup.group(registries);
|
Map<Long, PaymentGroup> lstGroups = RegistryLiabilitiesGroup.group(groups);
|
||||||
for (Map.Entry<Long, List<Registry>> group : groups.entrySet()) {
|
for (Map.Entry<Long, List<Registry>> group : groups.entrySet()) {
|
||||||
Long groupId = group.getKey();
|
Long groupId = group.getKey();
|
||||||
Function<RegistryTradingParams, Registry> findByCode = rgsCode -> group.getValue()
|
Function<RegistryTradingParams, Registry> findByCode = rgsCode -> group.getValue()
|
||||||
|
|
|
||||||
|
|
@ -4,44 +4,56 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import ru.clearing.classes.statics.data.registry.Registry;
|
import ru.clearing.classes.statics.data.registry.Registry;
|
||||||
import ru.spcex.clearing.service.registry.RegistryManager;
|
import ru.spcex.clearing.service.registry.RegistryManager;
|
||||||
|
import ru.spcex.platform.enumeration.RegistryTradingParams;
|
||||||
|
import ru.spcex.platform.utils.collection.Pair;
|
||||||
|
import ru.spcex.platform.utils.text.TextUtil;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import static ru.spcex.platform.enumeration.RegistryTradingParams.CS_T;
|
||||||
import static ru.spcex.platform.enumeration.RegistryTradingParams.LS_T;
|
import static ru.spcex.platform.enumeration.RegistryTradingParams.LS_T;
|
||||||
|
|
||||||
public class RegistryLiabilitiesGroup {
|
public class RegistryLiabilitiesGroup {
|
||||||
private Logger log = LoggerFactory.getLogger(getClass());
|
private static final Logger log = LoggerFactory.getLogger(RegistryLiabilitiesGroup.class);
|
||||||
|
|
||||||
public static RegistryLiabilitiesGroup builder() {
|
public static Map<Long, PaymentGroup> group(Map<Long, List<Registry>> registriesCollection) {
|
||||||
return new RegistryLiabilitiesGroup();
|
|
||||||
}
|
|
||||||
|
|
||||||
private RegistryLiabilitiesGroup() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Map<Long, PaymentGroup> group(Collection<Registry> registriesCollection) {
|
|
||||||
Map<Long, PaymentGroup> result = new HashMap<>();
|
Map<Long, PaymentGroup> result = new HashMap<>();
|
||||||
List<Registry> registries = registriesCollection
|
List<Pair<Registry, Registry>> lstCstGroup = registriesCollection.entrySet()
|
||||||
.stream()
|
.stream()
|
||||||
.filter(rgs -> RegistryManager.equalsByCode(LS_T, rgs))
|
.map(entry -> {
|
||||||
|
Long groupId = entry.getKey();
|
||||||
|
List<Registry> value = entry.getValue();
|
||||||
|
Registry ls_t = obtain(value, LS_T);
|
||||||
|
Registry cs_t = obtain(value, CS_T);
|
||||||
|
if (ls_t == null || cs_t == null) {
|
||||||
|
log.error("groupId {} registries {} failed to find LS_T/CS_T", groupId, value.stream()
|
||||||
|
.map(Registry::getId)
|
||||||
|
.map(Object::toString)
|
||||||
|
.collect(TextUtil.join));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return new Pair<>(ls_t, cs_t);
|
||||||
|
})
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
for (int i = 0; i < registries.size(); i++) {
|
for (int i = 0; i < lstCstGroup.size(); i++) {
|
||||||
Registry ls_t = registries.get(i);
|
var pair = lstCstGroup.get(i);
|
||||||
if (ls_t == null) {
|
if (pair == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Registry ls_t = pair.getFirst();
|
||||||
LinkedList<Registry> relatedLiabilities = new LinkedList<>();
|
LinkedList<Registry> relatedLiabilities = new LinkedList<>();
|
||||||
relatedLiabilities.add(ls_t);
|
relatedLiabilities.add(ls_t);
|
||||||
for (int j = i + 1; j < registries.size(); j++) {
|
for (int j = i + 1; j < lstCstGroup.size(); j++) {
|
||||||
Registry related = registries.get(j);
|
var anotherGroup = lstCstGroup.get(j);
|
||||||
if (related == null) {
|
if (anotherGroup == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (rgsOfTheSameAgents(ls_t, related)) {
|
Registry related = anotherGroup.getFirst();
|
||||||
|
if (rgsOfTheSameAgents(pair.getFirst(), pair.getSecond(), anotherGroup.getFirst(), anotherGroup.getSecond())) {
|
||||||
relatedLiabilities.add(related);
|
relatedLiabilities.add(related);
|
||||||
registries.set(j, null);
|
lstCstGroup.set(j, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PaymentGroup paymentGroup = new PaymentGroup();
|
PaymentGroup paymentGroup = new PaymentGroup();
|
||||||
|
|
@ -57,10 +69,25 @@ public class RegistryLiabilitiesGroup {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean rgsOfTheSameAgents(Registry ls_t1, Registry ls_t2) {
|
private static boolean rgsOfTheSameAgents(Registry ls_t1, Registry cs_t1, Registry ls_t2, Registry cs_t2) {
|
||||||
return Objects.equals(ls_t1.getSecurityId(), ls_t2.getSecurityId())
|
return Objects.equals(ls_t1.getSecurityId(), ls_t2.getSecurityId())
|
||||||
&& Objects.equals(ls_t1.getTradingClearingRegistryId(), ls_t2.getTradingClearingRegistryId())
|
&& Objects.equals(ls_t1.getTradingClearingRegistryId(), ls_t2.getTradingClearingRegistryId())
|
||||||
|
&& Objects.equals(cs_t1.getTradingClearingRegistryId(), cs_t2.getTradingClearingRegistryId())
|
||||||
&& Objects.equals(ls_t1.getCompanyId(), ls_t2.getCompanyId())
|
&& Objects.equals(ls_t1.getCompanyId(), ls_t2.getCompanyId())
|
||||||
&& Objects.equals(ls_t1.getCounterPartyId(), ls_t2.getCounterPartyId());
|
&& Objects.equals(ls_t1.getCounterPartyId(), ls_t2.getCounterPartyId());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Registry obtain(Collection<Registry> group, RegistryTradingParams cde) {
|
||||||
|
List<Registry> allRegistries = group
|
||||||
|
.stream()
|
||||||
|
.filter(rgs -> RegistryManager.equalsByCode(cde, rgs))
|
||||||
|
.toList();
|
||||||
|
if (allRegistries.size() > 1) {
|
||||||
|
throw new IllegalStateException("more than one " + cde + " entry in group " + group.iterator().next().getGroupId());
|
||||||
|
}
|
||||||
|
if (allRegistries.size() == 1) {
|
||||||
|
return allRegistries.iterator().next();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue