diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoBalanceRegisterReport.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoBalanceRegisterReport.java index 76fa6e752..1df0cdba5 100644 --- a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoBalanceRegisterReport.java +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoBalanceRegisterReport.java @@ -3,6 +3,7 @@ package ru.spcex.clearing.registry.reports.bean; import com.opencsv.bean.CsvBindByName; import com.opencsv.bean.CsvBindByPosition; import com.opencsv.bean.CsvDate; +import com.opencsv.bean.CsvNumber; import java.math.BigDecimal; import java.time.LocalDate; @@ -41,6 +42,7 @@ public class DepoBalanceRegisterReport implements CSVReport { @CsvBindByName(column = "Количество ЦБ") @CsvBindByPosition(position = 7) + @CsvNumber(value = "#0.00", writeFormat = "#0.00") private BigDecimal sum; public String getHouseName() { diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoPaymentInstructionRegisterReport.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoPaymentInstructionRegisterReport.java new file mode 100644 index 000000000..540f3bfb7 --- /dev/null +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/DepoPaymentInstructionRegisterReport.java @@ -0,0 +1,148 @@ +package ru.spcex.clearing.registry.reports.bean; + +import com.opencsv.bean.CsvBindByName; +import com.opencsv.bean.CsvBindByPosition; +import com.opencsv.bean.CsvDate; +import com.opencsv.bean.CsvNumber; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.time.LocalTime; + +public class DepoPaymentInstructionRegisterReport implements CSVReport { + @CsvBindByName(column = "Номер распоряжения") + @CsvBindByPosition(position = 0) + private Long number; + + @CsvBindByName(column = "Дата распоряжения") + @CsvBindByPosition(position = 1) + @CsvDate(value = "dd.MM.yyyy") + private LocalDate date; + + @CsvBindByName(column = "Время формирования") + @CsvBindByPosition(position = 2) + @CsvDate(value = "HH:mm:ss") + private LocalTime time; + + @CsvBindByName(column = "Номер торгового банковского/клирингового счета списания") + @CsvBindByPosition(position = 3) + private String accountSender; + + @CsvBindByName(column = "Наименование владельца счета списания") + @CsvBindByPosition(position = 4) + private String sender; + + @CsvBindByName(column = "Код владельца счета списания") + @CsvBindByPosition(position = 5) + private String codeSender; + + @CsvBindByName(column = "Номер торгового банковского/клирингового счета зачисления") + @CsvBindByPosition(position = 6) + private String accountAddressee; + + @CsvBindByName(column = "Наименование владельца счета зачисления") + @CsvBindByPosition(position = 7) + private String addressee; + + @CsvBindByName(column = "Код владельца счета зачисления") + @CsvBindByPosition(position = 8) + private String codeAddressee; + + @CsvBindByName(column = "Код ЦБ") + @CsvBindByPosition(position = 9) + private String security; + + @CsvBindByName(column = "Количество ЦБ") + @CsvBindByPosition(position = 10) + @CsvNumber(value = "#0.00", writeFormat = "#0.00") + private BigDecimal amount; + + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public LocalTime getTime() { + return time; + } + + public void setTime(LocalTime time) { + this.time = time; + } + + public String getAccountSender() { + return accountSender; + } + + public void setAccountSender(String accountSender) { + this.accountSender = accountSender; + } + + public String getSender() { + return sender; + } + + public void setSender(String sender) { + this.sender = sender; + } + + public String getCodeSender() { + return codeSender; + } + + public void setCodeSender(String codeSender) { + this.codeSender = codeSender; + } + + public String getAccountAddressee() { + return accountAddressee; + } + + public void setAccountAddressee(String accountAddressee) { + this.accountAddressee = accountAddressee; + } + + public String getAddressee() { + return addressee; + } + + public void setAddressee(String addressee) { + this.addressee = addressee; + } + + public String getCodeAddressee() { + return codeAddressee; + } + + public void setCodeAddressee(String codeAddressee) { + this.codeAddressee = codeAddressee; + } + + public String getSecurity() { + return security; + } + + public void setSecurity(String security) { + this.security = security; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } +} diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyBalanceRegisterReport.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyBalanceRegisterReport.java index 0be588bc2..471f78687 100644 --- a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyBalanceRegisterReport.java +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyBalanceRegisterReport.java @@ -3,6 +3,7 @@ package ru.spcex.clearing.registry.reports.bean; import com.opencsv.bean.CsvBindByName; import com.opencsv.bean.CsvBindByPosition; import com.opencsv.bean.CsvDate; +import com.opencsv.bean.CsvNumber; import java.math.BigDecimal; import java.time.LocalDate; @@ -41,6 +42,7 @@ public class MoneyBalanceRegisterReport implements CSVReport { @CsvBindByName(column = "Остаток денежных средств") @CsvBindByPosition(position = 7) + @CsvNumber(value = "#0.00", writeFormat = "#0.00") private BigDecimal sum; diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyPaymentInstructionRegisterReport.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyPaymentInstructionRegisterReport.java new file mode 100644 index 000000000..73d505bfc --- /dev/null +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/bean/MoneyPaymentInstructionRegisterReport.java @@ -0,0 +1,148 @@ +package ru.spcex.clearing.registry.reports.bean; + +import com.opencsv.bean.CsvBindByName; +import com.opencsv.bean.CsvBindByPosition; +import com.opencsv.bean.CsvDate; +import com.opencsv.bean.CsvNumber; + +import java.math.BigDecimal; +import java.time.LocalDate; +import java.time.LocalTime; + +public class MoneyPaymentInstructionRegisterReport implements CSVReport { + @CsvBindByName(column = "Номер распоряжения") + @CsvBindByPosition(position = 0) + private Long number; + + @CsvBindByName(column = "Дата распоряжения") + @CsvBindByPosition(position = 1) + @CsvDate(value = "dd.MM.yyyy") + private LocalDate date; + + @CsvBindByName(column = "Время формирования") + @CsvBindByPosition(position = 2) + @CsvDate(value = "HH:mm:ss") + private LocalTime time; + + @CsvBindByName(column = "Номер торгового банковского/клирингового счета списания") + @CsvBindByPosition(position = 3) + private String accountSender; + + @CsvBindByName(column = "Наименование владельца счета списания") + @CsvBindByPosition(position = 4) + private String sender; + + @CsvBindByName(column = "Код владельца счета списания") + @CsvBindByPosition(position = 5) + private String codeSender; + + @CsvBindByName(column = "Номер торгового банковского/клирингового счета зачисления") + @CsvBindByPosition(position = 6) + private String accountAddressee; + + @CsvBindByName(column = "Наименование владельца счета зачисления") + @CsvBindByPosition(position = 7) + private String addressee; + + @CsvBindByName(column = "Код владельца счета зачисления") + @CsvBindByPosition(position = 8) + private String codeAddressee; + + @CsvBindByName(column = "Код валюты") + @CsvBindByPosition(position = 9) + private String currencyCode; + + @CsvBindByName(column = "Сумма денежных средств") + @CsvBindByPosition(position = 10) + @CsvNumber(value = "#0.00", writeFormat = "#0.00") + private BigDecimal sum; + + + public Long getNumber() { + return number; + } + + public void setNumber(Long number) { + this.number = number; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public LocalTime getTime() { + return time; + } + + public void setTime(LocalTime time) { + this.time = time; + } + + public String getAccountSender() { + return accountSender; + } + + public void setAccountSender(String accountSender) { + this.accountSender = accountSender; + } + + public String getSender() { + return sender; + } + + public void setSender(String sender) { + this.sender = sender; + } + + public String getCodeSender() { + return codeSender; + } + + public void setCodeSender(String codeSender) { + this.codeSender = codeSender; + } + + public String getAccountAddressee() { + return accountAddressee; + } + + public void setAccountAddressee(String accountAddressee) { + this.accountAddressee = accountAddressee; + } + + public String getAddressee() { + return addressee; + } + + public void setAddressee(String addressee) { + this.addressee = addressee; + } + + public String getCodeAddressee() { + return codeAddressee; + } + + public void setCodeAddressee(String codeAddressee) { + this.codeAddressee = codeAddressee; + } + + public String getCurrencyCode() { + return currencyCode; + } + + public void setCurrencyCode(String currencyCode) { + this.currencyCode = currencyCode; + } + + public BigDecimal getSum() { + return sum; + } + + public void setSum(BigDecimal sum) { + this.sum = sum; + } +} diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/builders/DepoPaymentInstructionRegisterReportBuilder.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/builders/DepoPaymentInstructionRegisterReportBuilder.java new file mode 100644 index 000000000..be84e51b9 --- /dev/null +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/builders/DepoPaymentInstructionRegisterReportBuilder.java @@ -0,0 +1,82 @@ +package ru.spcex.clearing.registry.reports.builders; + +import ru.clearing.classes.statics.data.account.Account; +import ru.clearing.classes.statics.data.company.Company; +import ru.clearing.classes.statics.data.payment.PaymentInstruction; +import ru.clearing.classes.statics.data.security.Security; +import ru.spcex.clearing.imdg.IMDGDistributedNames; +import ru.spcex.clearing.registry.reports.bean.DepoPaymentInstructionRegisterReport; +import ru.spcex.platform.imdg.api.Imdg; +import ru.spcex.platform.imdg.api.ImdgProvider; + +import java.time.LocalTime; +import java.time.ZoneId; + +public class DepoPaymentInstructionRegisterReportBuilder extends CSVReportBuilder { + + private final Imdg accountImdg; + private final Imdg companyImdg; + private final Imdg securityImdg; + + public DepoPaymentInstructionRegisterReportBuilder(ImdgProvider imdgProvider) { + companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class); + accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); + securityImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Security, Security.class); + } + + @Override + protected String reportName() { + return "depo_payment_instruction_register"; + } + + @Override + protected Class getReportClass() { + return DepoPaymentInstructionRegisterReport.class; + } + + @Override + protected DepoPaymentInstructionRegisterReport toRow(PaymentInstruction input) { + DepoPaymentInstructionRegisterReport entity = new DepoPaymentInstructionRegisterReport(); + + entity.setNumber(input.getId()); + entity.setDate(input.getClearingDate()); + + if (input.getCreated() != null) entity.setTime(LocalTime.from(input.getCreated().atZone(ZoneId.systemDefault()))); + + if (input.getCreditLeg_accountId() != null) { + Account account = accountImdg.getSingleObjectByID(input.getCreditLeg_accountId()); + if (account != null) entity.setAccountSender(account.getAccount()); + } + + if (input.getSenderId() != null) { + Company company = companyImdg.getSingleObjectByID(input.getSenderId()); + if (company != null) { + entity.setSender(company.getShortName()); + entity.setCodeSender(company.getClearingCode()); + } + } + + if (input.getDebitLeg_accountId() != null) { + Account account = accountImdg.getSingleObjectByID(input.getDebitLeg_accountId()); + if (account != null) entity.setAccountAddressee(account.getAccount()); + } + + if (input.getAddresseeId() != null) { + Company company = companyImdg.getSingleObjectByID(input.getAddresseeId()); + if (company != null) { + entity.setAddressee(company.getShortName()); + entity.setCodeAddressee(company.getClearingCode()); + } + } + + + if (input.getCreditLeg_securityId() != null) { + Security security = securityImdg.getSingleObjectByID(input.getCreditLeg_securityId()); + if (security != null) entity.setSecurity(security.getSecuritySymbol()); + } + entity.setAmount(input.getCreditLeg_amount()); + + return entity; + } + +} diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/builders/MoneyPaymentInstructionRegisterReportBuilder.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/builders/MoneyPaymentInstructionRegisterReportBuilder.java new file mode 100644 index 000000000..2f4e7ba68 --- /dev/null +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/reports/builders/MoneyPaymentInstructionRegisterReportBuilder.java @@ -0,0 +1,75 @@ +package ru.spcex.clearing.registry.reports.builders; + +import ru.clearing.classes.statics.data.account.Account; +import ru.clearing.classes.statics.data.company.Company; +import ru.clearing.classes.statics.data.payment.PaymentInstruction; +import ru.spcex.clearing.imdg.IMDGDistributedNames; +import ru.spcex.clearing.registry.reports.bean.MoneyPaymentInstructionRegisterReport; +import ru.spcex.platform.imdg.api.Imdg; +import ru.spcex.platform.imdg.api.ImdgProvider; + +import java.time.LocalTime; +import java.time.ZoneId; + +public class MoneyPaymentInstructionRegisterReportBuilder extends CSVReportBuilder { + + private final Imdg accountImdg; + private final Imdg companyImdg; + + public MoneyPaymentInstructionRegisterReportBuilder(ImdgProvider imdgProvider) { + companyImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class); + accountImdg = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); + } + + @Override + protected String reportName() { + return "money_payment_instruction_register"; + } + + @Override + protected Class getReportClass() { + return MoneyPaymentInstructionRegisterReport.class; + } + + @Override + protected MoneyPaymentInstructionRegisterReport toRow(PaymentInstruction input) { + MoneyPaymentInstructionRegisterReport entity = new MoneyPaymentInstructionRegisterReport(); + + entity.setNumber(input.getId()); + entity.setDate(input.getClearingDate()); + + if (input.getCreated() != null) entity.setTime(LocalTime.from(input.getCreated().atZone(ZoneId.systemDefault()))); + + if (input.getCreditLeg_accountId() != null) { + Account account = accountImdg.getSingleObjectByID(input.getCreditLeg_accountId()); + if (account != null) entity.setAccountSender(account.getAccount()); + } + + if (input.getSenderId() != null) { + Company company = companyImdg.getSingleObjectByID(input.getSenderId()); + if (company != null) { + entity.setSender(company.getShortName()); + entity.setCodeSender(company.getClearingCode()); + } + } + + if (input.getDebitLeg_accountId() != null) { + Account account = accountImdg.getSingleObjectByID(input.getDebitLeg_accountId()); + if (account != null) entity.setAccountAddressee(account.getAccount()); + } + + if (input.getAddresseeId() != null) { + Company company = companyImdg.getSingleObjectByID(input.getAddresseeId()); + if (company != null) { + entity.setAddressee(company.getShortName()); + entity.setCodeAddressee(company.getClearingCode()); + } + } + + entity.setCurrencyCode(input.getCreditLeg_currencyCode()); + entity.setSum(input.getCreditLeg_amount()); + + return entity; + } + +} diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterService.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterService.java index 3082faa86..0c0c7230c 100644 --- a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterService.java +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterService.java @@ -6,6 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import ru.clearing.classes.statics.data.account.Account; import ru.clearing.classes.statics.data.misc.Session; @@ -17,6 +18,10 @@ import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.platform.messaging.domain.BaseRequest; import ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest; import ru.spcex.clearing.platform.messaging.service.QueueConsumer; +import ru.spcex.clearing.registry.config.SFTPReportsConfig; +import ru.spcex.clearing.registry.config.settings.RegistryServiceSettings; +import ru.spcex.clearing.registry.reports.ReportUtils; +import ru.spcex.clearing.registry.reports.builders.DepoPaymentInstructionRegisterReportBuilder; import ru.spcex.clearing.registry.util.PreClearMap; import ru.spcex.platform.enumeration.AccountType; import ru.spcex.platform.enumeration.TransactionStatus; @@ -24,11 +29,9 @@ import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.utils.validation.IValidator; +import java.io.File; import java.time.Instant; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Function; import static ru.spcex.platform.enumeration.Task.createRegistry_GORD; @@ -47,11 +50,22 @@ public class DepoPaymentInstructionRegisterService extends QueueConsumer impleme private final Function, IValidator> fieldValuesValidator; private final PreClearMap preClearMap; + private final DepoPaymentInstructionRegisterReportBuilder reportBuilder; + private final File outFolder; + private final RegistryServiceSettings settings; + + private final SFTPReportsConfig.ReportsGateway reportsSftpGateway; + private final boolean deleteAfterSend; + + @Autowired public DepoPaymentInstructionRegisterService(Consumer kafkaQueue, Producer kafkaProducer, ImdgProvider imdgProvider, - Function, IValidator> fieldValuesValidator) { + Function, IValidator> fieldValuesValidator, + @Qualifier("reportOutDir") File outFolder, + RegistryServiceSettings settings, + SFTPReportsConfig.ReportsGateway reportsSftpGateway) { super(kafkaQueue, kafkaProducer); this.depoPaymentInstructionRegisterMap = imdgProvider.getImdg(IMDGDistributedNames.Map_DepoPaymentInstructionRegister, DepoPaymentInstructionRegister.class); this.paymentInstructionMap = imdgProvider.getImdg(IMDGDistributedNames.Map_PaymentInstruction, PaymentInstruction.class); @@ -61,6 +75,11 @@ public class DepoPaymentInstructionRegisterService extends QueueConsumer impleme this.accountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); this.fieldValuesValidator = fieldValuesValidator; preClearMap = PreClearMap.instanceForInstantField(depoPaymentInstructionRegisterMap, "created"); + reportBuilder = new DepoPaymentInstructionRegisterReportBuilder(imdgProvider); + this.outFolder = outFolder; + this.settings = settings; + this.reportsSftpGateway = reportsSftpGateway; + this.deleteAfterSend = settings.getReportsStore().isDeleteAfterSend(); } @Override @@ -73,6 +92,7 @@ public class DepoPaymentInstructionRegisterService extends QueueConsumer impleme public void depoPaymentInstructionRegisterNew(BaseRequest userRequest) { + boolean csvMode = settings.isCsvMode(); log.debug("LauncherCommandRequest received from {}", createRegistry_GORR.topic()); String piQuery = "transactionStatus='" + TransactionStatus.sent.getKey() + "' OR transactionStatus = '" + @@ -97,12 +117,18 @@ public class DepoPaymentInstructionRegisterService extends QueueConsumer impleme } paymentInstructions = null; log.debug("Filtered {} PaymentInstruction, with depo account's", depoPaymentInstructions.size()); - preClearMap.preClearMap(); + if (!csvMode) preClearMap.preClearMap(); + List paymentInstructionsForReport = new ArrayList<>(); for (PaymentInstruction paymentInstruction : depoPaymentInstructions) { if (isDuplicateInMap(paymentInstruction).isEmpty()) { - insertDepoPaymentInstructionRegister(paymentInstruction); + if (!csvMode) insertDepoPaymentInstructionRegister(paymentInstruction); + else paymentInstructionsForReport.add(paymentInstruction); } } + if (csvMode) { + File reportFile = reportBuilder.buildReport(paymentInstructionsForReport, outFolder); + ReportUtils.sendFilesToSftp(List.of(reportFile), reportsSftpGateway, deleteAfterSend); + } log.debug("successfully processed"); } diff --git a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterService.java b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterService.java index 879212841..77fc92399 100644 --- a/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterService.java +++ b/clearing-parent/registry-service/src/main/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterService.java @@ -6,6 +6,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import ru.clearing.classes.statics.data.account.Account; import ru.clearing.classes.statics.data.company.Company; @@ -16,6 +17,10 @@ import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.platform.messaging.domain.BaseRequest; import ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest; import ru.spcex.clearing.platform.messaging.service.QueueConsumer; +import ru.spcex.clearing.registry.config.SFTPReportsConfig; +import ru.spcex.clearing.registry.config.settings.RegistryServiceSettings; +import ru.spcex.clearing.registry.reports.ReportUtils; +import ru.spcex.clearing.registry.reports.builders.MoneyPaymentInstructionRegisterReportBuilder; import ru.spcex.clearing.registry.util.PreClearMap; import ru.spcex.platform.enumeration.AccountType; import ru.spcex.platform.enumeration.TransactionStatus; @@ -24,11 +29,9 @@ import ru.spcex.platform.imdg.api.ImdgProvider; import ru.spcex.platform.utils.enumeration.IEnumKey; import ru.spcex.platform.utils.validation.IValidator; +import java.io.File; import java.time.Instant; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.function.Function; import static ru.spcex.platform.enumeration.Task.createRegistry_GORR; @@ -45,11 +48,22 @@ public class MoneyPaymentInstructionRegisterService extends QueueConsumer implem private final Imdg accountMap; private final PreClearMap preClearMap; + private final MoneyPaymentInstructionRegisterReportBuilder reportBuilder; + private final File outFolder; + private final RegistryServiceSettings settings; + + private final SFTPReportsConfig.ReportsGateway reportsSftpGateway; + private final boolean deleteAfterSend; + + @Autowired public MoneyPaymentInstructionRegisterService(Consumer kafkaQueue, Producer kafkaProducer, ImdgProvider imdgProvider, - Function, IValidator> fieldValuesValidator) { + Function, IValidator> fieldValuesValidator, + @Qualifier("reportOutDir") File outFolder, + RegistryServiceSettings settings, + SFTPReportsConfig.ReportsGateway reportsSftpGateway) { super(kafkaQueue, kafkaProducer); this.moneyPaymentInstructionRegisterMap = imdgProvider.getImdg(IMDGDistributedNames.Map_MoneyPaymentInstructionRegister, MoneyPaymentInstructionRegister.class); this.paymentInstructionMap = imdgProvider.getImdg(IMDGDistributedNames.Map_PaymentInstruction, PaymentInstruction.class); @@ -58,6 +72,11 @@ public class MoneyPaymentInstructionRegisterService extends QueueConsumer implem this.accountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); this.fieldValuesValidator = fieldValuesValidator; preClearMap = PreClearMap.instanceForInstantField(moneyPaymentInstructionRegisterMap, "created"); + reportBuilder = new MoneyPaymentInstructionRegisterReportBuilder(imdgProvider); + this.outFolder = outFolder; + this.settings = settings; + this.reportsSftpGateway = reportsSftpGateway; + this.deleteAfterSend = settings.getReportsStore().isDeleteAfterSend(); } @Override @@ -69,6 +88,7 @@ public class MoneyPaymentInstructionRegisterService extends QueueConsumer implem } public void moneyPaymentInstructionRegisterNew(BaseRequest userRequest) { + boolean csvMode = settings.isCsvMode(); log.debug("LauncherCommandRequest received from {}", TOPIC_NAME); String piQuery = "transactionStatus='" + TransactionStatus.sent.getKey() + "' OR transactionStatus = '" + @@ -94,12 +114,18 @@ public class MoneyPaymentInstructionRegisterService extends QueueConsumer implem paymentInstructions = null; log.debug("Filtered {} PaymentInstruction, with money account's", moneyPaymentInstructions.size()); - preClearMap.preClearMap(); + if (!csvMode) preClearMap.preClearMap(); + List paymentInstructionsForReport = new ArrayList<>(); for (PaymentInstruction paymentInstruction : moneyPaymentInstructions) { if (isDuplicateInMap(paymentInstruction).isEmpty()) { - insertMoneyPaymentInstructionRegister(paymentInstruction); + if (!csvMode) insertMoneyPaymentInstructionRegister(paymentInstruction); + else paymentInstructionsForReport.add(paymentInstruction); } } + if (csvMode) { + File reportFile = reportBuilder.buildReport(paymentInstructionsForReport, outFolder); + ReportUtils.sendFilesToSftp(List.of(reportFile), reportsSftpGateway, deleteAfterSend); + } log.debug("successfully processed"); } diff --git a/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterServiceTest.java b/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterServiceTest.java index 21227154c..a123d3f5c 100644 --- a/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterServiceTest.java +++ b/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/DepoPaymentInstructionRegisterServiceTest.java @@ -1,10 +1,12 @@ package ru.spcex.clearing.registry.service; +import com.opencsv.exceptions.CsvValidationException; import org.apache.kafka.clients.consumer.MockConsumer; import org.apache.kafka.clients.producer.Producer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; @@ -17,7 +19,10 @@ import ru.clearing.classes.statics.data.registry.TradingClearingRegistry; import ru.clearing.platform.dictionary.InOutDirectionDictionary; import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest; +import ru.spcex.clearing.registry.config.SFTPTestConfig; +import ru.spcex.clearing.registry.config.TestConfig; import ru.spcex.clearing.registry.config.ValidationConfig; +import ru.spcex.clearing.registry.config.settings.RegistryServiceSettings; import ru.spcex.clearing.test.TestUtils; import ru.spcex.clearing.test.config.ImdgTestConfig; import ru.spcex.clearing.test.config.KafkaTestConfig; @@ -27,8 +32,10 @@ import ru.spcex.platform.enumeration.TransactionStatus; import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.ImdgProvider; -import javax.annotation.PostConstruct; +import java.io.File; +import java.io.IOException; import java.math.BigDecimal; +import java.util.Arrays; import java.util.Collection; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -39,8 +46,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; DepoPaymentInstructionRegisterService.class, ImdgTestConfig.class, KafkaTestConfig.class, + TestConfig.class, + SFTPTestConfig.class, }) -class DepoPaymentInstructionRegisterServiceTest { +class DepoPaymentInstructionRegisterServiceTest implements InitializingBean { private Imdg depoPaymentInstructionRegisterMap; private Imdg paymentInstructionMap; private Imdg sessionMap; @@ -61,6 +70,9 @@ class DepoPaymentInstructionRegisterServiceTest { @Autowired private DepoPaymentInstructionRegisterService depoPaymentInstructionRegisterService; + @Autowired + private RegistryServiceSettings settings; + @Autowired @Qualifier("hazelcastServiceTest") private ImdgProvider imdgProvider; @@ -69,9 +81,12 @@ class DepoPaymentInstructionRegisterServiceTest { @Qualifier("mockProducer") private Producer producer; + @Autowired + @Qualifier("reportOutDir") + private File outFolder; - @PostConstruct - private void init() { + @Override + public void afterPropertiesSet() throws Exception { ImdgTestConfig.waitAvailableImdgProviderAndAddAdminWithDefaultId(); //initializing of imdg @@ -83,11 +98,6 @@ class DepoPaymentInstructionRegisterServiceTest { accountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); // initialing objects for imdg repos - } - - @Test - @Order(1) - void executionRegisterNewForClearingDate() { Session session = new Session(); session.setSection(SESSION_SECTION); session.setId(SESSION_ID); @@ -119,7 +129,13 @@ class DepoPaymentInstructionRegisterServiceTest { account.setId(TRADING_CLEARING_REGISTRY_DEPO_ACCOUNT_ID); account.setAccountType(AccountType.Depo.getKey()); accountMap.update(account); + } + @Test + @Order(1) + void executionRegisterNewForClearingDate() { + boolean oldCsvMode = settings.isCsvMode(); + settings.setCsvMode(false); //making launcherCommand LauncherCommandRequest launcherCommandRequest = new LauncherCommandRequest(); @@ -142,5 +158,36 @@ class DepoPaymentInstructionRegisterServiceTest { assertEquals(TRADING_CLEARING_REGISTRY_CODE, depoPaymentInstructionRegister.getTradingClearingRegistry()); assertEquals(IN_OUT_DIRECTION_DICTIONARY_ID, depoPaymentInstructionRegister.getDirection()); assertEquals(SET_CREDIT_LEG_AMOUNT, depoPaymentInstructionRegister.getQuantity()); + + settings.setCsvMode(oldCsvMode); + } + + @Test + @Order(2) + void executionRegisterNewForClearingDate_CSV() throws CsvValidationException, IOException { + ReportTestUtils.resetDir(outFolder); + boolean oldCsvMode = settings.isCsvMode(); + settings.setCsvMode(true); + + //making launcherCommand + LauncherCommandRequest launcherCommandRequest = new LauncherCommandRequest(); + + String jsonString = TestUtils.getJsonStringForSystem(launcherCommandRequest, 0L); + + TestUtils.addRecordToKafka((MockConsumer) depoPaymentInstructionRegisterService.getConsumer(), + Task.createRegistry_GORD.topic(), + requestCount++, + 0, + jsonString); + + TestUtils.waitingSendAndCheckRecord(0L, producer); + + File outFile = outFolder.listFiles()[0]; + File expectedFile = new File(getClass().getClassLoader().getResource("expected_reports/depo_payment_instruction_register_expected.csv").getFile()); + ReportTestUtils.compareCSVFiles(expectedFile, outFile, Arrays.asList("Время формирования", "Дата распоряжения")); + + settings.setCsvMode(oldCsvMode); + + ReportTestUtils.deleteDir(outFolder); } } \ No newline at end of file diff --git a/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyBalanceRegisterServiceTest.java b/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyBalanceRegisterServiceTest.java index 285a14b08..597d7f739 100644 --- a/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyBalanceRegisterServiceTest.java +++ b/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyBalanceRegisterServiceTest.java @@ -163,6 +163,7 @@ class MoneyBalanceRegisterServiceTest implements InitializingBean { @Test @Order(2) void executionRegisterNewForClearingDate_CSV() throws CsvValidationException, IOException { + ReportTestUtils.resetDir(outFolder); RegistryHistory source = init(); boolean oldCsvMode = settings.isCsvMode(); settings.setCsvMode(true); diff --git a/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterServiceTest.java b/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterServiceTest.java index a2b2b1c50..4fedfe052 100644 --- a/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterServiceTest.java +++ b/clearing-parent/registry-service/src/test/java/ru/spcex/clearing/registry/service/MoneyPaymentInstructionRegisterServiceTest.java @@ -1,10 +1,12 @@ package ru.spcex.clearing.registry.service; +import com.opencsv.exceptions.CsvValidationException; import org.apache.kafka.clients.consumer.MockConsumer; import org.apache.kafka.clients.producer.Producer; import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; @@ -16,7 +18,10 @@ import ru.clearing.classes.statics.data.payment.PaymentInstruction; import ru.clearing.classes.statics.data.register.MoneyPaymentInstructionRegister; import ru.spcex.clearing.imdg.IMDGDistributedNames; import ru.spcex.clearing.platform.messaging.domain.cud.schedule.LauncherCommandRequest; +import ru.spcex.clearing.registry.config.SFTPTestConfig; +import ru.spcex.clearing.registry.config.TestConfig; import ru.spcex.clearing.registry.config.ValidationConfig; +import ru.spcex.clearing.registry.config.settings.RegistryServiceSettings; import ru.spcex.clearing.test.TestUtils; import ru.spcex.clearing.test.config.ImdgTestConfig; import ru.spcex.clearing.test.config.KafkaTestConfig; @@ -26,8 +31,11 @@ import ru.spcex.platform.enumeration.TransactionStatus; import ru.spcex.platform.imdg.api.Imdg; import ru.spcex.platform.imdg.api.ImdgProvider; -import javax.annotation.PostConstruct; +import java.io.File; +import java.io.IOException; import java.math.BigDecimal; +import java.time.Instant; +import java.util.Arrays; import java.util.Collection; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -38,8 +46,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; MoneyPaymentInstructionRegisterService.class, ImdgTestConfig.class, KafkaTestConfig.class, + TestConfig.class, + SFTPTestConfig.class, }) -class MoneyPaymentInstructionRegisterServiceTest { +class MoneyPaymentInstructionRegisterServiceTest implements InitializingBean { private static int requestCount = 0; private Imdg moneyPaymentInstructionRegisterMap; private Imdg paymentInstructionMap; @@ -65,19 +75,22 @@ class MoneyPaymentInstructionRegisterServiceTest { @Autowired private MoneyPaymentInstructionRegisterService moneyPaymentInstructionRegisterService; - @PostConstruct - private void init() { + @Autowired + @Qualifier("reportOutDir") + private File outFolder; + + @Autowired + private RegistryServiceSettings settings; + + @Override + public void afterPropertiesSet() throws Exception { ImdgTestConfig.waitAvailableImdgProviderAndAddAdminWithDefaultId(); companyMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Company, Company.class); moneyPaymentInstructionRegisterMap = imdgProvider.getImdg(IMDGDistributedNames.Map_MoneyPaymentInstructionRegister, MoneyPaymentInstructionRegister.class); paymentInstructionMap = imdgProvider.getImdg(IMDGDistributedNames.Map_PaymentInstruction, PaymentInstruction.class); sessionMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Session, Session.class); accountMap = imdgProvider.getImdg(IMDGDistributedNames.Map_Account, Account.class); - } - @Test - @Order(1) - void executionRegisterNewForClearingDate() { Long sessionId = 1L; Long companySenderId = 1L; Long companyAddresseeId = 2L; @@ -95,6 +108,7 @@ class MoneyPaymentInstructionRegisterServiceTest { paymentInstruction.setSenderId(companySenderId); paymentInstruction.setAddresseeId(companyAddresseeId); paymentInstruction.setTransactionStatus(TransactionStatus.sent.getKey()); + paymentInstruction.setCreated(Instant.now()); paymentInstructionMap.insert(paymentInstruction); Company companySender = new Company(); companySender.setId(companySenderId); @@ -109,6 +123,15 @@ class MoneyPaymentInstructionRegisterServiceTest { account.setId(CREDITLEG_ACCOUNT_ID_CONST); account.setAccountType(AccountType.Clrn.getKey()); accountMap.update(account); + } + + + + @Test + @Order(1) + void executionRegisterNewForClearingDate() throws CsvValidationException, IOException { + boolean oldCsvMode = settings.isCsvMode(); + settings.setCsvMode(false); //making launcherCommand LauncherCommandRequest launcherCommandRequest = new LauncherCommandRequest(); @@ -131,5 +154,34 @@ class MoneyPaymentInstructionRegisterServiceTest { assertEquals(DEBITLEG_ACCOUNT_CONST, moneyPaymentInstructionRegister.getDebitLegAccount()); assertEquals(COMPANY_SHORT_NAME_SENDER_CONST, moneyPaymentInstructionRegister.getSender()); assertEquals(COMPANY_SHORT_NAME_ADDRESSEE_CONST, moneyPaymentInstructionRegister.getAddressee()); + + settings.setCsvMode(oldCsvMode); + } + + @Test + @Order(2) + void executionRegisterNewForClearingDate_CSV() throws CsvValidationException, IOException { + ReportTestUtils.resetDir(outFolder); + boolean oldCsvMode = settings.isCsvMode(); + settings.setCsvMode(true); + //making launcherCommand + LauncherCommandRequest launcherCommandRequest = new LauncherCommandRequest(); + + String jsonString = TestUtils.getJsonStringForSystem(launcherCommandRequest, 0L); + + TestUtils.addRecordToKafka((MockConsumer) moneyPaymentInstructionRegisterService.getConsumer(), + Task.createRegistry_GORR.topic(), + requestCount++, + 0, + jsonString); + + TestUtils.waitingSendAndCheckRecord(0L, producer); + + File outFile = outFolder.listFiles()[0]; + File expectedFile = new File(getClass().getClassLoader().getResource("expected_reports/money_payment_instruction_register_expected.csv").getFile()); + ReportTestUtils.compareCSVFiles(expectedFile, outFile, Arrays.asList("Время формирования", "Дата распоряжения")); + + settings.setCsvMode(oldCsvMode); + ReportTestUtils.deleteDir(outFolder); } } \ No newline at end of file diff --git a/clearing-parent/registry-service/src/test/resources/expected_reports/depo_payment_instruction_register_expected.csv b/clearing-parent/registry-service/src/test/resources/expected_reports/depo_payment_instruction_register_expected.csv new file mode 100644 index 000000000..5413578c9 --- /dev/null +++ b/clearing-parent/registry-service/src/test/resources/expected_reports/depo_payment_instruction_register_expected.csv @@ -0,0 +1,2 @@ +"Номер распоряжения","Дата распоряжения","Время формирования","Номер торгового банковского/клирингового счета списания","Наименование владельца счета списания","Код владельца счета списания","Номер торгового банковского/клирингового счета зачисления","Наименование владельца счета зачисления","Код владельца счета зачисления","Код ЦБ","Количество ЦБ" +"1",,,,,,,,,,"1.00" diff --git a/clearing-parent/registry-service/src/test/resources/expected_reports/money_payment_instruction_register_expected.csv b/clearing-parent/registry-service/src/test/resources/expected_reports/money_payment_instruction_register_expected.csv new file mode 100644 index 000000000..e1f78d0bd --- /dev/null +++ b/clearing-parent/registry-service/src/test/resources/expected_reports/money_payment_instruction_register_expected.csv @@ -0,0 +1,2 @@ +"Номер распоряжения","Дата распоряжения","Время формирования","Номер торгового банковского/клирингового счета списания","Наименование владельца счета списания","Код владельца счета списания","Номер торгового банковского/клирингового счета зачисления","Наименование владельца счета зачисления","Код владельца счета зачисления","Код валюты","Сумма денежных средств" +"1",,"12:48:14",,"COMPANY_SHORT_NAME_SENDER_CONST",,,"COMPANY_SHORT_NAME_ADDRESSEE_CONST",,"CREDITLEG_CURRENCYCODE_CONST","1.00"