buddhism/pages/institutionalStructure/utils/data-formatter.js
2025-07-31 15:29:40 +08:00

65 lines
1.7 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 '暂无数据'
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) {
return rows.map(item => ({
topLeft: InstitutionalDataFormatter.formatTopLeft(item.startYear, item.proName),
topRight: InstitutionalDataFormatter.getStatusText(item.state),
bottomLeft: `建造金额:${InstitutionalDataFormatter.formatAmount(item.totalAmount)}`,
bottomRight: `捐赠人数:${item.donorCount}`
}))
}
}