Enhance price set import functionality with preview options and update handling. Added new endpoints for previewing product library pulls and Excel imports, along with support for update-only imports. Updated internationalization messages for import success and error scenarios.

This commit is contained in:
wuweihua
2026-09-30 16:15:44 +08:00
parent 90cde8da4a
commit 6e301ecee7
19 changed files with 1260 additions and 260 deletions
@@ -6,24 +6,55 @@
{{ headerTitle }}
<span class="sub">{{ typeHint }}</span>
</span>
<div class="card-actions">
<el-button size="small" :disabled="!priceSetId || pulling" @click="handlePullLibrary">拉取产品列表</el-button>
<div v-if="hasBuildMode" class="card-actions">
<el-button
v-if="isFixedType(setType)"
v-if="showPullAction"
size="small"
:disabled="(livePersist && !priceSetId) || pulling"
@click="handlePullLibrary"
>拉取产品列表</el-button>
<el-button
v-if="showImportAction"
type="primary"
size="small"
:disabled="!priceSetId || pulling"
:disabled="(livePersist && !priceSetId) || pulling"
@click="openImport"
>导入 Excel 建价</el-button>
>{{ importActionLabel }}</el-button>
</div>
</div>
</template>
<div v-if="!livePersist" class="build-choice">
<p class="build-choice__tip">创建价格方式</p>
<div class="build-choice__cards">
<button
type="button"
class="build-card build-card--pull"
:class="{ selected: isPullBuildMode(priceBuildMode) }"
:disabled="choosingMode"
@click="chooseBuildMode('PULL')"
>
<div class="build-card__title">按产品价建价</div>
<div class="build-card__desc">按产品库基础价生成套餐产品列表,可再导入 Excel 修改价格</div>
</button>
<button
type="button"
class="build-card build-card--import"
:class="{ selected: isImportBuildMode(priceBuildMode) }"
:disabled="!isFixedType(setType) || choosingMode"
@click="chooseBuildMode('IMPORT')"
>
<div class="build-card__title">Excel 导入建价</div>
<div class="build-card__desc">上传威狮 B2C 小包价表,直接生成套餐销售价</div>
</button>
</div>
</div>
<div class="filters">
<el-input v-model="query.keyword" clearable :placeholder="phKeyword" style="width: 180px" />
<el-select v-model="query.taxMode" clearable :placeholder="phTax" style="width: 140px">
<!-- <el-select v-model="query.taxMode" clearable :placeholder="phTax" style="width: 140px">
<el-option v-for="d in bms_tax_mode" :key="d.value" :label="d.label" :value="d.value" />
</el-select>
</el-select> -->
<el-button size="small" @click="noop">查询</el-button>
</div>
@@ -54,6 +85,7 @@
</el-table-column>
</el-table>
<surcharge-picker-dialog
v-model="scVisible"
:title="scTitle"
@@ -87,7 +119,7 @@
<excel-import-dialog
ref="importRef"
title="导入 Excel 建价"
:title="importDialogTitle"
width="560px"
:show-update-support="false"
:import-desc="importDesc"
@@ -122,11 +154,13 @@ import {
formatWeightLabel,
isDiscountType,
isFixedType,
isImportBuildMode,
isPullBuildMode,
setTypeLabel,
surchargeAppliesToRow,
toDetailPayload
} from '../../utils/priceSet'
import { getPriceSet, listSetDetailByProduct, importSetDetailPricesAsync, pullSetDetailsFromLibraryAsync, updateSetDetail, updateSetDetailSurchargeAsync, delSetDetail } from '@/api/price/set'
import { getPriceSet, listSetDetailByProduct, importSetDetailPricesAsync, pullSetDetailsFromLibraryAsync, previewPullFromLibrary, previewImportDetailPrices, updateSetDetail, updateSetDetailSurchargeAsync, delSetDetail } from '@/api/price/set'
const props = defineProps({
details: { type: Array, required: true },
@@ -134,13 +168,16 @@ const props = defineProps({
priceSetId: { type: [Number, String], default: undefined },
psvId: { type: [Number, String], default: undefined },
setType: { type: String, default: 'FIXED' },
priceBuildMode: { type: String, default: undefined },
effectiveTime: { type: String, default: undefined },
versionNo: { type: String, default: '' },
refData: { type: Object, required: true },
headerTitle: { type: String, default: '第 2 步 · 编辑产品价格' }
headerTitle: { type: String, default: '第 2 步 · 编辑产品价格' },
/** true=edit live DB; false=create local draft until finish */
livePersist: { type: Boolean, default: true }
})
const emit = defineEmits(['update:psvId'])
const emit = defineEmits(['update:psvId', 'update:priceBuildMode'])
const { proxy } = getCurrentInstance()
const { bms_tax_mode } = useDict('bms_tax_mode')
const { hydrateRow } = useDetailCascade(props.refData)
@@ -182,14 +219,53 @@ const importProgressPercent = ref(0)
const importProgressMessage = ref('')
const importProgressStatus = ref(undefined)
const progressDialogTitle = ref('正在导入价格')
const importDesc = [
'适用威狮B2C小包价表(含「产品目录」)。',
'按产品+国家+分区(有则匹配)+重量段:命中则更新运费/挂号费,未命中则新增套餐明细。',
'仅固定价套餐可导入;可与「拉取产品列表」二选一或叠加使用。'
]
const hasBuildMode = computed(() => {
if (isPullBuildMode(props.priceBuildMode) || isImportBuildMode(props.priceBuildMode)) return true
// Legacy / already has products: skip choice cards
return (props.products || []).some(p => p && p.productCode)
})
const choosingMode = ref(false)
const isModifyImport = computed(() => {
if (isPullBuildMode(props.priceBuildMode)) return true
if (isImportBuildMode(props.priceBuildMode)) return false
return (props.products || []).some(p => p && p.productCode)
})
const importActionLabel = computed(() =>
isModifyImport.value ? '导入修改价格' : '导入 Excel 建价'
)
const importDialogTitle = computed(() => importActionLabel.value)
const importDesc = computed(() => {
if (isModifyImport.value) {
return [
'适用威狮B2C小包价表(含「产品目录」)。',
'按产品+国家+分区(有则匹配)+重量段匹配已有套餐明细,仅更新运费/挂号费。',
'仅固定价套餐可导入;未匹配行不会新增明细。'
]
}
return [
'适用威狮B2C小包价表(含「产品目录」)。',
'按产品+国家+分区(有则匹配)+重量段:命中则更新运费/挂号费,未命中则新增套餐明细。',
'仅固定价套餐可导入。'
]
})
const typeHint = computed(() => '· 当前:' + setTypeLabel(props.setType))
const emptyTip = '暂无产品,请先拉取产品列表或导入 Excel 建价'
const typeHint = computed(() => {
if (props.livePersist) return ''
if (isPullBuildMode(props.priceBuildMode)) return '· 按产品价建价'
if (isImportBuildMode(props.priceBuildMode)) return '· Excel 导入'
if (hasBuildMode.value) return '· 当前:' + setTypeLabel(props.setType)
return '· 请选择创建价格方式'
})
// PULL: hide pull btn (already pulled), show modify-import.
// IMPORT: show create-import.
const showPullAction = computed(() => false)
const showImportAction = computed(() => hasBuildMode.value && isFixedType(props.setType))
const emptyTip = computed(() => {
if (props.livePersist) return '暂无产品'
if (isPullBuildMode(props.priceBuildMode)) return '暂无产品,请先返回上一步重新拉取或等待拉取完成'
if (isImportBuildMode(props.priceBuildMode)) return '暂无产品,请点击右上角导入 Excel 建价'
return '暂无产品,请先选择创建价格方式'
})
/** Resolve version id: prop first, else reload package current version. */
@@ -220,9 +296,30 @@ function clearDetailCache() {
Object.keys(detailByProduct).forEach(k => delete detailByProduct[k])
}
/** Clear local product/detail draft when create wizard switches build mode. */
function clearLocalDraft() {
clearDetailCache()
if (Array.isArray(props.details)) {
props.details.splice(0, props.details.length)
}
if (Array.isArray(props.products)) {
props.products.splice(0, props.products.length)
}
priceVisible.value = false
editCode.value = ''
editName.value = ''
editRows.value = []
editGroup.value = null
}
async function loadProductDetails(productCode, force) {
if (!productCode) return []
if (!force && detailByProduct[productCode]) return detailByProduct[productCode]
if (!props.livePersist) {
const rows = (props.details || []).filter(d => d && d.productCode === productCode)
detailByProduct[productCode] = rows
return rows
}
const psvId = await resolvePsvId(false)
if (!psvId) return []
const res = await listSetDetailByProduct(psvId, productCode)
@@ -264,8 +361,46 @@ const pulling = ref(false)
function noop() {}
async function chooseBuildMode(mode) {
if (!mode || choosingMode.value || props.livePersist) return
if (mode === props.priceBuildMode) return
choosingMode.value = true
try {
emit('update:priceBuildMode', mode)
} finally {
choosingMode.value = false
}
}
let localKeySeq = 0
/** Apply preview products/details into parent reactive arrays (create defer mode). */
function applyLocalDraft(products, details) {
clearDetailCache()
const hydrated = (details || []).map(d => {
const row = hydrateRow({ ...d }, props.setType)
row.psdId = undefined
row.pspId = undefined
row.priceSetId = undefined
row.psvId = undefined
row._localKey = 'L' + (++localKeySeq)
return row
})
if (Array.isArray(props.details)) {
props.details.splice(0, props.details.length, ...hydrated)
}
if (Array.isArray(props.products)) {
props.products.splice(0, props.products.length, ...((products || []).map(p => ({ ...p }))))
}
hydrated.forEach(d => {
if (!d || !d.productCode) return
if (!detailByProduct[d.productCode]) detailByProduct[d.productCode] = []
detailByProduct[d.productCode].push(d)
})
}
async function openImport() {
if (!props.priceSetId) {
if (props.livePersist && !props.priceSetId) {
proxy.$modal.msgWarning('缺少套餐ID')
return
}
@@ -273,15 +408,16 @@ async function openImport() {
proxy.$modal.msgWarning('仅固定价套餐支持导入')
return
}
const psvId = await resolvePsvId(true)
if (!psvId) {
proxy.$modal.msgWarning('缺少套餐版本,请先保存基本信息')
return
if (props.livePersist) {
const psvId = await resolvePsvId(true)
if (!psvId) {
proxy.$modal.msgWarning('缺少套餐版本,请先保存基本信息')
return
}
}
importRef.value && importRef.value.open()
}
async function runImportAsync(uploadFile) {
const raw = uploadFile && (uploadFile.raw || uploadFile)
if (!raw) {
@@ -293,9 +429,23 @@ async function runImportAsync(uploadFile) {
importProgressMessage.value = '上传并解析中…'
importProgressStatus.value = undefined
try {
if (!props.livePersist) {
importProgressMessage.value = '正在解析价表…'
importProgressPercent.value = 30
const res = await previewImportDetailPrices(raw, {
updateOnly: isModifyImport.value,
details: isModifyImport.value ? (props.details || []) : undefined
})
const data = res.data || {}
const products = isModifyImport.value ? (props.products || []) : (data.products || [])
applyLocalDraft(products, data.details || [])
importProgressPercent.value = 100
importProgressStatus.value = 'success'
return data.message || '导入完成'
}
const { task } = await importSetDetailPricesAsync(
raw,
{ priceSetId: props.priceSetId, psvId: await resolvePsvId(false) },
{ priceSetId: props.priceSetId, psvId: await resolvePsvId(false), updateOnly: isModifyImport.value },
applyBuildProgress
)
importProgressPercent.value = 100
@@ -337,6 +487,7 @@ function applyBuildProgress(t) {
}
async function onImportSuccess() {
if (!props.livePersist) return
await reloadDetailsFromServer()
}
@@ -370,7 +521,7 @@ async function reloadDetailsFromServer() {
}
async function handlePullLibrary() {
if (!props.priceSetId) {
if (props.livePersist && !props.priceSetId) {
proxy.$modal.msgWarning('缺少套餐ID')
return
}
@@ -387,11 +538,11 @@ async function handlePullLibrary() {
await pullAllFromLibrary({ force, silent: false })
}
/** Backend pull product/product_country into prc_price_set_detail (async). */
/** Pull product library: live -> async DB; create defer -> preview into local state. */
async function pullAllFromLibrary(opts = {}) {
const silent = !!opts.silent
const force = !!opts.force || !!opts.replace
if (!props.priceSetId) {
if (props.livePersist && !props.priceSetId) {
if (!silent) proxy.$modal.msgWarning('缺少套餐ID')
return { added: 0 }
}
@@ -402,6 +553,19 @@ async function pullAllFromLibrary(opts = {}) {
importProgressStatus.value = undefined
pulling.value = true
try {
if (!props.livePersist) {
importProgressMessage.value = '正在拉取产品库…'
importProgressPercent.value = 40
const res = await previewPullFromLibrary()
const data = res.data || {}
applyLocalDraft(data.products || [], data.details || [])
importProgressPercent.value = 100
importProgressStatus.value = 'success'
if (!silent) {
proxy.$modal.msgSuccess(data.message || '拉取完成')
}
return { added: (props.products || []).length }
}
const { task } = await pullSetDetailsFromLibraryAsync(
{ priceSetId: props.priceSetId, psvId: await resolvePsvId(false), force },
applyBuildProgress
@@ -495,8 +659,9 @@ function openSurchargeFromDrawer(row) {
}
/** persist one detail row to DB immediately */
/** persist one detail row to DB immediately (edit mode only) */
async function persistDetail(row, withSurcharge) {
if (!props.livePersist) return
if (!row || !row.psdId || !props.priceSetId) return
const payload = toDetailPayload(row)
payload.priceSetId = props.priceSetId
@@ -506,7 +671,12 @@ async function persistDetail(row, withSurcharge) {
/** drawer hover remove surcharge -> write snapshot for this row */
async function onRowSurchargeChange(row) {
if (!row || !row.psdId) return
if (!row) return
if (!props.livePersist) {
proxy.$modal.msgSuccess('附加费已移除(完成时统一落库)')
return
}
if (!row.psdId) return
try {
await persistDetail(row, true)
proxy.$modal.msgSuccess('附加费已移除')
@@ -515,9 +685,14 @@ async function onRowSurchargeChange(row) {
}
}
/** drawer field change -> immediate DB update */
/** drawer field change -> immediate DB update (edit) or local only (create) */
async function onRowPriceChange(row) {
if (!row || !row.psdId) return
if (!row) return
if (!props.livePersist) {
proxy.$modal.msgSuccess('价格已更新(完成时统一落库)')
return
}
if (!row.psdId) return
const prevFv = row._editPrevFv
const prevReg = row._editPrevReg
try {
@@ -533,10 +708,14 @@ async function saveDiscount() {
const hit = (props.details || []).find(d => d.productCode === discCode.value)
if (hit) {
hit.discountValue = discValue.value
try {
await persistDetail(hit, false)
proxy.$modal.msgSuccess('折扣已写入数据库')
} catch (e) {}
if (!props.livePersist) {
proxy.$modal.msgSuccess('折扣已更新(完成时统一落库)')
} else {
try {
await persistDetail(hit, false)
proxy.$modal.msgSuccess('折扣已写入数据库')
} catch (e) {}
}
}
discVisible.value = false
}
@@ -640,11 +819,15 @@ function onSurchargeConfirm(picked) {
? `已纳入 ${applied} 项附加费${scopeText};${skipped} 项因适用国家不匹配被跳过`
: `已纳入 ${ids.size} 项附加费${scopeText}`)
: `已清空附加费${scopeText}`
scVisible.value = false
if (!props.livePersist) {
proxy.$modal.msgSuccess(okText + '(完成时统一落库)')
return
}
const payload = targets.filter(r => r.psdId).map(r => toDetailPayload(r)).map(p => {
p.priceSetId = props.priceSetId
return p
})
scVisible.value = false
if (!payload.length) {
proxy.$modal.msgSuccess(okText)
return
@@ -658,14 +841,16 @@ function onSurchargeConfirm(picked) {
}
async function removeProduct(code) {
try {
const rows = await loadProductDetails(code)
const ids = rows.filter(d => d && d.psdId).map(d => d.psdId)
if (ids.length) {
await delSetDetail(ids.join(','))
if (props.livePersist) {
try {
const rows = await loadProductDetails(code)
const ids = rows.filter(d => d && d.psdId).map(d => d.psdId)
if (ids.length) {
await delSetDetail(ids.join(','))
}
} catch (e) {
return
}
} catch (e) {
return
}
delete detailByProduct[code]
if (Array.isArray(props.details)) {
@@ -681,7 +866,7 @@ async function removeProduct(code) {
proxy.$modal.msgSuccess('已移除产品明细')
}
defineExpose({ pullAllFromLibrary })
defineExpose({ pullAllFromLibrary, clearLocalDraft })
</script>
<style scoped>
@@ -707,6 +892,60 @@ defineExpose({ pullAllFromLibrary })
font-size: 13px;
word-break: break-all;
}
.build-choice {
padding: 8px 0 16px;
text-align: left;
}
.build-choice__tip {
margin: 0 0 12px;
color: #606266;
font-size: 13px;
font-weight: 600;
}
.build-choice__cards {
display: flex;
justify-content: flex-start;
gap: 12px;
flex-wrap: wrap;
margin-bottom: 8px;
}
.build-card {
width: 260px;
min-height: 88px;
padding: 14px 16px;
border: 1px solid #e4e7ed;
border-radius: 8px;
background: #fff;
cursor: pointer;
text-align: left;
transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
}
.build-card.selected {
border-color: #1890ff;
background: #e6f7ff;
}
.build-card:hover:not(:disabled) {
box-shadow: 0 4px 14px rgba(0, 0, 0, 0.06);
}
.build-card:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.build-card--pull:hover:not(:disabled) { border-color: #67c23a; }
.build-card--import:hover:not(:disabled) { border-color: #409eff; }
.build-card__title {
font-size: 16px;
font-weight: 600;
color: #303133;
margin-bottom: 10px;
}
.build-card--pull .build-card__title { color: #67c23a; }
.build-card--import .build-card__title { color: #409eff; }
.build-card__desc {
font-size: 13px;
line-height: 1.6;
color: #909399;
}
.card-actions {
flex: none;
display: flex;