http://jira.mfd.msk:8088/browse/CLS-358 for review
This commit is contained in:
parent
05a42541de
commit
c8dc5cdbcc
2 changed files with 102 additions and 58 deletions
|
|
@ -0,0 +1,41 @@
|
|||
package ru.spcex.clearing.reports.reports;
|
||||
|
||||
import ru.clearing.classes.statics.data.registry.Registry;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PairRegistryId {
|
||||
private Long groupId;
|
||||
private LocalDate settlementDate;
|
||||
|
||||
public PairRegistryId() {
|
||||
|
||||
}
|
||||
|
||||
public PairRegistryId(Registry registry) {
|
||||
this.groupId = registry.getGroupId();
|
||||
this.settlementDate = registry.getSettlementDate();
|
||||
}
|
||||
|
||||
public Long getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public void setGroupId(Long groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
||||
public LocalDate getSettlementDate() {
|
||||
return settlementDate;
|
||||
}
|
||||
|
||||
public void setSettlementDate(LocalDate settlementDate) {
|
||||
this.settlementDate = settlementDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(groupId, settlementDate);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +1,36 @@
|
|||
package ru.spcex.clearing.reports.reports.ks;
|
||||
|
||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
import org.springframework.stereotype.Component;
|
||||
import ru.clearing.classes.statics.data.misc.Session;
|
||||
import ru.clearing.classes.statics.data.registry.Registry;
|
||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||
import ru.spcex.clearing.reports.reports.CSVReportBuilder;
|
||||
import ru.spcex.clearing.reports.reports.PairRegistryId;
|
||||
import ru.spcex.clearing.reports.reports.SessionIdParam;
|
||||
import ru.spcex.platform.enumeration.AccountType;
|
||||
import ru.spcex.platform.enumeration.RegistryDesignation;
|
||||
import ru.spcex.platform.enumeration.RegistryTradingParams;
|
||||
import ru.spcex.platform.enumeration.ReportType;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||
import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Component
|
||||
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
|
||||
public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam> {
|
||||
private final Imdg<Registry> registryImdg;
|
||||
private final Imdg<Session> sessionImdg;
|
||||
|
|
@ -57,10 +70,11 @@ public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam
|
|||
LocalDate nowDate = LocalDate.now();
|
||||
|
||||
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
|
||||
String sql = RegistryCodeSqlBuilder.getInstance(RegistryTradingParams.TM_T, RegistryTradingParams.OM_T).build();
|
||||
ImdgPredicate finalPredicate = pb.and(
|
||||
pb.equals("sessionId", params.getSessionId()),
|
||||
pb.equals("clearingDate", nowDate),
|
||||
pb.regex("registryCode","[T|O]M.T"),
|
||||
pb.sql(sql),
|
||||
pb.or(
|
||||
pb.equals("accountType", AccountType.Clrn.getKey()),
|
||||
pb.equals("accountType", AccountType.Info.getKey())
|
||||
|
|
@ -68,67 +82,53 @@ public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam
|
|||
);
|
||||
Collection<Registry> registries = registryImdg.getCollectionObjectsByPredicate(finalPredicate);
|
||||
|
||||
Map<String, Boolean> registryAlreadySaved = new HashMap<>();
|
||||
Map<String, Registry> omtRegistries = new HashMap<>();
|
||||
Map<String, Registry> tmtRegistries = new HashMap<>();
|
||||
for (Registry registry : registries) {
|
||||
if (registry.getSettlementDate() == null || registry.getGroupId() == null) continue;
|
||||
registryAlreadySaved.put(registry.getGroupId() + registry.getSettlementDate().toString(), false);
|
||||
if (registry.getRegistryCode().startsWith("O")) {
|
||||
Registry existOMTRegistry = omtRegistries.get(registry.getGroupId() + registry.getSettlementDate().toString());
|
||||
if (existOMTRegistry != null && existOMTRegistry.getBalance() != null) {
|
||||
log.warn("Duplicated registry OM*T (id = {} and id = {}). Use id = {}",
|
||||
registry.getId(),
|
||||
existOMTRegistry,
|
||||
existOMTRegistry);
|
||||
} else omtRegistries.put(registry.getGroupId() + registry.getSettlementDate().toString(), registry);
|
||||
}
|
||||
if (registry.getRegistryCode().startsWith("T")) {
|
||||
Registry existTMTRegistry = tmtRegistries.get(registry.getGroupId() + registry.getSettlementDate().toString());
|
||||
if (existTMTRegistry != null && existTMTRegistry.getBalance() != null) {
|
||||
log.warn("Duplicated registry TM*T (id = {} and id = {}). Use id = {}",
|
||||
registry.getId(),
|
||||
existTMTRegistry,
|
||||
existTMTRegistry);
|
||||
} else tmtRegistries.put(registry.getGroupId() + registry.getSettlementDate().toString(), registry);
|
||||
}
|
||||
}
|
||||
// todo check filter (skip register without log)
|
||||
Map<PairRegistryId, List<Registry>> registryMap = registries.stream()
|
||||
.filter(r -> r.getGroupId() != null && r.getSettlementDate() != null)
|
||||
.collect(Collectors.groupingBy(PairRegistryId::new));
|
||||
|
||||
Map<Long, String> createdAtForSessionId = new HashMap<>();
|
||||
lines = new ArrayList<>(registries.size());
|
||||
for (Registry registry : registries) {
|
||||
lines = new ArrayList<>(registryMap.size());
|
||||
for (Map.Entry<PairRegistryId, List<Registry>> entry : registryMap.entrySet()) {
|
||||
PairRegistryId pairRegistryId = entry.getKey();
|
||||
List<Registry> groupedRegistry = entry.getValue();
|
||||
try {
|
||||
if (registry.getSettlementDate() == null || registry.getGroupId() == null) {
|
||||
log.warn("Registry without GroupId or SettlementDate, id = {}. Skipped", registry.getId());
|
||||
continue;
|
||||
}
|
||||
if (registryAlreadySaved.get(registry.getGroupId() + registry.getSettlementDate().toString())) continue;
|
||||
|
||||
Registry tmtRegistry = tmtRegistries.get(registry.getGroupId() + registry.getSettlementDate().toString());
|
||||
Registry omtRegistry = omtRegistries.get(registry.getGroupId() + registry.getSettlementDate().toString());
|
||||
|
||||
if (!tmtRegistry.getTradingCode().equalsIgnoreCase(omtRegistry.getTradingCode()) ||
|
||||
!tmtRegistry.getSessionId().equals(omtRegistry.getSessionId()) ||
|
||||
!tmtRegistry.getAccount().equals(omtRegistry.getAccount())) {
|
||||
log.warn("Invalid registry OM*T (id = {}) and TM*T (id = {}) registry pair: fields not equal. GroupId = {}, SettlementDate = {}. Skipped",
|
||||
omtRegistry.getId(),
|
||||
tmtRegistry.getId(),
|
||||
registry.getSettlementDate().toString(),
|
||||
registry.getGroupId());
|
||||
if (groupedRegistry.size() != 2) {
|
||||
log.warn("Invalid OM_T and TM_T registries for GroupId = {} and SettlementDate = {}. Registries with id = [{}] was skipped",
|
||||
pairRegistryId.getGroupId(),
|
||||
pairRegistryId.getSettlementDate(),
|
||||
groupedRegistry.stream().map(r -> String.valueOf(r.getId())).collect(Collectors.joining(", "))
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
String firmId = registry.getTradingCode() != null ? registry.getTradingCode() : "";
|
||||
String sessionId = String.valueOf(registry.getSessionId());
|
||||
|
||||
String sessionDate = createdAtForSessionId.get(registry.getSessionId());
|
||||
if (sessionDate == null) {
|
||||
Session session = sessionImdg.getSingleObjectByID(registry.getSessionId());
|
||||
sessionDate = session != null && session.getCreated() != null ? dateTimeFormatter.format(LocalDateTime.ofInstant(session.getCreated(), ZoneId.systemDefault())) : "";
|
||||
createdAtForSessionId.put(registry.getSessionId(), sessionDate);
|
||||
Registry tmtRegistry = null;
|
||||
Registry omtRegistry = null;
|
||||
for (Registry registry : groupedRegistry) {
|
||||
if (RegistryDesignation.O.equalsByKey(registry.getRegistryDesignation())) omtRegistry = registry;
|
||||
if (RegistryDesignation.T.equalsByKey(registry.getRegistryDesignation())) tmtRegistry = registry;
|
||||
}
|
||||
|
||||
String account = registry.getAccount();
|
||||
if (tmtRegistry == null || omtRegistry == null) {
|
||||
log.warn("Invalid TM_T and OM_T registry pair, {} doubled. Registry pair was skipped", tmtRegistry == null ? "OM_T" : "TM_T");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!tmtRegistry.getAccount().equals(omtRegistry.getAccount()) ||
|
||||
!tmtRegistry.getTradingCode().equals(omtRegistry.getTradingCode())) {
|
||||
log.warn("Invalid TM_T and OM_T registry pair, account or tradingCode not equals. Registry pair was skipped");
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
String firmId = tmtRegistry.getTradingCode() != null ? tmtRegistry.getTradingCode() : "";
|
||||
String sessionId = String.valueOf(tmtRegistry.getSessionId());
|
||||
|
||||
Session session = sessionImdg.getSingleObjectByID(tmtRegistry.getSessionId());
|
||||
String sessionDate = "";
|
||||
if (session != null && session.getCreated() != null)
|
||||
sessionDate = dateTimeFormatter.format(LocalDateTime.ofInstant(session.getCreated(), ZoneId.systemDefault()));
|
||||
|
||||
String account = tmtRegistry.getAccount();
|
||||
|
||||
BigDecimal tmtBalance = tmtRegistry.getBalance() != null ? tmtRegistry.getBalance() : BigDecimal.ZERO;
|
||||
BigDecimal omtBalance = omtRegistry.getBalance() != null ? omtRegistry.getBalance() : BigDecimal.ZERO;
|
||||
|
|
@ -146,9 +146,12 @@ public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam
|
|||
line[valueIdx++] = date;
|
||||
lines.add(line);
|
||||
|
||||
registryAlreadySaved.put(registry.getGroupId() + registry.getSettlementDate().toString(), true);
|
||||
} catch (Throwable e) {
|
||||
log.error("Can't create report row for registry (id = %d), skipped".formatted(registry.getId()), e);
|
||||
log.error(
|
||||
"Can't create report row for registries (id = [%s]), row was skipped".formatted(
|
||||
groupedRegistry.stream().map(r -> String.valueOf(r.getId())).collect(Collectors.joining(", "))
|
||||
), e
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue