fix(web-admin): 科研捐赠导出补齐运费并按批次合并单元格

列表未返回样本运费批次导致运费列为空。导出按同一运费批次合并运输方式与运费列,并按模板对齐储存状态等字段。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-11-06 12:06:49 +08:00
1 parent 6be55186b0
commit d413398b3d
6 files changed
+193 -17

No files matched your search

@@ -198,6 +198,19 @@ const DELIVERY: Record<string, string> = {
pgc: '剖宫产'
}
/** 科研表模板多用「剖腹产」。 */
const RESEARCH_DELIVERY: Record<string, string> = {
sc: '顺产',
pgc: '剖腹产'
}
export type LedgerMergeRange = {
startRow: number
endRow: number
startCol: number
endCol: number
}
function text(value: unknown): string {
return value == null ? '' : String(value).trim()
}
@@ -299,10 +312,55 @@ function statusText(status: unknown, voidType: unknown): string {
return reason ? `${base}(${reason})` : base
}
/** 科研表储存状态:废弃写类别文案,正常样本留空,对齐线下大表。 */
function researchStorageStatus(status: unknown, voidType: unknown): string {
const code = text(status)
if (code === 'yzf') return voidTypeText(voidType)
if (code === 'sdfq') return PROTOCOL_STATUS.sdfq
return ''
}
function voidReason(row: Row): string {
return text(row.voidDesc) || voidTypeText(row.voidType)
}
function researchTransportText(row: ProtocolAuditFlatRow): string {
return text(row.freightTransportLabel) || transportText(row)
}
function researchFreightText(row: ProtocolAuditFlatRow): string {
return text(row.freightAmountLabel)
}
function researchMaternalBloodText(value: unknown): string {
const formatted = formatLedgerDateTime(value)
if (formatted) return formatted
return text(value) ? text(value) : '无母血'
}
/** 相邻且 freightId 相同的导出行,合并运输方式/运费列(Excel 行号含表头)。 */
export function buildFreightMergeRanges(freightIds: string[]): LedgerMergeRange[] {
const merges: LedgerMergeRange[] = []
let index = 0
while (index < freightIds.length) {
const freightId = text(freightIds[index])
if (!freightId) {
index += 1
continue
}
let end = index + 1
while (end < freightIds.length && text(freightIds[end]) === freightId) end += 1
if (end - index >= 2) {
const startRow = index + 2
const endRow = end + 1
merges.push({ startRow, endRow, startCol: 17, endCol: 17 })
merges.push({ startRow, endRow, startCol: 18, endCol: 18 })
}
index = end
}
return merges
}
/** 导出前加载作废类型字典,避免列表外的编码原样写入 Excel。 */
export async function prepareCollectionLedgerExport(): Promise<void> {
const { loadVoidTypeDict, getVoidTypeOptions } = await import('./void-type-dict')
@@ -439,7 +497,7 @@ function storageLedgerRow(row: ProtocolAuditFlatRow, index: number, research: bo
cells[3] = dayText(collectTime)
cells[4] = text(protocol.monthlyVerifyNo)
cells[5] = text(row.labStorageCode)
cells[6] = statusText(status, protocol.voidType)
cells[6] = researchStorageStatus(status, protocol.voidType)
cells[7] = voidReason(protocol)
cells[8] = text(row.motherName)
cells[9] = text(row.sampleTypeName)
@@ -449,12 +507,13 @@ function storageLedgerRow(row: ProtocolAuditFlatRow, index: number, research: bo
cells[13] = formatLedgerDateTime(collectTime)
cells[14] = text(row.salesUserName)
cells[15] = text(row.regionManagerUserName)
cells[16] = transportText(row)
cells[16] = researchTransportText(row)
cells[17] = researchFreightText(row)
cells[18] = label(SEX, row.newbornSex)
cells[19] = weightText(row.newbornWeightGram)
cells[20] = label(DELIVERY, row.deliveryMode)
cells[20] = label(RESEARCH_DELIVERY, row.deliveryMode) || label(DELIVERY, row.deliveryMode)
cells[21] = text(row.birthOrder)
cells[22] = formatLedgerDateTime(row.maternalBloodCollectTime)
cells[22] = researchMaternalBloodText(row.maternalBloodCollectTime)
cells[23] = text(row.consentFormUrls) ? '1' : ''
cells[24] = text(row.remark)
return cells
@@ -506,8 +565,23 @@ export function buildCellStorageLedgerRows(rows: ProtocolAuditFlatRow[]): string
return rows.map((row, index) => storageLedgerRow(row, index, false))
}
export function buildResearchLedgerRows(rows: ProtocolAuditFlatRow[]): string[][] {
return rows.map((row, index) => storageLedgerRow(row, index, true))
export function buildResearchLedgerRows(rows: ProtocolAuditFlatRow[]): {
rows: string[][]
merges: LedgerMergeRange[]
} {
const freightIds = rows.map((row) => text(row.freightId))
const built = rows.map((row, index) => storageLedgerRow(row, index, true))
const merges = buildFreightMergeRanges(freightIds)
// 合并区域内仅保留首行运输/运费,其余清空,避免合并后重复显示
for (const merge of merges) {
if (merge.startCol !== 17 && merge.startCol !== 18) continue
const colIndex = merge.startCol - 1
for (let excelRow = merge.startRow + 1; excelRow <= merge.endRow; excelRow += 1) {
const dataIndex = excelRow - 2
if (dataIndex >= 0 && dataIndex < built.length) built[dataIndex][colIndex] = ''
}
}
return { rows: built, merges }
}
export function buildCustomerServiceLedgerRows(rows: Row[]): string[][] {
@@ -576,7 +650,8 @@ export async function saveCollectionLedgerWorkbook(
sheetName: string,
headers: string[],
rows: string[][],
filenamePrefix: string
filenamePrefix: string,
merges: LedgerMergeRange[] = []
) {
const ExcelJS = (await import('exceljs')).default
const FileSaver = (await import('file-saver')).default
@@ -593,8 +668,15 @@ export async function saveCollectionLedgerWorkbook(
const cell = added.getCell(index + 1)
cell.value = value
cell.numFmt = '@'
cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }
})
})
merges.forEach((merge) => {
if (merge.endRow <= merge.startRow) return
sheet.mergeCells(merge.startRow, merge.startCol, merge.endRow, merge.endCol)
const cell = sheet.getCell(merge.startRow, merge.startCol)
cell.alignment = { vertical: 'middle', horizontal: 'center', wrapText: true }
})
headers.forEach((header, index) => {
sheet.getColumn(index + 1).width = Math.min(28, Math.max(12, header.replace(/\n/g, '').length + 2))
})