const DEFAULT_MAX_RESULTS = 1000
|
const DEFAULT_PRINT_PAGE_SIZE = 20
|
const HIDDEN_EXPORT_COLUMNS = new Set(['selection', 'operation'])
|
const DEFAULT_REPORT_STYLE = {
|
titleAlign: 'center',
|
titleLevel: 'strong',
|
orientation: 'portrait',
|
density: 'compact',
|
showSequence: true,
|
showBorder: true
|
}
|
|
function normalizeSelectedIds(selectedRows = []) {
|
if (!Array.isArray(selectedRows)) {
|
return []
|
}
|
|
return selectedRows
|
.map((row) => row?.id)
|
.filter((id) => id !== void 0 && id !== null)
|
}
|
|
function normalizeFlatQueryParams(queryParams = {}) {
|
if (!queryParams || typeof queryParams !== 'object') {
|
return {}
|
}
|
|
const { current, pageSize, size, ...rest } = queryParams
|
const normalizedPageSize = pageSize ?? size
|
return {
|
...(current !== void 0 ? { current } : {}),
|
...(normalizedPageSize !== void 0 ? { pageSize: normalizedPageSize } : {}),
|
...rest
|
}
|
}
|
|
export function toExportColumns(columns = []) {
|
if (!Array.isArray(columns)) {
|
return []
|
}
|
|
return columns
|
.map((column) => {
|
if (!column || typeof column !== 'object') {
|
return null
|
}
|
|
const source = column.source ?? column.prop
|
const label = column.label
|
if (!source || !label || HIDDEN_EXPORT_COLUMNS.has(source)) {
|
return null
|
}
|
|
return {
|
source,
|
label,
|
...(column.align !== void 0 ? { align: column.align } : {})
|
}
|
})
|
.filter(Boolean)
|
}
|
|
export function buildReportStyleMeta({ reportTitle = '', meta = {} } = {}) {
|
const reportStyle = {
|
...DEFAULT_REPORT_STYLE,
|
...(meta?.reportStyle ?? {})
|
}
|
|
return {
|
...meta,
|
reportTitle,
|
reportStyle
|
}
|
}
|
|
export function buildPreviewColumns({ columns = [], reportStyle = {} } = {}) {
|
const normalizedColumns = toExportColumns(columns)
|
|
if (reportStyle?.showSequence === false) {
|
return normalizedColumns
|
}
|
|
return [
|
{ source: '__sequence__', label: '序号', align: 'center' },
|
...normalizedColumns
|
]
|
}
|
|
export function buildListExportPayload({
|
reportTitle = '',
|
selectedRows = [],
|
queryParams = {},
|
columns = [],
|
meta = {}
|
} = {}) {
|
const ids = normalizeSelectedIds(selectedRows)
|
const normalizedColumns = toExportColumns(columns)
|
const reportMeta = {
|
reportTitle,
|
...meta
|
}
|
|
if (ids.length > 0) {
|
return {
|
ids,
|
columns: normalizedColumns,
|
meta: reportMeta
|
}
|
}
|
|
return {
|
...normalizeFlatQueryParams(queryParams),
|
ids,
|
columns: normalizedColumns,
|
meta: reportMeta
|
}
|
}
|
|
export function buildPrintPageQuery({ queryParams = {}, total = 0, maxResults = DEFAULT_MAX_RESULTS } = {}) {
|
const normalizedQueryParams = normalizeFlatQueryParams(queryParams)
|
const normalizedTotal = Number(total)
|
const parsedMaxResults = Number(maxResults)
|
const normalizedMaxResults =
|
Number.isInteger(parsedMaxResults) && parsedMaxResults > 0 ? parsedMaxResults : DEFAULT_MAX_RESULTS
|
const fallbackPageSize = Number(normalizedQueryParams.pageSize)
|
|
let pageSize = DEFAULT_PRINT_PAGE_SIZE
|
if (Number.isFinite(normalizedTotal) && normalizedTotal > 0) {
|
pageSize = Math.min(normalizedTotal, normalizedMaxResults)
|
} else if (Number.isFinite(fallbackPageSize) && fallbackPageSize > 0) {
|
pageSize = Math.min(fallbackPageSize, normalizedMaxResults)
|
}
|
|
return {
|
...normalizedQueryParams,
|
current: 1,
|
pageSize
|
}
|
}
|
|
export function buildListPrintPayload({
|
selectedRows = [],
|
queryParams = {},
|
total = 0,
|
maxResults = DEFAULT_MAX_RESULTS
|
} = {}) {
|
const ids = normalizeSelectedIds(selectedRows)
|
if (ids.length > 0) {
|
return { ids }
|
}
|
|
return buildPrintPageQuery({ queryParams, total, maxResults })
|
}
|
|
export { DEFAULT_MAX_RESULTS }
|