clearing-service CLS-290 рефакторинг stage 6, 8
This commit is contained in:
parent
8755f3e1ca
commit
b80431f22e
6 changed files with 80 additions and 93 deletions
|
|
@ -9,7 +9,7 @@ import ru.spcex.clearing.imdg.IMDGDistributedNames;
|
|||
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.RegistryOnObligationsAndSettlementRequirementsPayload;
|
||||
import ru.spcex.clearing.session.stage.task.FormingRegistersOnOSPayload;
|
||||
import ru.spcex.clearing.session.stage.util.RegistryUtil;
|
||||
import ru.spcex.platform.enumeration.RegistryDesignation;
|
||||
import ru.spcex.platform.enumeration.RegistryInstrumentType;
|
||||
|
|
@ -39,10 +39,10 @@ public class FormingRegistersOnOS implements ISessionStage {
|
|||
|
||||
@Override
|
||||
public StageResult<?> submit(Task<?> task) {
|
||||
RegistryOnObligationsAndSettlementRequirementsPayload payload = (RegistryOnObligationsAndSettlementRequirementsPayload) task.getData();
|
||||
FormingRegistersOnOSPayload payload = (FormingRegistersOnOSPayload) task.getData();
|
||||
switch (task.getTaskType()) {
|
||||
case FormingRegistersOnOS -> {
|
||||
return createRegistryOnObligationsAndSettlementRequirements(payload.getSessionId());
|
||||
return createRegistryOnObligationsAndSettlementRequirements();
|
||||
}
|
||||
default -> {
|
||||
throw new IllegalStateException("Unknown task type: " + task.getTaskType());
|
||||
|
|
@ -53,12 +53,10 @@ public class FormingRegistersOnOS implements ISessionStage {
|
|||
/**
|
||||
* Select Registry by: registryCode = [O/T][S/M][*][T] & registryStatus=OK
|
||||
*
|
||||
* @param sessionId
|
||||
* @return
|
||||
*/
|
||||
protected Collection<Registry> selectRegistry(Long sessionId) {
|
||||
String registrySQL = "sessionId = " + sessionId;
|
||||
registrySQL += " and (registryDesignation=" + RegistryDesignation.O.getKey() + " or registryDesignation=" + RegistryDesignation.T.getKey() + ")";
|
||||
protected Collection<Registry> selectRegistry() {
|
||||
String registrySQL = "(registryDesignation=" + RegistryDesignation.O.getKey() + " or registryDesignation=" + RegistryDesignation.T.getKey() + ")";
|
||||
registrySQL += " and (registryInstrumentType=" + RegistryInstrumentType.S.getKey() + " or registryInstrumentType=" + RegistryInstrumentType.M.getKey() + ")";
|
||||
registrySQL += " and (registryUnit=" + RegistryUnit.T.getKey() + ")";
|
||||
registrySQL += " and registryStatus=" + RegistryStatus.OK.getKey() + ")";
|
||||
|
|
@ -67,8 +65,8 @@ public class FormingRegistersOnOS implements ISessionStage {
|
|||
return result;
|
||||
}
|
||||
|
||||
protected StageResult<?> createRegistryOnObligationsAndSettlementRequirements(Long sessionId) {
|
||||
Collection<Registry> forRegistries = selectRegistry(sessionId);
|
||||
protected StageResult<?> createRegistryOnObligationsAndSettlementRequirements() {
|
||||
Collection<Registry> forRegistries = selectRegistry();
|
||||
ArrayList<Registry> newRegistries = new ArrayList<>();
|
||||
|
||||
//todo oreder by обрабатываться группами по полю registry.groupId
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import ru.spcex.platform.enumeration.RegistryInstrumentType;
|
|||
import ru.spcex.platform.enumeration.RegistryUnit;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.imdg.api.ImdgTransaction;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
|
||||
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
|
||||
|
||||
|
|
@ -26,10 +27,12 @@ import java.util.Collection;
|
|||
public class UnlockResources implements ISessionStage {
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
|
||||
protected final ImdgProvider imdgProvider;
|
||||
private final Imdg<Registry> registryImdg;
|
||||
|
||||
@Autowired
|
||||
public UnlockResources(ImdgProvider imdgProvider) {
|
||||
this.imdgProvider = imdgProvider;
|
||||
this.registryImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
|
||||
|
||||
}
|
||||
|
|
@ -39,7 +42,7 @@ public class UnlockResources implements ISessionStage {
|
|||
switch (task.getTaskType()) {
|
||||
case UnlockResources -> {
|
||||
UnlockResourcesPayload payload = (UnlockResourcesPayload) task.getData();
|
||||
return unlockResources(payload.getSdfMode(), payload.getSessionId(),
|
||||
return unlockResources(payload.getSdfMode(),
|
||||
payload.getAccount(),
|
||||
payload.getSecurityId(),
|
||||
payload.getFullNames(),
|
||||
|
|
@ -52,26 +55,18 @@ public class UnlockResources implements ISessionStage {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Select Registry by: account; fullName* / securityId
|
||||
*/
|
||||
protected Collection<Registry> selectRegistry(Long sessionId, String account, Long securityId, Collection<String> fullNames) {
|
||||
protected Collection<Registry> selectRegistryForSDF04(String account, Collection<String> fullNames) {
|
||||
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
|
||||
ImdgPredicate queryPart;
|
||||
if (securityId != null) {
|
||||
queryPart = pb.in("securityId", securityId);
|
||||
} else if (fullNames != null && !fullNames.isEmpty()) {
|
||||
queryPart = pb.in("fullName", fullNames.toArray(new String[fullNames.size()]));
|
||||
} else {
|
||||
throw new IllegalArgumentException("Required securityId or fullNames");
|
||||
}
|
||||
ImdgPredicate query = pb.and(
|
||||
pb.and(
|
||||
pb.equals("sessionId", sessionId),
|
||||
pb.equals("registryDesignation", RegistryDesignation.A.getKey()) // не все нужны, только с этим кодом отфильтруем.
|
||||
pb.equals("registryDesignation", RegistryDesignation.A.getKey()),
|
||||
pb.equals("registryInstrumentType", RegistryInstrumentType.M.getKey()),
|
||||
// pb.equals("registryCapacity", *),
|
||||
pb.or(pb.equals("registryUnit", RegistryUnit.F.getKey()),
|
||||
pb.equals("registryUnit", RegistryUnit.B.getKey()))
|
||||
),
|
||||
pb.equals("account", account),
|
||||
queryPart
|
||||
pb.in("fullName", fullNames.toArray(new String[fullNames.size()]))
|
||||
);
|
||||
|
||||
Collection<Registry> result = registryImdg.getCollectionObjectsByPredicate(query);
|
||||
|
|
@ -79,50 +74,74 @@ public class UnlockResources implements ISessionStage {
|
|||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select Registry by: account; fullName* / securityId
|
||||
*/
|
||||
protected Collection<Registry> selectRegistryForSDF12(String account, Long securityId) {
|
||||
ImdgPredicateBuilder pb = registryImdg.predicateBuilder();
|
||||
ImdgPredicate query = pb.and(
|
||||
pb.and(
|
||||
pb.equals("registryDesignation", RegistryDesignation.A.getKey()),
|
||||
pb.equals("registryInstrumentType", RegistryInstrumentType.S.getKey()),
|
||||
// pb.equals("registryCapacity", *),
|
||||
pb.or(pb.equals("registryUnit", RegistryUnit.F.getKey()),
|
||||
pb.equals("registryUnit", RegistryUnit.B.getKey()))
|
||||
),
|
||||
pb.equals("account", account),
|
||||
pb.in("securityId", securityId)
|
||||
);
|
||||
|
||||
Collection<Registry> result = registryImdg.getCollectionObjectsByPredicate(query);
|
||||
log.trace("Selected {} registry's by sql: {}", result.size(), query);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param sdfMode UnlockResourcesPayload.sdfMode
|
||||
* @param sessionId
|
||||
* @param account
|
||||
* @param securityId
|
||||
* @param fullNames
|
||||
* @return
|
||||
*/
|
||||
protected StageResult<?> unlockResources(String sdfMode, Long sessionId, String account, Long securityId, Collection<String> fullNames, BigDecimal value) {
|
||||
Collection<Registry> forRegistries = selectRegistry(sessionId, account, securityId, fullNames);
|
||||
protected StageResult<?> unlockResources(String sdfMode, String account, Long securityId, Collection<String> fullNames, BigDecimal value) {
|
||||
Collection<Registry> forRegistries;
|
||||
|
||||
//todo при перезапуске после незапланированного завершения стадии: надо ли проверять уже созданные регистры и не создавать дубликаты?
|
||||
if (UnlockResourcesPayload.MODE_SDF04.equals(sdfMode)) {
|
||||
forRegistries = forRegistries.stream().filter(
|
||||
(Registry r) ->
|
||||
RegistryDesignation.A.equalsByKey(r.getRegistryDesignation())
|
||||
&&
|
||||
RegistryInstrumentType.M.equalsByKey(r.getRegistryInstrumentType())
|
||||
&&
|
||||
(RegistryUnit.F.equalsByKey(r.getRegistryUnit()) || RegistryUnit.B.equalsByKey(r.getRegistryUnit()))
|
||||
).toList();
|
||||
forRegistries = selectRegistryForSDF04(account, fullNames);
|
||||
} else if (UnlockResourcesPayload.MODE_SDF12.equals(sdfMode)) {
|
||||
forRegistries = forRegistries.stream().filter(
|
||||
(Registry r) ->
|
||||
RegistryDesignation.A.equalsByKey(r.getRegistryDesignation())
|
||||
&&
|
||||
RegistryInstrumentType.S.equalsByKey(r.getRegistryInstrumentType())
|
||||
&&
|
||||
(RegistryUnit.F.equalsByKey(r.getRegistryUnit()) || RegistryUnit.B.equalsByKey(r.getRegistryUnit()))
|
||||
).toList();
|
||||
forRegistries = selectRegistryForSDF12(account, securityId);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Mode not support: " + sdfMode);
|
||||
}
|
||||
|
||||
for (Registry registry : forRegistries) {
|
||||
boolean modified = unlockRegistry(registry, value);
|
||||
if (modified) {
|
||||
registry.setUpdated(Instant.now());
|
||||
registryImdg.update(registry);
|
||||
log.trace("Registry {} changed; value +- registry",
|
||||
registry.getId(), registry.getRegistryCode(), value);
|
||||
ImdgTransaction tx = imdgProvider.newTransaction();
|
||||
boolean txOk = false;
|
||||
try {
|
||||
log.debug("Processing transaction {}, input {} registers.", tx, forRegistries.size());
|
||||
Imdg<Registry> registryTxImdg = tx.getImdg(IMDGDistributedNames.Map_Registry, Registry.class);
|
||||
int nUpdates = 0;
|
||||
for (Registry registry : forRegistries) {
|
||||
boolean modified = unlockRegistry(registry, value);
|
||||
if (modified) {
|
||||
registry.setUpdated(Instant.now());
|
||||
registryTxImdg.update(registry);
|
||||
nUpdates++;
|
||||
log.trace("Registry {} (registryCode={}) changed; value +- {}",
|
||||
registry.getId(), registry.getRegistryCode(), value);
|
||||
}
|
||||
}
|
||||
log.info("Updated {} registry's (under transaction {}", nUpdates, tx);
|
||||
txOk = true;
|
||||
} finally {
|
||||
if (txOk) {
|
||||
log.debug("Commit transaction {}.", tx);
|
||||
tx.commitTransaction();
|
||||
} else {
|
||||
log.info("Rollback transaction {}", tx);
|
||||
tx.rollbackTransaction();
|
||||
}
|
||||
}
|
||||
//todo рекомендуется делать транзакцией.
|
||||
|
||||
StageResult<Collection<Registry>> res = new StageResult<>(null, true);
|
||||
res.setStageResult(forRegistries);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,5 @@
|
|||
package ru.spcex.clearing.session.stage.task;
|
||||
|
||||
public class FormingRegistersOnOSPayload {
|
||||
|
||||
}
|
||||
|
|
@ -1,31 +0,0 @@
|
|||
package ru.spcex.clearing.session.stage.task;
|
||||
|
||||
public class RegistryOnObligationsAndSettlementRequirementsPayload {
|
||||
private Long sessionId;
|
||||
// private Long companyId;
|
||||
// private Long securityId;
|
||||
|
||||
public Long getSessionId() {
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
public void setSessionId(Long sessionId) {
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
// public Long getCompanyId() {
|
||||
// return companyId;
|
||||
// }
|
||||
//
|
||||
// public void setCompanyId(Long companyId) {
|
||||
// this.companyId = companyId;
|
||||
// }
|
||||
//
|
||||
// public Long getSecurityId() {
|
||||
// return securityId;
|
||||
// }
|
||||
//
|
||||
// public void setSecurityId(Long securityId) {
|
||||
// this.securityId = securityId;
|
||||
// }
|
||||
}
|
||||
|
|
@ -12,8 +12,6 @@ public class UnlockResourcesPayload {
|
|||
*/
|
||||
private String sdfMode;
|
||||
|
||||
private Long sessionId;
|
||||
|
||||
/**
|
||||
* sDf04.c_acc_cred
|
||||
* sDf12.depoCodeSender
|
||||
|
|
@ -46,13 +44,6 @@ public class UnlockResourcesPayload {
|
|||
}
|
||||
|
||||
|
||||
public Long getSessionId() {
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
public void setSessionId(Long sessionId) {
|
||||
this.sessionId = sessionId;
|
||||
}
|
||||
|
||||
public String getAccount() {
|
||||
return account;
|
||||
|
|
|
|||
|
|
@ -68,4 +68,9 @@ public class ImdgTransactionProviderHazelcast implements ImdgTransaction {
|
|||
public void setHz(HazelcastInstance hz) {
|
||||
this.hz = hz;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ImdgTransaction{" + (ctx == null ? null : ctx.getTxnId()) + "}";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue