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 548618afb..fc4ef7079 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 @@ -250,10 +250,9 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini tradingClearingRegistry.setTradingClearingRegistryPurpose(registryPurpose.getKey()); Company company = companyImdg.getSingleObjectByID(req.getCompanyId()); - String code = "%4s".formatted(company.getClearingCode()).replace(' ', '0'); - code += registryPurpose.getKey(); - if (registryPurpose == TradingClearingRegistryPurpose.C) code += tradingRegistryType + id; - else code += "AT" + id; + Long seqId = companySequenceNextId(req.getCompanyId()); + String code = makeCode(company.getClearingCode(), registryPurpose, tradingRegistryType, seqId); + log.debug("For new TCR.id={} of companyId={} next sequence={}; code={}", id, req.getCompanyId(), seqId, code); tradingClearingRegistry.setCode(code); Instant now = Instant.now(); @@ -323,7 +322,9 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini tradingClearingRegistry.setTradingClearingRegistryPurpose(registryPurpose.getKey()); Company company = companyImdg.getSingleObjectByID(req.getCompanyId()); - String code = makeCode(company.getClearingCode(), registryPurpose, tradingRegistryType, id); + Long seqId = companySequenceNextId(req.getCompanyId()); + String code = makeCode(company.getClearingCode(), registryPurpose, tradingRegistryType, seqId); + log.debug("For new TCR.id={} of companyId={} next sequence={}; code={}", id, req.getCompanyId(), seqId, code); tradingClearingRegistry.setCode(code); Instant now = Instant.now(); @@ -341,6 +342,40 @@ public class TradingClearingRegistryService extends QueueConsumer implements Ini return null; } + protected Long companySequenceNextId(Long companyId) { + Collection existTCR = tradingClearingRegistryImdg.getCollectionObjectsByFieldValues(Map.of( + "companyId", companyId + )); + if (existTCR.isEmpty()) { + log.trace("For company id={} not found exist TCR.", companyId); + return 1L; + } + log.trace("For company id={} found {} exist TCR.", companyId, existTCR.size()); + long maxN = 0; + for (TradingClearingRegistry tcr : existTCR) { + Long codeN = parseCodeSeqId(tcr.getCode()); + if (codeN != null) { + if (maxN < codeN) + maxN = codeN; + } + } + return maxN + 1; + } + + protected Long parseCodeSeqId(String code) { + if (code == null || code.isBlank()) + return null; + try { + String toParse = code.trim(); + if (toParse.length() > 5) + toParse = toParse.substring(toParse.length() - 5); + return Long.parseLong(toParse); + } catch (NumberFormatException nan) { + log.warn("Can not parse number from TCR code \"{}\": {}", code, nan.getMessage()); + } + return null; + } + protected String makeCode(String companyClearingCode, TradingClearingRegistryPurpose registryPurpose, String tradingRegistryType, Long id) { String code; code = companyClearingCode; 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 640fa8105..e9a2760f5 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 @@ -337,4 +337,13 @@ class TradingClearingRegistryServiceTest { "000000105", TradingClearingRegistryPurpose.M, "ER", 100012L )); } + + @Test + void parseCodeSeqId() { + Assertions.assertEquals(Long.valueOf(1L), tradingClearingRegistryService.parseCodeSeqId("0005CAT00001")); + Assertions.assertEquals(Long.valueOf(5001), tradingClearingRegistryService.parseCodeSeqId("0005CAT05001")); + Assertions.assertEquals(null, tradingClearingRegistryService.parseCodeSeqId("0005CAT")); + Assertions.assertEquals(null, tradingClearingRegistryService.parseCodeSeqId("")); + Assertions.assertEquals(null, tradingClearingRegistryService.parseCodeSeqId(null)); + } } \ No newline at end of file