This commit is contained in:
zlwu committed 2026-06-21 09:19:07 +08:00
1 parent c3be9d1dce
commit 299609706c
328 files changed
+144899 -13873

No files matched your search

@@ -0,0 +1,171 @@
import type { Plan, PlanProduct, TransportArrangeResult, TransportInventoryLink } from '@/api/pl'
import { planApi } from '@/api/pl'
import DbPoundSlipQrcodeDialog from '@/components/business/db/DbPoundSlipQrcodeDialog.vue'
import { useDictStore } from '@/store/modules/dict'
import { pickBxData } from '@/utils/http/bx'
import { formatPlanProductListText } from '@/utils/pl-display'
export function usePlPlanArrangeActions() {
const dictStore = useDictStore()
const plan = ref<Partial<Plan> | null>(null)
const products = ref<PlanProduct[]>([])
const driverUrl = ref('')
const licensePlate = ref('')
const inventoryRecords = ref<TransportInventoryLink[]>([])
const qrcodeVisible = ref(false)
const selectedInventoryId = ref('')
const selectedInventoryUnit = ref('')
const qrcodeDialogRef = ref<InstanceType<typeof DbPoundSlipQrcodeDialog> | null>(null)
const showArrangeActions = computed(
() =>
Boolean(
plan.value?.transportStatus === 'arranged' &&
plan.value?.followStatus !== 'cancelled' &&
(driverUrl.value || inventoryRecords.value.length)
)
)
function getUnitLabel(unit: string) {
return dictStore.getLabel('crm_product_unit', unit) || unit
}
function resetArrangeActions() {
plan.value = null
products.value = []
driverUrl.value = ''
licensePlate.value = ''
inventoryRecords.value = []
qrcodeVisible.value = false
selectedInventoryId.value = ''
selectedInventoryUnit.value = ''
}
function applyArrangeResult(result: TransportArrangeResult, contextPlan?: Partial<Plan>, contextProducts?: PlanProduct[]) {
if (contextPlan) plan.value = contextPlan
if (contextProducts) products.value = contextProducts
driverUrl.value = result.driverUrl || ''
licensePlate.value = result.licensePlate || ''
inventoryRecords.value = result.inventoryRecords || []
}
async function loadArrangeActions(planId: string) {
resetArrangeActions()
const planRes = await planApi.get(planId)
const data = pickBxData<Plan>(planRes)
if (!data) return
plan.value = data
products.value = Array.isArray(data.products) ? data.products : []
if (data.transportStatus !== 'arranged' || data.followStatus === 'cancelled') {
return
}
const infoRes = await planApi.getArrangeInfo(planId)
const info = pickBxData<{ driverUrl?: string; licensePlate?: string; inventoryRecords?: TransportInventoryLink[] }>(
infoRes
)
driverUrl.value = info?.driverUrl || ''
licensePlate.value = info?.licensePlate || data.licensePlate || ''
inventoryRecords.value = info?.inventoryRecords || []
}
function buildPlanSummaryText(options?: {
startPoint?: string
endPoint?: string
licensePlate?: string
driverUrl?: string
}) {
const lines = ['【运输安排】', `计划号:${plan.value?.planNo || '—'}`]
lines.push(`车牌:${options?.licensePlate || licensePlate.value || plan.value?.licensePlate || '—'}`)
lines.push(`时间:${plan.value?.pickupTime || plan.value?.planDate || '—'}`)
const productText = products.value.map(p => formatPlanProductListText(p, getUnitLabel)).join(';')
lines.push(`产品:${productText || '—'}`)
lines.push(
`路线:${plan.value?.pickupAddress || options?.startPoint || '—'} → ${
plan.value?.destinationAddress || plan.value?.destinationName || options?.endPoint || '—'
}`
)
lines.push(`司机链接:${options?.driverUrl || driverUrl.value || '—'}`)
return lines.join('\n')
}
async function copyDriverUrl(url?: string) {
const target = url || driverUrl.value
if (!target) return
await navigator.clipboard.writeText(target)
ElMessage.success('已复制司机链接')
}
async function copyPlanSummary(options?: {
startPoint?: string
endPoint?: string
licensePlate?: string
driverUrl?: string
}) {
await navigator.clipboard.writeText(buildPlanSummaryText(options))
ElMessage.success('已复制计划简述')
}
function resolveInventoryUnitLabel(record: TransportInventoryLink) {
const product = products.value.find(item => item.id === record.planProductId)
const unitRaw = product?.unit || ''
return unitRaw ? getUnitLabel(unitRaw) : '吨'
}
async function copyPoundSlipQrcode() {
if (!inventoryRecords.value.length) return
if (inventoryRecords.value.length === 1) {
selectedInventoryId.value = inventoryRecords.value[0].dbInventoryId
selectedInventoryUnit.value = resolveInventoryUnitLabel(inventoryRecords.value[0])
qrcodeVisible.value = true
await nextTick()
await qrcodeDialogRef.value?.copyQrcodeImage()
return
}
try {
const { value } = await ElMessageBox.prompt('请选择要复制二维码的产品行', '复制磅单二维码', {
inputPlaceholder: inventoryRecords.value.map((item, idx) => `${idx + 1}. ${item.productName}`).join('\n'),
confirmButtonText: '确定',
inputValidator: val => {
const index = Number(val) - 1
if (!Number.isInteger(index) || index < 0 || index >= inventoryRecords.value.length) {
return '请输入有效序号'
}
return true
}
})
const index = Number(value) - 1
const record = inventoryRecords.value[index]
selectedInventoryId.value = record.dbInventoryId
selectedInventoryUnit.value = resolveInventoryUnitLabel(record)
qrcodeVisible.value = true
await nextTick()
await qrcodeDialogRef.value?.copyQrcodeImage()
} catch {
// 用户取消
}
}
return {
plan,
products,
driverUrl,
licensePlate,
inventoryRecords,
qrcodeVisible,
selectedInventoryId,
selectedInventoryUnit,
qrcodeDialogRef,
showArrangeActions,
resetArrangeActions,
applyArrangeResult,
loadArrangeActions,
buildPlanSummaryText,
copyDriverUrl,
copyPlanSummary,
copyPoundSlipQrcode
}
}