import type { NormalizedCandidate } from './recommendation.service.ts';
function escapeHtml(value: string): string {
return value
.replaceAll('&', '&')
.replaceAll('<', '<')
.replaceAll('>', '>')
.replaceAll('"', '"');
}
/**
* 生成媒体资源推荐表 Excel (HTML table format compatible with MS Excel)
* 按模板生成:账号名称、ID、账号类型、地区、链接、粉丝数、赞藏数、
* 图文/视频报价、推荐等级、推荐理由等字段
*/
export function generateExcelBuffer(candidates: NormalizedCandidate[], taskFileName?: string): Buffer {
const title = taskFileName ? taskFileName.replace(/\.[^.]+$/, '') : '达人推荐表';
const now = new Date().toLocaleDateString('zh-CN');
const headerRow = `
| ${index + 1} |
${escapeHtml(mapPlatformDisplay(c.platform))} |
${escapeHtml(c.displayName)} |
${escapeHtml(c.platformUserId)} |
${escapeHtml(c.location || '-')} |
${escapeHtml(c.profileUrl)} |
${formatFans(c.fansCount)} |
${c.imagePrice > 0 ? `¥${c.imagePrice}` : '-'} |
${c.videoPrice > 0 ? `¥${c.videoPrice}` : '-'} |
${c.minPrice > 0 ? `¥${c.minPrice}` : '-'} |
${escapeHtml(c.contentTags.join('、') || '-')} |
${c.score} |
${c.styleMatch}% |
${escapeHtml(c.recommendStatus)} |
${escapeHtml(c.recommendReason || '-')} |
${escapeHtml(c.riskNote || '-')} |
${escapeHtml(c.sourceProvider)} |
${escapeHtml(c.displayName)} |
${escapeHtml(c.gender || '-')} |
${formatWan(c.fansCount)} |
${formatWan(c.likedCollectCount || 0)} |
${escapeHtml(c.contentType || c.contentTags.join('、') || '-')} |
${escapeHtml(c.city || c.location || '-')} |
${escapeHtml(c.geoLocation || c.location || '-')} |
${escapeHtml(c.xiaohongshuUrl || (c.platform === 'xiaohongshu' ? c.profileUrl : '-'))} |
${escapeHtml(c.cooperationMethod || '-')} |
${c.imagePrice > 0 ? `¥${c.imagePrice}` : '-'} |
${c.videoPrice > 0 ? `¥${c.videoPrice}` : '-'} |
`
).join('');
const html = `