buddhism/pages/institutionalStructure/utils/data-formatter.js

240 lines
6.4 KiB
JavaScript
Raw Normal View History

/**
* 建制数据格式化工具类
*/
export class InstitutionalDataFormatter {
/**
* 格式化金额
* @param {number} amount 金额
* @returns {string} 格式化后的金额字符串
*/
static formatAmount(amount) {
if (!amount) return "不详";
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 超过一亿以亿为单位
if (amount >= 100000000) {
return `${(amount / 100000000).toFixed(1)}亿`;
2025-08-01 14:22:32 +08:00
} else if (amount >= 10000) {
return `${(amount / 10000).toFixed(1)}`;
2025-08-01 14:22:32 +08:00
} else {
return `${amount.toLocaleString()}`;
2025-08-01 14:22:32 +08:00
}
}
/**
* 格式化历史金额适用于古代项目
* @param {number} amount 金额
* @param {string} currency 货币单位
* @returns {string} 格式化后的金额字符串
*/
static formatHistoricalAmount(amount, currency = "银元") {
if (!amount) return "不详";
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 超过一亿以亿为单位
if (amount >= 100000000) {
return `${currency}${(amount / 100000000).toFixed(1)}亿`;
} else if (amount >= 10000) {
return `${currency}${(amount / 10000).toFixed(0)}`;
} else {
return `${currency}${amount.toLocaleString()}`;
}
}
/**
* 格式化左上角内容
* @param {string} projectName 项目名称
* @returns {string} 格式化后的内容
*/
2025-08-01 14:22:32 +08:00
static formatTopLeft(projectName) {
2025-08-01 11:54:29 +08:00
// 如果年份和项目名称都为空,返回暂无数据
if (!projectName) return "暂无数据";
return projectName;
2025-08-01 14:22:32 +08:00
}
/**
* 格式化项目名称
* @param {string} projectName 项目名称
* @param {boolean} isHistorical 是否为历史项目
* @returns {string} 格式化后的项目名称
*/
static formatProjectName(projectName, isHistorical = false) {
if (!projectName) return "暂无数据";
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 如果是历史项目,可能需要特殊处理
if (isHistorical && projectName.includes("寺")) {
2025-08-01 14:22:32 +08:00
// 对于寺庙项目,保持原有格式
return projectName;
2025-08-01 14:22:32 +08:00
}
2025-08-14 11:22:53 +08:00
return projectName;
2025-08-01 14:22:32 +08:00
}
/**
* 格式化年份显示
* @param {string} year 年份
* @returns {string} 格式化后的年份
*/
static formatYear(year) {
if (!year) return "暂无数据";
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 如果已经是格式化过的字符串(包含"年"字),直接返回
if (typeof year === "string" && year.includes("年")) {
return year;
2025-08-01 14:22:32 +08:00
}
2025-08-14 11:22:53 +08:00
return `${year}`;
2025-08-01 14:22:32 +08:00
}
/**
* 格式化历史年份适用于古代项目
* @param {string} year 年份
* @returns {string} 格式化后的年份
*/
static formatHistoricalYear(year) {
if (!year) return "暂无数据";
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 如果已经是格式化过的字符串,直接返回
if (typeof year === "string") {
if (year.includes("年")) {
return year;
2025-08-01 14:22:32 +08:00
}
if (year.includes("时期") || year.includes("朝代")) {
return year;
2025-08-01 14:22:32 +08:00
}
}
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 如果是公元前的年份
if (year < 0) {
return `公元前${Math.abs(year)}`;
2025-08-01 14:22:32 +08:00
}
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 如果是公元后的年份但小于1000年
if (year < 1000) {
return `${year}`;
2025-08-01 14:22:32 +08:00
}
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 正常年份
return `${year}`;
}
/**
* 获取状态文本
* @param {string} state 状态码
* @returns {string} 状态文本
*/
static getStatusText(state) {
switch (state) {
case "1":
return "规划";
case "2":
return "进行中";
case "3":
return "已完成";
case "4":
return "已取消";
default:
return "未知状态";
}
}
2025-08-01 14:22:32 +08:00
/**
* 判断是否为历史项目
* @param {string} year 年份
* @returns {boolean} 是否为历史项目
*/
static isHistoricalProject(year) {
if (!year) return false;
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 处理字符串格式的年份(如"916年"、"明末时期"
if (typeof year === "string") {
2025-08-01 14:22:32 +08:00
// 如果包含"年"字,提取数字部分
if (year.includes("年")) {
const yearNum = parseInt(year.replace("年", ""));
return yearNum < 1900;
2025-08-01 14:22:32 +08:00
}
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 如果包含历史时期描述,认为是历史项目
if (
year.includes("时期") ||
year.includes("朝代") ||
year.includes("古代")
) {
return true;
2025-08-01 14:22:32 +08:00
}
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 尝试解析纯数字
const yearNum = parseInt(year);
2025-08-01 14:22:32 +08:00
if (!isNaN(yearNum)) {
return yearNum < 1900;
2025-08-01 14:22:32 +08:00
}
}
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
// 处理数字格式的年份
const yearNum = parseInt(year);
return yearNum < 1900;
2025-08-01 14:22:32 +08:00
}
/**
* 格式化捐赠人数
* @param {number} count 人数
* @param {boolean} isHistorical 是否为历史项目
* @returns {string} 格式化后的人数
*/
static formatDonorCount(count, isHistorical = false) {
if (!count || count === 0) return "持续招募中";
2025-08-14 11:22:53 +08:00
2025-08-01 14:22:32 +08:00
if (isHistorical) {
return `${count}`;
2025-08-01 14:22:32 +08:00
} else {
return `${count}`;
2025-08-01 14:22:32 +08:00
}
}
/**
* 转换后端数据为前端格式
* @param {Array} rows 后端数据
* @returns {Array} 转换后的数据
*/
static transformData(rows) {
console.log("原始数据:", rows); // 添加调试日志
2025-08-14 11:22:53 +08:00
return rows.map((item) => {
console.log("处理项目:", item); // 添加调试日志
2025-08-14 11:22:53 +08:00
const year =
item.formedYear || item.startYear || item.start_year || item.year;
const isHistorical = InstitutionalDataFormatter.isHistoricalProject(year);
2025-08-14 11:22:53 +08:00
const projectName = item.proName || item.pro_name || item.projectName;
2025-08-14 11:22:53 +08:00
2025-08-01 11:54:29 +08:00
return {
2025-08-14 11:22:53 +08:00
topLeft: InstitutionalDataFormatter.formatTopLeft(
InstitutionalDataFormatter.formatProjectName(
projectName,
isHistorical,
),
2025-08-14 11:22:53 +08:00
),
2025-08-01 11:54:29 +08:00
topRight: InstitutionalDataFormatter.getStatusText(item.state),
2025-08-14 11:22:53 +08:00
year: isHistorical
2025-08-01 14:22:32 +08:00
? InstitutionalDataFormatter.formatHistoricalYear(year)
: InstitutionalDataFormatter.formatYear(year),
2025-08-14 11:22:53 +08:00
amount: isHistorical
? // ? InstitutionalDataFormatter.formatHistoricalAmount(
InstitutionalDataFormatter.formatAmount(
item.totalAmount || item.total_amount,
)
: InstitutionalDataFormatter.formatAmount(
item.totalAmount || item.total_amount,
),
2025-08-14 11:22:53 +08:00
donorCount: InstitutionalDataFormatter.formatDonorCount(
item.donorCount || item.donor_count || 0,
isHistorical,
2025-08-14 11:22:53 +08:00
),
2025-08-01 11:54:29 +08:00
// 保存原始数据,用于跳转
2025-08-01 14:22:32 +08:00
formedId: item.id,
// 保存项目类型信息
2025-08-14 11:22:53 +08:00
isHistorical: isHistorical,
};
});
}
2025-08-14 11:22:53 +08:00
}