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;
|
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.misc.Session;
|
||||||
import ru.clearing.classes.statics.data.registry.Registry;
|
import ru.clearing.classes.statics.data.registry.Registry;
|
||||||
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
||||||
import ru.spcex.clearing.reports.reports.CSVReportBuilder;
|
import ru.spcex.clearing.reports.reports.CSVReportBuilder;
|
||||||
|
import ru.spcex.clearing.reports.reports.PairRegistryId;
|
||||||
import ru.spcex.clearing.reports.reports.SessionIdParam;
|
import ru.spcex.clearing.reports.reports.SessionIdParam;
|
||||||
import ru.spcex.platform.enumeration.AccountType;
|
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.enumeration.ReportType;
|
||||||
import ru.spcex.platform.imdg.api.Imdg;
|
import ru.spcex.platform.imdg.api.Imdg;
|
||||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||||
|
import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.ZoneId;
|
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> {
|
public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam> {
|
||||||
private final Imdg<Registry> registryImdg;
|
private final Imdg<Registry> registryImdg;
|
||||||
private final Imdg<Session> sessionImdg;
|
private final Imdg<Session> sessionImdg;
|
||||||
|
|
@ -57,10 +70,11 @@ public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam
|
||||||
LocalDate nowDate = LocalDate.now();
|
LocalDate nowDate = LocalDate.now();
|
||||||
|
|
||||||
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
|
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
|
||||||
|
String sql = RegistryCodeSqlBuilder.getInstance(RegistryTradingParams.TM_T, RegistryTradingParams.OM_T).build();
|
||||||
ImdgPredicate finalPredicate = pb.and(
|
ImdgPredicate finalPredicate = pb.and(
|
||||||
pb.equals("sessionId", params.getSessionId()),
|
pb.equals("sessionId", params.getSessionId()),
|
||||||
pb.equals("clearingDate", nowDate),
|
pb.equals("clearingDate", nowDate),
|
||||||
pb.regex("registryCode","[T|O]M.T"),
|
pb.sql(sql),
|
||||||
pb.or(
|
pb.or(
|
||||||
pb.equals("accountType", AccountType.Clrn.getKey()),
|
pb.equals("accountType", AccountType.Clrn.getKey()),
|
||||||
pb.equals("accountType", AccountType.Info.getKey())
|
pb.equals("accountType", AccountType.Info.getKey())
|
||||||
|
|
@ -68,67 +82,53 @@ public class KSRepCashNettoReportBuilder extends CSVReportBuilder<SessionIdParam
|
||||||
);
|
);
|
||||||
Collection<Registry> registries = registryImdg.getCollectionObjectsByPredicate(finalPredicate);
|
Collection<Registry> registries = registryImdg.getCollectionObjectsByPredicate(finalPredicate);
|
||||||
|
|
||||||
Map<String, Boolean> registryAlreadySaved = new HashMap<>();
|
// todo check filter (skip register without log)
|
||||||
Map<String, Registry> omtRegistries = new HashMap<>();
|
Map<PairRegistryId, List<Registry>> registryMap = registries.stream()
|
||||||
Map<String, Registry> tmtRegistries = new HashMap<>();
|
.filter(r -> r.getGroupId() != null && r.getSettlementDate() != null)
|
||||||
for (Registry registry : registries) {
|
.collect(Collectors.groupingBy(PairRegistryId::new));
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<Long, String> createdAtForSessionId = new HashMap<>();
|
lines = new ArrayList<>(registryMap.size());
|
||||||
lines = new ArrayList<>(registries.size());
|
for (Map.Entry<PairRegistryId, List<Registry>> entry : registryMap.entrySet()) {
|
||||||
for (Registry registry : registries) {
|
PairRegistryId pairRegistryId = entry.getKey();
|
||||||
|
List<Registry> groupedRegistry = entry.getValue();
|
||||||
try {
|
try {
|
||||||
if (registry.getSettlementDate() == null || registry.getGroupId() == null) {
|
if (groupedRegistry.size() != 2) {
|
||||||
log.warn("Registry without GroupId or SettlementDate, id = {}. Skipped", registry.getId());
|
log.warn("Invalid OM_T and TM_T registries for GroupId = {} and SettlementDate = {}. Registries with id = [{}] was skipped",
|
||||||
continue;
|
pairRegistryId.getGroupId(),
|
||||||
}
|
pairRegistryId.getSettlementDate(),
|
||||||
if (registryAlreadySaved.get(registry.getGroupId() + registry.getSettlementDate().toString())) continue;
|
groupedRegistry.stream().map(r -> String.valueOf(r.getId())).collect(Collectors.joining(", "))
|
||||||
|
);
|
||||||
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());
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
String firmId = registry.getTradingCode() != null ? registry.getTradingCode() : "";
|
Registry tmtRegistry = null;
|
||||||
String sessionId = String.valueOf(registry.getSessionId());
|
Registry omtRegistry = null;
|
||||||
|
for (Registry registry : groupedRegistry) {
|
||||||
String sessionDate = createdAtForSessionId.get(registry.getSessionId());
|
if (RegistryDesignation.O.equalsByKey(registry.getRegistryDesignation())) omtRegistry = registry;
|
||||||
if (sessionDate == null) {
|
if (RegistryDesignation.T.equalsByKey(registry.getRegistryDesignation())) tmtRegistry = registry;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 tmtBalance = tmtRegistry.getBalance() != null ? tmtRegistry.getBalance() : BigDecimal.ZERO;
|
||||||
BigDecimal omtBalance = omtRegistry.getBalance() != null ? omtRegistry.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;
|
line[valueIdx++] = date;
|
||||||
lines.add(line);
|
lines.add(line);
|
||||||
|
|
||||||
registryAlreadySaved.put(registry.getGroupId() + registry.getSettlementDate().toString(), true);
|
|
||||||
} catch (Throwable e) {
|
} 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