etreschenkov 2024-06-05 18:01:13 +03:00
parent c9c5fc9297
commit 7ce2445fc9

View file

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