feat: 完善履约路由、订单服务与自动核销
This commit is contained in:
@@ -45,6 +45,7 @@ test('normalizeScheduledJobsConfig ensures default jobs exist', () => {
|
||||
assert.equal(ids.includes('cost-finance-sync'), false)
|
||||
assert.ok(ids.includes('channel-assign'))
|
||||
assert.ok(ids.includes('ledger-post'))
|
||||
assert.ok(ids.includes('voucher-consume-dispatch'))
|
||||
})
|
||||
|
||||
test('旧的 cost_finance_sync 任务已彻底移除,不再出现在配置里', () => {
|
||||
|
||||
@@ -19,6 +19,7 @@ const COST_SNAPSHOT_JOB_ID = 'cost-snapshot'
|
||||
const SALES_FACTS_JOB_ID = 'sales-facts'
|
||||
const LEDGER_POST_JOB_ID = 'ledger-post'
|
||||
const ORDER_SERVICE_AUTO_PUBLISH_JOB_ID = 'order-service-auto-publish'
|
||||
const VOUCHER_CONSUME_DISPATCH_JOB_ID = 'voucher-consume-dispatch'
|
||||
|
||||
export function getScheduledJobsFilePath() {
|
||||
return SCHEDULED_JOBS_FILE_PATH
|
||||
@@ -93,6 +94,7 @@ export function normalizeScheduledJobsConfig(rawValue: unknown) {
|
||||
const hasOrderServiceAutoPublish = jobs.some(
|
||||
(item) => item.id === ORDER_SERVICE_AUTO_PUBLISH_JOB_ID,
|
||||
)
|
||||
const hasVoucherConsumeDispatch = jobs.some((item) => item.id === VOUCHER_CONSUME_DISPATCH_JOB_ID)
|
||||
|
||||
if (!hasWorkOrderTimeout) {
|
||||
jobs.push(createDefaultWorkOrderTimeoutJob())
|
||||
@@ -116,6 +118,7 @@ export function normalizeScheduledJobsConfig(rawValue: unknown) {
|
||||
if (!hasSalesFacts) jobs.push(createDefaultSalesFactsJob())
|
||||
if (!hasLedgerPost) jobs.push(createDefaultLedgerPostJob())
|
||||
if (!hasOrderServiceAutoPublish) jobs.push(createDefaultOrderServiceAutoPublishJob())
|
||||
if (!hasVoucherConsumeDispatch) jobs.push(createDefaultVoucherConsumeDispatchJob())
|
||||
|
||||
return {
|
||||
enabled: typeof source.enabled === 'boolean' ? source.enabled : true,
|
||||
@@ -166,6 +169,9 @@ function normalizeScheduledJob(rawValue: unknown) {
|
||||
if (type === 'order_service_auto_publish') {
|
||||
return normalizeOrderServiceAutoPublishJob(rawValue)
|
||||
}
|
||||
if (type === 'voucher_consume_dispatch') {
|
||||
return normalizeVoucherConsumeDispatchJob(rawValue)
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
@@ -237,6 +243,7 @@ function createDefaultScheduledJobsConfig() {
|
||||
createDefaultSalesFactsJob(),
|
||||
createDefaultLedgerPostJob(),
|
||||
createDefaultOrderServiceAutoPublishJob(),
|
||||
createDefaultVoucherConsumeDispatchJob(),
|
||||
],
|
||||
}
|
||||
}
|
||||
@@ -322,6 +329,19 @@ function normalizeOrderServiceAutoPublishJob(rawValue: JsonObject) {
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeVoucherConsumeDispatchJob(rawValue: JsonObject) {
|
||||
const config = isPlainObject(rawValue.config) ? rawValue.config : {}
|
||||
return {
|
||||
id: VOUCHER_CONSUME_DISPATCH_JOB_ID,
|
||||
type: 'voucher_consume_dispatch',
|
||||
enabled: rawValue.enabled !== false,
|
||||
intervalSeconds: normalizeRangeInteger(rawValue.intervalSeconds, 5, 1, 60),
|
||||
config: {
|
||||
batchSize: normalizeRangeInteger(config.batchSize, 20, 1, 200),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeLedgerPostJob(rawValue: JsonObject) {
|
||||
const config = isPlainObject(rawValue.config) ? rawValue.config : {}
|
||||
return {
|
||||
@@ -385,6 +405,16 @@ function createDefaultOrderServiceAutoPublishJob() {
|
||||
}
|
||||
}
|
||||
|
||||
function createDefaultVoucherConsumeDispatchJob() {
|
||||
return {
|
||||
id: VOUCHER_CONSUME_DISPATCH_JOB_ID,
|
||||
type: 'voucher_consume_dispatch',
|
||||
enabled: true,
|
||||
intervalSeconds: 5,
|
||||
config: { batchSize: 20 },
|
||||
}
|
||||
}
|
||||
|
||||
function createDefaultSalesFactsJob() {
|
||||
return {
|
||||
id: SALES_FACTS_JOB_ID,
|
||||
|
||||
@@ -86,7 +86,15 @@ export async function runChannelAssignJob(job: JsonObject = {}) {
|
||||
export async function runCarrierDispatchJob(job: JsonObject = {}) {
|
||||
const config = jobConfig(job)
|
||||
const limit = clampInt(config.limit, 200, 1, 2000)
|
||||
const reconciled = await reconcilePendingCarriers({ limit: Math.min(limit, 200) })
|
||||
const reconcileCursor =
|
||||
config.carrierReconcileAfterId != null
|
||||
? Math.max(0, Math.trunc(Number(config.carrierReconcileAfterId)))
|
||||
: await readWatermarkItemId('carrier_reconcile')
|
||||
const reconciled = await reconcilePendingCarriers({
|
||||
limit: Math.min(limit, 200),
|
||||
afterId: reconcileCursor,
|
||||
})
|
||||
await touchWatermark('carrier_reconcile', { lastItemId: reconciled.nextCursor })
|
||||
const repairedAssigned = await reconcilePendingCarriers({
|
||||
limit: Math.min(limit, 200),
|
||||
includeAssignedMissingCarrier: true,
|
||||
@@ -335,7 +343,8 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
|
||||
message:
|
||||
`人工载体挂载 ${attached},缺配置 ${missingManual.rows.length},` +
|
||||
`缺履约载体 ${missingCarrier.rows.length},多载体冲突 ${conflictingCarriers.rows.length},异常收口 ${resolved},` +
|
||||
`自动转人工 ${fallback.switched},链路补载体 ${reconciled.workOrdersCreated + reconciled.affiliateTasksCreated + repairedAssigned.workOrdersCreated + repairedAssigned.affiliateTasksCreated},` +
|
||||
`自动转人工 ${fallback.switched},本轮补载体扫描 ${reconciled.scanned}、创建 ${reconciled.workOrdersCreated + reconciled.affiliateTasksCreated}、失败 ${reconciled.failed},` +
|
||||
`已分配补载体 ${repairedAssigned.workOrdersCreated + repairedAssigned.affiliateTasksCreated},` +
|
||||
`兜底 ${reconciled.fallbacksSelected + repairedAssigned.fallbacksSelected}`,
|
||||
attached,
|
||||
missingManual: missingManual.rows.length,
|
||||
@@ -541,6 +550,7 @@ const PIPELINE_RUNNERS: Record<
|
||||
(job?: JsonObject) => Promise<Record<string, unknown>>
|
||||
> = {
|
||||
channel_assign: runChannelAssignJob,
|
||||
carrier_reconcile: runCarrierDispatchJob,
|
||||
carrier_dispatch: runCarrierDispatchJob,
|
||||
product_normalize: runProductNormalizeJob,
|
||||
cost_snapshot: runCostSnapshotJob,
|
||||
|
||||
@@ -14,6 +14,7 @@ import type { WatermarkPipeline } from '../watermark-store.js'
|
||||
|
||||
export const FINANCE_PIPELINES = [
|
||||
{ name: 'channel_assign', label: '渠道判定', jobId: 'channel-assign' },
|
||||
{ name: 'carrier_reconcile', label: '载体补建', jobId: 'carrier-dispatch' },
|
||||
{ name: 'carrier_dispatch', label: '履约载体', jobId: 'carrier-dispatch' },
|
||||
{ name: 'product_normalize', label: '商品归一', jobId: 'product-normalize' },
|
||||
{ name: 'cost_snapshot', label: '成本快照', jobId: 'cost-snapshot' },
|
||||
|
||||
@@ -15,6 +15,7 @@ import {
|
||||
runProductNormalizeJob,
|
||||
runSalesFactsJob,
|
||||
} from './finance-pipeline-jobs.js'
|
||||
import { runVoucherConsumeDispatchJob } from '../fulfillment/voucher-consume-queue-service.js'
|
||||
|
||||
const timers = new Map<string, NodeJS.Timeout>()
|
||||
const jobStates = new Map<string, JsonObject>()
|
||||
@@ -88,7 +89,7 @@ function rescheduleJob(job: JsonObject) {
|
||||
if (!jobId) return
|
||||
const latestJob = getScheduledJobById(jobId)
|
||||
if (!latestJob || latestJob.enabled !== true) return
|
||||
const intervalMs = Math.max(10, Number(latestJob.intervalSeconds || 20)) * 1000
|
||||
const intervalMs = Math.max(1, Number(latestJob.intervalSeconds || 20)) * 1000
|
||||
scheduleJob(latestJob, intervalMs)
|
||||
}
|
||||
|
||||
@@ -234,6 +235,11 @@ function dispatchJob(job: JsonObject) {
|
||||
if (job.type === 'order_service_auto_publish') {
|
||||
return runOrderServiceAutoPublishJobs(job)
|
||||
}
|
||||
if (job.type === 'voucher_consume_dispatch') {
|
||||
return runVoucherConsumeDispatchJob(
|
||||
job.config && typeof job.config === 'object' ? (job.config as Record<string, unknown>) : {},
|
||||
)
|
||||
}
|
||||
|
||||
throw createHttpError(`不支持的定时任务类型:${job.type}`, {
|
||||
statusCode: 400,
|
||||
|
||||
Reference in New Issue
Block a user