import React, { useEffect, useMemo, useState } from 'react'; import { Card, Col, DatePicker, Empty, Row, Segmented, Spin, Statistic, Table, Typography, message, } from 'antd'; import { ArrowDownOutlined, ArrowUpOutlined } from '@ant-design/icons'; import { PageContainer } from '@ant-design/pro-components'; import { Line } from '@ant-design/charts'; import type { ColumnsType } from 'antd/es/table'; import { getOutdoorFairStatistics } from '@/services/jobportal/outdoorFair'; import type { OutdoorFairStatisticsResult, OutdoorFairStatisticsSeriesItem, } from '@/services/jobportal/outdoorFair'; const { RangePicker } = DatePicker; const { Text } = Typography; type Granularity = 'day' | 'week' | 'month' | 'year'; const GRAN_OPTIONS: { label: string; value: Granularity }[] = [ { label: '按日', value: 'day' }, { label: '按周', value: 'week' }, { label: '按月', value: 'month' }, { label: '按年', value: 'year' }, ]; const formatBound = (value: any, suffix: string): string | undefined => { if (value && typeof value.format === 'function') { return `${value.format('YYYY-MM-DD')} ${suffix}`; } return undefined; }; const FairStatistics: React.FC = () => { const [granularity, setGranularity] = useState('month'); const [dateRange, setDateRange] = useState(null); const [loading, setLoading] = useState(false); const [result, setResult] = useState(null); const fetchStats = async (gran: Granularity, range: any) => { setLoading(true); try { const res = await getOutdoorFairStatistics({ granularity: gran, startTime: formatBound(range?.[0], '00:00:00'), endTime: formatBound(range?.[1], '23:59:59'), }); if (res?.code === 200 && res.data) { setResult(res.data); } else { message.error(res?.msg || '统计失败'); } } catch (e) { message.error('统计失败'); } finally { setLoading(false); } }; // 粒度或日期变化即查询;区间半选(只选了一端)不查询,避免中途无效查询 useEffect(() => { const complete = dateRange && dateRange[0] && dateRange[1]; const empty = !dateRange || (!dateRange[0] && !dateRange[1]); if (complete) { fetchStats(granularity, dateRange); } else if (empty) { fetchStats(granularity, null); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [granularity, dateRange]); const chartData = useMemo(() => { if (!result?.series) return []; const rows: { time: string; category: string; value: number }[] = []; result.series.forEach((item: OutdoorFairStatisticsSeriesItem) => { rows.push({ time: item.time, category: '招聘会数', value: item.fairCount }); rows.push({ time: item.time, category: '参会单位数', value: item.companyCount }); rows.push({ time: item.time, category: '职位数', value: item.jobCount }); }); return rows; }, [result]); const columns: ColumnsType = [ { title: '时间', dataIndex: 'time', width: 140 }, { title: '招聘会数', dataIndex: 'fairCount', width: 120 }, { title: '参会单位数', dataIndex: 'companyCount', width: 120 }, { title: '职位数', dataIndex: 'jobCount', width: 120 }, ]; return ( 统计粒度 setGranularity(v as Granularity)} options={GRAN_OPTIONS} /> 时间区间 setDateRange(v)} style={{ width: 360 }} /> {result ? ( <>
{chartData.length > 0 ? ( ) : ( )} rowKey="time" dataSource={result.series || []} columns={columns} pagination={false} size="small" /> ) : ( )}
); }; const RateText: React.FC<{ label: string; rate?: number | null }> = ({ label, rate }) => { if (rate === null || rate === undefined) { return ( {label}:-- ); } const up = rate >= 0; const color = up ? '#cf1322' : '#3f8600'; const pct = `${(rate * 100).toFixed(1)}%`; return ( {label}: {up ? : } {pct} ); }; export default FairStatistics;