feat(order): localize batch import workbook

This commit is contained in:
2026-09-03 16:33:33 +08:00
parent 0307acf859
commit 3664f036cc
6 changed files with 329 additions and 43 deletions
@@ -1,22 +1,93 @@
package com.baiyue.order.service;
import com.baiyue.common.core.api.ApiResponse;
import com.baiyue.common.core.api.PageResult;
import com.baiyue.order.client.MerchantPricingCatalogClient;
import com.baiyue.order.client.MerchantWarehouseCatalogClient;
import com.baiyue.order.dto.CustomerAddressDtos;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class MerchantBatchImportServiceTest {
private final CustomerAddressService addresses = mock(CustomerAddressService.class);
private final MerchantWarehouseCatalogClient warehouses = mock(MerchantWarehouseCatalogClient.class);
private final MerchantPricingCatalogClient pricing = mock(MerchantPricingCatalogClient.class);
private MerchantBatchImportService service;
@BeforeEach
void setUp() {
service = new MerchantBatchImportService();
ReflectionTestUtils.setField(service, "addresses", addresses);
ReflectionTestUtils.setField(service, "warehouses", warehouses);
ReflectionTestUtils.setField(service, "pricing", pricing);
ReflectionTestUtils.setField(service, "json", new ObjectMapper());
when(addresses.list(88L, "PICKUP")).thenReturn(List.of(address(11L, "工厂", "PICKUP")));
when(addresses.list(88L, "PRIVATE_DESTINATION")).thenReturn(List.of(address(12L, "美国门店", "PRIVATE_DESTINATION")));
var domestic = new MerchantWarehouseCatalogClient.Warehouse(21L, "CN-YW", "义乌收货仓", "DOMESTIC", "CN", "ACTIVE");
var overseas = new MerchantWarehouseCatalogClient.Warehouse(22L, "US-LAX", "洛杉矶海外仓", "OVERSEAS", "US", "ACTIVE");
when(warehouses.page("Bearer test", 1, 200, "ACTIVE"))
.thenReturn(ApiResponse.success(new PageResult<>(List.of(domestic, overseas), 1, 200, 2, 1)));
when(pricing.channels("Bearer test", 21L)).thenReturn(ApiResponse.success(List.of(
new MerchantPricingCatalogClient.Channel("US-SEA", "美森海运"))));
}
@Test
void templateUsesStableRowIdentityAndAuthoritativeDraftSchema() throws Exception {
byte[] content = new MerchantBatchImportService().template();
void templateUsesChineseHeadersDropdownsAndSeparateGuideWithoutExampleRow() throws Exception {
byte[] content = service.template(88L, "Bearer test");
try (var workbook = new XSSFWorkbook(new ByteArrayInputStream(content))) {
var sheet = workbook.getSheetAt(0);
assertThat(sheet.getRow(0).getCell(0).getStringCellValue()).isEqualTo("clientRowId");
assertThat(sheet.getRow(0).getCell(11).getStringCellValue()).isEqualTo("senderAddressId");
assertThat(sheet.getRow(0).getCell(21).getStringCellValue()).isEqualTo("privateDestinationAddressId");
assertThat(sheet.getRow(0).getCell(32).getStringCellValue()).isEqualTo("cargo.woodenProduct");
var sheet = workbook.getSheet("订单导入");
assertThat(sheet.getRow(0).getCell(0).getStringCellValue()).isEqualTo("客户行号");
assertThat(sheet.getRow(0).getCell(10).getStringCellValue()).isEqualTo("取货地址");
assertThat(sheet.getRow(0).getCell(11).getStringCellValue()).isEqualTo("国内收货仓");
assertThat(sheet.getRow(0).getCell(29).getStringCellValue()).isEqualTo("是否木制品");
assertThat((Object) sheet.getRow(1)).isNull();
assertThat(sheet.getDataValidations()).hasSizeGreaterThanOrEqualTo(10);
assertThat(workbook.getSheet("填写说明")).isNotNull();
assertThat(workbook.isSheetHidden(workbook.getSheetIndex("选项数据"))).isTrue();
}
}
@Test
void importMapsChineseAndBusinessLabelsToCodesAndAuthoritativeIds() throws Exception {
byte[] content = service.template(88L, "Bearer test");
try (var workbook = new XSSFWorkbook(new ByteArrayInputStream(content)); var out = new ByteArrayOutputStream()) {
var row = workbook.getSheet("订单导入").createRow(1);
row.createCell(0).setCellValue("第1行"); row.createCell(1).setCellValue("测试客户");
row.createCell(2).setCellValue("海运"); row.createCell(8).setCellValue("平台上门提货");
row.createCell(10).setCellValue("工厂|张三|深圳|科技园1号");
row.createCell(11).setCellValue("义乌收货仓|CN-YW"); row.createCell(12).setCellValue("美森海运|US-SEA");
row.createCell(13).setCellValue("买单报关"); row.createCell(14).setCellValue("不包税");
row.createCell(17).setCellValue("私人地址"); row.createCell(18).setCellValue("美国门店|张三|深圳|科技园1号");
row.createCell(23).setCellValue("蓝牙音箱"); row.createCell(24).setCellValue("纸箱");
row.createCell(25).setCellValue("1"); row.createCell(26).setCellValue("1.2"); row.createCell(27).setCellValue("0.1");
row.createCell(29).setCellValue("否"); row.createCell(30).setCellValue("是");
workbook.write(out);
var item = service.parse(88L, "Bearer test", out.toByteArray()).get(0);
assertThat(item.parseError()).isNull();
assertThat(item.draft().transportMode()).isEqualTo("SEA");
assertThat(item.draft().pickupMethod()).isEqualTo("PICKUP");
assertThat(item.draft().senderAddressId()).isEqualTo(11L);
assertThat(item.draft().receivingWarehouseId()).isEqualTo(21L);
assertThat(item.draft().channelCode()).isEqualTo("US-SEA");
assertThat(item.draft().taxMode()).isEqualTo("BUY_DECLARATION");
assertThat(item.draft().cargoes().get(0).packageType()).isEqualTo("CARTON");
assertThat(item.draft().cargoes().get(0).dangerousGoods()).isTrue();
}
}
private static CustomerAddressDtos.Response address(long id, String name, String type) {
return new CustomerAddressDtos.Response(id, type, name, "测试公司", "张三", "13800000000", null,
"CN", "广东", "深圳", "南山", "科技园1号", "518000", true);
}
}