feat(scheduling): persist notifications, batch notify flow, dedup protection

- Add tab_scheduling_notifications table with bootstrap via ensureSchedulingTables()
- Notify endpoint rewritten: dedup by (suggestion_id, candidate_plate), history list, PATCH /:id for execute/cancel lifecycle
- Batch notify endpoint returns success/skipped/failed counts
- Suggestions response now carries notificationId + notificationStatus per candidate (joined from active-notification map)
- UI: select mode with checkboxes, floating action bar, confirmation modal listing each swap; already-notified items are dimmed and skipped
- Detail view badges show sent/executed state, preventing duplicate notify

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
kkfluous
2026-04-16 23:43:21 +08:00
parent 31716c6547
commit 3ef0d4edfa
12 changed files with 614 additions and 33 deletions

View File

@@ -1,10 +1,11 @@
import { useState, useEffect, useCallback, useMemo, useRef } from 'react';
import { Filter, RotateCcw, X, Search, ChevronDown } from 'lucide-react';
import { Filter, RotateCcw, X, Search, ChevronDown, CheckSquare, Send } from 'lucide-react';
import { motion, AnimatePresence } from 'motion/react';
import { fetchSuggestions } from './api';
import type { SchedulingResponse, SchedulingSuggestion } from './types';
import { fetchSuggestions, sendNotifyBatch } from './api';
import type { SchedulingResponse, SchedulingSuggestion, CandidateVehicle } from './types';
import SuggestionList from './SuggestionList';
import SuggestionDetail from './SuggestionDetail';
import Blur from '../../components/Blur';
type TypeFilter = 'all' | 'qualified' | 'hopeless';
@@ -143,6 +144,13 @@ function SkeletonPage() {
);
}
function pickBestCandidate(s: SchedulingSuggestion): CandidateVehicle | null {
// Prefer a candidate that can qualify and isn't already notified
const available = s.candidates.filter(c => !c.notificationStatus || c.notificationStatus === 'cancelled');
if (available.length === 0) return null;
return available.find(c => c.canQualifyAfterSwap) ?? available[0];
}
export default function SchedulingModule() {
const [data, setData] = useState<SchedulingResponse | null>(null);
const [loading, setLoading] = useState(false);
@@ -152,6 +160,11 @@ export default function SchedulingModule() {
const [showFilter, setShowFilter] = useState(false);
const [filters, setFilters] = useState<AdvancedFilters>(EMPTY_FILTERS);
const [tempFilters, setTempFilters] = useState<AdvancedFilters>(EMPTY_FILTERS);
const [selectMode, setSelectMode] = useState(false);
const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());
const [showBatchConfirm, setShowBatchConfirm] = useState(false);
const [batchInFlight, setBatchInFlight] = useState(false);
const [batchResultMsg, setBatchResultMsg] = useState<string | null>(null);
const loadData = useCallback(async () => {
setLoading(true);
@@ -168,6 +181,55 @@ export default function SchedulingModule() {
if (!stillExists) setSelectedSuggestion(null);
}, [data, selectedSuggestion]);
const toggleSelect = useCallback((id: string) => {
setSelectedIds(prev => {
const next = new Set(prev);
if (next.has(id)) next.delete(id); else next.add(id);
return next;
});
}, []);
const exitSelectMode = useCallback(() => {
setSelectMode(false);
setSelectedIds(new Set());
setShowBatchConfirm(false);
}, []);
const batchItems = useMemo(() => {
if (!data) return [];
return [...selectedIds]
.map(id => data.suggestions.find(s => s.id === id))
.filter((s): s is SchedulingSuggestion => !!s)
.map(s => {
const candidate = pickBestCandidate(s);
if (!candidate) return null;
return { suggestion: s, candidate };
})
.filter((x): x is { suggestion: SchedulingSuggestion; candidate: CandidateVehicle } => !!x);
}, [data, selectedIds]);
const handleBatchSubmit = useCallback(async () => {
if (batchItems.length === 0) return;
setBatchInFlight(true);
try {
const resp = await sendNotifyBatch({
items: batchItems.map(i => ({
suggestionId: i.suggestion.id,
currentPlate: i.suggestion.currentVehicle.plateNumber,
candidatePlate: i.candidate.plateNumber,
})),
});
setBatchResultMsg(resp.message);
await loadData();
exitSelectMode();
} catch (e) {
console.error('batch notify failed:', e);
setBatchResultMsg('批量通知失败,请重试');
} finally {
setBatchInFlight(false);
}
}, [batchItems, loadData, exitSelectMode]);
const filterOptions = useMemo(() => {
if (!data) return { regions: [], vehicleTypes: [], customers: [], departments: [], managers: [] };
const r = new Set<string>(), t = new Set<string>(), c = new Set<string>(), d = new Set<string>(), m = new Set<string>();
@@ -284,6 +346,18 @@ export default function SchedulingModule() {
className="p-1.5 text-slate-400 hover:text-slate-600 transition-colors rounded-lg hover:bg-slate-50 cursor-pointer">
<RotateCcw size={15} className={loading ? 'animate-spin' : ''} />
</button>
<button
onClick={() => {
if (selectMode) exitSelectMode();
else { setSelectMode(true); setSelectedSuggestion(null); }
}}
className={`relative p-1.5 transition-colors rounded-lg cursor-pointer ${
selectMode ? 'text-blue-600 bg-blue-50' : 'text-slate-400 hover:text-slate-600 hover:bg-slate-50'
}`}
title={selectMode ? '退出多选' : '多选模式'}
>
<CheckSquare size={15} />
</button>
<button
onClick={() => { setShowFilter(!showFilter); setTempFilters(filters); }}
className={`relative p-1.5 transition-colors rounded-lg cursor-pointer ${
@@ -398,13 +472,115 @@ export default function SchedulingModule() {
))}
</div>
) : (
<SuggestionList suggestions={filteredSuggestions} onSelect={setSelectedSuggestion} />
<SuggestionList
suggestions={filteredSuggestions}
onSelect={setSelectedSuggestion}
selectMode={selectMode}
selectedIds={selectedIds}
onToggleSelect={toggleSelect}
/>
)}
</div>
{selectedSuggestion && (
<SuggestionDetail suggestion={selectedSuggestion} onClose={() => setSelectedSuggestion(null)} onNotifySuccess={handleNotifySuccess} />
)}
{/* Batch action bar */}
<AnimatePresence>
{selectMode && (
<motion.div
initial={{ y: 80, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: 80, opacity: 0 }}
className="fixed bottom-4 left-3 right-3 md:left-auto md:right-6 md:bottom-6 md:w-[360px] z-40 bg-slate-900 text-white rounded-2xl shadow-2xl px-4 py-3 flex items-center justify-between gap-3"
>
<div className="flex items-center gap-2">
<span className="text-xs font-medium"></span>
<span className="text-lg font-black">{selectedIds.size}</span>
<span className="text-xs text-slate-400"></span>
</div>
<div className="flex items-center gap-2">
<button
onClick={exitSelectMode}
className="text-xs font-medium text-slate-300 hover:text-white px-2 py-1.5 cursor-pointer transition-colors"
>
</button>
<button
onClick={() => setShowBatchConfirm(true)}
disabled={selectedIds.size === 0}
className="flex items-center gap-1.5 text-xs font-bold bg-blue-600 hover:bg-blue-500 disabled:bg-slate-700 disabled:text-slate-400 text-white px-3 py-1.5 rounded-lg cursor-pointer disabled:cursor-not-allowed transition-colors"
>
<Send size={12} />
</button>
</div>
</motion.div>
)}
</AnimatePresence>
{/* Batch confirmation modal */}
{showBatchConfirm && (
<div className="fixed inset-0 bg-black/50 backdrop-blur-sm z-[70] flex items-end sm:items-center justify-center" onClick={() => !batchInFlight && setShowBatchConfirm(false)}>
<motion.div
initial={{ y: 40, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
onClick={e => e.stopPropagation()}
className="bg-white rounded-t-2xl sm:rounded-2xl shadow-2xl w-full sm:max-w-md overflow-hidden flex flex-col max-h-[80vh] sm:mx-4"
>
<div className="bg-slate-800 px-4 py-3 flex items-center justify-between flex-shrink-0">
<span className="text-white font-bold text-sm"></span>
<button
onClick={() => !batchInFlight && setShowBatchConfirm(false)}
disabled={batchInFlight}
className="text-slate-400 hover:text-white transition-colors p-1 cursor-pointer disabled:opacity-50"
>
<X size={18} />
</button>
</div>
<div className="px-4 py-3 overflow-y-auto flex-1">
<p className="text-xs text-slate-500 mb-3">
<span className="font-bold text-slate-800">{batchItems.length}</span>
</p>
<div className="space-y-2">
{batchItems.map(({ suggestion, candidate }) => (
<div key={suggestion.id} className="text-[11px] bg-slate-50 rounded-lg px-3 py-2 flex items-center justify-between gap-2">
<div className="flex items-center gap-1.5 min-w-0">
<span className="font-mono font-bold text-slate-900"><Blur>{suggestion.currentVehicle.plateNumber}</Blur></span>
<span className="text-slate-400"></span>
<span className="font-mono font-bold text-blue-700"><Blur>{candidate.plateNumber}</Blur></span>
</div>
{candidate.canQualifyAfterSwap ? (
<span className="text-emerald-600 text-[9px] font-bold flex-shrink-0"></span>
) : (
<span className="text-amber-500 text-[9px] font-bold flex-shrink-0"></span>
)}
</div>
))}
</div>
{batchResultMsg && (
<p className="mt-3 text-[11px] text-slate-500">{batchResultMsg}</p>
)}
</div>
<div className="border-t border-slate-100 px-4 py-3 flex-shrink-0 flex gap-2">
<button
onClick={() => setShowBatchConfirm(false)}
disabled={batchInFlight}
className="flex-1 py-2 text-xs font-bold text-slate-500 bg-slate-50 hover:bg-slate-100 rounded-lg cursor-pointer disabled:opacity-50 transition-colors"
>
</button>
<button
onClick={handleBatchSubmit}
disabled={batchInFlight || batchItems.length === 0}
className="flex-1 py-2 text-xs font-bold text-white bg-blue-600 hover:bg-blue-500 rounded-lg cursor-pointer disabled:opacity-50 transition-colors"
>
{batchInFlight ? '发送中...' : `确认发送 ${batchItems.length}`}
</button>
</div>
</motion.div>
</div>
)}
</div>
</div>
);

View File

@@ -65,7 +65,10 @@ export default function SuggestionDetail({ suggestion: s, onClose, onNotifySucce
};
const renderCandidate = (c: CandidateVehicle) => {
const sent = sentPlates.has(c.plateNumber);
const sent =
sentPlates.has(c.plateNumber) ||
c.notificationStatus === 'sent' ||
c.notificationStatus === 'executed';
return (
<div key={c.plateNumber} className="rounded-xl border border-slate-200 overflow-hidden bg-white">
<div className="flex items-center justify-between px-3 py-2">

View File

@@ -1,5 +1,5 @@
import { useState, useMemo } from 'react';
import { ArrowRightLeft, ChevronRight, ArrowDown, ArrowUp, ArrowUpDown } from 'lucide-react';
import { ArrowRightLeft, ChevronRight, ArrowDown, ArrowUp, ArrowUpDown, CheckCircle, Check } from 'lucide-react';
import { motion } from 'motion/react';
import type { SchedulingSuggestion } from './types';
import Blur from '../../components/Blur';
@@ -7,6 +7,13 @@ import Blur from '../../components/Blur';
interface Props {
suggestions: SchedulingSuggestion[];
onSelect: (s: SchedulingSuggestion) => void;
selectMode?: boolean;
selectedIds?: Set<string>;
onToggleSelect?: (id: string) => void;
}
function hasActiveNotification(s: SchedulingSuggestion): boolean {
return s.candidates.some(c => c.notificationStatus === 'sent' || c.notificationStatus === 'executed');
}
function fmtRate(rate: number): string {
@@ -16,7 +23,7 @@ function fmtRate(rate: number): string {
type SortKey = 'default' | 'avgDaily' | 'completion';
type SortDir = 'asc' | 'desc';
export default function SuggestionList({ suggestions, onSelect }: Props) {
export default function SuggestionList({ suggestions, onSelect, selectMode = false, selectedIds, onToggleSelect }: Props) {
const [sortKey, setSortKey] = useState<SortKey>('default');
const [sortDir, setSortDir] = useState<SortDir>('desc');
@@ -73,6 +80,17 @@ export default function SuggestionList({ suggestions, onSelect }: Props) {
{sorted.map((s, idx) => {
const isRescue = s.type === 'rescue_hopeless';
const v = s.currentVehicle;
const notified = hasActiveNotification(s);
const isSelected = selectedIds?.has(s.id) ?? false;
const canSelect = selectMode && !notified;
const handleClick = () => {
if (selectMode) {
if (canSelect) onToggleSelect?.(s.id);
} else {
onSelect(s);
}
};
return (
<motion.div
@@ -80,9 +98,26 @@ export default function SuggestionList({ suggestions, onSelect }: Props) {
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: Math.min(idx * 0.02, 0.3) }}
className="px-4 py-3 hover:bg-slate-50/60 cursor-pointer transition-colors active:bg-slate-100 flex items-center gap-3"
onClick={() => onSelect(s)}
className={`px-4 py-3 hover:bg-slate-50/60 transition-colors flex items-center gap-3 ${
canSelect || !selectMode ? 'cursor-pointer active:bg-slate-100' : 'cursor-default opacity-60'
} ${isSelected ? 'bg-blue-50/60' : ''}`}
onClick={handleClick}
>
{/* Checkbox (select mode) */}
{selectMode && (
<div
className={`w-4 h-4 rounded flex-shrink-0 flex items-center justify-center border transition-colors ${
isSelected
? 'bg-blue-600 border-blue-600 text-white'
: notified
? 'bg-slate-100 border-slate-200'
: 'bg-white border-slate-300'
}`}
>
{isSelected && <Check size={12} strokeWidth={3} />}
</div>
)}
{/* Color bar */}
<div className={`w-1 h-10 rounded-full flex-shrink-0 ${isRescue ? 'bg-blue-400' : 'bg-amber-400'}`} />
@@ -96,6 +131,11 @@ export default function SuggestionList({ suggestions, onSelect }: Props) {
<span className="text-[9px] text-slate-400">{v.vehicleType}</span>
<span className="text-[9px] text-slate-300">·</span>
<span className="text-[9px] text-slate-400">{v.region}</span>
{notified && (
<span className="text-[9px] font-bold text-emerald-600 bg-emerald-50 px-1.5 py-0.5 rounded flex items-center gap-0.5">
<CheckCircle size={9} />
</span>
)}
</div>
<span className="text-[10px] flex-shrink-0">
<span className="text-slate-500"> </span>
@@ -115,10 +155,12 @@ export default function SuggestionList({ suggestions, onSelect }: Props) {
</div>
{/* Right */}
<div className="flex items-center gap-0.5 flex-shrink-0">
<span className="text-[9px] text-slate-400"></span>
<ChevronRight size={14} className="text-slate-300" />
</div>
{!selectMode && (
<div className="flex items-center gap-0.5 flex-shrink-0">
<span className="text-[9px] text-slate-400"></span>
<ChevronRight size={14} className="text-slate-300" />
</div>
)}
</motion.div>
);
})}

View File

@@ -1,5 +1,13 @@
import { fetchJson } from '../../auth/api-client';
import type { SchedulingResponse } from './types';
import type {
SchedulingResponse,
NotifyRequest,
NotifyBatchRequest,
NotifyBatchResult,
NotificationRecord,
NotificationStatus,
UpdateNotificationRequest,
} from './types';
const BASE = '/api/scheduling';
@@ -10,14 +18,44 @@ export async function fetchSuggestions(targetId?: number): Promise<SchedulingRes
return fetchJson<SchedulingResponse>(`${BASE}/suggestions${qs ? `?${qs}` : ''}`);
}
export async function sendNotify(body: {
suggestionId: string;
currentPlate: string;
candidatePlate: string;
}): Promise<{ success: boolean; message: string }> {
return fetchJson<{ success: boolean; message: string }>(`${BASE}/notify`, {
export async function sendNotify(
body: NotifyRequest,
): Promise<{ success: boolean; message: string; record?: NotificationRecord }> {
return fetchJson(`${BASE}/notify`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}
export async function sendNotifyBatch(
body: NotifyBatchRequest,
): Promise<{ success: boolean; message: string; result: NotifyBatchResult }> {
return fetchJson(`${BASE}/notify/batch`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}
export async function fetchNotifications(
status?: NotificationStatus,
limit?: number,
): Promise<{ records: NotificationRecord[] }> {
const params = new URLSearchParams();
if (status) params.set('status', status);
if (limit) params.set('limit', String(limit));
const qs = params.toString();
return fetchJson(`${BASE}/notify${qs ? `?${qs}` : ''}`);
}
export async function updateNotification(
id: number,
body: UpdateNotificationRequest,
): Promise<{ success: boolean; record?: NotificationRecord; message?: string }> {
return fetchJson(`${BASE}/notify/${id}`, {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
});
}

View File

@@ -6,6 +6,11 @@ export type {
SchedulingTargetOption,
SchedulingResponse,
NotifyRequest,
NotifyBatchRequest,
NotifyBatchResult,
NotificationStatus,
NotificationRecord,
UpdateNotificationRequest,
ReasonLine,
ReasonBlock,
} from '../../shared/scheduling/types';