Files
one-pipe-system/src/utils/business/calculate.ts
sexygoat 222e5bb11a Initial commit: One Pipe System
完整的管理系统,包含账户管理、卡片管理、套餐管理、财务管理等功能模块。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-01-22 16:35:33 +08:00

120 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 业务计算工具函数
*/
/**
* 计算佣金
* @param amount 交易金额(分)
* @param rate 佣金比例0-100
* @param type 佣金类型percentage-百分比, fixed-固定金额
*/
export function calculateCommission(
amount: number,
rate: number,
type: 'percentage' | 'fixed' = 'percentage'
): number {
if (type === 'fixed') {
return rate
}
return Math.floor((amount * rate) / 100)
}
/**
* 计算折扣价
* @param originalPrice 原价(分)
* @param discount 折扣0-100
*/
export function calculateDiscountPrice(originalPrice: number, discount: number): number {
return Math.floor((originalPrice * discount) / 100)
}
/**
* 计算流量使用率
* @param used 已用流量MB
* @param total 总流量MB
*/
export function calculateFlowUsageRate(used: number, total: number): number {
if (total === 0) return 0
return Math.min(100, Math.round((used / total) * 100))
}
/**
* 计算剩余天数
* @param expireTime 过期时间
*/
export function calculateRemainingDays(expireTime: string): number {
const now = new Date().getTime()
const expire = new Date(expireTime).getTime()
const diff = expire - now
if (diff <= 0) return 0
return Math.ceil(diff / (1000 * 60 * 60 * 24))
}
/**
* 计算环比增长率
* @param current 当前值
* @param previous 上期值
*/
export function calculateGrowthRate(current: number, previous: number): number {
if (previous === 0) return current > 0 ? 100 : 0
return Math.round(((current - previous) / previous) * 100)
}
/**
* 计算平均值
* @param values 数值数组
*/
export function calculateAverage(values: number[]): number {
if (values.length === 0) return 0
const sum = values.reduce((acc, val) => acc + val, 0)
return Math.round((sum / values.length) * 100) / 100
}
/**
* 计算提现手续费
* @param amount 提现金额(分)
* @param feeRate 手续费率0-100
* @param feeType 手续费类型percentage-百分比, fixed-固定
*/
export function calculateWithdrawalFee(
amount: number,
feeRate: number,
feeType: 'percentage' | 'fixed' = 'percentage'
): number {
if (feeType === 'fixed') {
return feeRate
}
return Math.floor((amount * feeRate) / 100)
}
/**
* 计算实际到账金额
* @param amount 提现金额(分)
* @param fee 手续费(分)
*/
export function calculateActualAmount(amount: number, fee: number): number {
return Math.max(0, amount - fee)
}
/**
* 计算分页偏移量
* @param page 当前页
* @param pageSize 每页数量
*/
export function calculateOffset(page: number, pageSize: number): number {
return (page - 1) * pageSize
}
/**
* 计算总页数
* @param total 总记录数
* @param pageSize 每页数量
*/
export function calculateTotalPages(total: number, pageSize: number): number {
return Math.ceil(total / pageSize)
}