wangzhibo
2026-05-09 53418655c061ac26f66058253f39200aced1ab1d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type { WarehousePlanEntry } from "../config/types.js";
 
export type CalendarCell = {
  day: number | null;
  dateKey: string | null;
  isToday: boolean;
  plans: WarehousePlanEntry[];
};
 
export function formatDateKey(y: number, m: number, d: number): string {
  return `${y}-${String(m).padStart(2, "0")}-${String(d).padStart(2, "0")}`;
}
 
/**
 * 当月日历网格(周一至周日),含首尾空白格。
 */
export function buildMonthRows(
  year: number,
  month: number,
  plansByDate: Record<string, WarehousePlanEntry[]>
): CalendarCell[][] {
  const firstDay = new Date(year, month - 1, 1);
  const daysInMonth = new Date(year, month, 0).getDate();
  const startOffset = (firstDay.getDay() + 6) % 7;
  const today = new Date();
  const todayKey = formatDateKey(
    today.getFullYear(),
    today.getMonth() + 1,
    today.getDate()
  );
 
  const cells: CalendarCell[] = [];
  for (let i = 0; i < startOffset; i++) {
    cells.push({
      day: null,
      dateKey: null,
      isToday: false,
      plans: [],
    });
  }
  for (let d = 1; d <= daysInMonth; d++) {
    const key = formatDateKey(year, month, d);
    cells.push({
      day: d,
      dateKey: key,
      isToday: key === todayKey,
      plans: plansByDate[key] ?? [],
    });
  }
  while (cells.length % 7 !== 0) {
    cells.push({
      day: null,
      dateKey: null,
      isToday: false,
      plans: [],
    });
  }
 
  const rows: CalendarCell[][] = [];
  for (let i = 0; i < cells.length; i += 7) {
    rows.push(cells.slice(i, i + 7));
  }
  return rows;
}