修改套餐价格

This commit is contained in:
wuweihua
2026-09-16 17:14:19 +08:00
parent 7172621fd3
commit 4441aa12bf
48 changed files with 4258 additions and 682 deletions
+188 -84
View File
@@ -1,59 +1,61 @@
<template>
<div class="app-container">
<el-steps :active="active" finish-status="success" align-center class="wizard-steps">
<el-step title="基本信息" />
<el-step title="套餐明细" />
<el-step title="绑定客户" />
<el-step :title="step1Title" />
<el-step :title="step2Title" />
</el-steps>
<!-- 第 1 步 -->
<step-basic-info v-show="active === 0" ref="basicRef" v-model="form.basic" />
<div v-show="active === 0">
<step-basic-info ref="basicRef" v-model="form.basic" />
</div>
<!-- 第 2 步 -->
<step-details
v-show="active === 1"
ref="detailsRef"
:details="form.details"
:price-set-id="form.priceSetId"
:set-type="form.basic.setType"
:version-no="form.version.versionNo"
:ref-data="refData"
/>
<!-- 第 3 步 -->
<step-customers
v-show="active === 2"
:customers="form.customers"
:effective-date="form.basic.effectiveDate"
:expire-date="form.basic.expireDate"
/>
<div class="wizard-actions">
<el-button @click="goBack">取消</el-button>
<el-button v-if="active > 0" @click="active--">上一步</el-button>
<el-button v-if="active < 2" type="primary" @click="nextStep">下一步</el-button>
<el-button type="info" :loading="saving" @click="saveDraft">保存草稿</el-button>
<el-button type="primary" :loading="saving" @click="saveAndSubmit">保存并提交审核</el-button>
<el-button v-if="active > 0 && !directStep2" @click="active--">上一步</el-button>
<el-button v-if="active === 0" type="primary" :loading="creating" @click="goStep2">创建套餐价格</el-button>
<el-button v-if="active === 0" type="info" :loading="saving" @click="saveDraft">保存草稿</el-button>
<el-button v-if="active === 1" type="primary" :loading="saving" @click="saveDraft">保存</el-button>
</div>
<el-dialog
v-model="progressVisible"
:title="'正在保存套餐'"
width="420px"
:close-on-click-modal="false"
:close-on-press-escape="false"
:show-close="false"
append-to-body
>
<div class="save-progress">
<el-progress :percentage="progressPercent" :status="progressStatus" />
<p class="save-progress__msg">{{ progressMessage }}</p>
<p class="save-progress__hint">请勿关闭页面,大量明细保存可能需要一些时间</p>
</div>
</el-dialog>
</div>
</template>
<script setup name="PriceSetCreate">
/**
* 新建 / 编辑套餐 · 流程编排
*
* 三步表单已拆分到 components/,页面只负责:
* 步骤切换、保存前校验、payload 组装、路由返回。
* 参考数据(产品 / 国家 / 附加费)由 composables 提供,整页共享一份缓存。
*/
import StepBasicInfo from './components/create/StepBasicInfo.vue'
import StepDetails from './components/create/StepDetails.vue'
import StepCustomers from './components/create/StepCustomers.vue'
import { usePriceSetRefData, useDetailCascade } from './composables/usePriceSetRefData'
import {
createEmptyBasicInfo,
createEmptyDetailRow,
nextVersionNo,
toCustomerPayload,
toDetailPayload
} from './utils/priceSet'
import { getPriceSet, savePriceSet } from '@/api/price/set'
import { getPriceSet, savePriceSetAsync } from '@/api/price/set'
const { proxy } = getCurrentInstance()
const route = useRoute()
@@ -63,71 +65,144 @@ const cascade = useDetailCascade(refData)
const active = ref(0)
const saving = ref(false)
const creating = ref(false)
const progressVisible = ref(false)
const progressPercent = ref(0)
const progressMessage = ref('')
const progressStatus = ref(undefined)
const basicRef = ref()
const detailsRef = ref()
const copyMode = computed(() => String(route.query.copyVersion || '') === '1')
/** Entered from detail: continue-config / copy-version jump to step2 — hide 上一步 */
const directStep2 = computed(() => copyMode.value || String(route.query.step || '') === '2')
const step1Title = '基本信息'
const step2Title = '编辑产品价格'
/** 整个套餐表单:基本信息 / 明细 / 绑定客户 */
const form = reactive({
priceSetId: undefined,
basic: createEmptyBasicInfo(),
version: { psvId: undefined, versionNo: 'V1', effectiveTime: undefined, expireTime: undefined },
details: [],
customers: []
})
/** 下一步:仅第 1 步需要校验 */
async function nextStep() {
if (active.value === 0) {
const valid = await basicRef.value.validate()
if (!valid) return
function mapSaveProgressMessage(message) {
const m = message || ''
if (!m) return ''
if (m === 'queued') return '排队中'
if (m === 'pulling library') return '拉取产品库'
if (m === 'details exist, skip pull') return '明细已存在,跳过拉取'
if (m === 'saving basic') return '保存基本信息'
if (m === 'saving version') return '保存版本'
if (m.startsWith('saving details ')) return '保存明细 ' + m.slice('saving details '.length)
if (m === 'saving surcharges') return '保存附加费'
if (m === 'saving customers') return '保存客户'
if (m === 'finishing') return '收尾中'
if (m === 'done') return '完成'
if (m === 'failed') return '失败'
return m
}
async function runSaveWithProgress(payload) {
progressVisible.value = true
progressPercent.value = 0
progressMessage.value = ''
progressStatus.value = undefined
try {
const result = await savePriceSetAsync(payload, (task) => {
progressPercent.value = Math.min(100, Number(task.percent) || 0)
progressMessage.value = mapSaveProgressMessage(task.message)
if (task.status === 'FAIL') {
progressStatus.value = 'exception'
} else if (task.status === 'SUCCESS') {
progressStatus.value = 'success'
} else {
progressStatus.value = undefined
}
})
progressPercent.value = 100
progressStatus.value = 'success'
return result.priceSetId
} catch (e) {
progressStatus.value = 'exception'
const msg = (e && e.task && e.task.error) || (e && e.message) || '保存失败'
progressMessage.value = msg
if (proxy && proxy.$modal) {
proxy.$modal.msgError(msg)
}
await new Promise(resolve => setTimeout(resolve, 800))
throw e
} finally {
progressVisible.value = false
}
active.value++
}
function saveDraft() {
save(false)
/** Step1 -> Step2: create package prices from library when needed */
async function goStep2() {
const validBasic = await basicRef.value.validate()
if (!validBasic) return
const hasPersistedDetails = (form.details || []).some(d => d && d.psdId)
if (form.priceSetId && form.version.psvId && hasPersistedDetails) {
active.value = 1
return
}
creating.value = true
try {
form.details.splice(0, form.details.length)
const priceSetId = await runSaveWithProgress({
...buildPayload(),
details: [],
pullFromLibrary: 'Y'
})
form.priceSetId = priceSetId
const fresh = await getPriceSet(priceSetId)
applySetData(fresh.data || {}, false)
active.value = 1
proxy.$modal.msgSuccess('已创建套餐价格,可在第 2 步编辑')
} catch (e) {
// surfaced
} finally {
creating.value = false
}
}
function saveAndSubmit() {
save(true)
}
/** 校验 + 保存;校验失败回到第 1 步 */
async function save(submit) {
const valid = await basicRef.value.validate()
if (!valid) {
async function saveDraft() {
const validBasic = await basicRef.value.validate()
if (!validBasic) {
active.value = 0
return
}
const ids = form.customers.filter(c => c.customerId).map(c => String(c.customerId))
if (new Set(ids).size !== ids.length) {
proxy.$modal.msgError('该客户已绑定套餐,一个客户只能绑定一个套餐')
return
}
saving.value = true
savePriceSet(buildPayload(submit)).then(res => {
form.priceSetId = res.data
proxy.$modal.msgSuccess(submit ? '已保存并提交审核' : '草稿已保存,套餐ID=' + res.data)
try {
const priceSetId = await runSaveWithProgress(buildPayload())
form.priceSetId = priceSetId
proxy.$modal.msgSuccess('草稿已保存,套餐ID=' + priceSetId)
goBack()
}).catch(() => {
// 错误提示由请求拦截器统一处理
}).finally(() => {
} catch (e) {
// surfaced
} finally {
saving.value = false
})
}
}
/** 表单 -> 后端 payload */
function buildPayload(submitFlag) {
const { basic } = form
function buildPayload() {
const { basic, version } = form
return {
priceSetId: form.priceSetId,
priceSetName: basic.priceSetName,
setType: basic.setType,
defaultDiscount: basic.setType === 'DISCOUNT' ? basic.defaultDiscount : undefined,
effectiveDate: basic.effectiveDate,
expireDate: basic.expireDate || undefined,
priceSetNote: basic.priceSetNote,
version: {
psvId: copyMode.value ? undefined : version.psvId,
versionNo: version.versionNo
// no effectiveTime: set on activate
},
details: form.details.filter(d => d.productCode).map(toDetailPayload),
customers: form.customers.filter(c => c.customerCode).map(toCustomerPayload),
submitFlag: submitFlag ? 'Y' : undefined
customers: form.customers.filter(c => c.customerCode).map(toCustomerPayload)
}
}
@@ -140,21 +215,44 @@ function goBack() {
}
}
/** 编辑 / 复用新建:回显已有套餐 */
function loadExisting(id) {
function applySetData(data, asCopy) {
form.priceSetId = data.priceSetId
Object.assign(form.basic, {
priceSetName: data.priceSetName,
setType: data.setType || 'FIXED',
defaultDiscount: data.defaultDiscount != null ? Number(data.defaultDiscount) : 88,
priceSetNote: data.priceSetNote
})
const versions = data.versions || []
const qPsv = route.query.psvId
const byQuery = qPsv != null ? versions.find(v => String(v.psvId) === String(qPsv)) : null
const current = byQuery || data.version || versions[0] || {}
if (asCopy) {
form.version.psvId = undefined
form.version.versionNo = nextVersionNo(versions)
form.version.effectiveTime = undefined
form.version.expireTime = undefined
} else {
form.version.psvId = current.psvId
form.version.versionNo = current.versionNo || nextVersionNo(versions)
form.version.effectiveTime = current.effectiveTime
form.version.expireTime = current.expireTime
}
const srcDetails = (current.details && current.details.length) ? current.details : (data.details || [])
form.details = srcDetails.map(d => {
const row = cascade.hydrateRow(d, form.basic.setType)
if (asCopy) row.psdId = undefined
return row
})
form.customers = (data.customers || []).map(c => ({ ...c }))
}
function loadExisting(id, asCopy) {
getPriceSet(id).then(res => {
const data = res.data || {}
form.priceSetId = data.priceSetId
Object.assign(form.basic, {
priceSetName: data.priceSetName,
setType: data.setType || 'FIXED',
defaultDiscount: data.defaultDiscount != null ? Number(data.defaultDiscount) : 88,
effectiveDate: data.effectiveDate,
expireDate: data.expireDate,
priceSetNote: data.priceSetNote
})
form.details = (data.details || []).map(d => cascade.hydrateRow(d, form.basic.setType))
form.customers = (data.customers || []).map(c => ({ ...c }))
applySetData(res.data || {}, asCopy)
if (asCopy || String(route.query.step || '') === '2') {
active.value = 1
}
})
}
@@ -162,9 +260,7 @@ onMounted(() => {
refData.loadProducts().finally(() => {
const id = route.query.priceSetId
if (id) {
loadExisting(id)
} else {
form.details.push(createEmptyDetailRow())
loadExisting(id, copyMode.value)
}
})
})
@@ -177,11 +273,19 @@ onMounted(() => {
padding: 20px 12px 12px;
border-radius: 6px;
}
.step-card {
margin-bottom: 16px;
}
.wizard-actions {
text-align: center;
padding: 8px 0 24px;
}
.save-progress__msg {
margin: 12px 0 4px;
color: #606266;
font-size: 13px;
word-break: break-all;
}
.save-progress__hint {
margin: 0;
color: #909399;
font-size: 12px;
}
</style>