diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryListValidationConfig.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryListValidationConfig.java index a6259188f..bcb899dc5 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryListValidationConfig.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryListValidationConfig.java @@ -1,5 +1,6 @@ package ru.spcex.clearing.account.config.validation; +import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; @@ -59,6 +60,7 @@ public class TradingClearingRegistryListValidationConfig { false), new ExistAllAccountId<>("accountId", TradingClearingRegistryListNewRequest::getCurrencyAccountList, + TradingClearingRegistryListNewRequest::getAccountId, false, null), DictionaryPresentRule.instance("stauts", @@ -154,12 +156,17 @@ public class TradingClearingRegistryListValidationConfig { public static class ExistAllAccountId implements IValidationRule> { String fieldName; - Function> accountIdGetter; + Function> accountIdListGetter; + Function accountIdGetter; boolean required; Function idGetter; - public ExistAllAccountId(String fieldName, Function> accountIdGetter, boolean required, Function idGetter) { + public ExistAllAccountId(String fieldName, + Function> accountIdListGetter, + Function accountIdGetter, + boolean required, Function idGetter) { this.fieldName = fieldName; + this.accountIdListGetter = accountIdListGetter; this.accountIdGetter = accountIdGetter; this.required = required; this.idGetter = idGetter; @@ -168,8 +175,15 @@ public class TradingClearingRegistryListValidationConfig { @Override public Optional validate(ImdgValidationContext context) { R validatedObject = context.getValidatedObject(); - List accounts = accountIdGetter.apply(validatedObject); - if (accounts == null || accounts.isEmpty()) { + List accounts = accountIdListGetter.apply(validatedObject); + if (accounts.isEmpty()){ + accounts = new ArrayList<>(); + } + Long singleAccountId = accountIdGetter.apply(validatedObject); + if (singleAccountId != null) { + accounts.add(singleAccountId); + } + if (accounts.isEmpty()) { if (required) return of(AccountError.RequiredFieldEmpty, fieldName); // обязательное поле else @@ -189,28 +203,26 @@ public class TradingClearingRegistryListValidationConfig { if (CurrencyCode.isRub(byIdObject.getCurrency()) || StringUtils.isEmpty(byIdObject.getCurrency())) { return of(AccountError.AccountIsNotACurrency, accountId, fieldName); // Счет %S не валютный } - } - } - // Проверка отсутствия других TradingClearingRegistryList с этими счетами - Imdg tradingClearingRegistryListImdg = context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistryList, TradingClearingRegistryList.class); - ImdgPredicateBuilder pb = tradingClearingRegistryListImdg.predicateBuilder(); - 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 inOtherLists = tradingClearingRegistryListImdg.getCollectionObjectsByPredicate(query); - if (!inOtherLists.isEmpty()) { - Collection duplicateAccounts = inOtherLists.stream() - .map(TradingClearingRegistryList::getAccountId) - .filter(accountId::equals) - .collect(Collectors.toSet()); - return of(AccountError.AccountForTradingClearingRegistryAlreadyUsed, duplicateAccounts, fieldName); // (5023) «Счет %s уже используется» + // Проверка отсутствия других TradingClearingRegistryList с этими счетами + Imdg tradingClearingRegistryListImdg = context.obtainMap(IMDGDistributedNames.Map_TradingClearingRegistryList, TradingClearingRegistryList.class); + ImdgPredicateBuilder pb = tradingClearingRegistryListImdg.predicateBuilder(); + ImdgPredicate query = pb.and(pb.equals("status", WorkflowStatus.Active.getKey()), + pb.equals("currency", byIdObject.getCurrency())); + 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 inOtherLists = tradingClearingRegistryListImdg.getCollectionObjectsByPredicate(query); + if (!inOtherLists.isEmpty()) { + Collection duplicateAccounts = inOtherLists.stream() + .map(TradingClearingRegistryList::getAccountId) + .filter(accountId::equals) + .collect(Collectors.toSet()); + return of(AccountError.AccountForTradingClearingRegistryAlreadyUsed, duplicateAccounts, fieldName); // (5023) «Счет %s уже используется» + } } }