etreschenkov 2024-06-05 15:41:00 +03:00
parent 743f721df5
commit c9c5fc9297
3 changed files with 57 additions and 32 deletions

View file

@ -1,6 +1,7 @@
package ru.spcex.clearing.account.config.validation;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
@ -57,7 +58,7 @@ public class TradingClearingRegistryListValidationConfig {
AccountError.TradingClearingRegistryNotFound,
false),
new ExistAllAccountId<>("accountId",
TradingClearingRegistryListNewRequest::getAccountId,
TradingClearingRegistryListNewRequest::getCurrencyAccountList,
false,
null),
DictionaryPresentRule.instance("stauts",
@ -153,11 +154,11 @@ public class TradingClearingRegistryListValidationConfig {
public static class ExistAllAccountId<R> implements IValidationRule<ImdgValidationContext<R>> {
String fieldName;
Function<R, Long> accountIdGetter;
Function<R, List<Long>> accountIdGetter;
boolean required;
Function<R, Long> idGetter;
public ExistAllAccountId(String fieldName, Function<R, Long> accountIdGetter, boolean required, Function<R, Long> idGetter) {
public ExistAllAccountId(String fieldName, Function<R, List<Long>> accountIdGetter, boolean required, Function<R, Long> idGetter) {
this.fieldName = fieldName;
this.accountIdGetter = accountIdGetter;
this.required = required;
@ -167,8 +168,8 @@ public class TradingClearingRegistryListValidationConfig {
@Override
public Optional<EnumMessage> validate(ImdgValidationContext<R> context) {
R validatedObject = context.getValidatedObject();
Long accountId = accountIdGetter.apply(validatedObject);
if (accountId == null) {
List<Long> accounts = accountIdGetter.apply(validatedObject);
if (accounts == null || accounts.isEmpty()) {
if (required)
return of(AccountError.RequiredFieldEmpty, fieldName); // обязательное поле
else
@ -177,36 +178,40 @@ public class TradingClearingRegistryListValidationConfig {
Imdg<Account> accountImdg = context.obtainMap(IMDGDistributedNames.Map_Account, Account.class);
// Проверить существование счетов
{
Account byIdObject = accountImdg.getSingleObjectByID(accountId);
if (byIdObject == null)
return of(AccountError.AccountNotFound, accountId, fieldName);
if (!IEnumKey.contains(byIdObject.getAccountType(), AccountType.Clrn, AccountType.Info)) {
return of(AccountError.AccountIsNotACurrency, accountId,
for (Long accountId : accounts) {
Account byIdObject = accountImdg.getSingleObjectByID(accountId);
if (byIdObject == null)
return of(AccountError.AccountNotFound, accountId, fieldName);
if (!IEnumKey.contains(byIdObject.getAccountType(), AccountType.Clrn, AccountType.Info)) {
return of(AccountError.AccountIsNotACurrency, accountId,
fieldName, "expected: CLRN/INFO but is " + byIdObject.getAccountType()); // Счет %S не
}
if (CurrencyCode.isRub(byIdObject.getCurrency()) || StringUtils.isEmpty(byIdObject.getCurrency())) {
return of(AccountError.AccountIsNotACurrency, accountId, fieldName); // Счет %S не валютный
}
if (CurrencyCode.isRub(byIdObject.getCurrency()) || StringUtils.isEmpty(byIdObject.getCurrency())) {
return of(AccountError.AccountIsNotACurrency, accountId, fieldName); // Счет %S не валютный
}
}
}
// Проверка отсутствия других TradingClearingRegistryList с этими счетами
Imdg<TradingClearingRegistryList> tradingClearingRegistryListImdg = context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistryList, TradingClearingRegistryList.class);
ImdgPredicateBuilder pb = tradingClearingRegistryListImdg.predicateBuilder();
ImdgPredicate query = pb.and(pb.equals("status", WorkflowStatus.Active.getKey()),
for (Long accountId : accounts) {
ImdgPredicate query = pb.and(pb.equals("status", WorkflowStatus.Active.getKey()),
pb.equals("accountId", accountId));
if (idGetter != null) {
Long id = idGetter.apply(validatedObject);
if (id == null) // never
return of(AccountError.RequiredFieldEmpty, "id");
query = pb.and(query, pb.not(pb.equals("id", id)));
}
Collection<TradingClearingRegistryList> inOtherLists = tradingClearingRegistryListImdg.getCollectionObjectsByPredicate(query);
if (!inOtherLists.isEmpty()) {
Collection<Long> duplicateAccounts = inOtherLists.stream()
if (idGetter != null) {
Long id = idGetter.apply(validatedObject);
if (id == null) // never
return of(AccountError.RequiredFieldEmpty, "id");
query = pb.and(query, pb.not(pb.equals("id", id)));
}
Collection<TradingClearingRegistryList> inOtherLists = tradingClearingRegistryListImdg.getCollectionObjectsByPredicate(query);
if (!inOtherLists.isEmpty()) {
Collection<Long> duplicateAccounts = inOtherLists.stream()
.map(TradingClearingRegistryList::getAccountId)
.filter(accountId::equals)
.collect(Collectors.toSet());
return of(AccountError.AccountForTradingClearingRegistryAlreadyUsed, duplicateAccounts, fieldName); // (5023) «Счет %s уже используется»
return of(AccountError.AccountForTradingClearingRegistryAlreadyUsed, duplicateAccounts, fieldName); // (5023) «Счет %s уже используется»
}
}
return empty();

View file

@ -54,6 +54,8 @@ import ru.spcex.platform.enumeration.WorkflowStatus;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.imdg.api.ImdgId;
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.utils.enumeration.EnumMessage;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.log.ExceptionUtils;
@ -197,17 +199,35 @@ public class ClientCodeService extends QueueConsumer implements InitializingBean
clientCodeNewRequest.setDepoAccountId(account.getId());
}
}
List<Long> currencyIds = new ArrayList<>();
for (MoneyAccountMsgRequest moneyAccountMsg : tkrAccount.getMoneyAccounts()) {
ImdgPredicateBuilder predicateBuilder = accountImdg.predicateBuilder();
ImdgPredicate currencyPredicate;
ImdgPredicate accountPredicate = predicateBuilder.equals("account", moneyAccountMsg.getAccount());
if (moneyAccountMsg.getCurrCode().equals(CurrencyCode.RUB.getKey())) {
Account account = accountImdg.getFirstObjectByFieldValues(
Map.of(
"account", moneyAccountMsg.getAccount()
)
currencyPredicate = predicateBuilder.or(
predicateBuilder.equals("currency", CurrencyCode.RUB.getKey()),
predicateBuilder.isNull("currency")
);
if (account != null) {
clientCodeNewRequest.setMoneyAccountId(account.getId());
}
} else {
currencyPredicate = predicateBuilder.equals("currency", moneyAccountMsg.getCurrCode());
}
Account account = accountImdg.getFirstObjectByPredicate(
predicateBuilder.and(accountPredicate, currencyPredicate)
);
if (account == null) {
log.warn("Account not found: currency: {}; account: {}", moneyAccountMsg.getCurrCode(),
moneyAccountMsg.getAccount());
continue;
}
if (!StringUtils.hasText(account.getCurrency()) || CurrencyCode.RUB.equalsByKey(account.getCurrency())) {
clientCodeNewRequest.setMoneyAccountId(account.getId());
} else {
currencyIds.add(account.getId());
}
}
if (!currencyIds.isEmpty()) {
clientCodeNewRequest.setCurrencyAccountList(currencyIds);
}
BaseRequest<ClientCodeNewRequest> request = new BaseRequest<>();
request.setRequestPayload(clientCodeNewRequest);

View file

@ -141,7 +141,6 @@ public class TradingClearingRegistryListService extends QueueConsumer implements
TradingClearingRegistryListNewRequest req = userRequest.getRequestPayload();
Account account = accountImdg.getSingleObjectByID(req.getAccountId());
Instant now = Instant.now();
List<Long> newIds=new ArrayList<>();
@ -153,6 +152,7 @@ public class TradingClearingRegistryListService extends QueueConsumer implements
tradingClearingRegistryList.setUpdated(now);
tradingClearingRegistryList.setTradingClearingRegistryId(req.getTradingClearingRegistryId());
tradingClearingRegistryList.setAccountId(currAccId);
Account account = accountImdg.getSingleObjectByID(currAccId);
tradingClearingRegistryList.setCurrency(account.getCurrency());
if (req.getStatus() == null) {
tradingClearingRegistryList.setStatus(ServiceStatus.Active.getKey());