zhou zhou
2026-03-30 40905cbd04c2e332cd4bc2b9e0c5b3e1da9cccfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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 }