import type { AdvertisingCostItem } from '@/types/advertisingCost'; export type HeatLevel = 0 | 1 | 2 | 3 | 4; export interface DayStat { date: string; itemCount: number; feeTotal: number; level: HeatLevel; } export interface HeatDayCell { date: string; inRange: boolean; isToday: boolean; isPageDate: boolean; stat: DayStat | null; level: HeatLevel; } function toDateKey(d: Date): string { const y = d.getFullYear(); const m = String(d.getMonth() + 1).padStart(2, '0'); const day = String(d.getDate()).padStart(2, '0'); return `${y}-${m}-${day}`; } function startOfDay(d: Date): Date { return new Date(d.getFullYear(), d.getMonth(), d.getDate()); } function addDays(d: Date, n: number): Date { const next = new Date(d); next.setDate(next.getDate() + n); return next; } /** 按花费分档:0 无数据,1~4 由浅到深 */ export function feeToLevel(feeTotal: number, maxFee: number): HeatLevel { if (feeTotal <= 0 || maxFee <= 0) return 0; const ratio = feeTotal / maxFee; if (ratio <= 0.25) return 1; if (ratio <= 0.5) return 2; if (ratio <= 0.75) return 3; return 4; } export function summarizeByDate(items: AdvertisingCostItem[]): Map { const map = new Map(); for (const item of items) { const prev = map.get(item.feeDate) ?? { itemCount: 0, feeTotal: 0 }; prev.itemCount += 1; prev.feeTotal += Number(item.feeAmount) || 0; map.set(item.feeDate, prev); } let maxFee = 0; for (const value of map.values()) { maxFee = Math.max(maxFee, value.feeTotal); } const result = new Map(); for (const [date, value] of map) { result.set(date, { date, itemCount: value.itemCount, feeTotal: value.feeTotal, level: feeToLevel(value.feeTotal, maxFee), }); } return result; } /** * GitHub 贡献图布局:列=周(旧→新),行=周日…周六。 * weeks 默认 13(约一季,适合窄面板)。 */ export function buildHeatmapWeeks(options: { weeks?: number; endDate?: Date; stats: Map; pageDate?: string; }): HeatDayCell[][] { const weeks = options.weeks ?? 13; const end = startOfDay(options.endDate ?? new Date()); const todayKey = toDateKey(end); // 对齐到本周周六(与 GitHub 行末一致:日一二三四五六) const endWeekday = end.getDay(); // 0=Sun const gridEnd = addDays(end, 6 - endWeekday); const totalDays = weeks * 7; const gridStart = addDays(gridEnd, -(totalDays - 1)); const columns: HeatDayCell[][] = []; for (let w = 0; w < weeks; w++) { const col: HeatDayCell[] = []; for (let dow = 0; dow < 7; dow++) { const date = addDays(gridStart, w * 7 + dow); const key = toDateKey(date); const inRange = date.getTime() <= end.getTime(); const stat = options.stats.get(key) ?? null; col.push({ date: key, inRange, isToday: key === todayKey, isPageDate: Boolean(options.pageDate && key === options.pageDate), stat, level: inRange ? (stat?.level ?? 0) : 0, }); } columns.push(col); } return columns; } export function monthLabelsForWeeks(weeks: HeatDayCell[][]): Array<{ index: number; label: string }> { const labels: Array<{ index: number; label: string }> = []; let lastMonth = -1; for (let i = 0; i < weeks.length; i++) { const first = weeks[i]?.[0]; if (!first) continue; const month = Number(first.date.slice(5, 7)); if (month !== lastMonth) { labels.push({ index: i, label: `${month}月` }); lastMonth = month; } } return labels; } export function cellTitle(cell: HeatDayCell): string { if (!cell.inRange) return cell.date; if (!cell.stat) { return cell.isPageDate ? `${cell.date} · 当前页面日期(未读取)` : `${cell.date} · 未读取`; } return `${cell.date} · ${cell.stat.itemCount} 条 · ¥${cell.stat.feeTotal.toFixed(2)}`; }