feat(equipment-acceptance): 预警扫描发送改异步并错开合同付款排程

手动扫描/发送入队由 schedule worker 执行,避免 Nginx 504;默认扫描 06:30、周五发送 18:30。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
sophieandCursor committed 2026-10-30 15:09:16 +08:00
1 parent 2e8405e2f0
commit 537e3e18f0
16 files changed
+334 -73

No files matched your search

@@ -7,6 +7,7 @@
* 配置只保存天数和系统用户,不保存发送时刻。
* 保存配置自行声明 Content-Type: application/json,供 Flask get_json 解析。
* 发送记录只读已落库的行。每位收件人的 Excel 可再下载。
* 扫描/发送改为入队,再用 jobs 接口轮询结果,避免 Nginx 504。
*/
import { requestBlob, requestJson } from '@/shared/api/client';
@@ -126,33 +127,43 @@ export function syncEquipmentAcceptance() {
return requestJson<EquipmentAcceptanceSyncResult>('/api/equipment-acceptance/sync', { method: 'POST' });
}
export interface EquipmentAcceptanceScanResult {
created: number;
updated: number;
cancelled: number;
}
export interface EquipmentAcceptanceSendResult {
sent: number;
failed: number;
records_sent: number;
records_failed: number;
export interface EquipmentAcceptanceJob {
id: number;
status: string;
message?: string | null;
result?: {
created?: number;
updated?: number;
cancelled?: number;
sent?: number;
failed?: number;
records_sent?: number;
records_failed?: number;
} | null;
}
/**
* 管理员立即同步 E10 并生成待发送记录。
* @returns 新增、更新和取消条数
* 管理员将扫描任务入队(Worker 同步并生成待发送,不发企微)。
* @returns 后台任务主键
*/
export function scanEquipmentAcceptanceAlerts() {
return requestJson<EquipmentAcceptanceScanResult>('/api/equipment-acceptance/alerts/scan', { method: 'POST' });
export function enqueueEquipmentAcceptanceAlertScan() {
return requestJson<{ job_id: number }>('/api/equipment-acceptance/alerts/scan', { method: 'POST' });
}
/**
* 管理员立即把待发送记录按收件人发到企业微信。
* @returns 成功和失败的收件人数
* 管理员将发送任务入队(Worker 按收件人发企微后改状态)。
* @returns 后台任务主键
*/
export function sendEquipmentAcceptanceAlerts() {
return requestJson<EquipmentAcceptanceSendResult>('/api/equipment-acceptance/alerts/send', { method: 'POST' });
export function enqueueEquipmentAcceptanceAlertSend() {
return requestJson<{ job_id: number }>('/api/equipment-acceptance/alerts/send', { method: 'POST' });
}
/**
* 查询本人提交的设备验收后台任务状态。
* @param jobId - jobs 表主键
*/
export function getEquipmentAcceptanceJob(jobId: number) {
return requestJson<EquipmentAcceptanceJob>(`/api/equipment-acceptance/jobs/${jobId}`);
}
export interface EquipmentAcceptanceSendRecord {
@@ -6,6 +6,7 @@
本文件引入 Element 样式并包住 ElConfigProvider。标题和页签压到两行,下面整块留给当前页。
view=config 打开预警配置;view=sends 打开发送记录;orderId 只在停留列表下钻。
同步只在停留列表。扫描和发送只在发送记录,且仅平台管理员可见。
扫描与发送改为入队后轮询 Job,避免长请求被 Nginx 504。
-->
<script setup lang="ts">
import { computed, ref } from 'vue';
@@ -27,9 +28,11 @@ import { useAuthStore } from '@/features/auth/stores/auth.store';
import { useElementProvider } from '@/features/admin/composables/useElementProvider';
import { useMessage } from '@/shared/composables/useMessage';
import {
scanEquipmentAcceptanceAlerts,
sendEquipmentAcceptanceAlerts,
enqueueEquipmentAcceptanceAlertScan,
enqueueEquipmentAcceptanceAlertSend,
getEquipmentAcceptanceJob,
syncEquipmentAcceptance,
type EquipmentAcceptanceJob,
} from '../api/equipment-acceptance.api';
import EquipmentAcceptanceConfigPage from '../views/EquipmentAcceptanceConfigPage.vue';
import EquipmentAcceptanceDetailPage from '../views/EquipmentAcceptanceDetailPage.vue';
@@ -38,6 +41,10 @@ import EquipmentAcceptanceSendsPage from '../views/EquipmentAcceptanceSendsPage.
type WorkspaceView = 'ledger' | 'sends' | 'config';
const JOB_POLL_MS = 2000;
const JOB_TIMEOUT_MS = 600_000;
const JOB_DONE = new Set(['success', 'failed', 'dead', 'cancelled']);
const { t } = useI18n();
const route = useRoute();
const router = useRouter();
@@ -49,6 +56,68 @@ const runningAction = ref<'scan' | 'send' | null>(null);
const listPage = ref<{ reload: () => Promise<void> } | null>(null);
const sendsPage = ref<{ reload: () => Promise<void> } | null>(null);
/**
* 轮询后台任务直到终态或超时。
* 核心逻辑:入队接口立即返回;真正扫描/发送在 schedule worker 里,不能把「已提交」当成完成。
* @param jobId - 刚入队的任务主键
* @returns 终态任务;超时返回 null
*/
async function waitForJob(jobId: number): Promise<EquipmentAcceptanceJob | null> {
const deadline = Date.now() + JOB_TIMEOUT_MS;
while (Date.now() < deadline) {
const job = await getEquipmentAcceptanceJob(jobId);
if (JOB_DONE.has(job.status)) return job;
await new Promise((resolve) => window.setTimeout(resolve, JOB_POLL_MS));
}
return null;
}
/**
* 按扫描任务结果提示。
* @param job - 已结束的任务;null 表示仍在后台跑
*/
function reportScanJob(job: EquipmentAcceptanceJob | null): void {
if (!job) {
msg.warning(t('admin.equipmentAcceptance.jobTimeout'));
return;
}
if (job.status !== 'success') {
msg.error(job.message || t('admin.equipmentAcceptance.jobError'));
return;
}
msg.success(t('admin.equipmentAcceptance.scanDone', {
created: Number(job.result?.created || 0),
updated: Number(job.result?.updated || 0),
cancelled: Number(job.result?.cancelled || 0),
}));
}
/**
* 按发送任务结果提示。
* @param job - 已结束的任务;null 表示仍在后台跑
*/
function reportSendJob(job: EquipmentAcceptanceJob | null): void {
if (!job) {
msg.warning(t('admin.equipmentAcceptance.jobTimeout'));
return;
}
if (job.status !== 'success') {
msg.error(job.message || t('admin.equipmentAcceptance.jobError'));
return;
}
const sent = Number(job.result?.sent || 0);
const failed = Number(job.result?.failed || 0);
if (sent > 0 && failed > 0) {
msg.warning(t('admin.equipmentAcceptance.sendPartial', { sent, failed }));
} else if (failed > 0) {
msg.error(t('admin.equipmentAcceptance.sendFailedCount', { failed }));
} else if (sent > 0) {
msg.success(t('admin.equipmentAcceptance.sendDone', { sent, failed: 0 }));
} else {
msg.info(t('admin.equipmentAcceptance.sendEmpty'));
}
}
const isAdmin = computed(() => Boolean(auth.isAdmin));
const activeView = computed<WorkspaceView>(() => {
if (route.query.view === 'config') return 'config';
@@ -105,8 +174,8 @@ function onTabChange(name: string | number): void {
}
/**
* 管理员在发送记录页确认后立即扫描或发送。
* 核心逻辑:确认框取消会拒绝,这里直接返回。扫描会同步 E10;发送把待发送记录推到企业微信。
* 管理员在发送记录页确认后,将扫描或发送入队并等待 Worker。
* 核心逻辑:确认框取消直接返回。入队后轮询 Job,完成再刷新列表并按 sent/failed 提示。
* @param action - scan 或 send
*/
async function askAlert(action: 'scan' | 'send'): Promise<void> {
@@ -125,21 +194,14 @@ async function askAlert(action: 'scan' | 'send'): Promise<void> {
}
runningAction.value = action;
try {
if (action === 'scan') {
const result = await scanEquipmentAcceptanceAlerts();
msg.success(t('admin.equipmentAcceptance.scanDone', {
created: result.created,
updated: result.updated,
cancelled: result.cancelled,
}));
} else {
const result = await sendEquipmentAcceptanceAlerts();
msg.success(t('admin.equipmentAcceptance.sendDone', {
sent: result.sent,
failed: result.failed,
}));
}
const queued = action === 'scan'
? await enqueueEquipmentAcceptanceAlertScan()
: await enqueueEquipmentAcceptanceAlertSend();
msg.info(t('admin.equipmentAcceptance.jobSubmitted'));
const job = await waitForJob(Number(queued.job_id));
await sendsPage.value?.reload();
if (action === 'scan') reportScanJob(job);
else reportSendJob(job);
} catch (error) {
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.syncFailed'));
} finally {
+9 -3
View File
@@ -6102,7 +6102,7 @@
"tabLedger": "Waiting orders",
"tabSends": "Send records",
"tabConfig": "Alert settings",
"sendsLead": "The daily scan creates records waiting to be sent. They appear here after the Friday 18:00 summary goes out to each recipient. Names stay as they were at send time.",
"sendsLead": "The daily scan creates records waiting to be sent. They appear here after the Friday 18:30 summary goes out to each recipient. Names stay as they were at send time.",
"excelColumn": "Excel attachment",
"sentExcelTitle": "Sent Excel files",
"sentExcelHint": "One file per recipient. You can download the copy that was sent.",
@@ -6111,10 +6111,16 @@
"sendsFailed": "Failed to load send records",
"scanAlerts": "Scan alerts",
"sendAlerts": "Send alerts",
"scanConfirm": "This syncs from E10 and creates records waiting to be sent.",
"sendConfirm": "This sends the waiting records to WeCom, one message per recipient.",
"scanConfirm": "This queues a background sync and creates records waiting to be sent. The page will not wait for the whole run.",
"sendConfirm": "This queues background WeCom sends, one message per recipient. Do not click again until it finishes, or people may get duplicates.",
"scanDone": "Added {created}, updated {updated}, cancelled {cancelled}",
"sendDone": "Sent to {sent} people, {failed} failed",
"sendPartial": "Sent to {sent} people; {failed} failed",
"sendFailedCount": "{failed} people failed. Refresh the list for reasons.",
"sendEmpty": "There are no pending alerts, so send records will not change",
"jobSubmitted": "The job was queued for the background worker",
"jobTimeout": "The job is still running in the background. Refresh send records later for the result.",
"jobError": "The background job failed",
"sendResult": "Result",
"failureReason": "Failure reason",
"failureReasonEmpty": "No failure reason was recorded",
+9 -3
View File
@@ -6102,7 +6102,7 @@
"tabLedger": "รายการค้าง",
"tabSends": "บันทึกการส่ง",
"tabConfig": "ตั้งค่าการเตือน",
"sendsLead": "การสแกนรายวันสร้างรายการที่รอส่ง หลังสรุปส่งทุกวันศุกร์ 18:00 ตามผู้รับแล้ว จะแสดงที่นี่ ชื่อคงตามตอนที่ส่ง",
"sendsLead": "การสแกนรายวันสร้างรายการที่รอส่ง หลังสรุปส่งทุกวันศุกร์ 18:30 ตามผู้รับแล้ว จะแสดงที่นี่ ชื่อคงตามตอนที่ส่ง",
"excelColumn": "ไฟล์แนบ Excel",
"sentExcelTitle": "ไฟล์ Excel ที่ส่งแล้ว",
"sentExcelHint": "หนึ่งไฟล์ต่อผู้รับหนึ่งคน ดาวน์โหลดสำเนาที่ส่งไปแล้วได้",
@@ -6111,10 +6111,16 @@
"sendsFailed": "โหลดบันทึกการส่งไม่สำเร็จ",
"scanAlerts": "สแกนการเตือน",
"sendAlerts": "ส่งการเตือน",
"scanConfirm": "ระบบจะซิงก์จาก E10 และสร้างรายการที่รอส่ง",
"sendConfirm": "ระบบจะส่งรายการที่รอส่งไป WeCom คนละหนึ่งข้อความ",
"scanConfirm": "ระบบจะส่งงานไปทำงานพื้นหลังเพื่อซิงก์และสร้างรายการที่รอส่ง หน้าจอจะไม่รอจนจบ",
"sendConfirm": "ระบบจะส่งงานพื้นหลังไป WeCom คนละหนึ่งข้อความ อย่ากดซ้ำจนกว่าจะเสร็จ เพื่อเลี่ยงข้อความซ้ำ",
"scanDone": "เพิ่ม {created} รายการ อัปเดต {updated} รายการ ยกเลิก {cancelled} รายการ",
"sendDone": "ส่งถึง {sent} คน ไม่สำเร็จ {failed} คน",
"sendPartial": "ส่งถึง {sent} คน และมี {failed} คนที่ไม่สำเร็จ",
"sendFailedCount": "{failed} คนส่งไม่สำเร็จ รีเฟรชรายการเพื่อดูสาเหตุ",
"sendEmpty": "ไม่มีรายการที่รอส่ง บันทึกการส่งจะไม่เปลี่ยน",
"jobSubmitted": "ส่งงานเข้าคิวพื้นหลังแล้ว",
"jobTimeout": "งานยังทำงานอยู่พื้นหลัง โปรดรีเฟรชบันทึกการส่งภายหลัง",
"jobError": "งานพื้นหลังล้มเหลว",
"sendResult": "ผลลัพธ์",
"failureReason": "สาเหตุที่ล้มเหลว",
"failureReasonEmpty": "ไม่ได้บันทึกสาเหตุ",
+9 -3
View File
@@ -6116,7 +6116,7 @@
"tabLedger": "停留情况",
"tabSends": "发送记录",
"tabConfig": "预警配置",
"sendsLead": "每日扫描生成待发送。默认每周五 18:00 按收件人汇总发出后记在这里。已发送的姓名保持发送当时的人。",
"sendsLead": "每日扫描生成待发送。默认每周五 18:30 按收件人汇总发出后记在这里。已发送的姓名保持发送当时的人。",
"excelColumn": "Excel 附件",
"sentExcelTitle": "已发送的 Excel",
"sentExcelHint": "每位收件人一份,可再次下载当时发出的文件。",
@@ -6125,10 +6125,16 @@
"sendsFailed": "读取发送记录失败",
"scanAlerts": "扫描预警",
"sendAlerts": "发送预警",
"scanConfirm": "将从 E10 同步,并生成待发送记录。",
"sendConfirm": "将把待发送记录按收件人发到企业微信。",
"scanConfirm": "将在后台同步并生成待发送记录,页面不会一直等待。",
"sendConfirm": "将在后台按收件人发到企业微信。发送完前请勿重复点击,以免重复通知。",
"scanDone": "新增 {created} 条,更新 {updated} 条,取消 {cancelled} 条",
"sendDone": "已向 {sent} 人发送,{failed} 人失败",
"sendPartial": "已向 {sent} 人发送,另有 {failed} 人失败",
"sendFailedCount": "{failed} 人发送失败,可刷新列表查看原因",
"sendEmpty": "没有可发送的预警,发送记录不会变化",
"jobSubmitted": "任务已提交后台执行",
"jobTimeout": "任务仍在后台执行,请稍后刷新发送记录查看结果",
"jobError": "后台任务执行失败",
"sendResult": "结果",
"failureReason": "失败原因",
"failureReasonEmpty": "没有记下失败原因",
+9 -3
View File
@@ -6102,7 +6102,7 @@
"tabLedger": "停留情況",
"tabSends": "發送記錄",
"tabConfig": "預警配置",
"sendsLead": "每日掃描產生待發送。預設每週五 18:00 按收件人彙總發出後記在這裡。已發送的姓名保持發送當時的人。",
"sendsLead": "每日掃描產生待發送。預設每週五 18:30 按收件人彙總發出後記在這裡。已發送的姓名保持發送當時的人。",
"excelColumn": "Excel 附件",
"sentExcelTitle": "已發送的 Excel",
"sentExcelHint": "每位收件人一份,可再次下載當時發出的檔案。",
@@ -6111,10 +6111,16 @@
"sendsFailed": "讀取發送記錄失敗",
"scanAlerts": "掃描預警",
"sendAlerts": "發送預警",
"scanConfirm": "將從 E10 同步,並產生待發送記錄。",
"sendConfirm": "將把待發送記錄按收件人發到企業微信。",
"scanConfirm": "將在背景同步並產生待發送記錄,頁面不會一直等待。",
"sendConfirm": "將在背景按收件人發到企業微信。發送完前請勿重複點擊,以免重複通知。",
"scanDone": "新增 {created} 條,更新 {updated} 條,取消 {cancelled} 條",
"sendDone": "已向 {sent} 人發送,{failed} 人失敗",
"sendPartial": "已向 {sent} 人發送,另有 {failed} 人失敗",
"sendFailedCount": "{failed} 人發送失敗,可重新整理列表查看原因",
"sendEmpty": "沒有可發送的預警,發送記錄不會變化",
"jobSubmitted": "任務已提交背景執行",
"jobTimeout": "任務仍在背景執行,請稍後重新整理發送記錄查看結果",
"jobError": "背景任務執行失敗",
"sendResult": "結果",
"failureReason": "失敗原因",
"failureReasonEmpty": "沒有記下失敗原因",