feat: 支持快递报价 Excel 导入与标准模板下载

This commit is contained in:
2026-10-22 14:21:11 +08:00
parent 49bbaa7362
commit 3f8f066a7c
7 changed files with 246 additions and 21 deletions
@@ -81,9 +81,9 @@ public class ExpressQuoteImportService {
draft.setFormat("CARRIER_MATRIX");
}
List<ExpressQuoteImportModels.RegionDraft> raw =
ExpressQuoteMatrixSheetParser.parse(quoteSheet, draft.getErrors());
ExpressQuoteMatrixSheetParser.parse(quoteSheet, draft.getErrors(), draft.getWarnings());
if (raw.isEmpty() && draft.getErrors().isEmpty()) {
draft.getErrors().add("未解析到任何目的地报价行");
draft.getErrors().add("未解析到任何目的地报价行(可解析行均为空或已跳过)");
}
List<ExpressQuoteImportModels.RegionDraft> merged = ExpressQuoteRegionMerger.merge(raw);
draft.setRegions(merged);
@@ -15,7 +15,7 @@ import java.util.Optional;
/**
* 解析承运商矩阵 / 标准模板「报价」页:支持同 Sheet 多段表头。
* 空单元格=该重量带跳过;非空且不可解析=记入 errors(调用方整单失败)。
* 空单元格 / 不可解析单元格 / 无有效带的行 → warning 并跳过,不拖垮整单。
*/
public final class ExpressQuoteMatrixSheetParser {
@@ -24,7 +24,8 @@ public final class ExpressQuoteMatrixSheetParser {
private ExpressQuoteMatrixSheetParser() {
}
public static List<ExpressQuoteImportModels.RegionDraft> parse(Sheet sheet, List<String> errors) {
public static List<ExpressQuoteImportModels.RegionDraft> parse(
Sheet sheet, List<String> errors, List<String> warnings) {
List<ExpressQuoteImportModels.RegionDraft> rows = new ArrayList<>();
if (sheet == null) {
errors.add("缺少报价工作表");
@@ -38,7 +39,6 @@ public final class ExpressQuoteMatrixSheetParser {
continue;
}
String c0 = cellText(row, 0);
String c1 = cellText(row, 1);
// 偏远区域行:内嵌新重量表头
if (isSectionMarker(c0) && hasWeightHeaders(row, 2)) {
@@ -78,7 +78,7 @@ public final class ExpressQuoteMatrixSheetParser {
continue;
}
ExpressQuoteImportModels.RegionDraft region = parseDataRow(row, columns, errors, r + 1);
ExpressQuoteImportModels.RegionDraft region = parseDataRow(row, columns, warnings, r + 1);
if (region != null) {
rows.add(region);
}
@@ -87,7 +87,8 @@ public final class ExpressQuoteMatrixSheetParser {
}
private static ExpressQuoteImportModels.RegionDraft parseDataRow(
Row row, List<ExpressQuoteImportModels.ColumnDef> columns, List<String> errors, int excelRow) {
Row row, List<ExpressQuoteImportModels.ColumnDef> columns,
List<String> warnings, int excelRow) {
String destRaw = cellText(row, 0);
if (!StringUtils.hasText(destRaw)) {
return null;
@@ -110,16 +111,16 @@ public final class ExpressQuoteMatrixSheetParser {
raw, col.getDefaultPriceMode(), col.getWeightFromG(), col.getWeightToG());
band.ifPresent(bands::add);
} catch (PriceCellParser.UnparseablePriceException ex) {
errors.add("第" + excelRow + "行「" + destRaw + "」列" + (col.getColIndex() + 1)
+ "无法解析: " + ex.getMessage());
warnings.add("第" + excelRow + "行「" + destRaw + "」列" + (col.getColIndex() + 1)
+ "已跳过(无法解析): " + ex.getMessage());
}
}
if (bands.isEmpty() && errors.stream().noneMatch(e -> e.contains("第" + excelRow + "行"))) {
errors.add("第" + excelRow + "行「" + destRaw + "」无任何有效重量带价格");
if (bands.isEmpty()) {
warnings.add("第" + excelRow + "行「" + destRaw + "」无有效重量带,已跳过该目的地");
return null;
}
region.setBands(bands);
return bands.isEmpty() ? null : region;
return region;
}
private static String normalizeDestination(String raw) {
@@ -99,7 +99,7 @@ public final class ExpressQuoteTemplateWriter {
"2. 报价页为矩阵:目的省 + 价区 + 各重量列;同价区且价格相同会自动合并。",
"3. 重量表头按字面转克,如 0.51-1KG → 510–1000g。",
"4. 单元格支持数字(元/票)或「2+0.7元/kg」(首重+续重);空单元格表示该带无报价。",
"5. 含「发/转单」等业务分支文案无法解析,将导致整单失败。",
"5. 「发极兔/转单」等分支文案、无价格的目的地行会跳过并给出警告,不拖垮整单。",
"6. 也可直接上传承运商多段矩阵表(无 Meta 时在提交时补全编码/名称/承运商/生效日)。",
};
for (int i = 0; i < lines.length; i++) {
@@ -26,14 +26,22 @@ class ExpressQuoteMatrixSheetParserTest {
try (InputStream in = Files.newInputStream(path); Workbook wb = WorkbookFactory.create(in)) {
Sheet sheet = wb.getSheetAt(0);
List<String> errors = new ArrayList<>();
List<String> warnings = new ArrayList<>();
List<ExpressQuoteImportModels.RegionDraft> raw =
ExpressQuoteMatrixSheetParser.parse(sheet, errors);
ExpressQuoteMatrixSheetParser.parse(sheet, errors, warnings);
List<ExpressQuoteImportModels.RegionDraft> merged = ExpressQuoteRegionMerger.merge(raw);
assertFalse(merged.isEmpty(), "应解析到分区");
// 样表含「发极兔/转单」等文案 → 应有错误
assertTrue(errors.stream().anyMatch(e -> e.contains("无法解析") || e.contains("发")),
"应对不可解析单元格报错: " + errors);
// 0.51-1KG → 510
assertTrue(errors.isEmpty(), "跳过策略下不应产生硬错误: " + errors);
assertTrue(warnings.stream().anyMatch(w -> w.contains("跳过")),
"应对发极兔/空行给出跳过警告: " + warnings);
boolean hasNeimeng = merged.stream()
.flatMap(r -> r.getDestinations().stream())
.anyMatch(d -> d.contains("内蒙古"));
assertTrue(hasNeimeng, "内蒙古应保留可解析重量带");
boolean hasXizang = merged.stream()
.flatMap(r -> r.getDestinations().stream())
.anyMatch(d -> d.contains("西藏"));
assertFalse(hasXizang, "西藏无价格应整行跳过");
boolean has510 = merged.stream()
.flatMap(r -> r.getBands().stream())
.anyMatch(b -> Integer.valueOf(510).equals(b.getWeightFromG()));
@@ -0,0 +1,198 @@
<script lang="ts" setup>
import type { ExpressQuoteVO } from '#/api/ec/express-quote';
import { computed, ref } from 'vue';
import {
Collapse,
CollapsePanel,
Descriptions,
DescriptionsItem,
Drawer,
Empty,
Spin,
Table,
Tag,
message,
} from 'ant-design-vue';
import { getExpressQuoteDetailApi } from '#/api/ec/express-quote';
defineOptions({ name: 'ExpressQuoteDetailDrawer' });
const MODE_LABEL: Record<string, string> = {
FLAT: '一口价',
PER_KG: '按千克',
REGION_WEIGHT: '分区×重量带',
};
const openFlag = ref(false);
const loading = ref(false);
const detail = ref<ExpressQuoteVO.Info | null>(null);
const versions = computed(() => detail.value?.versions ?? []);
const bandColumns = [
{
title: '重量(克)',
key: 'weight',
customRender: ({ record }: { record: ExpressQuoteVO.Band }) =>
formatWeightRange(record.weightFromG, record.weightToG),
},
{
title: '方式',
key: 'priceMode',
customRender: ({ record }: { record: ExpressQuoteVO.Band }) =>
record.priceMode === 'FIRST_PLUS_KG' ? '首重+续重/kg' : '元/票',
},
{
title: '价格',
key: 'price',
customRender: ({ record }: { record: ExpressQuoteVO.Band }) =>
formatBandPrice(record),
},
];
function formatWeightRange(from?: number, to?: number | null) {
const f = from ?? 0;
if (to == null) return `${f}g+`;
return `${f}–${to}g`;
}
function formatBandPrice(b: ExpressQuoteVO.Band) {
if (b.priceMode === 'FIRST_PLUS_KG') {
return `${b.firstAmount ?? 0} + ${b.continuePerKg ?? 0}元/kg(首重${b.firstWeightG ?? 0}g)`;
}
return `${b.flatAmount ?? 0} 元/票`;
}
function money(v?: number | null) {
if (v == null || Number.isNaN(Number(v))) return '—';
return `¥${Number(v).toFixed(2)}`;
}
async function open(id: number) {
openFlag.value = true;
loading.value = true;
detail.value = null;
try {
detail.value = await getExpressQuoteDetailApi(id);
} catch (error) {
console.error(error);
message.error('加载报价详情失败');
openFlag.value = false;
} finally {
loading.value = false;
}
}
function close() {
openFlag.value = false;
}
defineExpose({ open, close });
</script>
<template>
<Drawer
v-model:open="openFlag"
:title="detail ? `报价详情 · ${detail.name}` : '报价详情'"
width="820"
destroy-on-close
>
<Spin :spinning="loading">
<template v-if="detail">
<Descriptions bordered :column="2" size="small" class="mb-4">
<DescriptionsItem label="编码">{{ detail.code }}</DescriptionsItem>
<DescriptionsItem label="承运商">
{{ detail.carrierCode }}
</DescriptionsItem>
<DescriptionsItem label="计费模式">
{{ MODE_LABEL[detail.billingMode] || detail.billingMode }}
</DescriptionsItem>
<DescriptionsItem label="状态">
<Tag :color="detail.status === 1 ? 'green' : 'red'">
{{ detail.status === 1 ? '启用' : '停用' }}
</Tag>
</DescriptionsItem>
<DescriptionsItem label="创建时间">
{{ detail.createTime || '—' }}
</DescriptionsItem>
<DescriptionsItem label="备注" :span="2">
{{ detail.remark || '—' }}
</DescriptionsItem>
</Descriptions>
<div class="mb-2 text-sm font-medium">版本历史(按生效日)</div>
<Empty v-if="!versions.length" description="暂无版本" />
<Collapse v-else accordion :default-active-key="[String(versions[0]?.id)]">
<CollapsePanel
v-for="v in versions"
:key="String(v.id)"
:header="`生效 ${v.effectiveFrom}${v.id === detail.currentVersion?.id ? '(当前)' : ''}`"
>
<Descriptions bordered :column="2" size="small" class="mb-3">
<DescriptionsItem
v-if="detail.billingMode === 'FLAT'"
label="一口价"
>
{{ money(v.flatAmount) }}
({{ v.unitScope === 'PIECE' ? '每件' : '每单' }})
</DescriptionsItem>
<template v-else-if="detail.billingMode === 'PER_KG'">
<DescriptionsItem label="元/kg">
{{ v.pricePerKg ?? '—' }}
</DescriptionsItem>
<DescriptionsItem label="最低消费">
{{ money(v.minAmount) }}
</DescriptionsItem>
</template>
<DescriptionsItem label="体积重">
{{ v.useVolumetric === 1 ? '启用' : '关闭' }}
(除数 {{ v.volumeDivisor ?? 6000 }})
</DescriptionsItem>
<DescriptionsItem label="计费重取整">
{{ v.weightRound === 'CEIL_KG' ? '向上整千克' : '不取整' }}
</DescriptionsItem>
<DescriptionsItem v-if="v.remark" label="版本备注" :span="2">
{{ v.remark }}
</DescriptionsItem>
</Descriptions>
<template v-if="detail.billingMode === 'REGION_WEIGHT'">
<div
v-for="(region, ri) in v.regions || []"
:key="region.id ?? ri"
class="mb-3 rounded border border-border p-3"
>
<div class="mb-2 flex flex-wrap items-center gap-2 text-sm">
<span class="font-medium">{{ region.regionName }}</span>
<Tag v-if="region.zoneLabel" color="blue">
{{ region.zoneLabel }}
</Tag>
<Tag v-if="region.isDefault === 1">默认分区</Tag>
</div>
<div class="mb-2 text-xs text-muted-foreground">
目的地:{{ (region.provinces || []).join('、') || '—' }}
</div>
<Table
size="small"
:pagination="false"
:columns="bandColumns"
:data-source="region.bands || []"
:row-key="
(r: ExpressQuoteVO.Band, i: number) => String(r.id ?? i)
"
/>
</div>
<Empty
v-if="!(v.regions && v.regions.length)"
description="该版本无分区"
/>
</template>
</CollapsePanel>
</Collapse>
</template>
</Spin>
</Drawer>
</template>
@@ -163,7 +163,7 @@ defineExpose({ open });
<Button>选择文件</Button>
</Upload>
<div class="mt-1 text-xs text-muted-foreground">
支持标准模板或承运商矩阵报价单;不可解析单元格将整单失败
支持标准模板或承运商矩阵;「发/转单」文案与无价格行会跳过并警告
</div>
</FormItem>
@@ -181,7 +181,14 @@ defineExpose({ open });
show-icon
class="mb-3"
:message="`可导入:${preview.regionCount} 分区,${preview.destinationCount} 目的地,共 ${preview.bandCount} 条重量带`"
:description="(preview.warnings || []).join('\n') || undefined"
/>
<Alert
v-if="preview?.warnings?.length"
type="warning"
show-icon
class="mb-3 whitespace-pre-wrap"
:message="`已跳过 ${preview.warnings.length} 处,请核对`"
:description="preview.warnings.join('\n')"
/>
<div
@@ -22,6 +22,7 @@ import {
useActionColumnVisible,
} from '#/composables/use-vxe-action-column';
import ExpressQuoteDetailDrawer from './components/ExpressQuoteDetailDrawer.vue';
import ExpressQuoteFormModal from './components/ExpressQuoteFormModal.vue';
import ExpressQuoteImportModal from './components/ExpressQuoteImportModal.vue';
@@ -35,6 +36,9 @@ const formModalRef = ref<InstanceType<typeof ExpressQuoteFormModal> | null>(
const importModalRef = ref<InstanceType<typeof ExpressQuoteImportModal> | null>(
null,
);
const detailDrawerRef = ref<InstanceType<
typeof ExpressQuoteDetailDrawer
> | null>(null);
const searchForm = ref({
keywords: '',
billingMode: undefined as string | undefined,
@@ -43,6 +47,7 @@ const searchForm = ref({
const gridReloadRef = ref<(() => void) | null>(null);
const actionCodes = [
'ec:express-quote:read',
'ec:express-quote:create',
'ec:express-quote:update',
'ec:express-quote:delete',
@@ -67,7 +72,7 @@ const actionColumn: NonNullable<
VxeGridProps<ExpressQuoteVO.Info>['columns']
>[number] = {
title: '操作',
minWidth: 160,
minWidth: 220,
fixed: 'right' as const,
slots: { default: 'action' },
};
@@ -209,6 +214,11 @@ async function handleDelete(row: ExpressQuoteVO.Info) {
#action="{ row }: { row: ExpressQuoteVO.Info }"
>
<Space>
<PermissionGate code="ec:express-quote:read">
<Button size="small" @click="detailDrawerRef?.open(row.id)">
详情
</Button>
</PermissionGate>
<PermissionGate code="ec:express-quote:update">
<Button size="small" @click="formModalRef?.open(true, row)">
编辑/调价
@@ -227,5 +237,6 @@ async function handleDelete(row: ExpressQuoteVO.Info) {
ref="importModalRef"
@success="gridApi.reload()"
/>
<ExpressQuoteDetailDrawer ref="detailDrawerRef" />
</Page>
</template>