mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 05:09:21 +08:00
feat: 增加贡献图直观展示数据读取情况
This commit is contained in:
@@ -0,0 +1,136 @@
|
||||
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<string, DayStat> {
|
||||
const map = new Map<string, { itemCount: number; feeTotal: number }>();
|
||||
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<string, DayStat>();
|
||||
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<string, DayStat>;
|
||||
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)}`;
|
||||
}
|
||||
@@ -4,9 +4,18 @@ function itemKey(item: AdvertisingCostItem): string {
|
||||
return `${item.feeDate}::${item.goodsId}`;
|
||||
}
|
||||
|
||||
function compareByAmountDesc(a: AdvertisingCostItem, b: AdvertisingCostItem): number {
|
||||
const byAmount = Number(b.feeAmount) - Number(a.feeAmount);
|
||||
if (byAmount !== 0) return byAmount;
|
||||
const byDate = b.feeDate.localeCompare(a.feeDate);
|
||||
if (byDate !== 0) return byDate;
|
||||
return a.goodsId.localeCompare(b.goodsId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并多次单日读取:按 (feeDate, goodsId) upsert。
|
||||
* 换日再读保留已有日期;同日再读覆盖该日同商品。
|
||||
* 合并后按金额降序。
|
||||
*/
|
||||
export function mergeAdvertisingItems(
|
||||
previous: AdvertisingCostItem[],
|
||||
@@ -19,10 +28,7 @@ export function mergeAdvertisingItems(
|
||||
for (const item of incoming) {
|
||||
map.set(itemKey(item), item);
|
||||
}
|
||||
return Array.from(map.values()).sort((a, b) => {
|
||||
const byDate = a.feeDate.localeCompare(b.feeDate);
|
||||
return byDate !== 0 ? byDate : a.goodsId.localeCompare(b.goodsId);
|
||||
});
|
||||
return Array.from(map.values()).sort(compareByAmountDesc);
|
||||
}
|
||||
|
||||
export function listFeeDates(items: AdvertisingCostItem[]): string[] {
|
||||
|
||||
Reference in New Issue
Block a user