550 lines
17 KiB
Vue
550 lines
17 KiB
Vue
<template>
|
||
<div class="order-sub-container" :style="{ height: containerHeight }">
|
||
<scan-box
|
||
ref="scanBox"
|
||
cache-key="storage-report"
|
||
:width="reportScanBoxConfig.width"
|
||
:tree-props="reportScanBoxConfig.treeProps"
|
||
:auto-submit="reportScanBoxConfig.autoSubmit"
|
||
:preset-tree="presetTree"
|
||
:available-fields="availableFieldsRef"
|
||
@condition-change="handleConditionChange"
|
||
@reset="handleConditionReset"
|
||
@condition-clear="handleConditionClear"
|
||
/>
|
||
|
||
<div class="main-content">
|
||
<div class="toolbar">
|
||
<div class="toolbar-left">
|
||
<el-button size="small" @click="loadData">刷新</el-button>
|
||
<ExportModeDropdown
|
||
:loading="exportLoading"
|
||
label="导出"
|
||
default-action="handleExportOnlyInOut"
|
||
:items="exportDropdownItems"
|
||
@action="handleExportAction"
|
||
/>
|
||
<el-button size="small" @click="openColumnConfigPanel">字段设置</el-button>
|
||
<el-button size="small" @click="toggleDetailVisible">{{ detailVisible ? '隐藏详情' : '显示详情' }}</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="split-stack" ref="splitStackRef">
|
||
<div class="upper-wrapper" :style="detailVisible ? upperPaneStyle : upperPaneFullStyle">
|
||
<upper-table
|
||
ref="upperTable"
|
||
row-key="__rid"
|
||
title="报表明细"
|
||
default-align="center"
|
||
table-id="storage-report-list"
|
||
:enable-column-config="true"
|
||
:data="list"
|
||
:columns="columns"
|
||
:table-size="tableSize"
|
||
:pagination="pagination"
|
||
:show-index="true"
|
||
show-selection
|
||
:loading="loading"
|
||
:query-time="queryTime"
|
||
@current-change="handleRowClick"
|
||
@selection-change="handleSelectionChange"
|
||
@sort-change="handleSortChange"
|
||
@page-change="handlePageChange"
|
||
@page-size-change="handlePageSizeChange"
|
||
@refresh="loadData"
|
||
>
|
||
<template #empty>
|
||
<div class="empty-tip">暂无数据,可在左侧调整筛选条件</div>
|
||
</template>
|
||
</upper-table>
|
||
</div>
|
||
|
||
<template v-if="detailVisible">
|
||
<div
|
||
class="pane-resizer"
|
||
title="拖动调整列表与详情区域高度"
|
||
@mousedown="beginSplitDrag"
|
||
/>
|
||
<div v-if="selectedRow" class="lower-level" :class="tableSize" :style="lowerPaneStyle">
|
||
<div class="detail-header">
|
||
<el-tabs v-model="activeTab" class="detail-tab">
|
||
<el-tab-pane label="库存明细" name="detail" />
|
||
</el-tabs>
|
||
</div>
|
||
<div class="detail-container">
|
||
<div v-if="activeTab === 'detail'" class="detail-page">
|
||
<el-descriptions :column="2" border size="small">
|
||
<el-descriptions-item
|
||
v-for="key in detailKeys"
|
||
:key="key"
|
||
:label="fieldLabel(key)"
|
||
:span="isWideKey(key) ? 2 : 1"
|
||
>
|
||
{{ detailValue(key) }}
|
||
</el-descriptions-item>
|
||
</el-descriptions>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div v-else class="no-selection" :style="lowerPaneStyle">
|
||
<div class="empty-tip">在表格中点击一行可查看该行完整字段</div>
|
||
</div>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
|
||
<AsyncExportProgressDialog
|
||
v-model:visible="exportDialogVisible"
|
||
title="库存报表导出进度"
|
||
:loading="exportLoading"
|
||
:progress-percent="exportProgressPercent"
|
||
:progress-status="exportProgressStatus"
|
||
:status-label="exportStatusLabel"
|
||
:task-id="exportTaskProgress.taskId"
|
||
:message="exportTaskProgress.message"
|
||
:error="exportTaskProgress.error"
|
||
:update-time="exportTaskProgress.updateTime"
|
||
/>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, reactive, ref } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import dayjs from 'dayjs'
|
||
import ScanBox from '@/components/ScanBox/index.vue'
|
||
import UpperTable from '@/components/UpperTable/index.vue'
|
||
import ExportModeDropdown from '@/components/ExportModeDropdown.vue'
|
||
import type { ExportDropdownItem } from '@/components/ExportModeDropdown.vue'
|
||
import AsyncExportProgressDialog from '@/components/AsyncExportProgressDialog.vue'
|
||
import { useAsyncExportTask } from '@/composables/useAsyncExportTask'
|
||
import {
|
||
exportInventoryReport,
|
||
getExportTaskProgress,
|
||
searchInventoryReport,
|
||
type StockReportExportBody,
|
||
type StockReportSearchBody
|
||
} from '@/api/stock.report/stock.report'
|
||
import * as indexApi from '@/api/index/index'
|
||
import {
|
||
buildReportTableColumns,
|
||
formatReportCell,
|
||
formatReportDateTime,
|
||
formatReportNumber,
|
||
normalizeScanConditionToReportWhere,
|
||
reportFieldLabels,
|
||
reportScanBoxAvailableFields,
|
||
reportScanBoxConfig,
|
||
sortReportDetailKeys
|
||
} from './report-config'
|
||
import { useSettingsStore } from '@/stores/modules/settings'
|
||
import { useVerticalSplitPane } from '@/composables/useVerticalSplitPane'
|
||
|
||
const scanBox = ref<any>(null)
|
||
const upperTable = ref<any>(null)
|
||
|
||
const presetTree = ref<any[]>([])
|
||
const availableFieldsRef = reactive(JSON.parse(JSON.stringify(reportScanBoxAvailableFields)))
|
||
|
||
const settingsStore = useSettingsStore()
|
||
const shortcutExpanded = computed(() => settingsStore.shortcutExpanded)
|
||
const containerHeight = computed(() =>
|
||
shortcutExpanded.value ? 'calc(100vh - 150px)' : 'calc(100vh - 100px)'
|
||
)
|
||
|
||
const {
|
||
splitStackRef,
|
||
beginSplitDrag,
|
||
lowerPaneStyle,
|
||
upperPaneStyle
|
||
} = useVerticalSplitPane({ defaultLowerPercent: 40 })
|
||
|
||
const upperPaneFullStyle = computed(() => ({
|
||
flex: '1 1 auto',
|
||
minHeight: '0',
|
||
overflow: 'hidden'
|
||
}))
|
||
|
||
const detailVisible = ref(true)
|
||
const activeTab = ref('detail')
|
||
|
||
function toggleDetailVisible() {
|
||
detailVisible.value = !detailVisible.value
|
||
}
|
||
|
||
function openColumnConfigPanel() {
|
||
upperTable.value?.openColumnConfig?.()
|
||
}
|
||
|
||
const tableSize = ref<'small' | 'default' | 'large'>('small')
|
||
const loading = ref(false)
|
||
const queryTime = ref(0)
|
||
|
||
const {
|
||
exportLoading,
|
||
exportDialogVisible,
|
||
exportTaskProgress,
|
||
exportProgressPercent,
|
||
exportProgressStatus,
|
||
exportStatusLabel,
|
||
submitExportTask,
|
||
submitExportTaskWithMode,
|
||
resumeExportTaskFromStorage,
|
||
hasPendingExportTask
|
||
} = useAsyncExportTask({
|
||
storageKey: 'storage.report.export.taskId',
|
||
submitExport: (payload) => exportInventoryReport(payload as StockReportExportBody),
|
||
queryProgress: (params) => getExportTaskProgress(params)
|
||
})
|
||
|
||
const list = ref<any[]>([])
|
||
const columns = ref(buildReportTableColumns([]))
|
||
|
||
const pagination = reactive({
|
||
currentPage: 1,
|
||
pageSize: 20,
|
||
total: 0
|
||
})
|
||
const currentSort = reactive<{ prop: string; order: '' | 'ascending' | 'descending' }>({
|
||
prop: '',
|
||
order: ''
|
||
})
|
||
|
||
const selectedRid = ref<string | null>(null)
|
||
const currentConditionRaw = ref<any>({})
|
||
|
||
const selectedRow = computed(() => list.value.find((r) => r.__rid === selectedRid.value) || null)
|
||
|
||
const detailKeys = computed(() => {
|
||
if (!selectedRow.value) return []
|
||
const keys = Object.keys(selectedRow.value).filter((k) => k !== '__rid')
|
||
return sortReportDetailKeys(keys)
|
||
})
|
||
|
||
function fieldLabel(key: string) {
|
||
return reportFieldLabels[key] || key
|
||
}
|
||
|
||
function detailValue(key: string) {
|
||
if (!selectedRow.value) return '--'
|
||
const v = (selectedRow.value as any)[key]
|
||
if (key === 'start_time' || key === 'end_time') return formatReportDateTime(v)
|
||
if (key === 'stock_id') return formatReportCell(v)
|
||
if (
|
||
key === 'begin_total' ||
|
||
key === 'end_total' ||
|
||
key === 'inbound_total' ||
|
||
key === 'outbound_total' ||
|
||
key === 'store_id' ||
|
||
key === 'space_id'
|
||
) {
|
||
return formatReportNumber(v)
|
||
}
|
||
return formatReportCell(v)
|
||
}
|
||
|
||
function isWideKey(key: string) {
|
||
return detailValue(key).length > 80
|
||
}
|
||
|
||
function withRowIds(rows: any[]) {
|
||
return rows.map((row, idx) => ({
|
||
...row,
|
||
__rid: `r-${idx}-${row.stock_id ?? ''}-${row.store_id ?? ''}-${row.item_code ?? ''}`
|
||
}))
|
||
}
|
||
|
||
const SORTABLE_FIELDS = new Set(['begin_total', 'inbound_total', 'outbound_total', 'end_total'])
|
||
|
||
/** 排序参数(与其他列表接口一致:sort:{ sortField, sortOrder },如 stock.transaction.list) */
|
||
function buildSortPayload(): StockReportSearchBody['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, sortOrder }
|
||
}
|
||
|
||
/** 查询与导出共用的业务条件(不含分页,导出走全量) */
|
||
function buildReportQueryPayload(): StockReportSearchBody {
|
||
const where = normalizeScanConditionToReportWhere(currentConditionRaw.value)
|
||
const sort = buildSortPayload()
|
||
const body: StockReportSearchBody = {}
|
||
if (Object.keys(where).length > 0) {
|
||
body.where = where
|
||
}
|
||
if (sort) {
|
||
body.sort = sort
|
||
}
|
||
return body
|
||
}
|
||
|
||
/** 列表查询请求体:在业务条件基础上附加分页参数 */
|
||
function buildReportSearchPayload(): StockReportSearchBody {
|
||
const body = buildReportQueryPayload()
|
||
body.pagination = { page: pagination.currentPage, pageSize: pagination.pageSize }
|
||
return body
|
||
}
|
||
|
||
/** 解析响应分页信息(兼容 res.data / res 两层结构) */
|
||
function parseReportPagination(res: any) {
|
||
const payload = res?.data && typeof res.data === 'object' ? res.data : res
|
||
const pg = payload?.pagination && typeof payload.pagination === 'object' ? payload.pagination : {}
|
||
return {
|
||
page: Number(pg.page) > 0 ? Number(pg.page) : pagination.currentPage,
|
||
pageSize: Number(pg.pageSize) > 0 ? Number(pg.pageSize) : pagination.pageSize,
|
||
total: Number(pg.total) > 0 ? Number(pg.total) : 0
|
||
}
|
||
}
|
||
|
||
/** 表格勾选的行(用于「导出选中」) */
|
||
const selectionRows = ref<any[]>([])
|
||
|
||
function handleSelectionChange(rows: any[]) {
|
||
selectionRows.value = rows || []
|
||
}
|
||
|
||
/** 导出选项:全部=当前筛选条件下全部;选中=勾选行(stock_id in 叠加当前筛选)。mode 语义:item=含 LOCK;不传=仅出入库 */
|
||
const exportDropdownItems: ExportDropdownItem[] = [
|
||
{ label: '仅出入库(不含锁定记录)', action: 'handleExportOnlyInOut' },
|
||
{ label: '全部(含锁定记录)', action: 'handleExportWithLock', divided: true },
|
||
{ label: '导出选中·仅出入库', action: 'handleExportSelectedOnlyInOut', divided: true },
|
||
{ label: '导出选中·全部(含锁定记录)', action: 'handleExportSelectedWithLock' }
|
||
]
|
||
|
||
/** 勾选行的有效 stock_id(去重去空;后端数组即 in 匹配,参考 inventory 导出选中写法) */
|
||
function collectSelectedStockIds(): number[] {
|
||
return Array.from(
|
||
new Set(
|
||
selectionRows.value
|
||
.map((row: any) => Number(row.stock_id))
|
||
.filter((id: number) => Number.isFinite(id) && id > 0)
|
||
)
|
||
)
|
||
}
|
||
|
||
/** 导出选中请求体:在 buildReportQueryPayload 的 where 基础上叠加 stock_id in */
|
||
function buildSelectedExportPayload(): Record<string, unknown> {
|
||
const stockIds = collectSelectedStockIds()
|
||
const body = buildReportQueryPayload()
|
||
const where: Record<string, any> = body.where ? { ...body.where } : {}
|
||
where.stock_id = stockIds.length === 1 ? stockIds[0] : stockIds
|
||
return { ...body, where }
|
||
}
|
||
|
||
/** 仅出入库:不传 export_mode,后端「变更记录」sheet 排除 LOCK,仅含出入库/移库等变动 */
|
||
async function handleExportOnlyInOut() {
|
||
if (hasPendingExportTask()) {
|
||
ElMessage.warning('检测到未完成导出任务,请等待当前任务完成后再试')
|
||
resumeExportTaskFromStorage()
|
||
return
|
||
}
|
||
await submitExportTask(buildReportQueryPayload() as Record<string, unknown>)
|
||
}
|
||
|
||
/** 全部:传 export_mode='item',后端「变更记录」sheet 含 LOCK(锁定/解锁)记录 */
|
||
async function handleExportWithLock() {
|
||
if (hasPendingExportTask()) {
|
||
ElMessage.warning('检测到未完成导出任务,请等待当前任务完成后再试')
|
||
resumeExportTaskFromStorage()
|
||
return
|
||
}
|
||
await submitExportTaskWithMode(buildReportQueryPayload() as Record<string, unknown>, 'item')
|
||
}
|
||
|
||
/** 导出选中·仅出入库(不传 export_mode) */
|
||
async function handleExportSelectedOnlyInOut() {
|
||
if (!collectSelectedStockIds().length) {
|
||
ElMessage.warning('请先在表格勾选要导出的记录')
|
||
return
|
||
}
|
||
if (hasPendingExportTask()) {
|
||
ElMessage.warning('检测到未完成导出任务,请等待当前任务完成后再试')
|
||
resumeExportTaskFromStorage()
|
||
return
|
||
}
|
||
await submitExportTask(buildSelectedExportPayload())
|
||
}
|
||
|
||
/** 导出选中·全部(含锁定记录,export_mode='item') */
|
||
async function handleExportSelectedWithLock() {
|
||
if (!collectSelectedStockIds().length) {
|
||
ElMessage.warning('请先在表格勾选要导出的记录')
|
||
return
|
||
}
|
||
if (hasPendingExportTask()) {
|
||
ElMessage.warning('检测到未完成导出任务,请等待当前任务完成后再试')
|
||
resumeExportTaskFromStorage()
|
||
return
|
||
}
|
||
await submitExportTaskWithMode(buildSelectedExportPayload(), 'item')
|
||
}
|
||
|
||
function handleExportAction(action: string) {
|
||
if (action === 'handleExportWithLock') {
|
||
void handleExportWithLock()
|
||
return
|
||
}
|
||
if (action === 'handleExportSelectedOnlyInOut') {
|
||
void handleExportSelectedOnlyInOut()
|
||
return
|
||
}
|
||
if (action === 'handleExportSelectedWithLock') {
|
||
void handleExportSelectedWithLock()
|
||
return
|
||
}
|
||
void handleExportOnlyInOut()
|
||
}
|
||
|
||
async function loadFilterData() {
|
||
try {
|
||
const [storesRes, presetRes]: any = await Promise.all([
|
||
indexApi.loadStores(),
|
||
indexApi.fastWhereTree('stock')
|
||
])
|
||
if (storesRes && storesRes.code === 200) {
|
||
const f = availableFieldsRef.find((i: any) => i.value === 'store_id')
|
||
if (f) f.options = storesRes.stores || []
|
||
}
|
||
if (presetRes && presetRes.code === 200) {
|
||
presetTree.value = presetRes.presetTree || []
|
||
}
|
||
} catch (e) {
|
||
console.error('加载筛选配置失败', e)
|
||
}
|
||
}
|
||
|
||
function handleConditionChange(condition: any) {
|
||
currentConditionRaw.value = condition
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleConditionClear() {
|
||
currentConditionRaw.value = {}
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleConditionReset() {
|
||
currentConditionRaw.value = {}
|
||
pagination.currentPage = 1
|
||
if (scanBox.value?.reset) {
|
||
scanBox.value.reset()
|
||
} else if (scanBox.value?.clear) {
|
||
scanBox.value.clear()
|
||
}
|
||
loadData()
|
||
}
|
||
|
||
async function loadData() {
|
||
if (loading.value) return
|
||
loading.value = true
|
||
const started = Date.now()
|
||
selectedRid.value = null
|
||
try {
|
||
const res: any = await searchInventoryReport(buildReportSearchPayload())
|
||
const payload = res?.data && typeof res.data === 'object' ? res.data : res
|
||
const ok =
|
||
res?.flag === 'success' ||
|
||
res?.code === 200 ||
|
||
res?.code === 1 ||
|
||
(payload && Array.isArray(payload.list))
|
||
if (ok) {
|
||
const raw = Array.isArray(payload?.list) ? payload.list : []
|
||
list.value = withRowIds(raw)
|
||
columns.value = buildReportTableColumns(raw)
|
||
const pg = parseReportPagination(res)
|
||
pagination.currentPage = pg.page
|
||
pagination.pageSize = pg.pageSize
|
||
pagination.total = pg.total
|
||
queryTime.value = Date.now() - started
|
||
if (raw.length === 0 && pagination.total === 0) {
|
||
ElMessage.info('当前条件下暂无数据')
|
||
}
|
||
} else {
|
||
list.value = []
|
||
columns.value = buildReportTableColumns([])
|
||
pagination.total = 0
|
||
ElMessage.error(payload?.message || res?.message || '查询失败')
|
||
}
|
||
} catch (e: any) {
|
||
list.value = []
|
||
columns.value = buildReportTableColumns([])
|
||
pagination.total = 0
|
||
ElMessage.error(e?.message || '请求失败')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleSortChange({ prop, order }: { prop: string; order: '' | 'ascending' | 'descending' | null }) {
|
||
currentSort.prop = prop || ''
|
||
currentSort.order = (order || '') as '' | 'ascending' | 'descending'
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handlePageChange(page: number) {
|
||
pagination.currentPage = page
|
||
loadData()
|
||
}
|
||
|
||
function handlePageSizeChange(size: number) {
|
||
pagination.pageSize = size
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleRowClick(row: any) {
|
||
selectedRid.value = row?.__rid ?? null
|
||
}
|
||
|
||
onMounted(() => {
|
||
loadFilterData()
|
||
loadData()
|
||
resumeExportTaskFromStorage()
|
||
})
|
||
</script>
|
||
|
||
|
||
<style scoped lang="scss">
|
||
@use "../../order/orderManagement/styles/order-sub.scss";
|
||
|
||
.order-sub-container .main-content .lower-level .detail-container {
|
||
flex: 1;
|
||
overflow: auto;
|
||
padding: 10px;
|
||
|
||
:deep(.el-descriptions__body) {
|
||
background: #fff;
|
||
}
|
||
|
||
:deep(.el-descriptions-item__cell) {
|
||
padding: 8px 12px;
|
||
}
|
||
}
|
||
|
||
.detail-page {
|
||
height: 100%;
|
||
overflow: auto;
|
||
}
|
||
|
||
.no-selection {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
color: #909399;
|
||
background-color: #fff;
|
||
|
||
.empty-tip {
|
||
text-align: center;
|
||
font-size: 14px;
|
||
}
|
||
}
|
||
|
||
.empty-tip {
|
||
text-align: center;
|
||
font-size: 13px;
|
||
}
|
||
</style>
|