1
This commit is contained in:
parent
349053e2ed
commit
452634fba9
@ -25,9 +25,44 @@ export interface StorageReportSearchBody {
|
|||||||
where?: {
|
where?: {
|
||||||
start_time?: string | Date
|
start_time?: string | Date
|
||||||
end_time?: string | Date
|
end_time?: string | Date
|
||||||
by_space?: boolean
|
|
||||||
store_ids?: number[]
|
store_ids?: number[]
|
||||||
}
|
}
|
||||||
|
sort?: {
|
||||||
|
sortField?: 'begin_total' | 'inbound_total' | 'outbound_total' | 'end_total'
|
||||||
|
sort_field?: 'begin_total' | 'inbound_total' | 'outbound_total' | 'end_total'
|
||||||
|
sortOrder?: 'ASC' | 'DESC' | 'asc' | 'desc'
|
||||||
|
sort_order?: 'ASC' | 'DESC' | 'asc' | 'desc'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StorageReportExportBody extends StorageReportSearchBody {}
|
||||||
|
|
||||||
|
export interface StorageReportExportTaskResponse {
|
||||||
|
task_id: string
|
||||||
|
status: 'submitted' | 'running' | 'success' | 'failed'
|
||||||
|
progress: number
|
||||||
|
message: string
|
||||||
|
error: string
|
||||||
|
result: Record<string, unknown> | null
|
||||||
|
update_time: number
|
||||||
|
code?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StorageReportExportTaskProgressParams {
|
||||||
|
task_id?: string
|
||||||
|
taskId?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface StorageReportExportTaskProgressResponse {
|
||||||
|
task_id: string
|
||||||
|
status: 'submitted' | 'running' | 'success' | 'failed' | 'not_found'
|
||||||
|
progress: number
|
||||||
|
message: string
|
||||||
|
error: string
|
||||||
|
download_url: string
|
||||||
|
result: Record<string, unknown> | null
|
||||||
|
update_time: number
|
||||||
|
code?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 与网关返回一致:成功时 data 内含 list */
|
/** 与网关返回一致:成功时 data 内含 list */
|
||||||
@ -57,3 +92,35 @@ export function searchInventoryReport(
|
|||||||
isSystemApi: true
|
isSystemApi: true
|
||||||
} as any)
|
} as any)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function exportInventoryReport(
|
||||||
|
data?: StorageReportExportBody
|
||||||
|
): Promise<StorageReportExportTaskResponse> {
|
||||||
|
return request({
|
||||||
|
url: '/system',
|
||||||
|
method: 'post',
|
||||||
|
params: {
|
||||||
|
method: 'storage.report.export',
|
||||||
|
v,
|
||||||
|
timestamp: new Date().getTime()
|
||||||
|
},
|
||||||
|
data: data ?? {},
|
||||||
|
isSystemApi: true
|
||||||
|
} as any)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getExportTaskProgress(
|
||||||
|
data: StorageReportExportTaskProgressParams
|
||||||
|
): Promise<StorageReportExportTaskProgressResponse> {
|
||||||
|
return request({
|
||||||
|
url: '/system',
|
||||||
|
method: 'post',
|
||||||
|
params: {
|
||||||
|
method: 'storage.report.exportTaskProgress',
|
||||||
|
v,
|
||||||
|
timestamp: new Date().getTime()
|
||||||
|
},
|
||||||
|
data,
|
||||||
|
isSystemApi: true
|
||||||
|
} as any)
|
||||||
|
}
|
||||||
|
|||||||
@ -681,6 +681,15 @@ function normalizeTextValue(field: any) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cloneConditionDefaultValue(value: any) {
|
||||||
|
if (value === undefined) return undefined
|
||||||
|
if (value === null) return null
|
||||||
|
if (Array.isArray(value) || typeof value === 'object') {
|
||||||
|
return JSON.parse(JSON.stringify(value))
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
function initCustomConditions(fields: any[], isSilent = false) {
|
function initCustomConditions(fields: any[], isSilent = false) {
|
||||||
fields.forEach(field => {
|
fields.forEach(field => {
|
||||||
let defaultValue: any = undefined
|
let defaultValue: any = undefined
|
||||||
@ -714,6 +723,10 @@ function initCustomConditions(fields: any[], isSilent = false) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Object.prototype.hasOwnProperty.call(field, 'defaultValue')) {
|
||||||
|
defaultValue = cloneConditionDefaultValue(field.defaultValue)
|
||||||
|
}
|
||||||
|
|
||||||
if (!customConditions[field.value]) {
|
if (!customConditions[field.value]) {
|
||||||
customConditions[field.value] = {
|
customConditions[field.value] = {
|
||||||
value: defaultValue,
|
value: defaultValue,
|
||||||
|
|||||||
@ -16,7 +16,7 @@
|
|||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<div class="toolbar-left">
|
<div class="toolbar-left">
|
||||||
<el-button size="small" type="primary" @click="loadData">刷新</el-button>
|
<el-button size="small" type="primary" @click="loadData">刷新</el-button>
|
||||||
<el-button size="small" :loading="exportLoading" @click="handleExport">导出 CSV</el-button>
|
<el-button size="small" :loading="exportLoading" @click="handleExportDetail">导出库存明细</el-button>
|
||||||
<span class="toolbar-title">库存报表</span>
|
<span class="toolbar-title">库存报表</span>
|
||||||
<el-tag size="small" type="info">共 {{ pagination.total }} 条</el-tag>
|
<el-tag size="small" type="info">共 {{ pagination.total }} 条</el-tag>
|
||||||
<template v-if="pagination.total > 0">
|
<template v-if="pagination.total > 0">
|
||||||
@ -26,13 +26,6 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p class="filter-hint">
|
|
||||||
结构与收货单页一致:左侧 ScanBox 选条件后自动查询。无任何有效条件时请求体为
|
|
||||||
<code>{}</code>。接口字段:期初 <code>begin_total</code>、入库 <code>inbound_total</code>、出库
|
|
||||||
<code>outbound_total</code>、期末 <code>end_total</code>;按库位时可能含
|
|
||||||
<code>space_id</code>/<code>space_name</code>。
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<div class="upper-wrapper">
|
<div class="upper-wrapper">
|
||||||
<upper-table
|
<upper-table
|
||||||
row-key="__rid"
|
row-key="__rid"
|
||||||
@ -45,6 +38,7 @@
|
|||||||
:loading="loading"
|
:loading="loading"
|
||||||
:query-time="queryTime"
|
:query-time="queryTime"
|
||||||
@current-change="handleRowClick"
|
@current-change="handleRowClick"
|
||||||
|
@sort-change="handleSortChange"
|
||||||
@page-change="handlePageChange"
|
@page-change="handlePageChange"
|
||||||
@page-size-change="handlePageSizeChange"
|
@page-size-change="handlePageSizeChange"
|
||||||
@refresh="loadData"
|
@refresh="loadData"
|
||||||
@ -77,19 +71,53 @@
|
|||||||
<div class="empty-tip">在表格中点击一行可查看该行完整字段</div>
|
<div class="empty-tip">在表格中点击一行可查看该行完整字段</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<el-dialog
|
||||||
|
v-model="exportDialogVisible"
|
||||||
|
title="库存报表导出进度"
|
||||||
|
width="min(520px, 92vw)"
|
||||||
|
append-to-body
|
||||||
|
align-center
|
||||||
|
:show-close="false"
|
||||||
|
:close-on-click-modal="false"
|
||||||
|
:close-on-press-escape="false"
|
||||||
|
>
|
||||||
|
<div class="export-progress-panel">
|
||||||
|
<div class="export-progress-panel__header">
|
||||||
|
<span class="export-progress-panel__meta">任务ID:{{ exportTaskProgress.taskId || '—' }}</span>
|
||||||
|
</div>
|
||||||
|
<el-progress :percentage="exportProgressPercent" :status="exportProgressBarStatus" :stroke-width="16" />
|
||||||
|
<div class="export-progress-panel__info">
|
||||||
|
<span>状态:{{ exportStatusText }}</span>
|
||||||
|
<span>进度:{{ exportProgressPercent }}%</span>
|
||||||
|
<span v-if="exportTaskProgress.updateTime">更新时间:{{ formatDateTime(exportTaskProgress.updateTime) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="exportTaskProgress.message || exportTaskProgress.error" class="export-progress-panel__message">
|
||||||
|
{{ exportTaskProgress.error || exportTaskProgress.message }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</el-dialog>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { computed, onMounted, reactive, ref } from 'vue'
|
import { computed, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
import dayjs from 'dayjs'
|
import dayjs from 'dayjs'
|
||||||
import ScanBox from '@/components/ScanBox/index.vue'
|
import ScanBox from '@/components/ScanBox/index.vue'
|
||||||
import UpperTable from '@/components/UpperTable/index.vue'
|
import UpperTable from '@/components/UpperTable/index.vue'
|
||||||
import { searchInventoryReport } from '@/api/storage.report/report'
|
import {
|
||||||
|
exportInventoryReport,
|
||||||
|
getExportTaskProgress,
|
||||||
|
searchInventoryReport,
|
||||||
|
type StorageReportExportBody,
|
||||||
|
type StorageReportSearchBody
|
||||||
|
} from '@/api/storage.report/report'
|
||||||
import * as indexApi from '@/api/index/index'
|
import * as indexApi from '@/api/index/index'
|
||||||
import {
|
import {
|
||||||
buildReportTableColumns,
|
buildReportTableColumns,
|
||||||
|
buildDefaultReportScanCondition,
|
||||||
|
buildDefaultReportWhere,
|
||||||
formatReportCell,
|
formatReportCell,
|
||||||
formatReportDateTime,
|
formatReportDateTime,
|
||||||
formatReportNumber,
|
formatReportNumber,
|
||||||
@ -125,9 +153,25 @@ const pagination = reactive({
|
|||||||
pageSize: 20,
|
pageSize: 20,
|
||||||
total: 0
|
total: 0
|
||||||
})
|
})
|
||||||
|
const currentSort = reactive<{ prop: string; order: '' | 'ascending' | 'descending' }>({
|
||||||
|
prop: 'outbound_total',
|
||||||
|
order: 'descending'
|
||||||
|
})
|
||||||
|
|
||||||
const selectedRid = ref<string | null>(null)
|
const selectedRid = ref<string | null>(null)
|
||||||
const currentConditionRaw = ref<any>(null)
|
const currentConditionRaw = ref<any>(buildDefaultReportScanCondition())
|
||||||
|
const exportDialogVisible = ref(false)
|
||||||
|
const exportTaskProgress = reactive({
|
||||||
|
taskId: '',
|
||||||
|
status: 'submitted' as 'submitted' | 'running' | 'success' | 'failed' | 'not_found',
|
||||||
|
progress: 0,
|
||||||
|
message: '',
|
||||||
|
error: '',
|
||||||
|
downloadUrl: '',
|
||||||
|
updateTime: 0
|
||||||
|
})
|
||||||
|
let exportPollingTimer: number | null = null
|
||||||
|
const STORAGE_REPORT_EXPORT_TASK_ID_KEY = 'storage.report.export.taskId'
|
||||||
|
|
||||||
const selectedRow = computed(() => fullList.value.find((r) => r.__rid === selectedRid.value) || null)
|
const selectedRow = computed(() => fullList.value.find((r) => r.__rid === selectedRid.value) || null)
|
||||||
|
|
||||||
@ -156,12 +200,12 @@ function detailValue(key: string) {
|
|||||||
if (!selectedRow.value) return '--'
|
if (!selectedRow.value) return '--'
|
||||||
const v = (selectedRow.value as any)[key]
|
const v = (selectedRow.value as any)[key]
|
||||||
if (key === 'start_time' || key === 'end_time') return formatReportDateTime(v)
|
if (key === 'start_time' || key === 'end_time') return formatReportDateTime(v)
|
||||||
|
if (key === 'stock_id') return formatReportCell(v)
|
||||||
if (
|
if (
|
||||||
key === 'begin_total' ||
|
key === 'begin_total' ||
|
||||||
key === 'end_total' ||
|
key === 'end_total' ||
|
||||||
key === 'inbound_total' ||
|
key === 'inbound_total' ||
|
||||||
key === 'outbound_total' ||
|
key === 'outbound_total' ||
|
||||||
key === 'stock_id' ||
|
|
||||||
key === 'store_id' ||
|
key === 'store_id' ||
|
||||||
key === 'space_id'
|
key === 'space_id'
|
||||||
) {
|
) {
|
||||||
@ -195,6 +239,152 @@ function applyClientPage() {
|
|||||||
list.value = fullList.value.slice(start, start + pagination.pageSize)
|
list.value = fullList.value.slice(start, start + pagination.pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SORTABLE_FIELDS = new Set(['begin_total', 'inbound_total', 'outbound_total', 'end_total'])
|
||||||
|
|
||||||
|
function buildSortPayload(): StorageReportSearchBody['sort'] | undefined {
|
||||||
|
if (!SORTABLE_FIELDS.has(currentSort.prop)) return undefined
|
||||||
|
if (!currentSort.order) return undefined
|
||||||
|
const sortOrder = currentSort.order === 'ascending' ? 'ASC' : 'DESC'
|
||||||
|
return {
|
||||||
|
sortField: currentSort.prop as any,
|
||||||
|
sort_field: currentSort.prop as any,
|
||||||
|
sortOrder,
|
||||||
|
sort_order: sortOrder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildReportQueryPayload(): StorageReportSearchBody {
|
||||||
|
const where = normalizeScanConditionToReportWhere(currentConditionRaw.value)
|
||||||
|
const effectiveWhere = Object.keys(where).length > 0 ? where : buildDefaultReportWhere()
|
||||||
|
const sort = buildSortPayload()
|
||||||
|
return sort ? { where: effectiveWhere, sort } : { where: effectiveWhere }
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeDownloadUrl(fileUrl: string) {
|
||||||
|
if (!fileUrl) return ''
|
||||||
|
if (/^https?:\/\//i.test(fileUrl)) return fileUrl
|
||||||
|
const normalizedPath = fileUrl.startsWith('/') ? fileUrl : `/${fileUrl}`
|
||||||
|
const baseApi = String(import.meta.env.VITE_APP_BASE_API || '')
|
||||||
|
const normalizedBase = baseApi.endsWith('/') ? baseApi.slice(0, -1) : baseApi
|
||||||
|
if (normalizedBase.startsWith('/')) {
|
||||||
|
return `${window.location.origin}${normalizedBase}${normalizedPath}`
|
||||||
|
}
|
||||||
|
return `${window.location.origin}${normalizedPath}`
|
||||||
|
}
|
||||||
|
|
||||||
|
function resetExportProgress() {
|
||||||
|
exportTaskProgress.taskId = ''
|
||||||
|
exportTaskProgress.status = 'submitted'
|
||||||
|
exportTaskProgress.progress = 0
|
||||||
|
exportTaskProgress.message = ''
|
||||||
|
exportTaskProgress.error = ''
|
||||||
|
exportTaskProgress.downloadUrl = ''
|
||||||
|
exportTaskProgress.updateTime = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveExportTaskId(taskId: string) {
|
||||||
|
if (!taskId) return
|
||||||
|
try {
|
||||||
|
window.localStorage.setItem(STORAGE_REPORT_EXPORT_TASK_ID_KEY, taskId)
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('保存导出任务ID失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSavedExportTaskId() {
|
||||||
|
try {
|
||||||
|
return String(window.localStorage.getItem(STORAGE_REPORT_EXPORT_TASK_ID_KEY) || '').trim()
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('读取导出任务ID失败:', error)
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function clearSavedExportTaskId() {
|
||||||
|
try {
|
||||||
|
window.localStorage.removeItem(STORAGE_REPORT_EXPORT_TASK_ID_KEY)
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('清理导出任务ID失败:', error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopExportPolling() {
|
||||||
|
if (exportPollingTimer) {
|
||||||
|
window.clearTimeout(exportPollingTimer)
|
||||||
|
exportPollingTimer = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function scheduleExportPolling(delay = 1500) {
|
||||||
|
stopExportPolling()
|
||||||
|
exportPollingTimer = window.setTimeout(() => {
|
||||||
|
void refreshExportTaskProgress()
|
||||||
|
}, delay)
|
||||||
|
}
|
||||||
|
|
||||||
|
function downloadFile(url: string) {
|
||||||
|
const finalUrl = normalizeDownloadUrl(url)
|
||||||
|
if (!finalUrl) {
|
||||||
|
throw new Error('导出文件地址为空')
|
||||||
|
}
|
||||||
|
const link = document.createElement('a')
|
||||||
|
link.href = finalUrl
|
||||||
|
link.target = '_blank'
|
||||||
|
document.body.appendChild(link)
|
||||||
|
link.click()
|
||||||
|
link.remove()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function refreshExportTaskProgress() {
|
||||||
|
if (!exportTaskProgress.taskId) return
|
||||||
|
try {
|
||||||
|
const res: any = await getExportTaskProgress({ taskId: exportTaskProgress.taskId })
|
||||||
|
if (!res) {
|
||||||
|
scheduleExportPolling()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
exportTaskProgress.status = res.status || exportTaskProgress.status
|
||||||
|
exportTaskProgress.progress = Number.isFinite(Number(res.progress)) ? Number(res.progress) : exportTaskProgress.progress
|
||||||
|
exportTaskProgress.message = res.message || ''
|
||||||
|
exportTaskProgress.error = res.error || ''
|
||||||
|
exportTaskProgress.downloadUrl = res.download_url || ''
|
||||||
|
exportTaskProgress.updateTime = Number(res.update_time || 0)
|
||||||
|
|
||||||
|
if (exportTaskProgress.status === 'success') {
|
||||||
|
stopExportPolling()
|
||||||
|
exportLoading.value = false
|
||||||
|
clearSavedExportTaskId()
|
||||||
|
try {
|
||||||
|
if (exportTaskProgress.downloadUrl) {
|
||||||
|
downloadFile(exportTaskProgress.downloadUrl)
|
||||||
|
ElMessage.success(exportTaskProgress.message || '导出完成,文件已开始下载')
|
||||||
|
} else {
|
||||||
|
ElMessage.warning(exportTaskProgress.message || '导出完成,但未返回下载地址')
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
setTimeout(() => {
|
||||||
|
exportDialogVisible.value = false
|
||||||
|
resetExportProgress()
|
||||||
|
}, 1200)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exportTaskProgress.status === 'failed' || exportTaskProgress.status === 'not_found') {
|
||||||
|
stopExportPolling()
|
||||||
|
exportLoading.value = false
|
||||||
|
clearSavedExportTaskId()
|
||||||
|
ElMessage.error(exportTaskProgress.error || exportTaskProgress.message || '导出任务失败')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
scheduleExportPolling()
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('查询库存报表导出进度失败:', error)
|
||||||
|
scheduleExportPolling(2000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function loadFilterData() {
|
async function loadFilterData() {
|
||||||
try {
|
try {
|
||||||
const [storesRes, presetRes]: any = await Promise.all([
|
const [storesRes, presetRes]: any = await Promise.all([
|
||||||
@ -220,13 +410,13 @@ function handleConditionChange(condition: any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleConditionClear() {
|
function handleConditionClear() {
|
||||||
currentConditionRaw.value = null
|
currentConditionRaw.value = buildDefaultReportScanCondition()
|
||||||
pagination.currentPage = 1
|
pagination.currentPage = 1
|
||||||
loadData()
|
loadData()
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleConditionReset() {
|
function handleConditionReset() {
|
||||||
currentConditionRaw.value = null
|
currentConditionRaw.value = buildDefaultReportScanCondition()
|
||||||
pagination.currentPage = 1
|
pagination.currentPage = 1
|
||||||
if (scanBox.value?.reset) {
|
if (scanBox.value?.reset) {
|
||||||
scanBox.value.reset()
|
scanBox.value.reset()
|
||||||
@ -241,8 +431,7 @@ async function loadData() {
|
|||||||
loading.value = true
|
loading.value = true
|
||||||
const started = Date.now()
|
const started = Date.now()
|
||||||
selectedRid.value = null
|
selectedRid.value = null
|
||||||
const where = normalizeScanConditionToReportWhere(currentConditionRaw.value)
|
const body: StorageReportSearchBody = buildReportQueryPayload()
|
||||||
const body = Object.keys(where).length > 0 ? { where } : {}
|
|
||||||
try {
|
try {
|
||||||
const res: any = await searchInventoryReport(body as any)
|
const res: any = await searchInventoryReport(body as any)
|
||||||
if (res && (res.flag === 'success' || res.code === 200)) {
|
if (res && (res.flag === 'success' || res.code === 200)) {
|
||||||
@ -273,6 +462,63 @@ async function loadData() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function handleSortChange({ prop, order }: { prop: string; order: '' | 'ascending' | 'descending' | null }) {
|
||||||
|
currentSort.prop = prop || ''
|
||||||
|
currentSort.order = (order || '') as '' | 'ascending' | 'descending'
|
||||||
|
pagination.currentPage = 1
|
||||||
|
loadData()
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleExportDetail() {
|
||||||
|
const savedTaskId = getSavedExportTaskId()
|
||||||
|
if (savedTaskId) {
|
||||||
|
ElMessage.warning('检测到未完成导出任务,请等待当前任务完成后再试')
|
||||||
|
resumeExportTaskFromStorage()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exportLoading.value) return
|
||||||
|
exportLoading.value = true
|
||||||
|
resetExportProgress()
|
||||||
|
exportDialogVisible.value = true
|
||||||
|
try {
|
||||||
|
const body: StorageReportExportBody = buildReportQueryPayload()
|
||||||
|
const res: any = await exportInventoryReport(body)
|
||||||
|
if (!res || !res.task_id) {
|
||||||
|
throw new Error(res?.message || '导出任务提交失败')
|
||||||
|
}
|
||||||
|
exportTaskProgress.taskId = res.task_id
|
||||||
|
exportTaskProgress.status = res.status || 'submitted'
|
||||||
|
exportTaskProgress.progress = Number.isFinite(Number(res.progress)) ? Number(res.progress) : 0
|
||||||
|
exportTaskProgress.message = res.message || '任务已受理'
|
||||||
|
exportTaskProgress.error = res.error || ''
|
||||||
|
exportTaskProgress.updateTime = Number(res.update_time || 0)
|
||||||
|
saveExportTaskId(exportTaskProgress.taskId)
|
||||||
|
scheduleExportPolling(800)
|
||||||
|
} catch (error: any) {
|
||||||
|
exportLoading.value = false
|
||||||
|
exportDialogVisible.value = false
|
||||||
|
resetExportProgress()
|
||||||
|
clearSavedExportTaskId()
|
||||||
|
ElMessage.error(error?.message || '导出任务提交失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resumeExportTaskFromStorage() {
|
||||||
|
const savedTaskId = getSavedExportTaskId()
|
||||||
|
if (!savedTaskId) return
|
||||||
|
exportTaskProgress.taskId = savedTaskId
|
||||||
|
exportTaskProgress.status = 'submitted'
|
||||||
|
exportTaskProgress.progress = 0
|
||||||
|
exportTaskProgress.message = '检测到未完成导出任务,正在恢复进度...'
|
||||||
|
exportTaskProgress.error = ''
|
||||||
|
exportTaskProgress.downloadUrl = ''
|
||||||
|
exportTaskProgress.updateTime = 0
|
||||||
|
exportDialogVisible.value = true
|
||||||
|
exportLoading.value = true
|
||||||
|
scheduleExportPolling(200)
|
||||||
|
}
|
||||||
|
|
||||||
function handlePageChange(page: number) {
|
function handlePageChange(page: number) {
|
||||||
pagination.currentPage = page
|
pagination.currentPage = page
|
||||||
applyClientPage()
|
applyClientPage()
|
||||||
@ -288,48 +534,62 @@ function handleRowClick(row: any) {
|
|||||||
selectedRid.value = row?.__rid ?? null
|
selectedRid.value = row?.__rid ?? null
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleExport() {
|
const exportProgressPercent = computed(() => {
|
||||||
if (!fullList.value.length) {
|
const value = Number(exportTaskProgress.progress || 0)
|
||||||
ElMessage.warning('暂无数据可导出,请先查询')
|
if (!Number.isFinite(value)) return 0
|
||||||
return
|
return Math.min(100, Math.max(0, Math.round(value)))
|
||||||
}
|
})
|
||||||
exportLoading.value = true
|
|
||||||
try {
|
|
||||||
const cols = columns.value.length > 0 ? columns.value : buildReportTableColumns([])
|
|
||||||
const headers = cols.map((c) => c.label)
|
|
||||||
const props = cols.map((c) => c.prop)
|
|
||||||
const lines = [
|
|
||||||
headers.map(escapeCsv).join(','),
|
|
||||||
...fullList.value.map((row) =>
|
|
||||||
props.map((p) => escapeCsv(row[p])).join(',')
|
|
||||||
)
|
|
||||||
]
|
|
||||||
const blob = new Blob(['\uFEFF' + lines.join('\n')], { type: 'text/csv;charset=utf-8;' })
|
|
||||||
const a = document.createElement('a')
|
|
||||||
a.href = URL.createObjectURL(blob)
|
|
||||||
a.download = `库存报表_${dayjs().format('YYYY-MM-DD_HHmmss')}.csv`
|
|
||||||
a.click()
|
|
||||||
URL.revokeObjectURL(a.href)
|
|
||||||
ElMessage.success('导出成功')
|
|
||||||
} catch (e: any) {
|
|
||||||
ElMessage.error(e?.message || '导出失败')
|
|
||||||
} finally {
|
|
||||||
exportLoading.value = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function escapeCsv(v: unknown) {
|
const exportProgressBarStatus = computed(() => {
|
||||||
const s = formatReportCell(v)
|
if (exportTaskProgress.status === 'failed' || exportTaskProgress.status === 'not_found') return 'exception'
|
||||||
if (/[",\n\r]/.test(s)) return `"${s.replace(/"/g, '""')}"`
|
if (exportTaskProgress.status === 'success' || exportProgressPercent.value >= 100) return 'success'
|
||||||
return s
|
return undefined
|
||||||
|
})
|
||||||
|
|
||||||
|
const exportStatusText = computed(() => {
|
||||||
|
const map: Record<string, string> = {
|
||||||
|
submitted: '已受理',
|
||||||
|
running: '进行中',
|
||||||
|
success: '成功',
|
||||||
|
failed: '失败',
|
||||||
|
not_found: '未找到'
|
||||||
|
}
|
||||||
|
return map[exportTaskProgress.status] || exportTaskProgress.status || '—'
|
||||||
|
})
|
||||||
|
|
||||||
|
function formatDateTime(value: string | number | null | undefined) {
|
||||||
|
if (!value) return '—'
|
||||||
|
let normalizedValue: string | number = value
|
||||||
|
if (typeof value === 'string' && /^\d+$/.test(value.trim())) {
|
||||||
|
const numericValue = Number(value.trim())
|
||||||
|
normalizedValue = value.trim().length <= 10 ? numericValue * 1000 : numericValue
|
||||||
|
} else if (typeof value === 'number' && Number.isFinite(value) && value > 0 && value < 1000000000000) {
|
||||||
|
normalizedValue = value * 1000
|
||||||
|
}
|
||||||
|
|
||||||
|
const date = new Date(normalizedValue)
|
||||||
|
if (Number.isNaN(date.getTime())) return String(value)
|
||||||
|
const y = date.getFullYear()
|
||||||
|
const m = String(date.getMonth() + 1).padStart(2, '0')
|
||||||
|
const d = String(date.getDate()).padStart(2, '0')
|
||||||
|
const hh = String(date.getHours()).padStart(2, '0')
|
||||||
|
const mm = String(date.getMinutes()).padStart(2, '0')
|
||||||
|
const ss = String(date.getSeconds()).padStart(2, '0')
|
||||||
|
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
loadFilterData()
|
loadFilterData()
|
||||||
loadData()
|
loadData()
|
||||||
|
resumeExportTaskFromStorage()
|
||||||
|
})
|
||||||
|
|
||||||
|
onBeforeUnmount(() => {
|
||||||
|
stopExportPolling()
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
.storage-report-container {
|
.storage-report-container {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|||||||
@ -4,7 +4,7 @@ import type { StorageReportListItem } from '@/api/storage.report/report'
|
|||||||
export const reportFieldLabels: Record<string, string> = {
|
export const reportFieldLabels: Record<string, string> = {
|
||||||
store_id: '货主ID',
|
store_id: '货主ID',
|
||||||
store_name: '货主',
|
store_name: '货主',
|
||||||
stock_id: '商品ID',
|
stock_id: 'SKU',
|
||||||
item_code: '商品编码',
|
item_code: '商品编码',
|
||||||
barcode: '条码',
|
barcode: '条码',
|
||||||
item_name: '商品名称',
|
item_name: '商品名称',
|
||||||
@ -66,21 +66,50 @@ export type ReportColumn = {
|
|||||||
width?: string | number
|
width?: string | number
|
||||||
minWidth?: string | number
|
minWidth?: string | number
|
||||||
align?: 'left' | 'center' | 'right'
|
align?: 'left' | 'center' | 'right'
|
||||||
|
sortable?: boolean | 'custom'
|
||||||
|
sortOrders?: string[]
|
||||||
formatter?: (row: StorageReportListItem) => string
|
formatter?: (row: StorageReportListItem) => string
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 主表格固定列(与真实接口字段一致);按库位时后端可额外返回 space_*,由 buildReportTableColumns 追加 */
|
/** 主表格固定列(与真实接口字段一致);按库位时后端可额外返回 space_*,由 buildReportTableColumns 追加 */
|
||||||
export const fixedReportColumns: ReportColumn[] = [
|
export const fixedReportColumns: ReportColumn[] = [
|
||||||
{ prop: 'store_name', label: '货主', minWidth: 120, formatter: (row) => row.store_name || '--' },
|
{ prop: 'store_name', label: '货主', minWidth: 120, formatter: (row) => row.store_name || '--' },
|
||||||
{ prop: 'store_id', label: '货主ID', width: 96, align: 'center', formatter: (row) => formatReportNumber(row.store_id) },
|
|
||||||
{ prop: 'item_name', label: '商品名称', minWidth: 140, formatter: (row) => row.item_name || '--' },
|
{ prop: 'item_name', label: '商品名称', minWidth: 140, formatter: (row) => row.item_name || '--' },
|
||||||
{ prop: 'item_code', label: '商品编码', minWidth: 120, formatter: (row) => row.item_code || '--' },
|
{ prop: 'item_code', label: '商品编码', minWidth: 120, formatter: (row) => row.item_code || '--' },
|
||||||
{ prop: 'barcode', label: '条码', minWidth: 140, formatter: (row) => row.barcode || '--' },
|
{ prop: 'barcode', label: '条码', minWidth: 140, formatter: (row) => row.barcode || '--' },
|
||||||
{ prop: 'stock_id', label: '商品ID', width: 96, align: 'center', formatter: (row) => formatReportNumber(row.stock_id) },
|
{ prop: 'stock_id', label: 'SKU', width: 96, align: 'center', formatter: (row) => formatReportCell(row.stock_id) },
|
||||||
{ prop: 'begin_total', label: '期初数量', width: 104, align: 'right', formatter: (row) => formatReportNumber(row.begin_total) },
|
{
|
||||||
{ prop: 'inbound_total', label: '区间内入库', width: 112, align: 'right', formatter: (row) => formatReportNumber(row.inbound_total) },
|
prop: 'begin_total',
|
||||||
{ prop: 'outbound_total', label: '区间内出库', width: 112, align: 'right', formatter: (row) => formatReportNumber(row.outbound_total) },
|
label: '期初数量',
|
||||||
{ prop: 'end_total', label: '期末数量', width: 104, align: 'right', formatter: (row) => formatReportNumber(row.end_total) },
|
width: 104,
|
||||||
|
align: 'right',
|
||||||
|
sortable: 'custom',
|
||||||
|
formatter: (row) => formatReportNumber(row.begin_total)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
prop: 'inbound_total',
|
||||||
|
label: '区间内入库',
|
||||||
|
width: 112,
|
||||||
|
align: 'right',
|
||||||
|
sortable: 'custom',
|
||||||
|
formatter: (row) => formatReportNumber(row.inbound_total)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
prop: 'outbound_total',
|
||||||
|
label: '区间内出库',
|
||||||
|
width: 112,
|
||||||
|
align: 'right',
|
||||||
|
sortable: 'custom',
|
||||||
|
formatter: (row) => formatReportNumber(row.outbound_total)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
prop: 'end_total',
|
||||||
|
label: '期末数量',
|
||||||
|
width: 104,
|
||||||
|
align: 'right',
|
||||||
|
sortable: 'custom',
|
||||||
|
formatter: (row) => formatReportNumber(row.end_total)
|
||||||
|
},
|
||||||
{
|
{
|
||||||
prop: 'start_time',
|
prop: 'start_time',
|
||||||
label: '区间开始',
|
label: '区间开始',
|
||||||
@ -94,9 +123,7 @@ export const fixedReportColumns: ReportColumn[] = [
|
|||||||
width: 168,
|
width: 168,
|
||||||
align: 'center',
|
align: 'center',
|
||||||
formatter: (row) => formatReportDateTime(row.end_time)
|
formatter: (row) => formatReportDateTime(row.end_time)
|
||||||
},
|
}
|
||||||
{ prop: 'space_id', label: '库位ID', width: 96, align: 'center', formatter: (row) => formatReportNumber(row.space_id) },
|
|
||||||
{ prop: 'space_name', label: '库位', minWidth: 120, formatter: (row) => row.space_name || '--' }
|
|
||||||
]
|
]
|
||||||
|
|
||||||
const FIXED_PROPS = new Set(fixedReportColumns.map((c) => c.prop))
|
const FIXED_PROPS = new Set(fixedReportColumns.map((c) => c.prop))
|
||||||
@ -107,7 +134,7 @@ export function buildReportTableColumns(rows: StorageReportListItem[]) {
|
|||||||
for (const row of rows) {
|
for (const row of rows) {
|
||||||
if (!row || typeof row !== 'object') continue
|
if (!row || typeof row !== 'object') continue
|
||||||
for (const k of Object.keys(row)) {
|
for (const k of Object.keys(row)) {
|
||||||
if (k === '__rid' || FIXED_PROPS.has(k)) continue
|
if (k === '__rid' || k === 'store_id' || k === 'space_id' || k === 'space_name' || FIXED_PROPS.has(k)) continue
|
||||||
extra.add(k)
|
extra.add(k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,15 +156,50 @@ function normalizeReportDateParam(value: unknown): string | undefined {
|
|||||||
return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : String(value)
|
return d.isValid() ? d.format('YYYY-MM-DD HH:mm:ss') : String(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeReportStartDateParam(value: unknown): string | undefined {
|
||||||
|
if (value == null || value === '') return undefined
|
||||||
|
const d = dayjs(value as string | number | Date)
|
||||||
|
return d.isValid() ? d.startOf('day').format('YYYY-MM-DD HH:mm:ss') : String(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizeReportEndDateParam(value: unknown): string | undefined {
|
||||||
|
if (value == null || value === '') return undefined
|
||||||
|
const d = dayjs(value as string | number | Date)
|
||||||
|
return d.isValid() ? d.endOf('day').format('YYYY-MM-DD HH:mm:ss') : String(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDefaultReportPeriodRange(): [string, string] {
|
||||||
|
const today = dayjs().startOf('day')
|
||||||
|
const start = today.subtract(29, 'day')
|
||||||
|
return [start.format('YYYY-MM-DD'), today.format('YYYY-MM-DD')]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildDefaultReportScanCondition() {
|
||||||
|
return {
|
||||||
|
report_period: {
|
||||||
|
value: getDefaultReportPeriodRange(),
|
||||||
|
operator: 'between',
|
||||||
|
type: 'daterange'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildDefaultReportWhere() {
|
||||||
|
const [start, end] = getDefaultReportPeriodRange()
|
||||||
|
return {
|
||||||
|
start_time: normalizeReportStartDateParam(start),
|
||||||
|
end_time: normalizeReportEndDateParam(end)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** ScanBox 侧栏可用字段(与 storage.report.search 的 where 对应) */
|
/** ScanBox 侧栏可用字段(与 storage.report.search 的 where 对应) */
|
||||||
export const reportScanBoxAvailableFields = [
|
export const reportScanBoxAvailableFields = [
|
||||||
// {
|
{
|
||||||
// value: 'report_period',
|
value: 'report_period',
|
||||||
// label: '统计时间',
|
label: '统计时间',
|
||||||
// type: 'date',
|
type: 'daterange',
|
||||||
// operators: ['between'],
|
defaultValue: getDefaultReportPeriodRange()
|
||||||
// defaultOperator: 'between'
|
},
|
||||||
// },
|
|
||||||
{
|
{
|
||||||
value: 'store_id',
|
value: 'store_id',
|
||||||
label: '货主',
|
label: '货主',
|
||||||
@ -145,51 +207,53 @@ export const reportScanBoxAvailableFields = [
|
|||||||
multiple: true,
|
multiple: true,
|
||||||
filterable: true,
|
filterable: true,
|
||||||
options: [] as { label: string; value: number }[]
|
options: [] as { label: string; value: number }[]
|
||||||
},
|
|
||||||
{
|
|
||||||
value: 'by_space',
|
|
||||||
label: '聚合方式',
|
|
||||||
type: 'select',
|
|
||||||
options: [
|
|
||||||
{ value: false, label: '按商品汇总' },
|
|
||||||
{ value: true, label: '按库位' }
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
/** 将 ScanBox 条件转为接口扁平 where;无有效条件时返回 {} */
|
/** 将 ScanBox 条件转为接口扁平 where;无有效条件时返回 {} */
|
||||||
export function normalizeScanConditionToReportWhere(condition: any): Record<string, unknown> {
|
export function normalizeScanConditionToReportWhere(condition: any): Record<string, unknown> {
|
||||||
const where: Record<string, unknown> = {}
|
const where: Record<string, unknown> = {}
|
||||||
if (!condition?.conditions || typeof condition.conditions !== 'object') {
|
const conds = condition?.conditions && typeof condition.conditions === 'object'
|
||||||
|
? condition.conditions
|
||||||
|
: condition && typeof condition === 'object'
|
||||||
|
? condition
|
||||||
|
: null
|
||||||
|
|
||||||
|
if (!conds) {
|
||||||
return where
|
return where
|
||||||
}
|
}
|
||||||
const conds = condition.conditions
|
|
||||||
|
|
||||||
const period = conds.report_period
|
const period = conds.report_period?.value ?? conds.report_period
|
||||||
if (period?.value != null && period.value !== '') {
|
const startTime = conds.start_time?.value ?? conds.start_time
|
||||||
const v = period.value
|
const endTime = conds.end_time?.value ?? conds.end_time
|
||||||
if (Array.isArray(v) && v.length >= 2) {
|
const dateRangeValue = Array.isArray(period)
|
||||||
const a = normalizeReportDateParam(v[0])
|
? period
|
||||||
const b = normalizeReportDateParam(v[1])
|
: Array.isArray(startTime) && startTime.length >= 2
|
||||||
if (a) where.start_time = a
|
? startTime
|
||||||
if (b) where.end_time = b
|
: Array.isArray(endTime) && endTime.length >= 2
|
||||||
}
|
? endTime
|
||||||
|
: null
|
||||||
|
|
||||||
|
if (dateRangeValue && dateRangeValue.length >= 2) {
|
||||||
|
const a = normalizeReportStartDateParam(dateRangeValue[0])
|
||||||
|
const b = normalizeReportEndDateParam(dateRangeValue[1])
|
||||||
|
if (a) where.start_time = a
|
||||||
|
if (b) where.end_time = b
|
||||||
|
} else {
|
||||||
|
const a = normalizeReportStartDateParam(startTime)
|
||||||
|
const b = normalizeReportEndDateParam(endTime)
|
||||||
|
if (a) where.start_time = a
|
||||||
|
if (b) where.end_time = b
|
||||||
}
|
}
|
||||||
|
|
||||||
const sid = conds.store_id
|
const sid = conds.store_id?.value ?? conds.store_id
|
||||||
if (sid?.value != null && sid.value !== '') {
|
if (sid != null && sid !== '') {
|
||||||
const raw = sid.value
|
const raw = sid
|
||||||
const arr = (Array.isArray(raw) ? raw : [raw])
|
const arr = (Array.isArray(raw) ? raw : [raw])
|
||||||
.map((x: unknown) => Number(x))
|
.map((x: unknown) => Number(x))
|
||||||
.filter((n) => Number.isFinite(n))
|
.filter((n) => Number.isFinite(n))
|
||||||
if (arr.length) where.store_ids = arr
|
if (arr.length) where.store_ids = arr
|
||||||
}
|
}
|
||||||
|
|
||||||
const bs = conds.by_space
|
|
||||||
if (bs && bs.value !== undefined && bs.value !== null && bs.value !== '') {
|
|
||||||
const val = bs.value
|
|
||||||
where.by_space = val === true || val === 'true' || val === 1 || val === '1'
|
|
||||||
}
|
|
||||||
|
|
||||||
return where
|
return where
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user