From add44fca4fb93f010669f3d42284b97b3d5a088d Mon Sep 17 00:00:00 2001 From: AKurakin Date: Tue, 16 Apr 2024 18:13:34 +0300 Subject: [PATCH] account-service http://jira.mfd.msk:8088/browse/CLS-585 ClearingAccountService SDF52 --- .../service/ClearingAccountService.java | 89 +++++++++++++++++-- 1 file changed, 81 insertions(+), 8 deletions(-) diff --git a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/ClearingAccountService.java b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/ClearingAccountService.java index 4d98310d8..6b41bca55 100644 --- a/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/ClearingAccountService.java +++ b/clearing-parent/account-service/src/main/java/ru/spcex/clearing/account/service/ClearingAccountService.java @@ -46,6 +46,7 @@ import ru.spcex.platform.imdg.api.predicate.ImdgPredicate; import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder; import ru.spcex.platform.utils.collection.Pair; import ru.spcex.platform.utils.enumeration.EnumMessage; +import ru.spcex.platform.utils.enumeration.IEnumKey; import ru.spcex.platform.utils.enumeration.IErrorEnumId; import ru.spcex.platform.utils.enumeration.IMessageResolver; import ru.spcex.platform.utils.validation.IValidator; @@ -135,7 +136,7 @@ public class ClearingAccountService extends QueueConsumer implements Initializin init(); } - public RequestInfoUpdate clearingAccountNew(BaseRequest userRequest) { + public synchronized RequestInfoUpdate clearingAccountNew(BaseRequest userRequest) { RequestInfoUpdate requestInfoUpdate = validationHelper.validateTillFirstError( userRequest, clearingAccountNewRequestValidator ); @@ -194,7 +195,7 @@ public class ClearingAccountService extends QueueConsumer implements Initializin return null; } - public RequestInfoUpdate clearingAccountUpdate(BaseRequest userRequest) { + public synchronized RequestInfoUpdate clearingAccountUpdate(BaseRequest userRequest) { RequestInfoUpdate requestInfoUpdate = validationHelper.validateTillFirstError( userRequest, clearingAccountUpdateRequestValidator ); @@ -241,7 +242,7 @@ public class ClearingAccountService extends QueueConsumer implements Initializin } - public RequestInfoUpdate accountNewSdf01(BaseRequest userRequest) { + public synchronized RequestInfoUpdate accountNewSdf01(BaseRequest userRequest) { log.debug("AccountSdf01Request received, id={}", userRequest.getId()); AccountSdf01Request req = userRequest.getRequestPayload(); @@ -333,6 +334,66 @@ public class ClearingAccountService extends QueueConsumer implements Initializin return null; } + /** + * Логика похожа на SDF01 + * + * @param accountValue account + * @param companyId + * @param userRequestId для лога + * @return + */ + private synchronized Account createSdf52Account(String accountValue, String acctType, Long companyId, Long userRequestId) { + Instant now = Instant.now(); + Account account = new Account(); + account.setAccount(accountValue); + account.setStatus(ServiceStatus.Active.getKey()); + if (accountValue.length() >= 7) { + String currency = accountValue.substring(4, 3); + account.setCurrency(currency); + } else { + log.warn("Sdf52: Wrong accountValue=\"{}\" - can not parse for get currency", accountValue); + } + account.setCompanyId(companyId); + account.setCreated(now); + account.setUpdated(now); + RequestInfoUpdate requestInfoUpdate = accountService.fillAccountFromRelation(account, userRequestId, true); + if (requestInfoUpdate != null) { + log.warn("Error fill new account from relation. {}", /*account.getId(),*/ requestInfoUpdate.getMessage()); + return null; + } + + Account existAccount = accountImdg.getFirstObjectBySQL("account = '%s' and accountType='%s'" + .formatted(accountValue, AccountType.Clrn.getKey())); + if (existAccount != null) { + log.debug("Sdf52: Account {} already exists", accountValue); + if (companyId.equals(existAccount.getCompanyId())) { + return existAccount; + } else { + log.debug("Sdf52: Account {} already exists for other companyId={}, do not apply for companyId={}", + accountValue, existAccount.getCompanyId(), companyId); + return null; + } + } + + Long clearingAccountId = -1L; + Long accountId = -1L; + ClearingAccount clearingAccount = null; + accountId = accountImdg.insert(account); + + clearingAccount = new ClearingAccount(); + clearingAccount.setCompanyId(companyId); + clearingAccount.setAccountId(accountId); + ClearingAccountType caType = IEnumKey.getEnumByKey(ClearingAccountType.class, acctType); + if (caType == null) { + log.warn("Wrong ClearingAccountType=\"{}\" for account \"{}\"", acctType, accountValue); + } + clearingAccount.setClearingAccountType(acctType); + clearingAccountId = clearingAccountImdg.insert(clearingAccount); + log.debug("New account {}, clearingAccount {} was created.", accountId, clearingAccountId); + + return account; + } + public RequestInfoUpdate accountUpdateSdf52(BaseRequest systemRequest) { log.debug("accountUpdateSdf52 StatementRequest received, id={}", systemRequest.getId()); @@ -376,8 +437,17 @@ public class ClearingAccountService extends QueueConsumer implements Initializin Account account = sDf52.getAccount() == null ? null : accountImdg.getFirstObjectByFieldValues(accountQuery); if (account == null) { if (SDFProcessService.SDF52_STATUS_3Open.equals(sDf52.getStatus())) { - log.debug("By generationId={} s_df52[{}].status={}, but account not found (query: {}). COntinuse with result OK for status 3", + //log.debug("By generationId={} s_df52[{}].status={}, but account not found (query: {}). Continue with result OK for status 3", + // groupId, sDf52.getId(), sDf52.getStatus(), accountQuery); + // todo http://jira.mfd.msk:8088/browse/CLS-585 добавят поле acc_type в формат дф52 + log.trace("By generationId={} s_df52[{}].status={}, account not found (query: {}). Try create new account.", groupId, sDf52.getId(), sDf52.getStatus(), accountQuery); + account = createSdf52Account(sDf52.getAccount(),sDf52.getAcc_type(), company.getId(), systemRequest.getId()); + if (account == null) { + log.warn("Can not create account: \"{}\", companyId={}. Ignore SDF52.id={}", + sDf52.getAccount(), company.getId(), sDf52.getId()); + continue; + } } else { String msg = messageResolver.resolve(new EnumMessage(AccountError.AccountNotFound, sDf52.getAccount())); log.warn("By generationId={} s_df52[{}] (query: {}) error: {}", groupId, sDf52.getId(), accountQuery, msg); @@ -387,7 +457,9 @@ public class ClearingAccountService extends QueueConsumer implements Initializin } } toProcessSDF53.add(new MutableTriple<>(sDf52, account, SDFProcessService.SDF_STATUS_OK)); - toUpdate.add(new Pair<>(sDf52, account)); + if (account != null) { + toUpdate.add(new Pair<>(sDf52, account)); + } } } log.debug("Selected to update {} account's", toUpdate.size()); @@ -403,6 +475,7 @@ public class ClearingAccountService extends QueueConsumer implements Initializin } if (newStatus.equalsByKey(account.getStatus())) { // одинаковых обычно не бывает. + log.trace("In sDF_52[{}] for account [{}] status {} not changed.", sdf.getId(), account.getId(), newStatus); continue; } if (AccountStatus.BLOCKED == newStatus || AccountStatus.CLOSE == newStatus) { // статус 0/2 @@ -446,9 +519,9 @@ public class ClearingAccountService extends QueueConsumer implements Initializin } } - protected RequestInfoUpdate accountUpdateSdf52_part2(BaseRequest systemRequest1, - SDf52 sdf, Account account, - BaseRequest secondSystemRequest2) { + protected synchronized RequestInfoUpdate accountUpdateSdf52_part2(BaseRequest systemRequest1, + SDf52 sdf, Account account, + BaseRequest secondSystemRequest2) { // 5. from notification: StatementRequest req = systemRequest1.getRequestPayload(); Long groupId = req.getGroupId();