buddhism/pages/institutionalStructure/utils/data-formatter.js
2025-08-01 11:54:29 +08:00

82 lines
2.3 KiB
JavaScript

/**
* 建制数据格式化工具类
*/
export class InstitutionalDataFormatter {
/**
* 格式化金额
* @param {number} amount 金额
* @returns {string} 格式化后的金额字符串
*/
static formatAmount(amount) {
if (!amount) return '不详'
if (amount >= 100000000) {
return `${(amount / 100000000).toFixed(1)}亿`
} else if (amount >= 10000) {
return `${(amount / 10000).toFixed(1)}`
} else {
return `${amount.toLocaleString()}`
}
}
/**
* 格式化左上角内容
* @param {string} year 年份
* @param {string} projectName 项目名称
* @returns {string} 格式化后的内容
*/
static formatTopLeft(year, projectName) {
// 如果年份和项目名称都为空,返回暂无数据
if (!year && !projectName) return '暂无数据'
// 如果只有年份,只显示年份
if (year && !projectName) return `${year}`
// 如果只有项目名称,只显示项目名称
if (!year && projectName) return projectName
// 如果都有,显示完整格式
return `${year}${projectName}`
}
/**
* 获取状态文本
* @param {string} state 状态码
* @returns {string} 状态文本
*/
static getStatusText(state) {
switch (state) {
case '1':
return '规划'
case '2':
return '进行中'
case '3':
return '已完成'
case '4':
return '已取消'
default:
return '未知状态'
}
}
/**
* 转换后端数据为前端格式
* @param {Array} rows 后端数据
* @returns {Array} 转换后的数据
*/
static transformData(rows) {
console.log('原始数据:', rows) // 添加调试日志
return rows.map(item => {
console.log('处理项目:', item) // 添加调试日志
return {
topLeft: InstitutionalDataFormatter.formatTopLeft(item.startYear || item.start_year || item.year, item.proName || item.pro_name || item.projectName),
topRight: InstitutionalDataFormatter.getStatusText(item.state),
bottomLeft: `建造金额:${InstitutionalDataFormatter.formatAmount(item.totalAmount || item.total_amount)}`,
bottomRight: `捐赠人数:${item.donorCount || item.donor_count || 0}`,
// 保存原始数据,用于跳转
formedId: item.id
}
})
}
}