http://jira.mfd.msk:8088/browse/CLS-359 sdf03#sbanknam/rbanknam, sdf03#sum_deb, sdf12#quantity

This commit is contained in:
ialbert 2023-06-26 16:06:36 +03:00
parent 4fa1cbf32a
commit da10e2bd0d
8 changed files with 235 additions and 46 deletions

View file

@ -1,10 +1,11 @@
package ru.spcex.clearing.service;
import java.util.function.Consumer;
public class SpecifUtil {
public static String[] split5SegmentsBy35Symbols(String str) {
if (str == null || str.length() < 1) return new String[0];
int size = divisionRoundUp(str.length(), 35);
size = Math.min(size, 6);
String[] res = new String[size];
for (int i = 0; i < res.length; i++) {
int startIndex = i * 35;
@ -14,6 +15,14 @@ public class SpecifUtil {
return res;
}
@SafeVarargs
public static void fillSegmentsBy35Symbols(String str, Consumer<String>... setters) {
String[] parts = split5SegmentsBy35Symbols(str);
for (int i = 0; i < setters.length && i < parts.length; i++) {
setters[i].accept(parts[i]);
}
}
public static int divisionRoundUp(int a, int b) {
int ostatok = a % b;
return a / b + (ostatok > 0 ? 1 : 0);

View file

@ -6,6 +6,7 @@ import ru.clearing.classes.statics.data.sdf.SDf03;
import ru.spcex.clearing.service.SpecifUtil;
import ru.spcex.platform.enumeration.Sender;
import ru.spcex.platform.imdg.api.Imdg;
import ru.spcex.platform.utils.number.BigDecimalUtil;
import ru.spcex.platform.utils.time.TimeUtil;
import java.time.Instant;
@ -75,7 +76,8 @@ public class Sdf03Builder {
sDf03.setPay_date(paymtDt.format(PROPERTY_DATE_FORMATTER_YY));
}
sDf03.setPay_val("RUR");
sDf03.setSum_deb(paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : null);
String sumDeb = paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : null;
sDf03.setSum_deb(BigDecimalUtil.limitDecimalPlaces(sumDeb, 2));
//37 sp_code varchar(2) Код назначения платежа
String[] splitPaymentPurpose = SpecifUtil.split5SegmentsBy35Symbols(paymentInstruction.getPaymentPurpose());
for (int i = 0; i < splitPaymentPurpose.length; i++) {

View file

@ -18,6 +18,7 @@ import ru.spcex.clearing.platform.messaging.domain.Consts;
import ru.spcex.clearing.platform.messaging.domain.cud.balance.ExportToFileRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.importexport.SwtExporterRequest;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.clearing.service.SpecifUtil;
import ru.spcex.clearing.service.builder.PaymentInstructionBuilder;
import ru.spcex.clearing.session.stage.ISessionStage;
import ru.spcex.clearing.session.stage.StageResult;
@ -33,6 +34,7 @@ import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
import ru.spcex.platform.utils.enumeration.IEnumKey;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
import ru.spcex.platform.utils.number.BigDecimalUtil;
import ru.spcex.platform.utils.time.TimeUtil;
import java.math.BigDecimal;
@ -356,11 +358,12 @@ public class FormingPaymentInstruction implements ISessionStage {
senderSbankName = company.getShortName();
}
}
sDf03.setSbanknam1(senderSbankName);
sDf03.setSbanknam2(senderSbankName);
sDf03.setSbanknam3(senderSbankName);
sDf03.setSbanknam4(senderSbankName);
sDf03.setSbanknam5(senderSbankName);
SpecifUtil.fillSegmentsBy35Symbols(senderSbankName,
sDf03::setSbanknam1,
sDf03::setSbanknam2,
sDf03::setSbanknam3,
sDf03::setSbanknam4,
sDf03::setSbanknam5);
sDf03.setC_acc_cred(paymentInstruction.getDebitLeg_account());
@ -373,15 +376,17 @@ public class FormingPaymentInstruction implements ISessionStage {
addresseeSbankName = company.getShortName();
}
}
sDf03.setRbanknam1(addresseeSbankName);
sDf03.setRbanknam2(addresseeSbankName);
sDf03.setRbanknam3(addresseeSbankName);
sDf03.setRbanknam4(addresseeSbankName);
sDf03.setRbanknam5(addresseeSbankName);
SpecifUtil.fillSegmentsBy35Symbols(addresseeSbankName,
sDf03::setRbanknam1,
sDf03::setRbanknam2,
sDf03::setRbanknam3,
sDf03::setRbanknam4,
sDf03::setRbanknam5);
sDf03.setPay_date(payDateFormatter.format(TimeUtil.toLocalDate(paymentInstruction.getPaymentDate())));
sDf03.setPay_val("RUR");
sDf03.setSum_deb(paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "");
String sumDeb = paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "";
sDf03.setSum_deb(BigDecimalUtil.limitDecimalPlaces(sumDeb, 2));
sDf03.setSpecif_1(paymentInstruction.getPaymentPurpose());
sDf03.setGenerationTime(Instant.now());
sDf03.setPaymentInstructionId(paymentInstruction.getId());
@ -395,7 +400,7 @@ public class FormingPaymentInstruction implements ISessionStage {
sDf12.setId(idGenerator.nextId());
sDf12.setOutDocument(sDf12.getId().toString());
sDf12.setDirection("DELFREE");
sDf12.setQuantity(paymentInstruction.getCreditLeg_amount().toString());
sDf12.setQuantity(BigDecimalUtil.limitDecimalPlaces(paymentInstruction.getCreditLeg_amount().toString(), 2));
sDf12.setSecurityCode(paymentInstruction.getCreditLeg_securityId().toString());
sDf12.setDepoCodeSender(paymentInstruction.getCreditLeg_account());
sDf12.setDepoCodeAdressee(paymentInstruction.getDebitLeg_account());

View file

@ -36,6 +36,7 @@ import ru.spcex.platform.utils.enumeration.EnumMessage;
import ru.spcex.platform.utils.enumeration.IEnumKey;
import ru.spcex.platform.utils.enumeration.IMessageResolver;
import ru.spcex.platform.utils.enumeration.SimpleMessageResolver;
import ru.spcex.platform.utils.number.BigDecimalUtil;
import ru.spcex.platform.utils.time.TimeUtil;
import java.time.Instant;
@ -308,7 +309,8 @@ public class FormingPaymentInstructionDealsFinalMkr implements ISessionStage {
sDf03.setPay_date(payDateFormatter.format(TimeUtil.toLocalDate(paymentInstruction.getPaymentDate())));
sDf03.setPay_val("RUR");
sDf03.setSum_deb(paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "");
String sumDeb = paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "";
sDf03.setSum_deb(BigDecimalUtil.limitDecimalPlaces(sumDeb, 2));
sDf03.setSpecif_1(paymentInstruction.getPaymentPurpose());
sDf03.setGenerationTime(Instant.now());
sDf03.setPaymentInstructionId(paymentInstruction.getId());
@ -322,7 +324,7 @@ public class FormingPaymentInstructionDealsFinalMkr implements ISessionStage {
sDf12.setId(idGenerator.nextId());
sDf12.setOutDocument(sDf12.getId().toString());
sDf12.setDirection("DELFREE");
sDf12.setQuantity(paymentInstruction.getCreditLeg_amount().toString());
sDf12.setQuantity(BigDecimalUtil.limitDecimalPlaces(paymentInstruction.getCreditLeg_amount().toString(), 2));
sDf12.setSecurityCode(paymentInstruction.getCreditLeg_securityId().toString());
sDf12.setDepoCodeSender(paymentInstruction.getCreditLeg_account());
sDf12.setDepoCodeAdressee(paymentInstruction.getDebitLeg_account());

View file

@ -17,6 +17,7 @@ import ru.spcex.clearing.imdg.IMDGDistributedNames;
import ru.spcex.clearing.platform.messaging.domain.Consts;
import ru.spcex.clearing.platform.messaging.domain.cud.balance.ExportToFileRequest;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.clearing.service.SpecifUtil;
import ru.spcex.clearing.service.builder.PaymentInstructionBuilder;
import ru.spcex.clearing.session.stage.ISessionStage;
import ru.spcex.clearing.session.stage.StageResult;
@ -30,6 +31,7 @@ import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
import ru.spcex.platform.utils.enumeration.IEnumKey;
import ru.spcex.platform.utils.number.BigDecimalUtil;
import java.math.BigDecimal;
import java.time.Instant;
@ -327,11 +329,12 @@ public class FormingPaymentInstructionDepositReturn implements ISessionStage {
senderSbankName = company.getShortName();
}
}
sDf03.setSbanknam1(senderSbankName);
sDf03.setSbanknam2(senderSbankName);
sDf03.setSbanknam3(senderSbankName);
sDf03.setSbanknam4(senderSbankName);
sDf03.setSbanknam5(senderSbankName);
SpecifUtil.fillSegmentsBy35Symbols(senderSbankName,
sDf03::setSbanknam1,
sDf03::setSbanknam2,
sDf03::setSbanknam3,
sDf03::setSbanknam4,
sDf03::setSbanknam5);
sDf03.setC_acc_cred(paymentInstruction.getDebitLeg_account());
@ -344,15 +347,17 @@ public class FormingPaymentInstructionDepositReturn implements ISessionStage {
addresseeSbankName = company.getShortName();
}
}
sDf03.setRbanknam1(addresseeSbankName);
sDf03.setRbanknam2(addresseeSbankName);
sDf03.setRbanknam3(addresseeSbankName);
sDf03.setRbanknam4(addresseeSbankName);
sDf03.setRbanknam5(addresseeSbankName);
SpecifUtil.fillSegmentsBy35Symbols(addresseeSbankName,
sDf03::setRbanknam1,
sDf03::setRbanknam2,
sDf03::setRbanknam3,
sDf03::setRbanknam4,
sDf03::setRbanknam5);
sDf03.setPay_date(payDateFormatter.format(paymentInstruction.getPaymentDate()));
sDf03.setPay_val("RUR");
sDf03.setSum_deb(paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "");
String sumDeb = paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "";
sDf03.setSum_deb(BigDecimalUtil.limitDecimalPlaces(sumDeb, 2));
sDf03.setSpecif_1(paymentInstruction.getPaymentPurpose());
sDf03.setGenerationTime(Instant.now());
sDf03.setPaymentInstructionId(paymentInstruction.getId());
@ -366,7 +371,7 @@ public class FormingPaymentInstructionDepositReturn implements ISessionStage {
sDf12.setId(idGenerator.nextId());
sDf12.setOutDocument(sDf12.getId().toString());
sDf12.setDirection("DELFREE");
sDf12.setQuantity(paymentInstruction.getCreditLeg_amount().toString());
sDf12.setQuantity(BigDecimalUtil.limitDecimalPlaces(paymentInstruction.getCreditLeg_amount().toString(), 2));
sDf12.setSecurityCode(paymentInstruction.getCreditLeg_securityId().toString());
sDf12.setDepoCodeSender(paymentInstruction.getCreditLeg_account());
sDf12.setDepoCodeAdressee(paymentInstruction.getDebitLeg_account());

View file

@ -18,6 +18,7 @@ import ru.spcex.clearing.platform.messaging.domain.Consts;
import ru.spcex.clearing.platform.messaging.domain.cud.balance.ExportToFileRequest;
import ru.spcex.clearing.platform.messaging.domain.cud.importexport.SwtExporterRequest;
import ru.spcex.clearing.platform.messaging.service.sender.KafkaSender;
import ru.spcex.clearing.service.SpecifUtil;
import ru.spcex.clearing.service.builder.PaymentInstructionBuilder;
import ru.spcex.clearing.session.stage.ISessionStage;
import ru.spcex.clearing.session.stage.StageResult;
@ -31,6 +32,7 @@ import ru.spcex.platform.imdg.api.predicate.ImdgPredicate;
import ru.spcex.platform.imdg.api.predicate.ImdgPredicateBuilder;
import ru.spcex.platform.imdg.api.predicate.specific.RegistryCodeSqlBuilder;
import ru.spcex.platform.utils.enumeration.IEnumKey;
import ru.spcex.platform.utils.number.BigDecimalUtil;
import ru.spcex.platform.utils.time.TimeUtil;
import java.math.BigDecimal;
@ -268,11 +270,13 @@ public class FormingPaymentInstructionReturnMkr implements ISessionStage {
senderSbankName = company.getShortName();
}
}
sDf03.setSbanknam1(senderSbankName);
sDf03.setSbanknam2(senderSbankName);
sDf03.setSbanknam3(senderSbankName);
sDf03.setSbanknam4(senderSbankName);
sDf03.setSbanknam5(senderSbankName);
SpecifUtil.fillSegmentsBy35Symbols(senderSbankName,
sDf03::setSbanknam1,
sDf03::setSbanknam2,
sDf03::setSbanknam3,
sDf03::setSbanknam4,
sDf03::setSbanknam5
);
sDf03.setC_acc_cred(paymentInstruction.getDebitLeg_account());
@ -285,15 +289,18 @@ public class FormingPaymentInstructionReturnMkr implements ISessionStage {
addresseeSbankName = company.getShortName();
}
}
sDf03.setRbanknam1(addresseeSbankName);
sDf03.setRbanknam2(addresseeSbankName);
sDf03.setRbanknam3(addresseeSbankName);
sDf03.setRbanknam4(addresseeSbankName);
sDf03.setRbanknam5(addresseeSbankName);
SpecifUtil.fillSegmentsBy35Symbols(addresseeSbankName,
sDf03::setRbanknam1,
sDf03::setRbanknam2,
sDf03::setRbanknam3,
sDf03::setRbanknam4,
sDf03::setRbanknam5
);
sDf03.setPay_date(payDateFormatter.format(TimeUtil.toLocalDate(paymentInstruction.getPaymentDate())));
sDf03.setPay_val("RUR");
sDf03.setSum_deb(paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "");
String sumDeb = paymentInstruction.getDebitLeg_amount() != null ? paymentInstruction.getDebitLeg_amount().toString() : "";
sDf03.setSum_deb(BigDecimalUtil.limitDecimalPlaces(sumDeb, 2));
sDf03.setSpecif_1(paymentInstruction.getPaymentPurpose());
sDf03.setGenerationTime(Instant.now());
sDf03.setPaymentInstructionId(paymentInstruction.getId());
@ -307,7 +314,7 @@ public class FormingPaymentInstructionReturnMkr implements ISessionStage {
sDf12.setId(idGenerator.nextId());
sDf12.setOutDocument(sDf12.getId().toString());
sDf12.setDirection("DELFREE");
sDf12.setQuantity(paymentInstruction.getCreditLeg_amount().toString());
sDf12.setQuantity(BigDecimalUtil.limitDecimalPlaces(paymentInstruction.getCreditLeg_amount().toString(), 2));
sDf12.setSecurityCode(paymentInstruction.getCreditLeg_securityId().toString());
sDf12.setDepoCodeSender(paymentInstruction.getCreditLeg_account());
sDf12.setDepoCodeAdressee(paymentInstruction.getDebitLeg_account());

View file

@ -3,6 +3,8 @@ package ru.spcex.clearing.service;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicReference;
class SpecifUtilTest {
@Test
public void testDivideRoundUp() {
@ -61,16 +63,153 @@ class SpecifUtilTest {
"01234567890123456789012345678912345" +
"outside of scope"
);
Assertions.assertEquals(6, splitted.length);
for (String s : splitted) {
Assertions.assertEquals("01234567890123456789012345678912345", s);
Assertions.assertEquals(7, splitted.length);
for (int i = 0; i < splitted.length; i++) {
if (i != 6) {
Assertions.assertEquals("01234567890123456789012345678912345", splitted[i]);
} else {
Assertions.assertEquals("outside of scope", splitted[i]);
}
}
}
// "";
}
@Test
public void testSplitAndFill() {
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
//40 symbols
SpecifUtil.fillSegmentsBy35Symbols("0123456789012345678901234567890123456789", part0::set, part1::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNotNull(part1.get());
Assertions.assertEquals("01234567890123456789012345678901234", part0.get());
Assertions.assertEquals("56789", part1.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
//35 symbols
SpecifUtil.fillSegmentsBy35Symbols("01234567890123456789012345678901234", part0::set, part1::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNull(part1.get());
Assertions.assertEquals("01234567890123456789012345678901234", part0.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
//10 symbols
SpecifUtil.fillSegmentsBy35Symbols("0123456789", part0::set, part1::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNull(part1.get());
Assertions.assertEquals("0123456789", part0.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
//0 symbols
SpecifUtil.fillSegmentsBy35Symbols("", part0::set, part1::set);
Assertions.assertNull(part0.get());
Assertions.assertNull(part1.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
//80 symbols
SpecifUtil.fillSegmentsBy35Symbols("0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789", part0::set, part1::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNotNull(part1.get());
Assertions.assertEquals("01234567890123456789012345678901234", part0.get());
Assertions.assertEquals("56789012345678901234567890123456789", part1.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
AtomicReference<String> part2 = new AtomicReference<>();
//80 symbols
SpecifUtil.fillSegmentsBy35Symbols("0123456789012345678901234567890123456789" +
"0123456789012345678901234567890123456789", part0::set, part1::set, part2::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNotNull(part1.get());
Assertions.assertNotNull(part2.get());
Assertions.assertEquals("01234567890123456789012345678901234", part0.get());
Assertions.assertEquals("56789012345678901234567890123456789", part1.get());
Assertions.assertEquals("0123456789", part2.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
AtomicReference<String> part2 = new AtomicReference<>();
AtomicReference<String> part3 = new AtomicReference<>();
AtomicReference<String> part4 = new AtomicReference<>();
//maximum symbols
SpecifUtil.fillSegmentsBy35Symbols("01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"outside of scope", part0::set, part1::set, part2::set, part3::set, part4::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNotNull(part1.get());
Assertions.assertNotNull(part2.get());
Assertions.assertNotNull(part3.get());
Assertions.assertNotNull(part4.get());
Assertions.assertEquals("01234567890123456789012345678912345", part0.get());
Assertions.assertEquals("01234567890123456789012345678912345", part1.get());
Assertions.assertEquals("01234567890123456789012345678912345", part2.get());
Assertions.assertEquals("01234567890123456789012345678912345", part3.get());
Assertions.assertEquals("01234567890123456789012345678912345", part4.get());
}
{
AtomicReference<String> part0 = new AtomicReference<>();
AtomicReference<String> part1 = new AtomicReference<>();
AtomicReference<String> part2 = new AtomicReference<>();
AtomicReference<String> part3 = new AtomicReference<>();
AtomicReference<String> part4 = new AtomicReference<>();
AtomicReference<String> part5 = new AtomicReference<>();
//> 35*5 symbols but 6 setters
SpecifUtil.fillSegmentsBy35Symbols("01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"01234567890123456789012345678912345" +
"outside of scope", part0::set, part1::set, part2::set, part3::set, part4::set, part5::set);
Assertions.assertNotNull(part0.get());
Assertions.assertNotNull(part1.get());
Assertions.assertNotNull(part2.get());
Assertions.assertNotNull(part3.get());
Assertions.assertNotNull(part4.get());
Assertions.assertNotNull(part5.get());
Assertions.assertEquals("01234567890123456789012345678912345", part0.get());
Assertions.assertEquals("01234567890123456789012345678912345", part1.get());
Assertions.assertEquals("01234567890123456789012345678912345", part2.get());
Assertions.assertEquals("01234567890123456789012345678912345", part3.get());
Assertions.assertEquals("01234567890123456789012345678912345", part4.get());
Assertions.assertEquals("outside of scope", part5.get());
}
}
}

View file

@ -1,5 +1,7 @@
package ru.spcex.platform.utils.number;
import ru.spcex.platform.utils.text.TextUtil;
import java.math.BigDecimal;
public class BigDecimalUtil {
@ -18,6 +20,24 @@ public class BigDecimalUtil {
}
}
/**
* Для кейсов когда есть String поле с десятичной частью, и хотим ограничить число знаков после запятой
*/
public static String limitDecimalPlaces(String number, int decimalPlaces) {
if (number == null) {
return null;
}
if (TextUtil.isEmpty(number)) {
return number;
}
int indexOfDot = number.lastIndexOf('.');
if (indexOfDot == -1 && (indexOfDot = number.lastIndexOf(',')) == -1) {
return number;
}
int maximumIndex = Math.min(number.length(), indexOfDot + decimalPlaces + 1);
return number.substring(0, maximumIndex);
}
public static BigDecimal safeBD(BigDecimal bd) {
return bd == null ? BigDecimal.ZERO : bd;
}