feat(cost-center): 同步工单补同步待完善价格,跳过改过用户付款金额的工单

This commit is contained in:
2026-11-16 17:48:19 +08:00
parent 7b5330e778
commit 9142bbfaf0
9 changed files with 300 additions and 16 deletions
@@ -147,6 +147,53 @@ export async function listWorkOrdersForPriceSyncByProductRuleId(
return result.rows
}
/**
* 查询固定时间之后创建、仍处于「待完善」的工单。
*
* 迁移 165 之后模板改价默认只刷新大厅 open 工单;只有手动「同步工单」显式要求
* 待完善时才按这个时间窗取候选,避免一次性重写历史积压的待完善订单。
* 用户付款金额有改价历史的工单单独计数并排除,避免覆盖人工核定的付款金额。
*/
export async function listPendingMaterialWorkOrdersForPriceSyncByProductRuleId(
ruleId: number | string,
createdAfter: string,
client?: Pick<PoolClient, 'query'>,
): Promise<{
orders: WorkOrderRow[]
paymentAmountAdjustedCount: number
}> {
const params = [Number(ruleId), createdAfter]
const baseWhere = `wo.product_rule_id = $1
AND wo.status = 'pending_material'
AND wo.created_at > $2::timestamptz`
const paymentAdjustedFilter = `(
wo.user_payment_amount_updated_at IS NOT NULL
OR EXISTS (
SELECT 1
FROM work_order_payment_amount_history h
WHERE h.work_order_id = wo.id
)
)`
const ordersSql = `${WORK_ORDER_SELECT}
WHERE ${baseWhere}
AND NOT ${paymentAdjustedFilter}
ORDER BY wo.id ASC`
const countSql = `SELECT COUNT(*)::int AS total
FROM work_orders wo
WHERE ${baseWhere}
AND ${paymentAdjustedFilter}`
const ordersResult = client
? await client.query<WorkOrderRow>(ordersSql, params)
: await query<WorkOrderRow>(ordersSql, params)
const countResult = client
? await client.query<{ total: number }>(countSql, params)
: await query<{ total: number }>(countSql, params)
return {
orders: ordersResult.rows,
paymentAmountAdjustedCount: Number(countResult.rows[0]?.total || 0),
}
}
export async function lockWorkProductRuleForPriceUpdate(
client: Pick<PoolClient, 'query'>,
ruleKey: string,