buddhism/pages/institutionalStructure/utils/data-formatter.js
2025-08-01 14:22:32 +08:00

221 lines
6.2 KiB
JavaScript
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.

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