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;
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<R> implements IValidationRule<ImdgValidationContext<R>> {
String fieldName;
Function<R, List<Long>> accountIdGetter;
Function<R, List<Long>> accountIdListGetter;
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) {
public ExistAllAccountId(String fieldName,
Function<R, List<Long>> accountIdListGetter,
Function<R, Long> accountIdGetter,
boolean required, Function<R, Long> 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<EnumMessage> validate(ImdgValidationContext<R> context) {
R validatedObject = context.getValidatedObject();
List<Long> accounts = accountIdGetter.apply(validatedObject);
if (accounts == null || accounts.isEmpty()) {
List<Long> 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<TradingClearingRegistryList> 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<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 уже используется»
// Проверка отсутствия других 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()),
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<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 уже используется»
}
}
}