feat(shipping-notice): 新增发货通知重复检测与处理接口,支持重复决策保存与重新执行检测

This commit is contained in:
hugo committed 2026-07-14 21:12:36 +08:00
1 parent 729546a7b7
commit ceb49461e8
72 files changed
+14270 -460

No files matched your search

@@ -1,23 +1,31 @@
/**
* features/chat/shipping/useShippingNoticeIntakes.ts
* 用途:发货通知选择即上传、创建 intake 与批量状态轮询编排。
* 依赖:file-assets.api、shipping.api、shipping.types。
* 逻辑:useChatShipping / useChatAttachments 在货代 Agent 下委托本 composable 管理预解析附件。
* 依赖:file-assets.api、shipping.api、shipping.types、shippingNoticeAttachmentIntegrity。
* 逻辑:useChatShipping / useChatAttachments 在货代 Agent 下委托本 composable 管理预解析附件;
* 完整性判定委托 shippingNoticeAttachmentIntegrity(与后端契约对齐)。
*/
import { computed, onBeforeUnmount, ref, type Ref } from 'vue';
import { uploadFileAsset } from '@/shared/api/file-assets.api';
import type {
PendingAttachment,
ShippingDuplicateAction,
ShippingDuplicateResult,
ShippingNoticeParseSummary,
ShippingNoticePendingAttachment,
} from '../types/shipping.types';
import type { ShippingNoticeIntakeResponse } from '../types/shipping.api.types';
import {
cancelShippingNoticeIntake,
createShippingNoticeIntake,
getShippingNoticeIntakeStatuses,
listActiveShippingNoticeIntakes,
resolveShippingNoticeDuplicate,
retryShippingNoticeIntake,
} from '@/features/chat/api/shipping.api';
import type {
PendingAttachment,
ShippingNoticeParseSummary,
ShippingNoticePendingAttachment,
} from '../types/shipping.types';
import { isShippingNoticeAttachmentParseIncomplete } from './shippingNoticeAttachmentIntegrity';
export { isShippingNoticeAttachmentParseIncomplete } from './shippingNoticeAttachmentIntegrity';
const TERMINAL_PARSE = new Set(['parsed', 'failed', 'parse_failed', 'consumed', 'cancelled', 'expired']);
const ACTIVE_PARSE = new Set(['waiting', 'queued', 'parsing', 'created']);
@@ -34,6 +42,46 @@ function mapServerParseStatus(status: string): ShippingNoticePendingAttachment['
return 'waiting';
}
/**
* 将服务端 duplicate 结果映射为前端附件状态。
*/
function mapServerDuplicate(remote: ShippingNoticeIntakeResponse): {
duplicate?: ShippingDuplicateResult;
duplicateResolution?: ShippingNoticePendingAttachment['duplicateResolution'];
} {
const dup = remote.duplicate;
if (!dup) return {};
const duplicate: ShippingDuplicateResult = {
status: dup.status,
blocking: Boolean(dup.blocking),
fingerprintStrength: dup.fingerprint_strength,
matches: (dup.matches || []).map((item) => ({
taskNo: item.task_no,
processStatus: item.process_status,
documentVersion: Number(item.document_version || 1),
matchType: item.match_type,
matchedFields: listSafe(item.matched_fields),
createdAt: item.created_at,
})),
allowedActions: (dup.allowed_actions || []) as ShippingDuplicateAction[],
};
const resolution = remote.duplicate_resolution;
return {
duplicate,
duplicateResolution: resolution
? {
action: resolution.action,
targetTaskNo: resolution.target_task_no || undefined,
reason: resolution.reason || undefined,
}
: undefined,
};
}
function listSafe(value: unknown): string[] {
return Array.isArray(value) ? value.map((item) => String(item)) : [];
}
function createPendingAttachment(file: File): ShippingNoticePendingAttachment {
return {
id: `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
@@ -76,12 +124,30 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
);
const hasParseFailures = computed(() =>
noticeAttachments.value.some((item) => item.parseStatus === 'failed'),
noticeAttachments.value.some(
(item) =>
item.parseStatus === 'failed'
|| isShippingNoticeAttachmentParseIncomplete(item.parseSummary),
),
);
const allParsedReady = computed(() =>
noticeAttachments.value.length > 0
&& noticeAttachments.value.every((item) => item.parseStatus === 'parsed'),
&& noticeAttachments.value.every(
(item) =>
item.parseStatus === 'parsed'
&& !isShippingNoticeAttachmentParseIncomplete(item.parseSummary),
),
);
const hasBlockingDuplicates = computed(() =>
noticeAttachments.value.some(
(item) => item.duplicate?.blocking && !item.duplicateResolution?.action,
),
);
const allReadyForConfirmation = computed(() =>
allParsedReady.value && !hasBlockingDuplicates.value && !hasParseFailures.value,
);
const stopPolling = () => {
@@ -137,6 +203,7 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
errorMessage: remote.error_message || undefined,
expiresAt: remote.expires_at || undefined,
assetNo: remote.asset_no || local.assetNo,
...mapServerDuplicate(remote),
});
}
};
@@ -218,7 +285,12 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
const getParsedIntakeNos = () =>
noticeAttachments.value
.filter((item) => item.parseStatus === 'parsed' && item.intakeNo)
.filter(
(item) =>
item.parseStatus === 'parsed'
&& item.intakeNo
&& !isShippingNoticeAttachmentParseIncomplete(item.parseSummary),
)
.map((item) => String(item.intakeNo));
const clearConsumedAttachments = () => {
@@ -233,11 +305,11 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
.filter((item) => item.parse_status !== 'consumed')
.map((item) => ({
id: `restored_${item.intake_no}`,
kind: 'shipping_notice',
type: 'document',
kind: 'shipping_notice' as const,
type: 'document' as const,
assetNo: item.asset_no || undefined,
intakeNo: item.intake_no,
uploadStatus: 'uploaded',
uploadStatus: 'uploaded' as const,
parseStatus: mapServerParseStatus(item.parse_status),
uploadProgress: 100,
originalFilename: item.original_filename || item.intake_no,
@@ -246,6 +318,7 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
errorCode: item.error_code || undefined,
errorMessage: item.error_message || undefined,
expiresAt: item.expires_at || undefined,
...mapServerDuplicate(item),
}));
const nonNotice = options.attachments.value.filter((item) => item.kind !== 'shipping_notice');
options.attachments.value = [...nonNotice, ...restored];
@@ -254,6 +327,26 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
}
};
const resolveDuplicateAttachment = async (
id: string,
action: ShippingDuplicateAction,
targetTaskNo?: string,
reason?: string,
) => {
const target = noticeAttachments.value.find((item) => item.id === id);
if (!target?.intakeNo) return;
const intake = await resolveShippingNoticeDuplicate(
target.intakeNo,
action,
targetTaskNo,
reason,
);
patchAttachment(target.id, {
parseStatus: mapServerParseStatus(intake.parse_status),
...mapServerDuplicate(intake),
});
};
onBeforeUnmount(() => {
stopPolling();
});
@@ -263,9 +356,12 @@ export function useShippingNoticeIntakes(options: UseShippingNoticeIntakesOption
hasBlockingUploadOrParse,
hasParseFailures,
allParsedReady,
hasBlockingDuplicates,
allReadyForConfirmation,
enqueueNoticeFiles,
removeNoticeAttachment,
retryNoticeAttachment,
resolveDuplicateAttachment,
getParsedIntakeNos,
clearConsumedAttachments,
restoreForConversation,