fix(web-admin): 质检导出附件包含工单级附件 (#186) #187

Merged
cykj merged 1 commits from fix/qc-export-work-order-attachments into V2.0 2026-12-02 12:29:05 +00:00
3 changed files with 200 additions and 0 deletions

No files matched your search

@@ -0,0 +1,19 @@
# 质检导出附件包含工单级附件
关联 Gitea #186。
## 目标
质检工单「导出附件」在保留任务附件的同时,打包工单级 `attachments`。
## 设计
- 仅改 `web-admin`;列表已返回 `attachments`,下载走现有接口。
- ZIP 路径:`{工单文件夹}/工单附件/{文件名}`。
- 仅有工单附件也可导出;两者皆无仍提示暂无附件。
## 验收
1. 有工单附件时 zip 含 `工单附件/` 目录。
2. 任务附件结构不变。
3. 仅工单附件可导出;皆无提示「所选工单暂无附件」。
@@ -0,0 +1,151 @@
import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import { register } from 'node:module'
import { pathToFileURL } from 'node:url'
// 隔离附件导出模块的 HTTP / 打包依赖,只测 zip 条目构建。
const emptyStub =
'data:text/javascript,' +
encodeURIComponent(`
export function fetchAttachmentDownloadUrl() { return Promise.resolve(null) }
export async function downloadAttachmentFile() {}
export function attachmentFullUrl(url) { return String(url || '') }
export default class JSZip { file() {} async generateAsync() { return new Blob() } }
export const saveAs = () => {}
`)
const hookSource = `
const emptyStub = ${JSON.stringify(emptyStub)}
export async function resolve(specifier, context, nextResolve) {
const base = String(specifier).split('?')[0]
if (
base === '@/api/business' ||
base.endsWith('/api/business') ||
base === '@/utils/download-attachment' ||
base.endsWith('/utils/download-attachment') ||
base === '@/composables/useAttachmentActions' ||
base.endsWith('/composables/useAttachmentActions') ||
base === 'jszip' ||
base === 'file-saver'
) {
return { shortCircuit: true, url: emptyStub }
}
return nextResolve(specifier, context)
}
`
register('data:text/javascript,' + encodeURIComponent(hookSource), pathToFileURL('./'))
const { buildQcWorkOrderZipEntries } = await import('./qc-work-order-attachment-export')
type WorkOrderLike = {
id: string
workOrderNo: string
workOrderType?: string
labStorageCode?: string
sampleCode?: string
attachments?: string
}
type BatchLike = {
id: string
batchNo: string
batchName: string
assigneeUserIds: string[]
tasks?: Array<{
id: string
taskNo?: string
itemName?: string
taskType?: string
taskStatus?: string
attachmentFiles?: Array<{ fileUrl: string; fileName?: string }>
}>
}
function wo(partial: WorkOrderLike): WorkOrderLike {
return { workOrderType: 'inbound', ...partial }
}
describe('buildQcWorkOrderZipEntries — work order attachments', () => {
it('includes work-order-level attachments under 工单附件/', () => {
const workOrders = [
wo({
id: 'wo-1',
workOrderNo: 'QWO-001',
labStorageCode: 'LAB01',
attachments: 'files/a.pdf,files/b.png'
})
]
const batchMap: Record<string, BatchLike[]> = { 'wo-1': [] }
const entries = buildQcWorkOrderZipEntries(workOrders as any, batchMap as any)
assert.equal(entries.length, 2)
assert.ok(entries.every((e) => e.zipPath.includes('/工单附件/')))
assert.ok(entries.some((e) => e.fileUrl === 'files/a.pdf' && e.zipPath.endsWith('/a.pdf')))
assert.ok(entries.some((e) => e.fileUrl === 'files/b.png' && e.zipPath.endsWith('/b.png')))
})
it('exports when only work-order attachments exist (no task files)', () => {
const workOrders = [
wo({
id: 'wo-2',
workOrderNo: 'QWO-002',
labStorageCode: 'LAB02',
attachments: 'minio/only-wo.docx'
})
]
const entries = buildQcWorkOrderZipEntries(workOrders as any, { 'wo-2': [] })
assert.equal(entries.length, 1)
assert.match(entries[0].zipPath, /\/工单附件\/only-wo\.docx$/)
})
it('keeps task attachments and adds work-order attachments together', () => {
const workOrders = [
wo({
id: 'wo-3',
workOrderNo: 'QWO-003',
labStorageCode: 'LAB03',
attachments: 'wo/report.pdf'
})
]
const batchMap: Record<string, BatchLike[]> = {
'wo-3': [
{
id: 'b1',
batchNo: 'B1',
batchName: '批次A',
assigneeUserIds: [],
tasks: [
{
id: 't1',
taskNo: 'T1',
itemName: '活率',
taskType: 'sysjc',
taskStatus: 'ywc',
attachmentFiles: [{ fileUrl: 'task/result.pdf', fileName: 'result.pdf' }]
}
]
}
]
}
const entries = buildQcWorkOrderZipEntries(workOrders as any, batchMap as any)
assert.equal(entries.length, 2)
assert.ok(entries.some((e) => e.zipPath.includes('/工单附件/') && e.fileUrl === 'wo/report.pdf'))
assert.ok(
entries.some(
(e) =>
e.fileUrl === 'task/result.pdf' &&
e.zipPath.includes('批次A') &&
!e.zipPath.includes('/工单附件/')
)
)
})
it('returns empty when neither work-order nor task attachments exist', () => {
const workOrders = [wo({ id: 'wo-4', workOrderNo: 'QWO-004', attachments: '' })]
const entries = buildQcWorkOrderZipEntries(workOrders as any, { 'wo-4': [] })
assert.equal(entries.length, 0)
})
})
@@ -92,6 +92,20 @@ function taskFolderName(
return ensureUniqueZipEntryName(base, used)
}
function parseCommaSeparatedAttachmentFiles(urls: string) {
return String(urls || '')
.split(',')
.map((u, i) => {
const fileUrl = u.trim()
if (!fileUrl) return null
const fileName = fileUrl.includes('/')
? fileUrl.split('/').pop() || `附件${i + 1}`
: fileUrl
return { fileUrl, fileName }
})
.filter(Boolean) as Array<{ fileUrl: string; fileName: string }>
}
export function buildQcWorkOrderZipEntries(
workOrders: QcWorkOrder[],
batchMap: Record<string, QcInspectionBatch[]>
@@ -101,6 +115,22 @@ export function buildQcWorkOrderZipEntries(
for (const wo of workOrders) {
const woFolder = workOrderFolderName(wo, usedWorkOrderFolders)
const workOrderFiles = parseCommaSeparatedAttachmentFiles(wo.attachments || '')
if (workOrderFiles.length) {
const usedWorkOrderFileNames = new Set<string>()
const contextHint = [wo.workOrderNo, '工单附件'].filter(Boolean).join(' · ')
workOrderFiles.forEach((file, fileIndex) => {
const fileName = resolveAttachmentFileName(file, fileIndex)
const uniqueName = ensureUniqueZipEntryName(fileName, usedWorkOrderFileNames)
entries.push({
zipPath: `${woFolder}/工单附件/${uniqueName}`,
fileUrl: file.fileUrl,
fileName,
contextHint
})
})
}
const batches = batchMap[wo.id] || []
const usedBatchFolders = new Set<string>()