Files
Wechat/utils/deal-stat-display.js

112 lines
4.1 KiB
JavaScript

/**
* 成交指标展示:后端有样本就展示(含真实 0%),无样本不造假。
* - 成交率/罚款率:有 rate / rate_text 即展示
* - 均时:有 avg_deal_hours_text 才展示
*/
function parseRate(textOrNum) {
if (textOrNum == null || textOrNum === '') return null;
if (typeof textOrNum === 'number') {
return Number.isFinite(textOrNum) ? textOrNum : null;
}
const s = String(textOrNum).trim().replace(/%/g, '');
const n = Number(s);
return Number.isFinite(n) ? n : null;
}
function formatRateText(rate, fallbackText) {
if (fallbackText) return fallbackText;
const pct = parseRate(rate);
if (pct == null) return '';
const asPercent = pct > 1 ? pct : pct * 100;
return `${asPercent.toFixed(1)}%`;
}
function hasRateValue(rate, text) {
if (text != null && String(text).trim() !== '') return true;
return rate != null && rate !== '';
}
/**
* 把接口 data 收成列表顶栏可用结构;全无可展示则返回 null
*/
export function sanitizeMyDealStat(raw, role = 'merchant') {
if (!raw || typeof raw !== 'object') return null;
const out = { role: raw.role || role };
const chips = [];
const dealRate = raw.deal_rate != null ? raw.deal_rate : raw.deal_rate_text;
if (hasRateValue(raw.deal_rate, raw.deal_rate_text)) {
const pct = parseRate(dealRate);
out.deal_rate = pct == null ? 0 : (pct > 1 ? pct / 100 : pct);
out.deal_rate_text = formatRateText(dealRate, raw.deal_rate_text);
out.deal_label = '成交率';
chips.push({ k: 'deal', t: `成交率 ${out.deal_rate_text}` });
}
const fineKey = role === 'dashou' ? 'fined_rate' : 'fine_rate';
const fineTextKey = role === 'dashou' ? 'fined_rate_text' : 'fine_rate_text';
const fineRate = raw[fineKey] != null
? raw[fineKey]
: (raw[fineTextKey] || raw.fine_rate_text || raw.fined_rate_text);
const fineText = raw[fineTextKey] || raw.fine_rate_text || raw.fined_rate_text;
if (hasRateValue(raw[fineKey] != null ? raw[fineKey] : raw.fine_rate, fineText)) {
const finePct = parseRate(fineRate);
out[fineKey] = finePct == null ? 0 : (finePct > 1 ? finePct / 100 : finePct);
out[fineTextKey] = formatRateText(fineRate, fineText);
out.fine_label = role === 'dashou' ? '被罚款率' : '罚款率';
chips.push({
k: 'fine',
t: `${out.fine_label} ${out[fineTextKey]}`,
});
}
if (raw.avg_deal_hours_text) {
out.avg_deal_hours_text = raw.avg_deal_hours_text;
out.avg_deal_hours = raw.avg_deal_hours;
chips.push({ k: 'avg', t: `均时 ${raw.avg_deal_hours_text}`, hl: true });
}
if (!chips.length) return null;
out.chips = chips;
out.order_count = raw.order_count;
out.completed_count = raw.completed_count;
return out;
}
/**
* 抢单池/组队卡片芯片:后端给了哪个字段就展示哪个(含 0%)
*/
export function buildDealStatChips(stat, opts = {}) {
if (!stat || typeof stat !== 'object') return [];
const dealLabel = opts.dealLabel || '成交率';
const fineLabel = opts.fineLabel || '罚款率';
const chips = [];
if (hasRateValue(stat.deal_rate, stat.deal_rate_text)) {
const text = formatRateText(
stat.deal_rate != null ? stat.deal_rate : stat.deal_rate_text,
stat.deal_rate_text,
);
chips.push({ k: 'deal', t: `${dealLabel} ${text}` });
}
const fineRaw = stat.fine_rate != null
? stat.fine_rate
: (stat.fined_rate != null ? stat.fined_rate : (stat.fine_rate_text || stat.fined_rate_text));
const fineText = stat.fine_rate_text || stat.fined_rate_text;
if (hasRateValue(stat.fine_rate != null ? stat.fine_rate : stat.fined_rate, fineText)) {
chips.push({ k: 'fine', t: `${fineLabel} ${formatRateText(fineRaw, fineText)}` });
}
if (stat.avg_deal_hours_text) {
chips.push({ k: 'avg', t: `均时 ${stat.avg_deal_hours_text}`, hl: true });
}
return chips;
}
export const DEAL_STAT_HELP = {
merchant: '成交率=已结算÷有效单量;罚款率=已缴纳罚单÷有效单量;均时=平均结算耗时。有样本即展示(含 0%)。',
dashou: '成交率=已结算÷接单量;被罚款率=已缴纳罚单÷接单量。有样本即展示(含 0%)。',
};