来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
117 lines
3.7 KiB
TypeScript
117 lines
3.7 KiB
TypeScript
import type { PoolClient } from 'pg';
|
|
import { pool } from '../db';
|
|
|
|
type Db = Pick<PoolClient, 'query'> | typeof pool;
|
|
|
|
export type LastSupplyCostSource =
|
|
| 'last_prep'
|
|
| 'last_purchase'
|
|
| 'supply_price'
|
|
| null;
|
|
|
|
export interface LastPurchasePriceResult {
|
|
price: number | null;
|
|
source: LastSupplyCostSource;
|
|
at?: string | null;
|
|
}
|
|
|
|
/**
|
|
* 报价成本缺失时的成交价记忆:
|
|
* - 有供应商:上次已完成备货价 → 同供应商已完成自采价 → 供货目录价
|
|
* - 自采(无供应商):上次自采实采/采购价
|
|
* 仅返回 > 0 的价格。
|
|
*/
|
|
export async function resolveLastPurchasePrice(input: {
|
|
skuCode: string;
|
|
supplierCode?: string | null;
|
|
branchCode?: string | null;
|
|
client?: Pick<PoolClient, 'query'> | null;
|
|
}): Promise<LastPurchasePriceResult> {
|
|
const skuCode = String(input.skuCode || '').trim();
|
|
const supplierCode = String(input.supplierCode || '').trim();
|
|
const branchCode = String(input.branchCode || '').trim() || null;
|
|
if (!skuCode) return { price: null, source: null };
|
|
|
|
const db: Db = input.client || pool;
|
|
|
|
if (supplierCode) {
|
|
const lastPrep = await db.query(
|
|
`SELECT purchase_price, prepare_time, created_at
|
|
FROM supplier_preparations
|
|
WHERE sku_code = $1
|
|
AND supplier_code = $2
|
|
AND COALESCE(purchase_price, 0) > 0
|
|
AND prepare_status >= 30
|
|
AND COALESCE(is_stockout, false) = false
|
|
AND ($3::text IS NULL OR branch_code IS NOT DISTINCT FROM $3)
|
|
ORDER BY COALESCE(prepare_time, created_at) DESC NULLS LAST
|
|
LIMIT 1`,
|
|
[skuCode, supplierCode, branchCode]
|
|
);
|
|
if (lastPrep.rows[0]) {
|
|
const price = Number(lastPrep.rows[0].purchase_price);
|
|
if (Number.isFinite(price) && price > 0) {
|
|
return {
|
|
price,
|
|
source: 'last_prep',
|
|
at: lastPrep.rows[0].prepare_time || lastPrep.rows[0].created_at || null,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
const lastPurchase = await db.query(
|
|
`SELECT actual_price, purchase_price, reviewed_at, submitted_at, created_at
|
|
FROM purchase_orders
|
|
WHERE sku_code = $1
|
|
AND ($2::text IS NULL OR branch_code IS NOT DISTINCT FROM $2)
|
|
AND (
|
|
($3::text = '' AND COALESCE(supplier_code, '') = '')
|
|
OR ($3::text <> '' AND COALESCE(supplier_code, '') = $3)
|
|
)
|
|
AND COALESCE(actual_price, purchase_price, 0) > 0
|
|
AND purchase_status >= 20
|
|
AND purchase_status <> 40
|
|
ORDER BY COALESCE(reviewed_at, submitted_at, created_at) DESC
|
|
LIMIT 1`,
|
|
[skuCode, branchCode, supplierCode]
|
|
);
|
|
const lastRow = lastPurchase.rows[0];
|
|
if (lastRow) {
|
|
const actual = Number(lastRow.actual_price);
|
|
const purchase = Number(lastRow.purchase_price);
|
|
const price = Number.isFinite(actual) && actual > 0 ? actual : purchase;
|
|
if (Number.isFinite(price) && price > 0) {
|
|
return {
|
|
price,
|
|
source: 'last_purchase',
|
|
at: lastRow.reviewed_at || lastRow.submitted_at || lastRow.created_at || null,
|
|
};
|
|
}
|
|
}
|
|
|
|
if (supplierCode) {
|
|
const supply = await db.query(
|
|
`SELECT supply_price FROM sku_suppliers
|
|
WHERE sku_code = $1 AND supplier_code = $2 AND status = 1
|
|
LIMIT 1`,
|
|
[skuCode, supplierCode]
|
|
);
|
|
if (supply.rows[0]?.supply_price != null) {
|
|
const price = Number(supply.rows[0].supply_price);
|
|
if (Number.isFinite(price) && price > 0) {
|
|
return { price, source: 'supply_price' };
|
|
}
|
|
}
|
|
}
|
|
|
|
return { price: null, source: null };
|
|
}
|
|
|
|
/** 与 resolveLastPurchasePrice 同义,便于分配/建议价调用 */
|
|
export async function resolveLastSupplyCostPrice(
|
|
input: Parameters<typeof resolveLastPurchasePrice>[0]
|
|
): Promise<LastPurchasePriceResult> {
|
|
return resolveLastPurchasePrice(input);
|
|
}
|