v3.5.4版本提交
CI / verify (pull_request) Waiting to run

This commit is contained in:
2026-08-21 15:48:15 +08:00
parent 26334ed072
commit a01217539c
32 changed files with 2278 additions and 147 deletions
+41 -7
View File
@@ -7,27 +7,61 @@ export function calcBenefitAmount(product: ProductPricing): number {
return product.benefitAmount ?? product.price;
}
export type ProductSaleUnit = 'BOTTLE' | 'BOX';
/** 销售数量 → 瓶当量 */
export function toBottleQuantity(quantity: number, bottlesPerUnit = 1): number {
const qty = Math.floor(Number(quantity) || 0);
const per = Math.floor(Number(bottlesPerUnit) || 0);
if (qty <= 0 || per <= 0) return 0;
return qty * per;
}
/**
* 城市起购瓶数 → 最少销售单位数量(向上取整)
* 例:同城 2 瓶、整箱 6 瓶/箱 → minSaleQty = 1
*/
export function toMinSaleQuantity(minBottleQty: number, bottlesPerUnit = 1): number {
const minBottles = Math.floor(Number(minBottleQty) || 0);
const per = Math.floor(Number(bottlesPerUnit) || 0);
if (minBottles <= 0) return 1;
if (per <= 0) return minBottles;
return Math.max(1, Math.ceil(minBottles / per));
}
export function saleUnitLabel(saleUnit: ProductSaleUnit | string | null | undefined): string {
return saleUnit === 'BOX' ? '箱' : '瓶';
}
export function validateMinPurchase(
deliveryType: 'LOCAL' | 'CROSS_CITY' | 'ON_SITE_PICKUP',
quantity: number,
localMinQty: number,
crossMinQty: number,
options?: { bottlesPerUnit?: number; saleUnit?: ProductSaleUnit },
): { ok: boolean; message?: string } {
const bottlesPerUnit = options?.bottlesPerUnit ?? 1;
const saleUnit = options?.saleUnit ?? (bottlesPerUnit > 1 ? 'BOX' : 'BOTTLE');
const bottleQty = toBottleQuantity(quantity, bottlesPerUnit);
const unit = saleUnitLabel(saleUnit);
if (deliveryType === 'ON_SITE_PICKUP') {
const min = localMinQty > 0 ? localMinQty : 2;
if (quantity < min) {
return { ok: false, message: `现场提货至少购买 ${min}` };
const minBottles = localMinQty > 0 ? localMinQty : 2;
if (bottleQty < minBottles) {
const minSale = toMinSaleQuantity(minBottles, bottlesPerUnit);
return { ok: false, message: `现场提货至少购买 ${minSale}${unit}` };
}
return { ok: true };
}
const min = deliveryType === 'LOCAL' ? localMinQty : crossMinQty;
if (quantity < min) {
const minBottles = deliveryType === 'LOCAL' ? localMinQty : crossMinQty;
if (bottleQty < minBottles) {
const minSale = toMinSaleQuantity(minBottles, bottlesPerUnit);
return {
ok: false,
message:
deliveryType === 'LOCAL'
? `同城配送至少购买 ${min}`
: `跨城配送至少购买 ${min} 瓶(1箱)`,
? `同城配送至少购买 ${minSale}${unit}`
: `跨城配送至少购买 ${minSale}${unit}`,
};
}
return { ok: true };