30 lines
2.7 KiB
TypeScript
30 lines
2.7 KiB
TypeScript
import { computed, reactive, ref } from 'vue'
|
|||
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
|
|
import { ApiResponseError, getApiErrorDetails, type ApiErrorDetails } from '@/shared/api/http'
|
||
|
|
import { useAccessPreviewStore } from '@/core/access/accessPreview'
|
||
|
|
import { useUnsavedWorkRegistration } from '@/core/unsaved-work/useUnsavedWorkRegistration'
|
||
|
|
import { commandCapacityInquiry } from './api/capacityOffers'
|
||
|
|
import type { CapacityInquiry, InquiryCommand, InquiryResult } from './capacityOfferTypes'
|
||
|
|
export function useCapacityInquiryCommands(key: string, accept: (value: InquiryResult) => void) {
|
||
|
|
const access = useAccessPreviewStore(), busy = ref(false), error = ref<ApiErrorDetails | null>(null)
|
||
|
|
const state = reactive<{ pending: { bookingNo: string; action: 'confirm' | 'cancel'; body: InquiryCommand } | null }>({ pending: null })
|
||
|
|
useUnsavedWorkRegistration({ key, label: computed(() => '待核对拼车命令'), isOpen: computed(() => !!state.pending), model: state, requiredPermission: computed(() => null) })
|
||
|
|
async function retry() {
|
||
|
|
if (!state.pending || busy.value) return
|
||
|
|
busy.value = true; error.value = null
|
||
|
|
try { const value = await commandCapacityInquiry(state.pending.bookingNo, state.pending.action, state.pending.body); state.pending = null; accept(value); ElMessage.success('意向已更新') }
|
||
|
|
catch (cause) { error.value = getApiErrorDetails(cause, '意向操作未完成'); if (cause instanceof ApiResponseError) state.pending = null }
|
||
|
|
finally { busy.value = false }
|
||
|
|
}
|
||
|
|
async function run(row: CapacityInquiry, action: 'confirm' | 'cancel') {
|
||
|
|
if (busy.value) return
|
||
|
|
const manage = access.can('platform:fleet:capacity-offer:manage')
|
||
|
|
if (action === 'confirm' ? !manage || !row.actions.canConfirm : !row.actions.canCancel || !(manage || access.can('platform:fleet:capacity-offer:list') && access.can('platform:fleet:capacity-offer:inquiry-cancel'))) return
|
||
|
|
if (state.pending) { ElMessage.warning('请先重试上一条结果未知的操作'); return }
|
||
|
|
try { await ElMessageBox.confirm(action === 'confirm' ? `确认承运 ${row.bookingNo},按约定货量占位并创建本土订单?` : `取消 ${row.bookingNo}?未登记来货的已确认承运将一并取消商业订单并释放预留。`, action === 'confirm' ? '确认拼车承运' : '取消拼车意向', { customClass: 'admin-density-confirm', confirmButtonText: '确认', cancelButtonText: '返回' }) } catch { return }
|
||
|
|
state.pending = { bookingNo: row.bookingNo, action, body: { tripRecordVersion: row.tripRecordVersion, offerRecordVersion: row.offerRecordVersion, recordVersion: row.recordVersion, idempotencyKey: crypto.randomUUID() } }
|
||
|
|
await retry()
|
||
|
|
}
|
||
|
|
return { busy, error, state, run, retry }
|
||
|
|
}
|