Merge branch 'dev' into runny827

feat(training): 新增精益培训视频与周任务管理

# Conflicts:
#	backend/app/models/__init__.py
This commit is contained in:
runny committed 2026-09-08 18:30:09 +08:00
1 parent 85d27e9ffa
commit 91b7233872
812 files changed
+73763 -18908

No files matched your search

@@ -0,0 +1,259 @@
/**
* features/chat/composables/useChatTransfer.ts
* 用途:在途仓自动调拨聊天编排入口——预检创建/恢复、选择范围、直接生成执行 Run、
* Run 进度与结果恢复、人工重试/关闭、下载。
* 依赖:transfer.api、transfer.types、useMessage、Vue refs/computed。
* 逻辑:前端只消费稳定 DTO;Run 是异步真相源,刷新/重进由 GET 恢复;
* 防重复点击(busy);错误统一 useMessage 展示 BusinessError。
*/
import { computed, ref, type Ref } from 'vue';
import { useMessage } from '@/shared/composables/useMessage';
import {
cancelTransferRun,
createTransferPrecheck,
downloadTransferErpInventory,
downloadTransferExceptions,
downloadTransferReceiptFiles,
executeTransferPrecheck,
getTransferBacklogSummary,
getTransferPrecheck,
getTransferRun,
listTransferBacklog,
listTransferSources,
manualCloseTransferItem,
retryTransferItem,
retryTransferSourceRpa,
} from '../api/transfer.api';
import type {
TransferBacklogItem,
TransferBacklogSummary,
TransferPrecheckDetail,
TransferPrecheckItem,
TransferPrecheckSummary,
TransferRunResult,
TransferSourceOption,
TransferSourceProgress,
} from '../types/transfer.types';
export function useChatTransfer() {
const { error: notifyError } = useMessage();
const precheckId = ref<string | null>(null);
const precheck = ref<TransferPrecheckSummary | null>(null);
const items = ref<TransferPrecheckItem[]>([]);
const sources = ref<TransferSourceProgress[]>([]);
const runNo = ref<string | null>(null);
const run = ref<TransferRunResult | null>(null);
const backlog = ref<TransferBacklogItem[]>([]);
const backlogSummary = ref<TransferBacklogSummary | null>(null);
const sourceOptions = ref<TransferSourceOption[]>([]);
const selected = ref<Set<string>>(new Set());
const busy = ref(false);
const error = ref('');
/** 全部可执行 Invoice(阻断/未成熟不可勾选) */
const executableInvoices = computed(() =>
items.value.filter((item) => item.result === 'executable').map((item) => item.invoice_no),
);
const selectedCount = computed(() => selected.value.size);
const estimatedQuantity = computed(() => precheck.value?.estimated_transfer_quantity ?? '0');
/** 创建预检(缺文件时来源级阶段展示等待/失败) */
async function createPrecheck(sourceCodes: string[], businessDate?: string) {
busy.value = true;
error.value = '';
try {
const summary = await createTransferPrecheck({ source_codes: sourceCodes, business_date: businessDate });
precheckId.value = summary.precheck_id;
await refreshPrecheck();
return summary;
} catch (e) {
error.value = String(e);
throw e;
} finally {
busy.value = false;
}
}
/** 恢复预检进度(页面刷新/会话恢复) */
async function refreshPrecheck() {
if (!precheckId.value) return null;
const detail = await getTransferPrecheck(precheckId.value);
applyDetail(detail);
return detail;
}
function applyDetail(detail: TransferPrecheckDetail) {
precheck.value = detail.summary;
items.value = detail.items;
sources.value = detail.sources;
// 默认勾选全部可执行项;阻断/未成熟不可选
selected.value = new Set(
detail.items.filter((item) => item.result === 'executable').map((item) => item.invoice_no),
);
}
function toggleSelect(invoiceNo: string, checked: boolean) {
const next = new Set(selected.value);
if (checked) next.add(invoiceNo);
else next.delete(invoiceNo);
selected.value = next;
}
/** 直接生成调拨单(决策 4/5:无独立确认页) */
async function execute() {
if (!precheckId.value) throw new Error('precheck not created');
if (selected.value.size === 0) {
notifyError('请至少选择一个可执行 Invoice');
return null;
}
busy.value = true;
error.value = '';
try {
const result = await executeTransferPrecheck(precheckId.value, [...selected.value]);
runNo.value = result.run_no;
await refreshRun();
return result;
} catch (e) {
error.value = String(e);
throw e;
} finally {
busy.value = false;
}
}
/** 恢复 Run 最终结果 */
async function refreshRun() {
if (!runNo.value) return null;
const data = await getTransferRun(runNo.value);
run.value = data;
return data;
}
/** 请求停止剩余任务 */
async function cancelRun(reason: string) {
if (!runNo.value) return null;
const result = await cancelTransferRun(runNo.value, reason);
await refreshRun();
return result;
}
/** 人工重试异常项 */
async function retry(itemId: number) {
const result = await retryTransferItem(itemId);
await refreshPrecheck();
return result;
}
/** 人工关闭异常项 */
async function manualClose(itemId: number, reason: string) {
const result = await manualCloseTransferItem(itemId, reason);
await refreshPrecheck();
return result;
}
/** 下载收料来源文件(zip) */
async function downloadReceiptFiles() {
if (!runNo.value) return null;
const result = await downloadTransferReceiptFiles(runNo.value);
saveBlob(result.blob, result.filename ?? `receipt-files-${runNo.value}.zip`);
return result;
}
/** 下载预检冻结的 ERP 库存表(csv) */
async function downloadErpInventory() {
if (!runNo.value) return null;
const result = await downloadTransferErpInventory(runNo.value);
saveBlob(result.blob, result.filename ?? `erp-inventory-${runNo.value}.csv`);
return result;
}
/** 下载七 Sheet 异常 Excel */
async function downloadExceptions() {
if (!runNo.value) return null;
const result = await downloadTransferExceptions(runNo.value);
saveBlob(result.blob, result.filename ?? `transfer-exceptions-${runNo.value}.xlsx`);
return result;
}
/** 加载来源实例列表(决策 3:平台仅分组,每来源附收料文件状态) */
async function loadSources() {
sourceOptions.value = await listTransferSources();
return sourceOptions.value;
}
/** 对指定来源重新触发收料 RPA 抓取,并刷新来源状态 */
async function retrySourceRpa(sourceCode: string) {
const status = await retryTransferSourceRpa(sourceCode);
await loadSources();
return status;
}
/** 加载待处理异常列表与汇总 */
async function loadBacklog() {
const [items, summary] = await Promise.all([listTransferBacklog(), getTransferBacklogSummary()]);
backlog.value = items;
backlogSummary.value = summary;
return { items, summary };
}
function saveBlob(blob: Blob, filename: string) {
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
link.click();
URL.revokeObjectURL(url);
}
function reset() {
precheckId.value = null;
precheck.value = null;
items.value = [];
sources.value = [];
runNo.value = null;
run.value = null;
backlog.value = [];
backlogSummary.value = null;
sourceOptions.value = [];
selected.value = new Set();
error.value = '';
}
return {
precheckId,
precheck,
items,
sources,
runNo,
run,
backlog,
backlogSummary,
sourceOptions,
selected,
selectedCount,
executableInvoices,
estimatedQuantity,
busy,
error,
createPrecheck,
refreshPrecheck,
toggleSelect,
execute,
refreshRun,
cancelRun,
retry,
manualClose,
loadSources,
retrySourceRpa,
loadBacklog,
downloadReceiptFiles,
downloadErpInventory,
downloadExceptions,
reset,
};
}
export type ChatTransfer = ReturnType<typeof useChatTransfer>;
export type { Ref };