import React, { useState, useEffect, useCallback, useMemo, useRef } from 'react';
import {
Card,
Row,
Col,
Statistic,
Table,
DatePicker,
Select,
Space,
Tag,
message,
Button,
Switch,
Tooltip,
} from 'antd';
import {
BarChartOutlined,
PieChartOutlined,
RiseOutlined,
FallOutlined,
ClockCircleOutlined,
CheckCircleOutlined,
ExclamationCircleOutlined,
ReloadOutlined,
SyncOutlined,
} from '@ant-design/icons';
import axios from 'axios';
import dayjs from 'dayjs';
const { RangePicker } = DatePicker;
const { Option } = Select;
const REFRESH_INTERVAL = 30000;
const getStatusColor = status => {
const colors = {
pending: 'orange',
assigned: 'blue',
in_progress: 'processing',
completed: 'green',
closed: 'default',
};
return colors[status] || 'default';
};
const getStatusText = status => {
const texts = {
pending: '待处理',
assigned: '已分配',
in_progress: '处理中',
completed: '已完成',
closed: '已关闭',
};
return texts[status] || status;
};
const getPriorityColor = priority => {
const colors = {
low: 'green',
medium: 'orange',
high: 'red',
urgent: 'magenta',
};
return colors[priority] || 'default';
};
const getPriorityText = priority => {
const texts = {
low: '低',
medium: '中',
high: '高',
urgent: '紧急',
};
return texts[priority] || priority;
};
function TicketStatistics() {
const [loading, setLoading] = useState(true);
const [autoRefresh, setAutoRefresh] = useState(false);
const [dateRange, setDateRange] = useState([dayjs().subtract(30, 'days'), dayjs()]);
const [lastUpdateTime, setLastUpdateTime] = useState(null);
const [statistics, setStatistics] = useState({
total: 0,
pending: 0,
inProgress: 0,
completed: 0,
closed: 0,
avgProcessingTime: 0,
byCategory: [],
byPriority: [],
byStatus: [],
byDevice: [],
trend: [],
});
const timerRef = useRef(null);
const fetchStatistics = useCallback(
async (isManual = false) => {
try {
if (isManual) {
setLoading(true);
}
const params = {
startDate: dateRange[0].format('YYYY-MM-DD'),
endDate: dateRange[1].format('YYYY-MM-DD'),
};
const response = await axios.get('/api/tickets/stats', { params });
setStatistics(response.data);
setLastUpdateTime(new Date());
} catch (error) {
message.error('获取统计数据失败');
console.error('获取统计数据失败:', error);
} finally {
setLoading(false);
}
},
[dateRange]
);
useEffect(() => {
fetchStatistics();
}, [fetchStatistics]);
useEffect(() => {
if (autoRefresh) {
timerRef.current = setInterval(() => {
fetchStatistics();
}, REFRESH_INTERVAL);
} else {
if (timerRef.current) {
clearInterval(timerRef.current);
timerRef.current = null;
}
}
return () => {
if (timerRef.current) {
clearInterval(timerRef.current);
}
};
}, [autoRefresh, fetchStatistics]);
const handleDateChange = useCallback(dates => {
if (dates) {
setDateRange(dates);
}
}, []);
const handleManualRefresh = useCallback(() => {
fetchStatistics(true);
}, [fetchStatistics]);
const getStatusColor = status => {
const colors = {
pending: 'orange',
assigned: 'blue',
in_progress: 'processing',
completed: 'green',
closed: 'default',
};
return colors[status] || 'default';
};
const getStatusText = status => {
const texts = {
pending: '待处理',
assigned: '已分配',
in_progress: '处理中',
completed: '已完成',
closed: '已关闭',
};
return texts[status] || status;
};
const getPriorityColor = priority => {
const colors = {
low: 'green',
medium: 'orange',
high: 'red',
urgent: 'magenta',
};
return colors[priority] || 'default';
};
const getPriorityText = priority => {
const texts = {
low: '低',
medium: '中',
high: '高',
urgent: '紧急',
};
return texts[priority] || priority;
};
const statusColumns = useMemo(
() => [
{
title: '状态',
dataIndex: 'status',
key: 'status',
width: 120,
render: status =>