- 确认履约改为单事务:锁订单项 → 复查快照 → 扣积分/备用金 → 写成本快照; 快照失败整体回滚,不再出现「钱扣了、成本没记」 - manual_case 的 subject_id 统一为 order_item_id,新增订单项级唯一索引(迁移 111), 消除 manual_expenses.id / platform_credit_entries.id 跨表撞车 - 人工 SKU 匹配增加 sku_code / sku_name 兜底;保存配置立即回填最近 30 天, 定时任务按窗口 rematch,确认时再按 SKU 兜底匹配 - 新增 manual_cost_missing 异常队列:从首次人工配置起算,确认履约立即收口 - 补单元测试与真实 PostgreSQL 集成测试(并发、重复、强制快照失败回滚、双资金链路) - 校准财务文档并新增上线初始化/验收手册(历史人工不补录,只保证配置后的新单) 验证:backend typecheck/lint/format/check:sql/test 843(839 pass / 4 skip); frontend typecheck/lint/test 172;迁移 111 在 scratch 库全量执行 + 重跑幂等。
807 lines
29 KiB
TypeScript
807 lines
29 KiB
TypeScript
import type { PoolClient } from 'pg'
|
|
|
|
import { withTransaction } from '../../db/client.js'
|
|
import { resolveFinanceDateRange } from '../../utils/finance-date-range.js'
|
|
import { costModelScopeSql } from './cost-center-shared.js'
|
|
|
|
export type CostSnapshotSyncInput = {
|
|
dateFrom?: unknown
|
|
dateTo?: unknown
|
|
from?: unknown
|
|
to?: unknown
|
|
}
|
|
|
|
export type CostSnapshotSyncResult = {
|
|
range: { fromDate: string; toDate: string }
|
|
createdCount: number
|
|
exceptionCreatedCount: number
|
|
resolvedExceptionCount: number
|
|
}
|
|
|
|
export type ActualCostSnapshotSyncResult = {
|
|
range: { fromDate: string; toDate: string }
|
|
createdCount: number
|
|
upgradedCount: number
|
|
directRewardCount: number
|
|
estimatedRewardCount: number
|
|
}
|
|
|
|
/**
|
|
* 按订单项批量生成预估成本快照。
|
|
*
|
|
* 只处理已有 canonical_product_id 的订单项;未归一订单项进入
|
|
* product_mapping_missing 异常,不在这里猜测商品。
|
|
*/
|
|
export async function syncEstimatedOrderItemCostSnapshots(
|
|
input: CostSnapshotSyncInput = {},
|
|
): Promise<CostSnapshotSyncResult> {
|
|
const range = resolveFinanceDateRange(input, { defaultDays: 7, maxDays: 400 })
|
|
|
|
return withTransaction(async (client) => {
|
|
const inserted = await client.query(
|
|
`
|
|
WITH item_scope AS (
|
|
SELECT
|
|
oi.id AS order_item_id,
|
|
oi.order_id,
|
|
o.provider,
|
|
o.shop_id,
|
|
oi.sku_code,
|
|
oi.sku_name,
|
|
oi.canonical_product_id,
|
|
COALESCE(o.paid_at, o.created_at) AS basis_at,
|
|
GREATEST(COALESCE(oi.normalized_quantity, oi.quantity, 1), 1)::numeric AS quantity,
|
|
COALESCE(oi.revenue_amount_fen, 0)::bigint AS revenue_fen,
|
|
wo.id AS work_order_id,
|
|
wo.product_rule_id AS work_product_rule_id,
|
|
COALESCE(oi.fulfillment_mode, 'manual') AS fulfillment_mode
|
|
FROM order_items oi
|
|
JOIN orders o ON o.id = oi.order_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT id, product_rule_id
|
|
FROM work_orders
|
|
WHERE order_item_id = oi.id
|
|
ORDER BY id DESC
|
|
LIMIT 1
|
|
) wo ON TRUE
|
|
WHERE o.pay_status IN ('paid', 'refunded')
|
|
AND (COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date
|
|
BETWEEN $1::date AND $2::date
|
|
),
|
|
resolved AS (
|
|
SELECT
|
|
s.*,
|
|
ci.id AS cost_item_id,
|
|
ci.pricing_mode,
|
|
v.id AS cost_version_id,
|
|
v.amount_fen,
|
|
v.rate_bps
|
|
FROM item_scope s
|
|
JOIN cost_items ci ON ci.status = 'active'
|
|
AND ${costModelScopeSql('s.fulfillment_mode')}
|
|
AND ci.pricing_mode IN ('per_item', 'per_order', 'percent_of_revenue')
|
|
AND ci.code <> 'sys_work_order_reward'
|
|
-- 人工成本只能在「确认履约」时按实际支出写 actual 快照,
|
|
-- 这里的版本金额是 0 元占位,不能拿来生成预估快照。
|
|
AND ci.cost_model <> 'manual_actual'
|
|
JOIN LATERAL (
|
|
SELECT v.id, v.amount_fen, v.rate_bps
|
|
FROM cost_versions v
|
|
WHERE v.cost_item_id = ci.id
|
|
AND v.status = 'active'
|
|
AND v.effective_from <= s.basis_at
|
|
AND (v.effective_to IS NULL OR v.effective_to > s.basis_at)
|
|
AND (
|
|
(v.scope_type = 'rule' AND s.work_product_rule_id IS NOT NULL
|
|
AND v.work_product_rule_id = s.work_product_rule_id)
|
|
OR (v.scope_type = 'product' AND s.canonical_product_id IS NOT NULL
|
|
AND v.canonical_product_id = s.canonical_product_id)
|
|
OR (v.scope_type = 'shop' AND v.provider = s.provider AND v.shop_id = s.shop_id)
|
|
OR (v.scope_type = 'provider' AND v.provider = s.provider)
|
|
OR (v.scope_type = 'global')
|
|
)
|
|
ORDER BY
|
|
CASE v.scope_type
|
|
WHEN 'rule' THEN 1
|
|
WHEN 'product' THEN 2
|
|
WHEN 'shop' THEN 3
|
|
WHEN 'provider' THEN 4
|
|
ELSE 5
|
|
END,
|
|
v.effective_from DESC,
|
|
v.id DESC
|
|
LIMIT 1
|
|
) v ON TRUE
|
|
WHERE s.canonical_product_id IS NOT NULL
|
|
),
|
|
calculated AS (
|
|
SELECT
|
|
r.*,
|
|
CASE r.pricing_mode
|
|
WHEN 'per_item' THEN ROUND(r.amount_fen::numeric * r.quantity)::bigint
|
|
WHEN 'per_order' THEN r.amount_fen
|
|
WHEN 'percent_of_revenue'
|
|
THEN ROUND((r.revenue_fen::numeric * r.rate_bps::numeric) / 10000.0)::bigint
|
|
ELSE r.amount_fen
|
|
END AS total_amount_fen
|
|
FROM resolved r
|
|
)
|
|
INSERT INTO cost_snapshots (
|
|
subject_type, subject_id, order_id, order_item_id, work_order_id,
|
|
cost_item_id, cost_version_id, canonical_product_id,
|
|
quantity, unit_amount_fen, total_amount_fen, snapshot_stage, source,
|
|
metadata_json, cost_basis_at, resolved_at, created_at
|
|
)
|
|
SELECT
|
|
'order_item', c.order_item_id, c.order_id, c.order_item_id, c.work_order_id,
|
|
c.cost_item_id, c.cost_version_id, c.canonical_product_id,
|
|
CASE WHEN c.pricing_mode = 'per_item' THEN c.quantity ELSE 1 END,
|
|
CASE WHEN c.pricing_mode = 'percent_of_revenue' THEN c.total_amount_fen ELSE c.amount_fen END,
|
|
c.total_amount_fen,
|
|
'estimated', 'resolved',
|
|
jsonb_build_object(
|
|
'provider', c.provider,
|
|
'shopId', c.shop_id,
|
|
'skuCode', c.sku_code,
|
|
'skuName', c.sku_name,
|
|
'pricingMode', c.pricing_mode,
|
|
'revenueFen', c.revenue_fen,
|
|
'normalizedQuantity', c.quantity,
|
|
'fulfillmentMode', c.fulfillment_mode,
|
|
'basisAt', c.basis_at
|
|
),
|
|
c.basis_at, NOW(), NOW()
|
|
FROM calculated c
|
|
ON CONFLICT (subject_type, subject_id, cost_item_id)
|
|
WHERE superseded_at IS NULL
|
|
DO NOTHING
|
|
`,
|
|
[range.fromDate, range.toDate],
|
|
)
|
|
|
|
const productExceptions = await insertAndResolveProductMappingExceptions(
|
|
client,
|
|
range.fromDate,
|
|
range.toDate,
|
|
)
|
|
const costExceptions = await insertAndResolveCostMissingExceptions(
|
|
client,
|
|
range.fromDate,
|
|
range.toDate,
|
|
)
|
|
|
|
return {
|
|
range,
|
|
createdCount: inserted.rowCount || 0,
|
|
exceptionCreatedCount: productExceptions.created + costExceptions.created,
|
|
resolvedExceptionCount: productExceptions.resolved + costExceptions.resolved,
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 工单验收后,把订单项当前预估快照升级为实际快照。
|
|
*
|
|
* 如果配置了 work_order_reward 成本项,则用工单实际 reward_amount 覆盖金额;
|
|
* 否则仍冻结当前版本金额,但把阶段标为 actual。旧快照保留 superseded_at。
|
|
*/
|
|
export async function syncActualWorkOrderCostSnapshots(
|
|
input: CostSnapshotSyncInput = {},
|
|
): Promise<ActualCostSnapshotSyncResult> {
|
|
const range = resolveFinanceDateRange(input, { defaultDays: 30, maxDays: 400 })
|
|
|
|
return withTransaction(async (client) => {
|
|
const directReward = await syncDirectWorkOrderRewardSnapshots(
|
|
client,
|
|
range.fromDate,
|
|
range.toDate,
|
|
)
|
|
const estimatedReward = await syncCommittedWorkOrderRewardSnapshots(
|
|
client,
|
|
range.fromDate,
|
|
range.toDate,
|
|
)
|
|
const upgraded = await client.query(
|
|
`
|
|
WITH accepted AS (
|
|
SELECT DISTINCT ON (wo.order_item_id)
|
|
wo.id AS work_order_id,
|
|
wo.order_item_id,
|
|
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount,
|
|
COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at, NOW()) AS accepted_at
|
|
FROM work_orders wo
|
|
JOIN order_items oi ON oi.id = wo.order_item_id
|
|
JOIN orders o ON o.id = oi.order_id
|
|
WHERE wo.status = 'accepted'
|
|
AND wo.order_item_id IS NOT NULL
|
|
AND (COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at)
|
|
AT TIME ZONE 'Asia/Shanghai')::date BETWEEN $1::date AND $2::date
|
|
ORDER BY wo.order_item_id, wo.id DESC
|
|
),
|
|
active AS (
|
|
SELECT
|
|
cs.*,
|
|
a.work_order_id AS actual_work_order_id,
|
|
a.reward_amount,
|
|
a.accepted_at,
|
|
ci.cost_model
|
|
FROM cost_snapshots cs
|
|
JOIN accepted a ON cs.subject_type = 'order_item' AND cs.subject_id = a.order_item_id
|
|
JOIN cost_items ci ON ci.id = cs.cost_item_id
|
|
WHERE cs.superseded_at IS NULL
|
|
AND cs.snapshot_stage IN ('estimated', 'actual')
|
|
AND ci.code <> 'sys_work_order_reward'
|
|
),
|
|
target AS (
|
|
SELECT *
|
|
FROM active
|
|
WHERE snapshot_stage = 'estimated'
|
|
OR total_amount_fen <> CASE
|
|
WHEN cost_model = 'work_order_reward' AND reward_amount > 0 THEN reward_amount
|
|
ELSE total_amount_fen
|
|
END
|
|
),
|
|
superseded AS (
|
|
UPDATE cost_snapshots cs
|
|
SET superseded_at = NOW()
|
|
FROM target t
|
|
WHERE cs.id = t.id
|
|
AND cs.superseded_at IS NULL
|
|
RETURNING
|
|
cs.id, cs.subject_id, cs.order_id, cs.order_item_id,
|
|
cs.cost_item_id, cs.cost_version_id, cs.canonical_product_id,
|
|
cs.quantity, cs.unit_amount_fen, cs.total_amount_fen, cs.metadata_json
|
|
)
|
|
INSERT INTO cost_snapshots (
|
|
subject_type, subject_id, order_id, order_item_id, work_order_id,
|
|
cost_item_id, cost_version_id, canonical_product_id,
|
|
quantity, unit_amount_fen, total_amount_fen, snapshot_stage, source,
|
|
metadata_json, cost_basis_at, resolved_at, created_at
|
|
)
|
|
SELECT
|
|
'order_item', s.subject_id, s.order_id, s.order_item_id, a.work_order_id,
|
|
s.cost_item_id, s.cost_version_id, s.canonical_product_id,
|
|
CASE WHEN ci.cost_model = 'work_order_reward' AND a.reward_amount > 0 THEN 1
|
|
ELSE s.quantity END,
|
|
CASE WHEN ci.cost_model = 'work_order_reward' AND a.reward_amount > 0
|
|
THEN a.reward_amount ELSE s.unit_amount_fen END,
|
|
CASE WHEN ci.cost_model = 'work_order_reward' AND a.reward_amount > 0
|
|
THEN a.reward_amount ELSE s.total_amount_fen END,
|
|
'actual', 'resolved',
|
|
s.metadata_json || jsonb_build_object(
|
|
'actualWorkOrderId', a.work_order_id,
|
|
'actualRewardFen', a.reward_amount,
|
|
'basisAt', a.accepted_at
|
|
),
|
|
a.accepted_at, NOW(), NOW()
|
|
FROM superseded s
|
|
JOIN accepted a ON a.order_item_id = s.subject_id
|
|
JOIN cost_items ci ON ci.id = s.cost_item_id
|
|
ON CONFLICT (subject_type, subject_id, cost_item_id)
|
|
WHERE superseded_at IS NULL
|
|
DO NOTHING
|
|
`,
|
|
[range.fromDate, range.toDate],
|
|
)
|
|
|
|
return {
|
|
range,
|
|
createdCount: directReward.inserted + estimatedReward.inserted + (upgraded.rowCount || 0),
|
|
upgradedCount: upgraded.rowCount || 0,
|
|
directRewardCount: directReward.inserted,
|
|
estimatedRewardCount: estimatedReward.inserted,
|
|
}
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 接单平台的实际成本不依赖预估快照:工单验收后直接按 reward_amount 写 actual。
|
|
* 系统成本项 sys_work_order_reward 的版本金额只是占位,不会进入预估阶段。
|
|
*/
|
|
async function syncDirectWorkOrderRewardSnapshots(
|
|
client: PoolClient,
|
|
fromDate: string,
|
|
toDate: string,
|
|
) {
|
|
const superseded = await client.query(
|
|
`
|
|
WITH system_item AS (
|
|
SELECT ci.id AS cost_item_id, v.id AS cost_version_id
|
|
FROM cost_items ci
|
|
JOIN LATERAL (
|
|
SELECT id
|
|
FROM cost_versions
|
|
WHERE cost_item_id = ci.id
|
|
AND status = 'active'
|
|
AND effective_from <= NOW()
|
|
ORDER BY effective_from DESC, id DESC
|
|
LIMIT 1
|
|
) v ON TRUE
|
|
WHERE ci.code = 'sys_work_order_reward'
|
|
AND ci.status = 'active'
|
|
),
|
|
accepted AS (
|
|
SELECT DISTINCT ON (wo.order_item_id)
|
|
wo.id AS work_order_id,
|
|
wo.order_item_id,
|
|
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount,
|
|
COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at, NOW()) AS accepted_at
|
|
FROM work_orders wo
|
|
JOIN order_items oi ON oi.id = wo.order_item_id
|
|
JOIN orders o ON o.id = oi.order_id
|
|
WHERE wo.status = 'accepted'
|
|
AND wo.order_item_id IS NOT NULL
|
|
AND COALESCE(wo.reward_amount, 0) >= 0
|
|
AND (COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at)
|
|
AT TIME ZONE 'Asia/Shanghai')::date BETWEEN $1::date AND $2::date
|
|
ORDER BY wo.order_item_id, wo.id DESC
|
|
)
|
|
UPDATE cost_snapshots cs
|
|
SET superseded_at = NOW()
|
|
FROM accepted a
|
|
JOIN system_item si ON TRUE
|
|
JOIN LATERAL (
|
|
SELECT id, snapshot_stage, total_amount_fen
|
|
FROM cost_snapshots
|
|
WHERE subject_type = 'order_item'
|
|
AND subject_id = a.order_item_id
|
|
AND cost_item_id = si.cost_item_id
|
|
AND superseded_at IS NULL
|
|
ORDER BY id DESC
|
|
LIMIT 1
|
|
FOR UPDATE
|
|
) current ON TRUE
|
|
WHERE cs.id = current.id
|
|
AND cs.superseded_at IS NULL
|
|
AND (current.snapshot_stage <> 'actual' OR current.total_amount_fen <> a.reward_amount)
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
const inserted = await client.query(
|
|
`
|
|
WITH system_item AS (
|
|
SELECT ci.id AS cost_item_id, v.id AS cost_version_id
|
|
FROM cost_items ci
|
|
JOIN LATERAL (
|
|
SELECT id
|
|
FROM cost_versions
|
|
WHERE cost_item_id = ci.id
|
|
AND status = 'active'
|
|
AND effective_from <= NOW()
|
|
ORDER BY effective_from DESC, id DESC
|
|
LIMIT 1
|
|
) v ON TRUE
|
|
WHERE ci.code = 'sys_work_order_reward'
|
|
AND ci.status = 'active'
|
|
),
|
|
accepted AS (
|
|
SELECT DISTINCT ON (wo.order_item_id)
|
|
wo.id AS work_order_id,
|
|
wo.order_item_id,
|
|
wo.product_rule_id,
|
|
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount,
|
|
COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at, NOW()) AS accepted_at
|
|
FROM work_orders wo
|
|
JOIN order_items oi ON oi.id = wo.order_item_id
|
|
JOIN orders o ON o.id = oi.order_id
|
|
WHERE wo.status = 'accepted'
|
|
AND wo.order_item_id IS NOT NULL
|
|
AND COALESCE(wo.reward_amount, 0) >= 0
|
|
AND (COALESCE(wo.accepted_at, wo.updated_at, o.paid_at, o.created_at)
|
|
AT TIME ZONE 'Asia/Shanghai')::date BETWEEN $1::date AND $2::date
|
|
ORDER BY wo.order_item_id, wo.id DESC
|
|
)
|
|
INSERT INTO cost_snapshots (
|
|
subject_type, subject_id, order_id, order_item_id, work_order_id,
|
|
cost_item_id, cost_version_id, canonical_product_id,
|
|
quantity, unit_amount_fen, total_amount_fen, snapshot_stage, source,
|
|
metadata_json, cost_basis_at, resolved_at, created_at
|
|
)
|
|
SELECT
|
|
'order_item', a.order_item_id, oi.order_id, a.order_item_id, a.work_order_id,
|
|
si.cost_item_id, COALESCE(rv.id, si.cost_version_id),
|
|
oi.canonical_product_id,
|
|
1, a.reward_amount, a.reward_amount, 'actual', 'resolved',
|
|
jsonb_build_object(
|
|
'source', 'system_work_order_reward',
|
|
'actualWorkOrderId', a.work_order_id,
|
|
'actualRewardFen', a.reward_amount,
|
|
'basisAt', a.accepted_at
|
|
),
|
|
a.accepted_at, NOW(), NOW()
|
|
FROM accepted a
|
|
CROSS JOIN system_item si
|
|
JOIN order_items oi ON oi.id = a.order_item_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT v.id
|
|
FROM cost_versions v
|
|
WHERE v.cost_item_id = si.cost_item_id
|
|
AND v.scope_type = 'rule'
|
|
AND v.work_product_rule_id = a.product_rule_id
|
|
AND v.status IN ('active', 'retired')
|
|
AND v.effective_from <= a.accepted_at
|
|
AND (v.effective_to IS NULL OR v.effective_to > a.accepted_at)
|
|
ORDER BY v.effective_from DESC, v.id DESC
|
|
LIMIT 1
|
|
) rv ON TRUE
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM cost_snapshots cs
|
|
WHERE cs.subject_type = 'order_item'
|
|
AND cs.subject_id = a.order_item_id
|
|
AND cs.cost_item_id = si.cost_item_id
|
|
AND cs.superseded_at IS NULL
|
|
AND cs.snapshot_stage = 'actual'
|
|
AND cs.total_amount_fen = a.reward_amount
|
|
)
|
|
ON CONFLICT (subject_type, subject_id, cost_item_id)
|
|
WHERE superseded_at IS NULL
|
|
DO NOTHING
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
return {
|
|
inserted: inserted.rowCount || 0,
|
|
superseded: superseded.rowCount || 0,
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 未验收工单的报酬在创建时已确定,先按 reward_amount 写 estimated 快照。
|
|
* 验收后由 syncDirectWorkOrderRewardSnapshots 升级为 actual;取消工单不计成本。
|
|
*/
|
|
async function syncCommittedWorkOrderRewardSnapshots(
|
|
client: PoolClient,
|
|
fromDate: string,
|
|
toDate: string,
|
|
) {
|
|
const superseded = await client.query(
|
|
`
|
|
WITH system_item AS (
|
|
SELECT ci.id AS cost_item_id
|
|
FROM cost_items ci
|
|
WHERE ci.code = 'sys_work_order_reward'
|
|
AND ci.status = 'active'
|
|
),
|
|
latest AS (
|
|
SELECT DISTINCT ON (wo.order_item_id)
|
|
wo.id AS work_order_id,
|
|
wo.order_item_id,
|
|
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount
|
|
FROM work_orders wo
|
|
JOIN orders o ON o.id = wo.order_id
|
|
WHERE wo.order_item_id IS NOT NULL
|
|
AND wo.status IN (
|
|
'pending_material', 'in_progress', 'unassigned', 'open',
|
|
'pending_acceptance', 'problem'
|
|
)
|
|
AND COALESCE(wo.reward_amount, 0) > 0
|
|
AND (COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date
|
|
BETWEEN $1::date AND $2::date
|
|
ORDER BY wo.order_item_id, wo.id DESC
|
|
)
|
|
UPDATE cost_snapshots cs
|
|
SET superseded_at = NOW()
|
|
FROM latest l
|
|
JOIN system_item si ON TRUE
|
|
WHERE cs.subject_type = 'order_item'
|
|
AND cs.subject_id = l.order_item_id
|
|
AND cs.cost_item_id = si.cost_item_id
|
|
AND cs.superseded_at IS NULL
|
|
AND cs.snapshot_stage = 'estimated'
|
|
AND (
|
|
cs.work_order_id IS DISTINCT FROM l.work_order_id
|
|
OR cs.total_amount_fen <> l.reward_amount
|
|
)
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
const inserted = await client.query(
|
|
`
|
|
WITH system_item AS (
|
|
SELECT ci.id AS cost_item_id, v.id AS cost_version_id
|
|
FROM cost_items ci
|
|
JOIN LATERAL (
|
|
SELECT id
|
|
FROM cost_versions
|
|
WHERE cost_item_id = ci.id
|
|
AND status = 'active'
|
|
AND effective_from <= NOW()
|
|
ORDER BY effective_from DESC, id DESC
|
|
LIMIT 1
|
|
) v ON TRUE
|
|
WHERE ci.code = 'sys_work_order_reward'
|
|
AND ci.status = 'active'
|
|
),
|
|
latest AS (
|
|
SELECT DISTINCT ON (wo.order_item_id)
|
|
wo.id AS work_order_id,
|
|
wo.order_item_id,
|
|
wo.product_rule_id,
|
|
wo.status,
|
|
GREATEST(COALESCE(wo.reward_amount, 0), 0)::bigint AS reward_amount,
|
|
COALESCE(wo.created_at, o.paid_at, o.created_at, NOW()) AS basis_at
|
|
FROM work_orders wo
|
|
JOIN orders o ON o.id = wo.order_id
|
|
WHERE wo.order_item_id IS NOT NULL
|
|
AND wo.status IN (
|
|
'pending_material', 'in_progress', 'unassigned', 'open',
|
|
'pending_acceptance', 'problem'
|
|
)
|
|
AND COALESCE(wo.reward_amount, 0) > 0
|
|
AND (COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date
|
|
BETWEEN $1::date AND $2::date
|
|
ORDER BY wo.order_item_id, wo.id DESC
|
|
)
|
|
INSERT INTO cost_snapshots (
|
|
subject_type, subject_id, order_id, order_item_id, work_order_id,
|
|
cost_item_id, cost_version_id, canonical_product_id,
|
|
quantity, unit_amount_fen, total_amount_fen, snapshot_stage, source,
|
|
metadata_json, cost_basis_at, resolved_at, created_at
|
|
)
|
|
SELECT
|
|
'order_item', l.order_item_id, oi.order_id, l.order_item_id, l.work_order_id,
|
|
si.cost_item_id, COALESCE(rv.id, si.cost_version_id),
|
|
oi.canonical_product_id,
|
|
1, l.reward_amount, l.reward_amount, 'estimated', 'resolved',
|
|
jsonb_build_object(
|
|
'source', 'work_order_reward_committed',
|
|
'workOrderStatus', l.status,
|
|
'estimatedRewardFen', l.reward_amount,
|
|
'basisAt', l.basis_at
|
|
),
|
|
l.basis_at, NOW(), NOW()
|
|
FROM latest l
|
|
CROSS JOIN system_item si
|
|
JOIN order_items oi ON oi.id = l.order_item_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT v.id
|
|
FROM cost_versions v
|
|
WHERE v.cost_item_id = si.cost_item_id
|
|
AND v.scope_type = 'rule'
|
|
AND v.work_product_rule_id = l.product_rule_id
|
|
AND v.status IN ('active', 'retired')
|
|
AND v.effective_from <= l.basis_at
|
|
AND (v.effective_to IS NULL OR v.effective_to > l.basis_at)
|
|
ORDER BY v.effective_from DESC, v.id DESC
|
|
LIMIT 1
|
|
) rv ON TRUE
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM work_orders accepted
|
|
WHERE accepted.order_item_id = l.order_item_id
|
|
AND accepted.status = 'accepted'
|
|
)
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM cost_snapshots cs
|
|
WHERE cs.subject_type = 'order_item'
|
|
AND cs.subject_id = l.order_item_id
|
|
AND cs.cost_item_id = si.cost_item_id
|
|
AND cs.superseded_at IS NULL
|
|
)
|
|
ON CONFLICT (subject_type, subject_id, cost_item_id)
|
|
WHERE superseded_at IS NULL
|
|
DO NOTHING
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
return {
|
|
inserted: inserted.rowCount || 0,
|
|
superseded: superseded.rowCount || 0,
|
|
}
|
|
}
|
|
|
|
async function insertAndResolveProductMappingExceptions(
|
|
client: PoolClient,
|
|
fromDate: string,
|
|
toDate: string,
|
|
) {
|
|
const inserted = await client.query(
|
|
`
|
|
WITH item_scope AS (
|
|
SELECT
|
|
oi.id AS order_item_id,
|
|
o.provider,
|
|
o.shop_id,
|
|
oi.sku_code,
|
|
oi.sku_name,
|
|
oi.canonical_product_id,
|
|
COALESCE(o.paid_at, o.created_at) AS basis_at,
|
|
wo.id AS work_order_id,
|
|
COALESCE(oi.fulfillment_mode, 'manual') AS fulfillment_mode
|
|
FROM order_items oi
|
|
JOIN orders o ON o.id = oi.order_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT id FROM work_orders WHERE order_item_id = oi.id ORDER BY id DESC LIMIT 1
|
|
) wo ON TRUE
|
|
WHERE o.pay_status IN ('paid', 'refunded')
|
|
AND (COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date
|
|
BETWEEN $1::date AND $2::date
|
|
)
|
|
INSERT INTO cost_exceptions (
|
|
subject_type, subject_id, order_item_id, work_order_id,
|
|
reason, status, detail_json, created_at
|
|
)
|
|
SELECT
|
|
'order_item', s.order_item_id, s.order_item_id, s.work_order_id,
|
|
'product_mapping_missing', 'open',
|
|
jsonb_build_object(
|
|
'provider', s.provider,
|
|
'shopId', s.shop_id,
|
|
'skuCode', s.sku_code,
|
|
'skuName', s.sku_name,
|
|
'fulfillmentMode', s.fulfillment_mode,
|
|
'basisAt', s.basis_at,
|
|
'message', '订单项没有归一到标准商品,销售事实会进入未归一桶'
|
|
),
|
|
NOW()
|
|
FROM item_scope s
|
|
WHERE s.canonical_product_id IS NULL
|
|
ON CONFLICT (subject_type, subject_id, reason) DO NOTHING
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
const resolved = await client.query(
|
|
`
|
|
UPDATE cost_exceptions ce
|
|
SET status = 'resolved', resolved_at = NOW()
|
|
WHERE ce.status = 'open'
|
|
AND ce.subject_type = 'order_item'
|
|
AND ce.reason = 'product_mapping_missing'
|
|
AND EXISTS (
|
|
SELECT 1 FROM order_items oi
|
|
WHERE oi.id = ce.subject_id AND oi.canonical_product_id IS NOT NULL
|
|
)
|
|
`,
|
|
)
|
|
|
|
return { created: inserted.rowCount || 0, resolved: resolved.rowCount || 0 }
|
|
}
|
|
|
|
async function insertAndResolveCostMissingExceptions(
|
|
client: PoolClient,
|
|
fromDate: string,
|
|
toDate: string,
|
|
) {
|
|
const inserted = await client.query(
|
|
`
|
|
WITH item_scope AS (
|
|
SELECT
|
|
oi.id AS order_item_id,
|
|
oi.canonical_product_id,
|
|
oi.fulfillment_config_id,
|
|
COALESCE(o.paid_at, o.created_at) AS basis_at,
|
|
wo.id AS work_order_id,
|
|
COALESCE(oi.fulfillment_mode, 'manual') AS fulfillment_mode
|
|
FROM order_items oi
|
|
JOIN orders o ON o.id = oi.order_id
|
|
LEFT JOIN LATERAL (
|
|
SELECT id FROM work_orders WHERE order_item_id = oi.id ORDER BY id DESC LIMIT 1
|
|
) wo ON TRUE
|
|
WHERE o.pay_status IN ('paid', 'refunded')
|
|
AND (COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date
|
|
BETWEEN $1::date AND $2::date
|
|
)
|
|
INSERT INTO cost_exceptions (
|
|
subject_type, subject_id, order_item_id, work_order_id,
|
|
reason, status, detail_json, created_at
|
|
)
|
|
SELECT
|
|
'order_item', s.order_item_id, s.order_item_id, s.work_order_id,
|
|
'cost_missing', 'open',
|
|
jsonb_build_object(
|
|
'canonicalProductId', s.canonical_product_id,
|
|
'fulfillmentMode', s.fulfillment_mode,
|
|
'basisAt', s.basis_at,
|
|
'message', '订单项没有匹配到任何成本版本'
|
|
),
|
|
NOW()
|
|
FROM item_scope s
|
|
WHERE s.canonical_product_id IS NOT NULL
|
|
-- 人工履约不靠成本版本结算:金额由「人工 SKU 配置 × 换算率」决定,
|
|
-- 并在客服确认履约时写 manual_case 实际快照,因此不参与成本缺失判定。
|
|
AND s.fulfillment_mode <> 'manual'
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM cost_snapshots cs
|
|
WHERE cs.subject_type = 'order_item'
|
|
AND cs.subject_id = s.order_item_id
|
|
AND cs.superseded_at IS NULL
|
|
)
|
|
ON CONFLICT (subject_type, subject_id, reason) DO NOTHING
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
const manualInserted = await client.query(
|
|
`
|
|
WITH cutoff AS (
|
|
SELECT MIN(created_at) AS enforce_from
|
|
FROM manual_fulfillment_configs
|
|
),
|
|
item_scope AS (
|
|
SELECT
|
|
oi.id AS order_item_id,
|
|
oi.fulfillment_config_id,
|
|
COALESCE(o.paid_at, o.created_at) AS basis_at
|
|
FROM order_items oi
|
|
JOIN orders o ON o.id = oi.order_id
|
|
WHERE o.pay_status IN ('paid', 'refunded')
|
|
AND (COALESCE(o.paid_at, o.created_at) AT TIME ZONE 'Asia/Shanghai')::date
|
|
BETWEEN $1::date AND $2::date
|
|
AND oi.fulfillment_mode = 'manual'
|
|
)
|
|
INSERT INTO cost_exceptions (
|
|
subject_type, subject_id, order_item_id, work_order_id,
|
|
reason, status, detail_json, created_at
|
|
)
|
|
SELECT
|
|
'order_item', s.order_item_id, s.order_item_id, NULL,
|
|
'manual_cost_missing', 'open',
|
|
jsonb_build_object(
|
|
'fulfillmentMode', 'manual',
|
|
'basisAt', s.basis_at,
|
|
'configMissing', s.fulfillment_config_id IS NULL,
|
|
'message', CASE
|
|
WHEN s.fulfillment_config_id IS NULL
|
|
THEN '人工订单项未配置履约渠道,请先在成本中心配置人工 SKU'
|
|
ELSE '人工订单项尚未确认履约并记录成本'
|
|
END
|
|
),
|
|
NOW()
|
|
FROM item_scope s
|
|
CROSS JOIN cutoff c
|
|
WHERE c.enforce_from IS NOT NULL
|
|
-- 只监控「第一次配置人工履约之后」的订单项:历史人工单按既定口径不补录。
|
|
AND s.basis_at >= c.enforce_from
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM cost_snapshots cs
|
|
WHERE cs.subject_type = 'manual_case'
|
|
AND cs.order_item_id = s.order_item_id
|
|
AND cs.superseded_at IS NULL
|
|
)
|
|
ON CONFLICT (subject_type, subject_id, reason) DO NOTHING
|
|
`,
|
|
[fromDate, toDate],
|
|
)
|
|
|
|
const resolved = await client.query(
|
|
`
|
|
UPDATE cost_exceptions ce
|
|
SET status = 'resolved', resolved_at = NOW()
|
|
WHERE ce.status = 'open'
|
|
AND ce.subject_type = 'order_item'
|
|
AND ce.reason = 'cost_missing'
|
|
AND EXISTS (
|
|
SELECT 1 FROM cost_snapshots cs
|
|
WHERE cs.subject_type = 'order_item'
|
|
AND cs.subject_id = ce.subject_id
|
|
AND cs.superseded_at IS NULL
|
|
)
|
|
`,
|
|
)
|
|
|
|
const manualResolved = await client.query(
|
|
`
|
|
UPDATE cost_exceptions ce
|
|
SET status = 'resolved', resolved_at = NOW()
|
|
WHERE ce.status = 'open'
|
|
AND ce.subject_type = 'order_item'
|
|
AND ce.reason = 'manual_cost_missing'
|
|
AND EXISTS (
|
|
SELECT 1 FROM cost_snapshots cs
|
|
WHERE cs.subject_type = 'manual_case'
|
|
AND cs.order_item_id = ce.subject_id
|
|
AND cs.superseded_at IS NULL
|
|
)
|
|
`,
|
|
)
|
|
|
|
return {
|
|
created: (inserted.rowCount || 0) + (manualInserted.rowCount || 0),
|
|
resolved: (resolved.rowCount || 0) + (manualResolved.rowCount || 0),
|
|
}
|
|
}
|