批量保存写入 operateBatchId;弹窗按批次合并时间线,检测材料复用附件只读组件。 Co-authored-by: Cursor <cursoragent@cursor.com>
446 lines
14 KiB
Vue
446 lines
14 KiB
Vue
<!-- 质检任务 · 检测结果修改记录 -->
|
||
<template>
|
||
<el-dialog
|
||
:model-value="visible"
|
||
:title="dialogTitle"
|
||
width="800px"
|
||
class="dialog-form-layout qc-task-result-change-log-dialog"
|
||
align-center
|
||
modal-class="protocol-dialog-mask"
|
||
append-to-body
|
||
destroy-on-close
|
||
:close-on-click-modal="false"
|
||
@update:model-value="onVisibleChange"
|
||
>
|
||
<DialogFormBody :loading="loading" body-class="qc-result-chg-body">
|
||
<el-empty
|
||
v-if="!loading && !timelineNodes.length"
|
||
description="暂无修改记录"
|
||
:image-size="72"
|
||
/>
|
||
<div v-else class="qc-result-chg-tl">
|
||
<div
|
||
v-for="(node, idx) in timelineNodes"
|
||
:key="node.key"
|
||
class="qc-result-chg-tl__item"
|
||
>
|
||
<div class="qc-result-chg-tl__rail">
|
||
<span class="qc-result-chg-tl__dot" />
|
||
<span v-if="idx < timelineNodes.length - 1" class="qc-result-chg-tl__line" />
|
||
</div>
|
||
<div class="qc-result-chg-tl__content">
|
||
<div class="qc-result-chg-tl__meta">
|
||
{{ node.operateTime || '—' }} · {{ node.operatorUserName || '—' }}
|
||
<template v-if="node.entries.length > 1">
|
||
· 共 {{ node.entries.length }} 项
|
||
</template>
|
||
</div>
|
||
|
||
<div v-if="node.sharedAttachment" class="qc-result-chg-tl__change">
|
||
<div class="qc-result-chg-tl__line-text">
|
||
{{ node.sharedAttachment.fieldLabel }}:
|
||
<template
|
||
v-if="
|
||
node.sharedAttachment.oldValue &&
|
||
node.sharedAttachment.oldValue !== node.sharedAttachment.newValue
|
||
"
|
||
>
|
||
已更新
|
||
</template>
|
||
<template v-else-if="!node.sharedAttachment.newValue">已清空</template>
|
||
</div>
|
||
<ProtocolAttachmentReadonly
|
||
v-if="attachmentFilesFromValue(node.sharedAttachment.newValue).length"
|
||
size="compact"
|
||
:files="attachmentFilesFromValue(node.sharedAttachment.newValue)"
|
||
@preview="onAttachmentPreview"
|
||
@download="onAttachmentDownload"
|
||
/>
|
||
<span
|
||
v-else-if="!node.sharedAttachment.newValue"
|
||
class="qc-result-chg-tl__line-text"
|
||
>—</span>
|
||
</div>
|
||
|
||
<div
|
||
v-for="entry in node.entries"
|
||
:key="entry.id"
|
||
class="qc-result-chg-tl__entry"
|
||
>
|
||
<div
|
||
v-if="showItemName && entry.itemName"
|
||
class="qc-result-chg-tl__item-name"
|
||
>
|
||
{{ entry.itemName }}
|
||
</div>
|
||
|
||
<template v-if="visibleChanges(entry, node).length">
|
||
<div
|
||
v-for="(chg, cIdx) in visibleChanges(entry, node)"
|
||
:key="`${entry.id}-${cIdx}`"
|
||
class="qc-result-chg-tl__change"
|
||
>
|
||
<template v-if="isAttachmentChange(chg)">
|
||
<div class="qc-result-chg-tl__line-text">
|
||
{{ chg.fieldLabel }}:
|
||
<template v-if="chg.oldValue && chg.oldValue !== chg.newValue">
|
||
已更新
|
||
</template>
|
||
<template v-else-if="!chg.newValue">已清空</template>
|
||
</div>
|
||
<ProtocolAttachmentReadonly
|
||
v-if="attachmentFilesFromValue(chg.newValue).length"
|
||
size="compact"
|
||
:files="attachmentFilesFromValue(chg.newValue)"
|
||
@preview="onAttachmentPreview"
|
||
@download="onAttachmentDownload"
|
||
/>
|
||
<span
|
||
v-else-if="!chg.newValue"
|
||
class="qc-result-chg-tl__line-text"
|
||
>—</span>
|
||
</template>
|
||
<div v-else class="qc-result-chg-tl__line-text">
|
||
<template v-if="chg.oldValue">
|
||
{{ chg.fieldLabel }}:{{ chg.oldValue || '—' }} →
|
||
{{ chg.newValue || '—' }}
|
||
</template>
|
||
<template v-else>
|
||
{{ chg.fieldLabel }}:{{ chg.newValue || '—' }}
|
||
</template>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
<div
|
||
v-else-if="!entry.hasFieldChange || !entry.changes?.length"
|
||
class="qc-result-chg-tl__line-text"
|
||
>
|
||
保存操作(字段无变更)
|
||
</div>
|
||
</div>
|
||
|
||
<div v-if="node.allNoFieldChange" class="qc-result-chg-tl__note">
|
||
即使未改值,点击保存也会记一条操作留痕
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</DialogFormBody>
|
||
<template #footer>
|
||
<el-button class="btn-ghost" @click="close">
|
||
<el-icon><Close /></el-icon>关闭
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, ref, watch } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import { Close } from '@element-plus/icons-vue'
|
||
import DialogFormBody from '@/components/business/dialog-form-body/index.vue'
|
||
import ProtocolAttachmentReadonly from '@/views/cell-storage/protocol/ProtocolAttachmentReadonly.vue'
|
||
import { downloadAttachmentFile } from '@/utils/download-attachment'
|
||
import {
|
||
qcAttachmentFullUrl,
|
||
qcPreviewAttachment
|
||
} from './composables/useQcViewAttachmentHelpers'
|
||
import {
|
||
fetchQcTaskResultChangeLogsBatch,
|
||
type QcTaskResultChangeItem,
|
||
type QcTaskResultChangeLog
|
||
} from '@/api/qc-task-result'
|
||
|
||
defineOptions({ name: 'QcTaskResultChangeLogDialog' })
|
||
|
||
type TimelineNode = {
|
||
key: string
|
||
operateTime: string
|
||
operatorUserName: string
|
||
entries: QcTaskResultChangeLog[]
|
||
allNoFieldChange: boolean
|
||
/** 多任务同次提交且检测材料一致时,提到节点级只展示一次 */
|
||
sharedAttachment: QcTaskResultChangeItem | null
|
||
}
|
||
|
||
/** 无批次号的旧数据:同操作人且时间差 ≤15s 合并为一次提交 */
|
||
const LEGACY_GROUP_WINDOW_MS = 15_000
|
||
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
visible: boolean
|
||
taskIds: string[]
|
||
titleSuffix?: string
|
||
}>(),
|
||
{
|
||
titleSuffix: ''
|
||
}
|
||
)
|
||
|
||
const emit = defineEmits<{
|
||
'update:visible': [value: boolean]
|
||
}>()
|
||
|
||
const loading = ref(false)
|
||
const entries = ref<QcTaskResultChangeLog[]>([])
|
||
let loadSeq = 0
|
||
|
||
const showItemName = computed(() => (props.taskIds || []).length > 1)
|
||
|
||
const dialogTitle = computed(() => {
|
||
const suffix = (props.titleSuffix || '').trim()
|
||
return suffix ? `修改记录 · ${suffix}` : '修改记录'
|
||
})
|
||
|
||
const timelineNodes = computed(() => groupChangeLogs(entries.value))
|
||
|
||
function onVisibleChange(v: boolean) {
|
||
emit('update:visible', v)
|
||
}
|
||
|
||
function close() {
|
||
emit('update:visible', false)
|
||
}
|
||
|
||
function isAttachmentChange(chg: QcTaskResultChangeItem) {
|
||
return (chg.fieldKey || '') === 'attachments'
|
||
}
|
||
|
||
function pickSharedAttachment(rows: QcTaskResultChangeLog[]) {
|
||
if (rows.length < 2) return null
|
||
const attChanges = rows
|
||
.map((r) => (r.changes || []).find((c) => isAttachmentChange(c)))
|
||
.filter(Boolean) as QcTaskResultChangeItem[]
|
||
if (!attChanges.length || attChanges.length !== rows.length) return null
|
||
const first = attChanges[0]
|
||
const same = attChanges.every(
|
||
(c) => c.newValue === first.newValue && c.oldValue === first.oldValue
|
||
)
|
||
return same ? first : null
|
||
}
|
||
|
||
function visibleChanges(entry: QcTaskResultChangeLog, node: TimelineNode) {
|
||
const list = entry.changes || []
|
||
if (!node.sharedAttachment) return list
|
||
return list.filter((c) => !isAttachmentChange(c))
|
||
}
|
||
|
||
function attachmentFilesFromValue(raw: string | undefined) {
|
||
const text = String(raw || '').trim()
|
||
if (!text) return [] as Array<Record<string, unknown>>
|
||
return text
|
||
.split(',')
|
||
.map((u, i) => {
|
||
const url = u.trim()
|
||
if (!url) return null
|
||
const name = url.includes('/') ? url.split('/').pop() || `附件${i + 1}` : url
|
||
return {
|
||
id: `chg_att_${i}_${name}`,
|
||
fileUrl: url,
|
||
fileName: name
|
||
}
|
||
})
|
||
.filter(Boolean) as Array<Record<string, unknown>>
|
||
}
|
||
|
||
function onAttachmentPreview(file: Record<string, unknown>) {
|
||
qcPreviewAttachment(file)
|
||
}
|
||
|
||
function onAttachmentDownload(file: Record<string, unknown>) {
|
||
downloadAttachmentFile(file, { fullFileUrl: qcAttachmentFullUrl })
|
||
}
|
||
|
||
function parseOperateTimeMs(text: string | undefined) {
|
||
const s = String(text || '').trim()
|
||
if (!s) return NaN
|
||
const t = Date.parse(s.replace(/-/g, '/'))
|
||
return Number.isFinite(t) ? t : NaN
|
||
}
|
||
|
||
function groupChangeLogs(list: QcTaskResultChangeLog[]): TimelineNode[] {
|
||
const sorted = [...(list || [])].sort((a, b) => {
|
||
const tb = parseOperateTimeMs(b.operateTime)
|
||
const ta = parseOperateTimeMs(a.operateTime)
|
||
if (Number.isFinite(tb) && Number.isFinite(ta) && tb !== ta) return tb - ta
|
||
return String(b.id || '').localeCompare(String(a.id || ''))
|
||
})
|
||
|
||
const batchBuckets = new Map<string, QcTaskResultChangeLog[]>()
|
||
const orphans: QcTaskResultChangeLog[] = []
|
||
for (const row of sorted) {
|
||
const batch = String(row.operateBatchId || '').trim()
|
||
if (batch) {
|
||
const arr = batchBuckets.get(batch) || []
|
||
arr.push(row)
|
||
batchBuckets.set(batch, arr)
|
||
} else {
|
||
orphans.push(row)
|
||
}
|
||
}
|
||
|
||
const nodes: TimelineNode[] = []
|
||
for (const [batch, rows] of batchBuckets) {
|
||
nodes.push(toNode(`batch:${batch}`, rows))
|
||
}
|
||
|
||
// 旧数据兜底:按时间序贪心合并同操作人短窗口
|
||
const used = new Set<string>()
|
||
for (let i = 0; i < orphans.length; i++) {
|
||
const head = orphans[i]
|
||
if (used.has(head.id)) continue
|
||
const group = [head]
|
||
used.add(head.id)
|
||
const headTs = parseOperateTimeMs(head.operateTime)
|
||
const headOp = String(head.operatorUserId || head.operatorUserName || '')
|
||
for (let j = i + 1; j < orphans.length; j++) {
|
||
const cur = orphans[j]
|
||
if (used.has(cur.id)) continue
|
||
const curOp = String(cur.operatorUserId || cur.operatorUserName || '')
|
||
if (curOp !== headOp) continue
|
||
const curTs = parseOperateTimeMs(cur.operateTime)
|
||
if (!Number.isFinite(headTs) || !Number.isFinite(curTs)) continue
|
||
if (Math.abs(curTs - headTs) > LEGACY_GROUP_WINDOW_MS) continue
|
||
group.push(cur)
|
||
used.add(cur.id)
|
||
}
|
||
nodes.push(toNode(`legacy:${head.id}`, group))
|
||
}
|
||
|
||
return nodes.sort((a, b) => {
|
||
const tb = parseOperateTimeMs(b.operateTime)
|
||
const ta = parseOperateTimeMs(a.operateTime)
|
||
if (Number.isFinite(tb) && Number.isFinite(ta) && tb !== ta) return tb - ta
|
||
return a.key.localeCompare(b.key)
|
||
})
|
||
}
|
||
|
||
function toNode(key: string, rows: QcTaskResultChangeLog[]): TimelineNode {
|
||
const first = rows[0]
|
||
return {
|
||
key,
|
||
operateTime: first?.operateTime || '',
|
||
operatorUserName: first?.operatorUserName || '',
|
||
entries: rows,
|
||
allNoFieldChange: rows.every((r) => !r.hasFieldChange),
|
||
sharedAttachment: pickSharedAttachment(rows)
|
||
}
|
||
}
|
||
|
||
async function loadLogs() {
|
||
const seq = ++loadSeq
|
||
const ids = (props.taskIds || []).map((x) => (x || '').trim()).filter(Boolean)
|
||
loading.value = true
|
||
entries.value = []
|
||
try {
|
||
const list = ids.length ? await fetchQcTaskResultChangeLogsBatch(ids) : []
|
||
if (seq !== loadSeq) return
|
||
entries.value = Array.isArray(list) ? list : []
|
||
} catch {
|
||
if (seq !== loadSeq) return
|
||
entries.value = []
|
||
ElMessage.error('加载修改记录失败')
|
||
} finally {
|
||
if (seq === loadSeq) loading.value = false
|
||
}
|
||
}
|
||
|
||
watch(
|
||
() => [props.visible, (props.taskIds || []).join(',')] as const,
|
||
([vis]) => {
|
||
if (vis) void loadLogs()
|
||
}
|
||
)
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
:deep(.qc-result-chg-body) {
|
||
min-height: 480px;
|
||
max-height: min(70vh, 640px);
|
||
overflow-y: auto;
|
||
}
|
||
|
||
.qc-result-chg-tl {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
padding: 4px 0 8px;
|
||
min-height: 420px;
|
||
}
|
||
|
||
.qc-result-chg-tl__item {
|
||
display: flex;
|
||
gap: 12px;
|
||
}
|
||
|
||
.qc-result-chg-tl__rail {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
width: 12px;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.qc-result-chg-tl__dot {
|
||
width: 10px;
|
||
height: 10px;
|
||
margin-top: 4px;
|
||
border-radius: 50%;
|
||
background: var(--mint-primary, #10b981);
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.qc-result-chg-tl__line {
|
||
width: 2px;
|
||
flex: 1;
|
||
min-height: 36px;
|
||
margin-top: 4px;
|
||
background: var(--mint-primary-bg, #ecfdf5);
|
||
}
|
||
|
||
.qc-result-chg-tl__content {
|
||
flex: 1;
|
||
min-width: 0;
|
||
padding-bottom: 14px;
|
||
}
|
||
|
||
.qc-result-chg-tl__meta {
|
||
font-size: 12px;
|
||
font-weight: 600;
|
||
color: var(--mint-primary-dark, #059669);
|
||
margin-bottom: 8px;
|
||
}
|
||
|
||
.qc-result-chg-tl__entry {
|
||
margin-bottom: 10px;
|
||
|
||
&:last-child {
|
||
margin-bottom: 0;
|
||
}
|
||
}
|
||
|
||
.qc-result-chg-tl__item-name {
|
||
font-size: 13px;
|
||
font-weight: 600;
|
||
color: var(--mint-text, #1e293b);
|
||
margin-bottom: 4px;
|
||
}
|
||
|
||
.qc-result-chg-tl__change {
|
||
margin: 4px 0;
|
||
}
|
||
|
||
.qc-result-chg-tl__line-text {
|
||
font-size: 13px;
|
||
color: var(--mint-text, #1e293b);
|
||
margin: 2px 0;
|
||
word-break: break-word;
|
||
}
|
||
|
||
.qc-result-chg-tl__note {
|
||
margin-top: 6px;
|
||
font-size: 12px;
|
||
color: var(--mint-text-muted, #334155);
|
||
}
|
||
</style>
|