Newer
Older
client_dwatch / lib / reports / facility-performance-labels.ts
/**
 * 施設別パフォーマンス表の「変化」列とステータスの表示名(D-163)。
 *
 * ★画面(`components/features/reports/FacilityPerformanceTable.tsx`)と CSV
 * (`lib/reports/facility-performance-csv.ts`)の両方がここを import する★。
 * 画面と CSV で文字が食い違わないよう、表から★中身を1文字も変えずに★移した純粋関数と定数。
 * 色(`diffTone()` / `statusColor()`)は画面だけのものなので表に残っている。
 */
import type { FacilityPerformance, FacilityStatus } from "@/lib/types";

/**
 * 「変化」列の合成値(モック :795-797 / 指示書F 5-7)
 *
 *   overallDiff = Math.round((稼働率の差分 + 完了率の差分) / 2)
 *
 * ★どちらかの diff が null なら null を返して `→`(中立)を出す★
 *   diff が null になるのは value / prev のどちらかが null のとき
 *   (新規施設・予定0件の期間。lib/judge/kpi.ts の toKpiValue)。
 *   片方だけで合成すると意味が変わってしまい、差を取れない以上
 *   「改善した」とも「悪化した」とも言えないため中立に寄せる。
 */
export function overallDiff(item: FacilityPerformance): number | null {
  const op = item.operation_rate.diff;
  const cv = item.completion_rate.diff;
  if (op === null || cv === null) return null;
  return Math.round((op + cv) / 2);
}

export function diffText(diff: number | null): string {
  if (diff === null || diff === 0) return "→";
  return diff > 0 ? `▲${diff}` : `▼${Math.abs(diff)}`;
}

/**
 * ステータスPill の文言と色(モック :794 / :809)
 * ★`no_data`(判定なし)はモック(`d-watch-mock.jsx`)に無い4値目★ —
 * 決定1(2026-09-11 ユーザー確定)で追加した意図した逸脱。稼働率が算出できない
 * (その期間の清掃予定が0件、または予定が全部これから)ときに出る
 *
 * ★画面は従来どおり `t()` に通して出し、CSV は日本語のまま出す★(D-163。CSV は言語を受け取らない。指示書AS D11)
 */
export const FACILITY_STATUS_LABEL: Record<FacilityStatus, string> = {
  good: "良好",
  warning: "注意",
  bad: "要改善",
  no_data: "判定なし",
};