diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryValidationConfig.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryValidationConfig.java index c98933433..845a3bebc 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryValidationConfig.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/config/validation/TradingClearingRegistryValidationConfig.java @@ -2,6 +2,7 @@ package ru.spcex.clearing.account.config.validation; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import ru.clearing.classes.statics.data.account.Account; import ru.clearing.classes.statics.data.account.ClearingAccount; import ru.clearing.classes.statics.data.account.DepoAccount; import ru.clearing.classes.statics.data.account.InformationAccount; @@ -17,6 +18,7 @@ import ru.spcex.clearing.validation.common.rules.DictionaryPresentRule; import ru.spcex.clearing.validation.common.rules.FieldRequiredRule; import ru.spcex.clearing.validation.common.rules.IdPresentRule; import ru.spcex.platform.classes.base.SpcexObjectBase; +import ru.spcex.platform.enumeration.AccountType; import ru.spcex.platform.enumeration.ServiceStatus; import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.validation.ImdgValidationContext; @@ -39,6 +41,7 @@ public class TradingClearingRegistryValidationConfig { context.setValidatedObject(tradingClearingRegistryNewRequest); Consumer addImdg = (s) -> context.addImdg(s, imdgForValidation.get(s)); addImdg.accept(IMDGDistributedNames.Map_Company); + addImdg.accept(IMDGDistributedNames.Map_Account); addImdg.accept(IMDGDistributedNames.Map_ClearingAccount); addImdg.accept(IMDGDistributedNames.Map_DepoAccount); addImdg.accept(IMDGDistributedNames.Map_InformationAccount); @@ -54,18 +57,34 @@ public class TradingClearingRegistryValidationConfig { TradingClearingRegistryNewRequest::getMoneyAccountId, AccountError.RequiredFieldEmpty, moneyAccountId -> { - Imdg clearingAccountImdg = context.obtainMap( - IMDGDistributedNames.Map_ClearingAccount, ClearingAccount.class + Imdg accountImdg = context.obtainMap( + IMDGDistributedNames.Map_Account, Account.class ); - ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByID(moneyAccountId); - if (clearingAccount == null) { - Imdg informationAccountImdg = context.obtainMap( - IMDGDistributedNames.Map_InformationAccount, InformationAccount.class - ); - InformationAccount infoAccount = informationAccountImdg.getSingleObjectByFieldValues( - Map.of("accountId",moneyAccountId)); - if (infoAccount == null) return AccountError.AccountNotFound; + Account account = accountImdg.getSingleObjectByID(moneyAccountId); + if (account == null) { + return AccountError.AccountNotFound; + } else { + if (AccountType.Clrn.equalsByKey(account.getAccountType()) || AccountType.Info.equalsByKey(account.getAccountType())) { + // ok + } else { + // неправильный тип + return AccountError.AccountNotFound; + } } +// Imdg clearingAccountImdg = context.obtainMap( +// IMDGDistributedNames.Map_ClearingAccount, ClearingAccount.class +// ); +// ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByFieldValues( +// Map.of("accountId",moneyAccountId) +// ); +// if (clearingAccount == null) { +// Imdg informationAccountImdg = context.obtainMap( +// IMDGDistributedNames.Map_InformationAccount, InformationAccount.class +// ); +// InformationAccount infoAccount = informationAccountImdg.getSingleObjectByFieldValues( +// Map.of("accountId",moneyAccountId)); +// if (infoAccount == null) return AccountError.AccountNotFound; +// } return null; }), FieldRequiredRule.instance("depoAccountId", @@ -75,7 +94,9 @@ public class TradingClearingRegistryValidationConfig { depoAccountId -> { if (depoAccountId == null) return null; Imdg depoAccountImdg = context.obtainMap(IMDGDistributedNames.Map_DepoAccount, DepoAccount.class); - DepoAccount depoAccount = depoAccountImdg.getSingleObjectByID(depoAccountId); + DepoAccount depoAccount = depoAccountImdg.getSingleObjectByFieldValues( + Map.of("accountId",depoAccountId) + ); if (depoAccount == null) return AccountError.AccountNotFound; return null; }), diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/InformationAccountService.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/InformationAccountService.java index 88eac050b..d2565cf70 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/InformationAccountService.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/InformationAccountService.java @@ -157,7 +157,7 @@ public class InformationAccountService extends QueueConsumer implements Initiali if (txOk) { imdgTransaction.commitTransaction(); TradingClearingRegistryNewRequest request = new TradingClearingRegistryNewRequest(); - request.setMoneyAccountId(informationAccountId); + request.setMoneyAccountId(accountId); request.setCompanyId(informationAccount.getCompanyId()); log.debug("Send message to kafka \"{}\": {}", Consts.DESTINATION_TRADING_CLEARING_REGISTRY_AUTO_NEW, LogFormatter.toStringWrapper(request)); kafkaSender.sendRequestToQueue(Consts.DESTINATION_TRADING_CLEARING_REGISTRY_AUTO_NEW, request); diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/TradingClearingRegistryService.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/TradingClearingRegistryService.java index 5c0774a12..ca10cdd66 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/TradingClearingRegistryService.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/TradingClearingRegistryService.java @@ -140,30 +140,76 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini tradingClearingRegistry.setMoneyAccountId(req.getMoneyAccountId()); tradingClearingRegistry.setDepoAccountId(req.getDepoAccountId()); - DepoAccount depoAccount = req.getDepoAccountId() != null ? depoAccountImdg.getSingleObjectByID(req.getDepoAccountId()) : null; - Account depoAccountMain = depoAccount != null ? accountImdg.getSingleObjectByID(depoAccount.getAccountId()) : null; + Account depoAccountMain = req.getDepoAccountId() != null ? accountImdg.getSingleObjectByID(req.getDepoAccountId()) : null; + DepoAccount depoAccount = depoAccountMain != null ? depoAccountImdg.getSingleObjectByFieldValues(Map.of("accountId", req.getDepoAccountId())) : null; - InformationAccount infoAccount = null; - ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByID(req.getMoneyAccountId()); - if (clearingAccount == null) infoAccount = informationAccountImdg.getSingleObjectByID(req.getMoneyAccountId()); - Long accountId = clearingAccount != null ? clearingAccount.getAccountId() : infoAccount.getAccountId(); - Account accountMain = accountImdg.getSingleObjectByID(accountId); +// InformationAccount infoAccount = null; + ClearingAccount clearingAccount = null; + Account accountMain = null; + if (req.getMoneyAccountId() != null) { + clearingAccount = clearingAccountImdg.getSingleObjectByFieldValues(Map.of("accountId", req.getMoneyAccountId())); +// if (clearingAccount == null) infoAccount = informationAccountImdg.getSingleObjectByFieldValues(Map.of("accountId", req.getMoneyAccountId())); + Long accountId = req.getMoneyAccountId(); //clearingAccount != null ? clearingAccount.getAccountId() : infoAccount.getAccountId(); + accountMain = accountImdg.getSingleObjectByID(accountId); + } - - // TODO - // Проверка выглядит очень сложно и запутанно, потому что репортеры сами не до конца понимают, какая она должна быть. - // Пока что она соответствует ТЗ, но возможно будет правиться, поэтому расписана без выноса кода в методы и - // прочих методов сокращения кода. В дальнейшем, после тестирования и окончательного выяснения вида проверки, - // её стоит вынести на этап валидации запроса. + // кроссвалидация if (req.getMoneyAccountId() == null && (req.getMoneyAccountId() == null || req.getDepoAccountId() == null)) { TradingClearingRegistry registryByCompany = tradingClearingRegistryImdg.getSingleObjectByFieldValues(Map.of("companyId", req.getCompanyId())); if (registryByCompany == null) { + log.trace("Not found registry with companyId={}", + req.getCompanyId()); return requestHelper.makeErrorResponse(userRequest, AccountError.TradingClearingRegistryNotFound, req.getCompanyId()); } } + if (tradingClearingRegistry.getMoneyAccountId() == null && tradingClearingRegistry.getDepoAccountId() != null) { + Collection registriesByCompany = tradingClearingRegistryImdg.getCollectionObjectsByFieldValues(Map.of("companyId", req.getCompanyId())); + TradingClearingRegistry targetExistTradingClearingRegistry = null; + for (TradingClearingRegistry existRegistry : registriesByCompany) { + if (existRegistry.getMoneyAccountId() != null && existRegistry.getDepoAccountId() == null) { + targetExistTradingClearingRegistry = existRegistry; + } + } + if (targetExistTradingClearingRegistry == null) { + log.warn("TradingClearingRegister with MoneyAccountId for companyId={} not found. Search exist accounts for company", req.getCompanyId()); + Long accountId; + InformationAccount infoAccount = null; + clearingAccount = clearingAccountImdg.getSingleObjectByFieldValues(Map.of("companyId", req.getCompanyId())); + if (clearingAccount == null) { + infoAccount = informationAccountImdg.getSingleObjectByFieldValues(Map.of("companyId", req.getCompanyId())); + if (infoAccount == null) { + log.warn("ClearingAccount and InfoAccount not exist for company {}.", req.getCompanyId()); + accountId = null; + } else { + accountId = infoAccount.getAccountId(); + log.debug("Found account.id={} and infoaccount.id={} by companyId={}", accountId, infoAccount.getId(), req.getCompanyId()); + } + } else { + accountId = clearingAccount.getAccountId(); + log.debug("Found account.id={} and clearingAccount.id={} by companyId={}", accountId, clearingAccount.getId(), req.getCompanyId()); + } + if (accountId != null) { + accountMain = accountImdg.getSingleObjectByID(accountId); + if (accountMain == null) { // never, только с инконсистентными данными + log.warn("Account {} not exist.", accountId); + } else { + tradingClearingRegistry.setMoneyAccountId(accountId); + log.debug("Set tradingClearingRegistry[{}].MoneyAccountId={}", tradingClearingRegistry.getId(), accountId); + } + } + } else { + log.debug("Update exist TCR.id={}, set DepoAccountId={}", targetExistTradingClearingRegistry, req.getDepoAccountId()); + targetExistTradingClearingRegistry.setDepoAccountId(req.getDepoAccountId()); + targetExistTradingClearingRegistry.setUpdated(Instant.now()); + tradingClearingRegistryImdg.update(targetExistTradingClearingRegistry); + log.debug("successfully processed, id {}. Updated exist TradingClearingRegistry.id={}", id, targetExistTradingClearingRegistry.getId()); + return null; + } + } + List activeStatuses = Arrays.asList(ServiceStatus.Active.getKey(), ServiceStatus.Reopened.getKey()); String status = null; if (depoAccountMain != null && depoAccountMain.getStatus() != null) { @@ -255,13 +301,13 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini tradingClearingRegistry.setMoneyAccountId(req.getMoneyAccountId()); tradingClearingRegistry.setDepoAccountId(req.getDepoAccountId()); - DepoAccount depoAccount = req.getDepoAccountId() != null ? depoAccountImdg.getSingleObjectByID(req.getDepoAccountId()) : null; - ClearingAccount clearingAccount = clearingAccountImdg.getSingleObjectByID(req.getMoneyAccountId()); + DepoAccount depoAccount = req.getDepoAccountId() != null ? depoAccountImdg.getSingleObjectByFieldValues(Map.of("accountId", req.getDepoAccountId())) : null; + ClearingAccount clearingAccount = req.getMoneyAccountId() != null ? clearingAccountImdg.getSingleObjectByFieldValues(Map.of("accountId", req.getMoneyAccountId())) : null; tradingClearingRegistry.setStatus(ServiceStatus.Active.getKey()); String tradingRegistryType; - if (req.getTradingClearingRegistryType()!=null) { + if (req.getTradingClearingRegistryType() != null) { tradingRegistryType = req.getTradingClearingRegistryType(); } else if (depoAccount != null) { tradingRegistryType = depoAccount.getDepoAccountType(); @@ -308,15 +354,16 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini TradingClearingRegistryUpdateRequest req = userRequest.getRequestPayload(); TradingClearingRegistry tradingClearingRegistry = tradingClearingRegistryImdg.getSingleObjectByID(req.getId()); - // Дополнительная проверка - if (req.getMoneyAccountId() == null && (req.getMoneyAccountId() == null || req.getDepoAccountId() == null)) { - TradingClearingRegistry registryByCompany = tradingClearingRegistryImdg.getSingleObjectByFieldValues(Map.of("companyId", req.getCompanyId())); - if (registryByCompany == null) { - return requestHelper.makeErrorResponse(userRequest, - AccountError.TradingClearingRegistryNotFound, - "companyId=" + req.getCompanyId()); - } - } + +// // Дополнительная проверка +// if (req.getMoneyAccountId() == null && (req.getMoneyAccountId() == null || req.getDepoAccountId() == null)) { +// TradingClearingRegistry registryByCompany = tradingClearingRegistryImdg.getSingleObjectByFieldValues(Map.of("companyId", tradingClearingRegistry.getCompanyId())); +// if (registryByCompany == null) { // never +// return requestHelper.makeErrorResponse(userRequest, +// AccountError.TradingClearingRegistryNotFound, +// "companyId=" + req.getCompanyId()); +// } +// } if (req.getStatus() != null) tradingClearingRegistry.setStatus(req.getStatus()); @@ -350,7 +397,6 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini } - /** * company-service сообщение об успешном добавлении ТКР клиента с параметром tradingClearingRegistry.code */ @@ -364,6 +410,7 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini log.debug("Send message to kafka \"{}\": {}", Consts.DESTINATION_CLIENT_CODE_NEW_UM_COMPANY, LogFormatter.toStringWrapper(request)); kafkaSender.sendRequestToQueue(Consts.DESTINATION_CLIENT_CODE_NEW_UM_COMPANY, request); } + /** * clearing-service сообщение на открытие клиринговых регистров; */ @@ -373,6 +420,7 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini log.debug("Send message to kafka \"{}\": {}", Consts.REGISTRY_NEW, LogFormatter.toStringWrapper(request)); kafkaSender.sendRequestToQueue(Consts.REGISTRY_NEW, request); } + /** * report-service сообщение на формирование уведомления о создании нового ТКР */ diff --git a/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/TradingClearingRegistryServiceTest.java b/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/TradingClearingRegistryServiceTest.java index d22e9120c..62d12239e 100644 --- a/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/TradingClearingRegistryServiceTest.java +++ b/clearing-parent/account-service/src/test/java/ru/spcex/clearing/account/service/TradingClearingRegistryServiceTest.java @@ -155,12 +155,12 @@ class TradingClearingRegistryServiceTest { void tradingClearingRegistryNew_moneyAccount_clearingAccount() { TradingClearingRegistryNewRequest tradingClearingRegistryNewRequest = new TradingClearingRegistryNewRequest(); tradingClearingRegistryNewRequest.setCompanyId(companyId); - tradingClearingRegistryNewRequest.setMoneyAccountId(clearingAccountId); + tradingClearingRegistryNewRequest.setMoneyAccountId(accountId); TradingClearingRegistry predictableTradingClearingRegistry = new TradingClearingRegistry(); predictableTradingClearingRegistry.setCompanyId(companyId); predictableTradingClearingRegistry.setCode("0077MAT"); - predictableTradingClearingRegistry.setMoneyAccountId(clearingAccountId); + predictableTradingClearingRegistry.setMoneyAccountId(accountId); predictableTradingClearingRegistry.setTradingClearingRegistryType("CAT"); predictableTradingClearingRegistry.setTradingClearingRegistryPurpose(TradingClearingRegistryPurpose.M.getKey()); predictableTradingClearingRegistry.setStatus(ServiceStatus.Active.getKey()); @@ -191,12 +191,12 @@ class TradingClearingRegistryServiceTest { void tradingClearingRegistryNew_moneyAccount_informationAccount() { TradingClearingRegistryNewRequest tradingClearingRegistryNewRequest = new TradingClearingRegistryNewRequest(); tradingClearingRegistryNewRequest.setCompanyId(companyId); - tradingClearingRegistryNewRequest.setMoneyAccountId(infoAccountId); + tradingClearingRegistryNewRequest.setMoneyAccountId(account2Id); TradingClearingRegistry predictableTradingClearingRegistry = new TradingClearingRegistry(); predictableTradingClearingRegistry.setCompanyId(companyId); predictableTradingClearingRegistry.setCode("0077MAT"); - predictableTradingClearingRegistry.setMoneyAccountId(infoAccountId); + predictableTradingClearingRegistry.setMoneyAccountId(account2Id); predictableTradingClearingRegistry.setTradingClearingRegistryType(TradingClearingRegistryType.Owner_A.getKey()); predictableTradingClearingRegistry.setTradingClearingRegistryPurpose(TradingClearingRegistryPurpose.M.getKey()); predictableTradingClearingRegistry.setStatus(ServiceStatus.Active.getKey()); @@ -228,14 +228,14 @@ class TradingClearingRegistryServiceTest { void tradingClearingRegistryNew_depoAccount() { TradingClearingRegistryNewRequest tradingClearingRegistryNewRequest = new TradingClearingRegistryNewRequest(); tradingClearingRegistryNewRequest.setCompanyId(companyId); - tradingClearingRegistryNewRequest.setMoneyAccountId(clearingAccountId); - tradingClearingRegistryNewRequest.setDepoAccountId(depoAccountId); + tradingClearingRegistryNewRequest.setMoneyAccountId(accountId); + tradingClearingRegistryNewRequest.setDepoAccountId(account2Id); TradingClearingRegistry predictableTradingClearingRegistry = new TradingClearingRegistry(); predictableTradingClearingRegistry.setCompanyId(companyId); predictableTradingClearingRegistry.setCode("0077CDAT"); - predictableTradingClearingRegistry.setMoneyAccountId(clearingAccountId); - predictableTradingClearingRegistry.setDepoAccountId(depoAccountId); + predictableTradingClearingRegistry.setMoneyAccountId(accountId); + predictableTradingClearingRegistry.setDepoAccountId(account2Id); predictableTradingClearingRegistry.setTradingClearingRegistryType("DAT"); predictableTradingClearingRegistry.setTradingClearingRegistryPurpose(TradingClearingRegistryPurpose.C.getKey()); predictableTradingClearingRegistry.setStatus(ServiceStatus.Active.getKey());