fix(admin): 恢复接单模板计价配置并统一成本价格链路

This commit is contained in:
yml2213
2026-11-02 17:57:49 +08:00
parent d871b93fde
commit a7bac9c1d8
27 changed files with 304 additions and 83 deletions
@@ -188,13 +188,11 @@ export function mapCostVersion(row: CostVersionRow) {
const revenueFen = Number(row.revenue_fen || 0)
const rewardAvgFen = Number(row.reward_avg_fen || 0)
const rewardFen = Number(row.reward_fen || 0)
const ruleUnitPriceFen = Number(row.rule_unit_price_fen || 0)
const ruleRewardAmountFen = Number(row.rule_reward_amount || 0)
const amountKind =
row.cost_item_code === 'sys_work_order_reward' &&
row.scope_type === 'rule' &&
ruleUnitPriceFen > 0 &&
ruleRewardAmountFen === 0
row.work_product_rule_pricing_mode === 'unit'
? 'unit_price'
: 'fixed'
return {
@@ -214,7 +212,7 @@ export function mapCostVersion(row: CostVersionRow) {
workProductRuleName: row.work_product_rule_name || '',
workProductRuleKey: row.work_product_rule_key || '',
ruleRewardAmountFen,
ruleUnitPriceFen,
ruleUnitPriceFen: Number(row.rule_unit_price_fen || 0),
revenueItemCount,
revenueFen,
revenueAvgFen: revenueItemCount > 0 ? Math.round(revenueFen / revenueItemCount) : 0,
@@ -113,6 +113,7 @@ export type CostVersionRow = {
work_product_rule_id: number | null
work_product_rule_name: string | null
work_product_rule_key: string | null
work_product_rule_pricing_mode?: 'fixed' | 'unit' | null
rule_reward_amount: number | null
rule_unit_price_fen: number | null
revenue_item_count: number | null
@@ -324,6 +324,7 @@ export async function listAdminCostVersions(payload: JsonObject = {}) {
p.canonical_name,
wpr.product_name AS work_product_rule_name,
wpr.rule_key AS work_product_rule_key,
wpr.pricing_mode AS work_product_rule_pricing_mode,
wpr.reward_amount AS rule_reward_amount,
wpr.unit_price_fen AS rule_unit_price_fen,
rev.revenue_item_count,
@@ -103,6 +103,25 @@ test('成本解析:卡券渠道按件计,取业务日期前的最新版本',
assert.equal(resolution.unitAmountFen, 5000)
})
test('成本解析:接单模板按 SKU 计件时,预估成本按数量乘每件金额', () => {
const resolution = resolveCost(
item({
fulfillment_channel: 'work_order',
canonical_product_id: null,
quantity: 2,
work_product_rule_id: 9,
work_product_rule_pricing_mode: 'unit',
}),
costItems,
versions,
pricingModes,
)
assert.ok(resolution)
assert.equal(resolution.costVersionId, 101)
assert.equal(resolution.unitAmountFen, 840)
assert.equal(resolution.totalAmountFen, 1680)
})
test('成本解析:生效日期晚于业务日期的版本不参与', () => {
const version = selectCostVersion(versions, {
costItemId: 4,
@@ -62,6 +62,7 @@ export type CostCandidateItem = {
basis_at: string
work_order_id: number | null
work_product_rule_id: number | null
work_product_rule_pricing_mode?: 'fixed' | 'unit' | null
work_order_status: string | null
reward_amount: number | null
revenue_fen: number
@@ -157,7 +158,14 @@ export function resolveCost(
})
if (!version) return null
const pricingMode = pricingModeByItemId.get(costItem.id) || costItem.pricing_mode
const pricingMode =
item.fulfillment_channel === 'work_order' &&
costItem.code === 'sys_work_order_reward' &&
item.work_product_rule_pricing_mode
? item.work_product_rule_pricing_mode === 'unit'
? 'per_item'
: 'per_order'
: pricingModeByItemId.get(costItem.id) || costItem.pricing_mode
const amount = Number(version.amount_fen) || 0
const quantity = Math.max(1, Number(item.quantity) || 1)
@@ -306,11 +314,13 @@ const CANDIDATE_SQL = `
), 0)::bigint AS refund_fen,
wo.id AS work_order_id,
wo.product_rule_id AS work_product_rule_id,
wpr.pricing_mode AS work_product_rule_pricing_mode,
wo.status AS work_order_status,
wo.reward_amount
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
LEFT JOIN work_orders wo ON wo.order_item_id = oi.id
LEFT JOIN work_product_rules wpr ON wpr.id = wo.product_rule_id
WHERE oi.fulfillment_status = 'assigned'
AND oi.fulfillment_channel IN (
'affiliate_dash', 'third_party_voucher', 'work_order', 'manual'
@@ -153,11 +153,7 @@ export async function listAdminProducts(payload: JsonObject = {}) {
`
SELECT
p.*,
CASE
WHEN tpl_rule.unit_price_fen > 0 AND tpl_rule.reward_amount = 0 THEN 'unit'
WHEN tpl_rule.id IS NOT NULL THEN 'fixed'
ELSE NULL
END AS template_pricing_mode,
tpl_rule.pricing_mode AS template_pricing_mode,
tpl_rule.enabled AS template_enabled,
tpl_rule.sharing_enabled AS template_sharing_enabled,
(
@@ -1551,10 +1547,11 @@ export async function setAdminProductPrice(
enabled: boolean
reward_amount: number
unit_price_fen: number
pricing_mode: 'fixed' | 'unit'
sharing_enabled: boolean
sharing_auto_from_order: boolean
}>(
`SELECT id, rule_key, product_name, enabled, reward_amount, unit_price_fen,
`SELECT id, rule_key, product_name, enabled, reward_amount, unit_price_fen, pricing_mode,
sharing_enabled, sharing_auto_from_order
FROM work_product_rules
WHERE rule_key = $1
@@ -1569,17 +1566,13 @@ export async function setAdminProductPrice(
})
}
const rawPricingMode = text(payload.pricingMode ?? payload.pricing_mode)
if (rawPricingMode && rawPricingMode !== 'fixed' && rawPricingMode !== 'unit') {
throw createHttpError('接单模板计价方式无效', {
if (rawPricingMode) {
throw createHttpError('接单模板计价方式只能在【接单模板】中设置', {
statusCode: 400,
errorCode: 'cost_work_order_pricing_mode_invalid',
errorCode: 'cost_work_order_pricing_mode_managed_by_template',
})
}
const pricingMode =
rawPricingMode ||
(Number(rule.unit_price_fen || 0) > 0 && Number(rule.reward_amount || 0) === 0
? 'unit'
: 'fixed')
const pricingMode: 'fixed' | 'unit' = rule.pricing_mode === 'unit' ? 'unit' : 'fixed'
if (pricingMode === 'fixed' && rule.sharing_enabled && rule.sharing_auto_from_order) {
throw createHttpError(
'拼单自动按件计价与固定金额冲突,请先在接单模板关闭拼单或改为手工拼单价',
@@ -128,6 +128,7 @@ export async function applyAdminTemplateProductBootstrap(
skuNick: template.sku_nick,
relSkuId: template.rel_sku_id,
sellerIds: template.seller_ids_json,
pricingMode: template.pricing_mode === 'unit' ? 'unit' : 'fixed',
unitPriceFen: Number(template.unit_price_fen || 0),
rewardAmountFen: Number(template.reward_amount || 0),
templateEnabled: template.enabled,
@@ -282,6 +283,7 @@ type TemplateBootstrapTemplateRow = {
category: string
unit_price_fen: number
reward_amount: number
pricing_mode: 'fixed' | 'unit'
mapping_id: number | null
mapping_type: string
sku_nick: string
@@ -696,6 +698,7 @@ async function loadTemplateProductBootstrapData(range: { fromDate: string; toDat
COALESCE(c.name, '') AS category,
r.unit_price_fen,
r.reward_amount,
r.pricing_mode,
m.id AS mapping_id,
COALESCE(m.mapping_type, '') AS mapping_type,
COALESCE(m.sku_nick, '') AS sku_nick,
@@ -327,7 +327,7 @@ export async function syncAdminWorkOrderTemplateCostVersions(ruleId?: number) {
SELECT
wpr.id AS rule_id,
CASE
WHEN wpr.unit_price_fen > 0 AND wpr.reward_amount = 0 THEN wpr.unit_price_fen
WHEN wpr.pricing_mode = 'unit' THEN wpr.unit_price_fen
ELSE wpr.reward_amount
END AS amount_fen
FROM work_product_rules wpr
@@ -364,7 +364,7 @@ export async function syncAdminWorkOrderTemplateCostVersions(ruleId?: number) {
SELECT
wpr.id AS rule_id,
CASE
WHEN wpr.unit_price_fen > 0 AND wpr.reward_amount = 0 THEN wpr.unit_price_fen
WHEN wpr.pricing_mode = 'unit' THEN wpr.unit_price_fen
ELSE wpr.reward_amount
END AS amount_fen
FROM work_product_rules wpr
@@ -416,7 +416,7 @@ export async function syncAdminWorkOrderTemplateCostVersions(ruleId?: number) {
'import',
'active',
CASE
WHEN wpr.unit_price_fen > 0 AND wpr.reward_amount = 0
WHEN wpr.pricing_mode = 'unit'
THEN '模板按件发单单价(数量取订单 SKU)'
ELSE '模板固定发单金额'
END,
@@ -469,7 +469,7 @@ export async function syncAdminWorkOrderTemplateCostVersions(ruleId?: number) {
SELECT
COUNT(*)::int AS rule_count,
COUNT(*) FILTER (
WHERE wpr.unit_price_fen > 0 AND wpr.reward_amount = 0
WHERE wpr.pricing_mode = 'unit'
)::int AS unit_price_rule_count,
COUNT(v.id) FILTER (
WHERE v.scope_type = 'rule' AND v.status = 'active'
@@ -45,6 +45,7 @@ test(
assert.ok(ruleId > 0)
assert.ok(productId > 0)
assert.equal(created.rule.enabled, false)
assert.equal(created.rule.pricingMode, 'fixed')
assert.equal(created.costConfigurationRequired, true)
const product = await query<{ canonical_sku: string; status: string }>(
@@ -77,7 +78,6 @@ test(
productId,
{
amountFen: 1234,
pricingMode: 'fixed',
changeReason: '接单模板成本集成测试',
syncOpenOrders: false,
},
@@ -100,10 +100,11 @@ test(
enabled: boolean
reward_amount: number
unit_price_fen: number
pricing_mode: string
amount_fen: number
}>(
`
SELECT wpr.enabled, wpr.reward_amount, wpr.unit_price_fen, v.amount_fen
SELECT wpr.enabled, wpr.reward_amount, wpr.unit_price_fen, wpr.pricing_mode, v.amount_fen
FROM work_product_rules wpr
JOIN cost_versions v ON v.work_product_rule_id = wpr.id AND v.status = 'active'
WHERE wpr.id = $1
@@ -115,6 +116,7 @@ test(
assert.equal(projection.rows[0]?.enabled, false)
assert.equal(Number(projection.rows[0]?.reward_amount), 1234)
assert.equal(Number(projection.rows[0]?.unit_price_fen), 0)
assert.equal(projection.rows[0]?.pricing_mode, 'fixed')
assert.equal(Number(projection.rows[0]?.amount_fen), 1234)
await assert.rejects(
@@ -124,6 +126,30 @@ test(
{
amountFen: 2000,
pricingMode: 'fixed',
syncOpenOrders: false,
},
{
sessionId: `it-${suffix}`,
userId: Number(adminUser.id),
username: adminUser.username,
displayName: adminUser.display_name || adminUser.username,
role: 'admin',
expiresAt: new Date(Date.now() + 60_000).toISOString(),
absoluteExpiresAt: new Date(Date.now() + 60_000).toISOString(),
sessionVersion: 1,
deviceId: 'integration-test',
permissions: [],
},
),
/计价方式只能在【接单模板】中设置/,
)
await assert.rejects(
() =>
setAdminProductPrice(
productId,
{
amountFen: 2000,
effectiveFrom: new Date(Date.now() + 60 * 60 * 1000).toISOString(),
syncOpenOrders: false,
},
@@ -143,11 +169,19 @@ test(
/暂不支持未来生效/,
)
const unitModeRule = await saveAdminWorkProductRule({
ruleId,
productName,
pricingMode: 'unit',
})
assert.equal(unitModeRule.rule.pricingMode, 'unit')
assert.equal(unitModeRule.rule.unitPriceFen, 1234)
assert.equal(unitModeRule.rule.rewardAmount, 0)
await setAdminProductPrice(
productId,
{
amountFen: 1300,
pricingMode: 'unit',
changeReason: '接单模板按件成本集成测试',
syncOpenOrders: false,
},
@@ -167,10 +201,11 @@ test(
const unitProjection = await query<{
reward_amount: number
unit_price_fen: number
pricing_mode: string
amount_fen: number
}>(
`
SELECT wpr.reward_amount, wpr.unit_price_fen, v.amount_fen
SELECT wpr.reward_amount, wpr.unit_price_fen, wpr.pricing_mode, v.amount_fen
FROM work_product_rules wpr
JOIN cost_versions v ON v.work_product_rule_id = wpr.id AND v.status = 'active'
WHERE wpr.id = $1
@@ -181,8 +216,18 @@ test(
)
assert.equal(Number(unitProjection.rows[0]?.reward_amount), 0)
assert.equal(Number(unitProjection.rows[0]?.unit_price_fen), 1300)
assert.equal(unitProjection.rows[0]?.pricing_mode, 'unit')
assert.equal(Number(unitProjection.rows[0]?.amount_fen), 1300)
const fixedModeRule = await saveAdminWorkProductRule({
ruleId,
productName,
pricingMode: 'fixed',
})
assert.equal(fixedModeRule.rule.pricingMode, 'fixed')
assert.equal(fixedModeRule.rule.rewardAmount, 1300)
assert.equal(fixedModeRule.rule.unitPriceFen, 0)
const enabled = await saveAdminWorkProductRule({
ruleId,
productName,
@@ -97,10 +97,7 @@ export async function syncWorkOrderTemplateProduct(
ruleKey: rule.rule_key,
categoryId: rule.category_id ? Number(rule.category_id) : null,
templateEnabled: rule.enabled === true,
pricingMode:
Number(rule.unit_price_fen || 0) > 0 && Number(rule.reward_amount || 0) === 0
? 'unit'
: 'fixed',
pricingMode: rule.pricing_mode === 'unit' ? 'unit' : 'fixed',
rewardAmountFen: Number(rule.reward_amount || 0),
unitPriceFen: Number(rule.unit_price_fen || 0),
sharingEnabled: rule.sharing_enabled === true,