You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
175 lines
7.1 KiB
175 lines
7.1 KiB
|
1 week ago
|
package common.ecommerce.edayun.adapter;
|
||
|
|
|
||
|
|
import common.ecommerce.edayun.dto.EdayunAccountFlowDtos;
|
||
|
|
import common.ecommerce.warehouse.enums.WarehouseFeeType;
|
||
|
|
import common.ecommerce.warehouse.model.BillingDocument;
|
||
|
|
import common.ecommerce.warehouse.model.FeeQuery;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
|
||
|
|
import java.math.BigDecimal;
|
||
|
|
import java.time.Instant;
|
||
|
|
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 交易明细({@code POST /webApi/accountFlow/list})映射。
|
||
|
|
*
|
||
|
|
* <p>重点:不按单号去重、金额是 string 可为负、无业务单号时时间段必填且 ≤180 天。</p>
|
||
|
|
*/
|
||
|
|
class EdayunFeeTest {
|
||
|
|
|
||
|
|
// ------------------------------------------------------------ 请求
|
||
|
|
|
||
|
|
/** 有业务单号时不强制时间段。 */
|
||
|
|
@Test
|
||
|
|
void bizNoAloneIsEnough() {
|
||
|
|
EdayunAccountFlowDtos.ListFlowRequest req = EdayunFeeMapper.toRequest(
|
||
|
|
FeeQuery.builder().externalOrderNo("ORD25037232211").build());
|
||
|
|
|
||
|
|
assertEquals("ORD25037232211", req.bizNo);
|
||
|
|
assertNull(req.beginBillingTime);
|
||
|
|
assertEquals(Integer.valueOf(1), req.pageNum);
|
||
|
|
assertEquals(Integer.valueOf(200), req.pageSize);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 没业务单号时必须给时间段。 */
|
||
|
|
@Test
|
||
|
|
void withoutBizNoRangeIsRequired() {
|
||
|
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||
|
|
() -> EdayunFeeMapper.toRequest(FeeQuery.builder().build()));
|
||
|
|
assertTrue(ex.getMessage().contains("bizNo or a billing time range"), ex.getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 没业务单号时时间段不能超过 180 天。 */
|
||
|
|
@Test
|
||
|
|
void rangeOver180DaysWithoutBizNoIsRejected() {
|
||
|
|
FeeQuery tooLong = FeeQuery.builder()
|
||
|
|
.settledAfter(Instant.parse("2026-01-01T00:00:00Z"))
|
||
|
|
.settledBefore(Instant.parse("2026-12-31T00:00:00Z"))
|
||
|
|
.build();
|
||
|
|
|
||
|
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||
|
|
() -> EdayunFeeMapper.toRequest(tooLong));
|
||
|
|
assertTrue(ex.getMessage().contains("180"), ex.getMessage());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 有业务单号时时间段可以长一些(文档只说「没有业务单号时」有 180 天限制)。 */
|
||
|
|
@Test
|
||
|
|
void rangeOver180DaysAllowedWithBizNo() {
|
||
|
|
EdayunAccountFlowDtos.ListFlowRequest req = EdayunFeeMapper.toRequest(FeeQuery.builder()
|
||
|
|
.externalOrderNo("ORD-1")
|
||
|
|
.settledAfter(Instant.parse("2026-01-01T00:00:00Z"))
|
||
|
|
.settledBefore(Instant.parse("2026-12-31T00:00:00Z"))
|
||
|
|
.build());
|
||
|
|
|
||
|
|
assertEquals("ORD-1", req.bizNo);
|
||
|
|
assertEquals(Long.valueOf(Instant.parse("2026-01-01T00:00:00Z").toEpochMilli()),
|
||
|
|
req.beginBillingTime);
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 时间是毫秒。 */
|
||
|
|
@Test
|
||
|
|
void rangeIsInMillis() {
|
||
|
|
EdayunAccountFlowDtos.ListFlowRequest req = EdayunFeeMapper.toRequest(FeeQuery.builder()
|
||
|
|
.settledAfter(Instant.parse("2026-11-01T00:00:00Z"))
|
||
|
|
.settledBefore(Instant.parse("2026-11-10T00:00:00Z"))
|
||
|
|
.build());
|
||
|
|
|
||
|
|
assertEquals(Long.valueOf(Instant.parse("2026-11-10T00:00:00Z").toEpochMilli()),
|
||
|
|
req.endBillingTime);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ------------------------------------------------------------ 响应
|
||
|
|
|
||
|
|
private static EdayunAccountFlowDtos.FlowRow chargeRow() {
|
||
|
|
EdayunAccountFlowDtos.FlowRow row = new EdayunAccountFlowDtos.FlowRow();
|
||
|
|
row.serialNumber = "LF-1741871641090";
|
||
|
|
row.relateBusinessCode = "ORD25037232211";
|
||
|
|
row.poNumber = "P-20261112";
|
||
|
|
row.operateType = 0;
|
||
|
|
row.accountType = 0;
|
||
|
|
row.billingName = "运费附加费计费项-偏远附加费";
|
||
|
|
row.billingCode = "farawayFee";
|
||
|
|
row.originalCurrency = "USD";
|
||
|
|
row.billingAmount = "-0.500";
|
||
|
|
row.accountCurrency = "USD";
|
||
|
|
row.exchangeRate = 1.1;
|
||
|
|
row.accountBalance = 998869.12;
|
||
|
|
row.billingTime = 1741145821000L;
|
||
|
|
return row;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void mapsDocument() {
|
||
|
|
BillingDocument doc = EdayunFeeMapper.toDocument(chargeRow(), "EDY-WH-1");
|
||
|
|
|
||
|
|
assertEquals("ORD25037232211", doc.documentNo());
|
||
|
|
assertEquals("LF-1741871641090", doc.serialNumber());
|
||
|
|
assertEquals("运费附加费计费项-偏远附加费", doc.feeName());
|
||
|
|
assertEquals(Instant.ofEpochMilli(1741145821000L), doc.settledAt());
|
||
|
|
assertEquals("P-20261112", doc.saleOrderNo());
|
||
|
|
// 交易明细是已发生账目,不是预估
|
||
|
|
assertEquals(Boolean.FALSE, doc.estimated());
|
||
|
|
assertEquals(new BigDecimal("998869.12"),
|
||
|
|
doc.accountBalance().amount());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 金额是 string 且可为负——扣费是负数,不能取绝对值。 */
|
||
|
|
@Test
|
||
|
|
void amountKeepsSign() {
|
||
|
|
BillingDocument charge = EdayunFeeMapper.toDocument(chargeRow(), "W");
|
||
|
|
assertEquals(new BigDecimal("-0.500"), charge.amount().amount());
|
||
|
|
assertEquals("USD", charge.amount().currency().getCurrencyCode());
|
||
|
|
|
||
|
|
EdayunAccountFlowDtos.FlowRow refund = chargeRow();
|
||
|
|
refund.billingAmount = "12.30";
|
||
|
|
assertEquals(new BigDecimal("12.30"),
|
||
|
|
EdayunFeeMapper.toDocument(refund, "W").amount().amount());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 币种优先账户币种,缺失才用原币。 */
|
||
|
|
@Test
|
||
|
|
void amountCurrencyPrefersAccountCurrency() {
|
||
|
|
EdayunAccountFlowDtos.FlowRow row = chargeRow();
|
||
|
|
row.accountCurrency = "CNY";
|
||
|
|
row.originalCurrency = "USD";
|
||
|
|
|
||
|
|
assertEquals("CNY", EdayunFeeMapper.toDocument(row, "W").amount().currency().getCurrencyCode());
|
||
|
|
|
||
|
|
row.accountCurrency = null;
|
||
|
|
assertEquals("USD", EdayunFeeMapper.toDocument(row, "W").amount().currency().getCurrencyCode());
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 计费项按编码关键字推断;认不出返回 null,不猜。 */
|
||
|
|
@Test
|
||
|
|
void feeTypeIsInferredFromCode() {
|
||
|
|
assertEquals(WarehouseFeeType.STORAGE, EdayunFeeMapper.feeTypeOf("storageFee"));
|
||
|
|
assertEquals(WarehouseFeeType.OPERATION, EdayunFeeMapper.feeTypeOf("handlingFee"));
|
||
|
|
assertEquals(WarehouseFeeType.LAST_MILE, EdayunFeeMapper.feeTypeOf("farawayFee"));
|
||
|
|
assertEquals(WarehouseFeeType.VALUE_ADDED, EdayunFeeMapper.feeTypeOf("labelServiceFee"));
|
||
|
|
// 认不出不猜
|
||
|
|
assertNull(EdayunFeeMapper.feeTypeOf("someUnknownCode"));
|
||
|
|
assertNull(EdayunFeeMapper.feeTypeOf(null));
|
||
|
|
}
|
||
|
|
|
||
|
|
/** 同一单号的同一计费项多条流水——每条都要保留,不能去重。 */
|
||
|
|
@Test
|
||
|
|
void multipleRowsForSameOrderAreKept() {
|
||
|
|
EdayunAccountFlowDtos.FlowRow a = chargeRow();
|
||
|
|
a.serialNumber = "LF-1";
|
||
|
|
EdayunAccountFlowDtos.FlowRow b = chargeRow();
|
||
|
|
b.serialNumber = "LF-2";
|
||
|
|
|
||
|
|
BillingDocument d1 = EdayunFeeMapper.toDocument(a, "W");
|
||
|
|
BillingDocument d2 = EdayunFeeMapper.toDocument(b, "W");
|
||
|
|
|
||
|
|
// 单号与计费项都相同,但流水号不同 → 是两笔
|
||
|
|
assertEquals(d1.documentNo(), d2.documentNo());
|
||
|
|
assertEquals(d1.feeName(), d2.feeName());
|
||
|
|
assertTrue(!d1.serialNumber().equals(d2.serialNumber()));
|
||
|
|
}
|
||
|
|
}
|