91 lines
3.7 KiB
TypeScript
91 lines
3.7 KiB
TypeScript
import type { RecycleExchange, RecycleExchangeActionType, RecycleExchangeStatus, RecycleStatus } from "@/types/Recycle";
|
|
import { comparePackageQuantity, getPackageQuantityText } from "@/utils/package";
|
|
|
|
/** 配置状态(回收物、规则)的展示文案。 */
|
|
export function getRecycleStatusText(status?: RecycleStatus): string {
|
|
return ({ ACTIVE: "启用", INACTIVE: "停用" } as Record<string, string>)[status || ""] || "未知";
|
|
}
|
|
|
|
export function getRecycleStatusTheme(status?: RecycleStatus): "success" | "warning" | "default" {
|
|
if (status === "ACTIVE") {
|
|
return "success";
|
|
}
|
|
if (status === "INACTIVE") {
|
|
return "warning";
|
|
}
|
|
return "default";
|
|
}
|
|
|
|
/** 兑换单状态文案。 */
|
|
export function getRecycleExchangeStatusText(status?: RecycleExchangeStatus): string {
|
|
return ({
|
|
PENDING: "待核销",
|
|
VERIFIED: "已核销",
|
|
CANCELLED: "已撤销",
|
|
REVOKED: "已冲正"
|
|
} as Record<string, string>)[status || ""] || "未知";
|
|
}
|
|
|
|
export function getRecycleExchangeStatusTheme(status?: RecycleExchangeStatus): "success" | "warning" | "danger" | "primary" {
|
|
if (status === "VERIFIED") {
|
|
return "success";
|
|
}
|
|
if (status === "PENDING") {
|
|
return "warning";
|
|
}
|
|
if (status === "CANCELLED") {
|
|
return "danger";
|
|
}
|
|
// 已冲正用主色,默认灰标签在深色模式下会变成白底不可读
|
|
return "primary";
|
|
}
|
|
|
|
/** 操作记录类型文案。 */
|
|
export function getRecycleActionText(type?: RecycleExchangeActionType): string {
|
|
return ({ VERIFY: "核销", CANCEL: "撤销", REVOKE: "冲正" } as Record<string, string>)[type || ""] || "未知操作";
|
|
}
|
|
|
|
/** 兑换单可执行的操作,服务端仍会按状态和权限二次校验。 */
|
|
export function canVerifyExchange(status?: RecycleExchangeStatus): boolean {
|
|
return status === "PENDING";
|
|
}
|
|
|
|
export function canCancelExchange(status?: RecycleExchangeStatus): boolean {
|
|
return status === "PENDING";
|
|
}
|
|
|
|
export function canRevokeExchange(status?: RecycleExchangeStatus): boolean {
|
|
return status === "VERIFIED";
|
|
}
|
|
|
|
/** 拼接「数量 + 单位」,后端 BigDecimal 可能是字符串。 */
|
|
export function getRecycleQuantityText(value: number | string | null | undefined, unit?: string | null): string {
|
|
const quantityText = getPackageQuantityText(value);
|
|
return unit ? `${quantityText} ${unit}` : quantityText;
|
|
}
|
|
|
|
/**
|
|
* 按整数倍数放大规则数量。
|
|
*
|
|
* 用 BigInt 做定点乘法,避免 0.5 × 3 这类组合出现二进制浮点尾数。
|
|
* 倍数可能来自 TDesign Stepper,它的 formatNumber 返回字符串(如 "2"、"2.0"),
|
|
* 所以必须先转数字,不能直接对入参用 Number.isFinite。
|
|
*/
|
|
export function multiplyRecycleQuantity(value: number | string | null | undefined, multiple: number | string | null | undefined): string {
|
|
const quantityText = getPackageQuantityText(value);
|
|
const [integerText, fractionText = ""] = quantityText.split(".");
|
|
const multipleValue = Number(multiple);
|
|
const multiplier = Number.isFinite(multipleValue) && 0 < multipleValue ? Math.trunc(multipleValue) : 0;
|
|
const digits = BigInt(`${integerText}${fractionText}`) * BigInt(multiplier);
|
|
const padded = digits.toString().padStart(fractionText.length + 1, "0");
|
|
const integer = fractionText.length ? padded.slice(0, -fractionText.length) : padded;
|
|
const fraction = fractionText.length ? padded.slice(-fractionText.length).replace(/0+$/, "") : "";
|
|
return fraction ? `${integer}.${fraction}` : integer;
|
|
}
|
|
|
|
/** 实际数量是否被有权限的操作人调整过(与预设值不同)。 */
|
|
export function isRecycleQuantityAdjusted(exchange: RecycleExchange): boolean {
|
|
return comparePackageQuantity(exchange.recycleQuantity ?? "0", exchange.defaultRecycleQuantity ?? "0") !== 0
|
|
|| comparePackageQuantity(exchange.exchangeQuantity ?? "0", exchange.defaultExchangeQuantity ?? "0") !== 0;
|
|
}
|