fix(统计管理):新增统计数据导出功能

This commit is contained in:
2025-04-13 01:37:22 +08:00
parent 1cc0aaab04
commit 4f39cbed1d
4 changed files with 131 additions and 22 deletions
@@ -1,10 +1,11 @@
// 设备维度统计组件 // 设备维度统计组件
import React, { useState, useEffect } from 'react'; import React, { forwardRef, useImperativeHandle, useState, useEffect } from 'react';
import { ProForm, ProFormDateRangePicker, ProTable } from '@ant-design/pro-components'; import { ProForm, ProFormDateRangePicker, ProTable } from '@ant-design/pro-components';
import { Card, Row, Col, Statistic, message } from 'antd'; import { Card, Row, Col, Statistic, message } from 'antd';
import { Column } from '@ant-design/plots'; import { Column } from '@ant-design/plots';
import moment from 'moment'; import moment from 'moment';
import { getDeviceStatistics } from '@/services/statistics/api'; import { getDeviceStatistics } from '@/services/statistics/api';
import { exportExcel } from '@/utils/export';
interface DeviceStatisticsItem { interface DeviceStatisticsItem {
groupKey: string; // 设备SN groupKey: string; // 设备SN
@@ -21,7 +22,11 @@ interface DeviceStatisticsItem {
completionRate: string; // 完成率 completionRate: string; // 完成率
} }
const DeviceStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) => { interface DeviceStatisticsProps {
refreshKey: number;
}
const DeviceStatistics = forwardRef(({ refreshKey }: DeviceStatisticsProps, ref) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([ const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([
moment().startOf('month'), moment().startOf('month'),
@@ -166,6 +171,17 @@ const DeviceStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) => {
}, },
]; ];
// 暴露导出方法
useImperativeHandle(ref, () => ({
exportData: (header: any[], fileName: string) => {
exportExcel({
data: statisticsData,
header,
fileName,
});
},
}));
return ( return (
<> <>
{/* 筛选条件 */} {/* 筛选条件 */}
@@ -277,6 +293,6 @@ const DeviceStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) => {
</Card> </Card>
</> </>
); );
}; });
export default DeviceStatistics; export default DeviceStatistics;
@@ -1,10 +1,11 @@
// 二维码维度统计组件 // 二维码维度统计组件
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, forwardRef, useImperativeHandle } from 'react';
import { ProForm, ProFormDateRangePicker, ProFormSelect, ProTable } from '@ant-design/pro-components'; import { ProForm, ProFormDateRangePicker, ProFormSelect, ProTable } from '@ant-design/pro-components';
import { Card, Radio, Row, Col, Statistic, message } from 'antd'; import { Card, Radio, Row, Col, Statistic, message } from 'antd';
import { Line } from '@ant-design/plots'; import { Line } from '@ant-design/plots';
import moment from 'moment'; import moment from 'moment';
import { getTimeStatistics } from '@/services/statistics/api'; import { getTimeStatistics } from '@/services/statistics/api';
import { exportExcel } from '@/utils/export';
// ... 其他 import // ... 其他 import
interface QrcodeStatisticsProps { interface QrcodeStatisticsProps {
@@ -14,12 +15,12 @@ interface QrcodeStatisticsProps {
refreshKey: number; refreshKey: number;
} }
const QrcodeStatistics: React.FC<QrcodeStatisticsProps> = ({ const QrcodeStatistics = forwardRef<{ exportData: (header: any[], fileName: string) => void }, QrcodeStatisticsProps>(({
qrcodeOptions, qrcodeOptions,
qrcodeId, qrcodeId,
onQrcodeChange, onQrcodeChange,
refreshKey, refreshKey,
}) => { }, ref) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [timeType, setTimeType] = useState<'hour' | 'day' | 'month'>('day'); const [timeType, setTimeType] = useState<'hour' | 'day' | 'month'>('day');
const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([ const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([
@@ -160,6 +161,17 @@ const QrcodeStatistics: React.FC<QrcodeStatisticsProps> = ({
}); });
}; };
// 暴露导出方法
useImperativeHandle(ref, () => ({
exportData: (header: any[], fileName: string) => {
exportExcel({
data: statisticsData,
header,
fileName,
});
},
}));
return ( return (
<> <>
{/* 筛选条件 */} {/* 筛选条件 */}
@@ -286,6 +298,6 @@ const QrcodeStatistics: React.FC<QrcodeStatisticsProps> = ({
</Card> </Card>
</> </>
); );
}; });
export default QrcodeStatistics; export default QrcodeStatistics;
@@ -1,9 +1,10 @@
import React, { useState, useEffect } from 'react'; import React, { forwardRef, useImperativeHandle, useState, useEffect } from 'react';
import { ProForm, ProFormDateRangePicker, ProTable } from '@ant-design/pro-components'; import { ProForm, ProFormDateRangePicker, ProTable } from '@ant-design/pro-components';
import { Card, Row, Col, Statistic, message } from 'antd'; import { Card, Row, Col, Statistic, message } from 'antd';
import { Column } from '@ant-design/plots'; import { Column } from '@ant-design/plots';
import moment from 'moment'; import moment from 'moment';
import { getTourismStatistics } from '@/services/statistics/api'; import { getTourismStatistics } from '@/services/statistics/api';
import { exportExcel } from '@/utils/export';
interface TourismStatisticsItem { interface TourismStatisticsItem {
groupKey: number; // 景区ID groupKey: number; // 景区ID
@@ -20,7 +21,11 @@ interface TourismStatisticsItem {
completionRate: string; // 完成率 completionRate: string; // 完成率
} }
const TourismStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) => { interface TourismStatisticsProps {
refreshKey: number;
}
const TourismStatistics = forwardRef(({ refreshKey }: TourismStatisticsProps, ref) => {
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([ const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([
moment().startOf('month'), moment().startOf('month'),
@@ -166,6 +171,17 @@ const TourismStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) =>
}, },
]; ];
// 暴露导出方法
useImperativeHandle(ref, () => ({
exportData: (header: any[], fileName: string) => {
exportExcel({
data: statisticsData,
header,
fileName,
});
},
}));
return ( return (
<> <>
{/* 筛选条件 */} {/* 筛选条件 */}
@@ -277,6 +293,6 @@ const TourismStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) =>
</Card> </Card>
</> </>
); );
}; });
export default TourismStatistics; export default TourismStatistics;
@@ -21,6 +21,7 @@ const Statistics: React.FC = () => {
moment().endOf('month') moment().endOf('month')
]); ]);
const [generating, setGenerating] = useState(false); const [generating, setGenerating] = useState(false);
const [activeTab, setActiveTab] = useState<string>('qrcode');
// 获取二维码列表 // 获取二维码列表
const fetchQrcodeList = async () => { const fetchQrcodeList = async () => {
@@ -74,12 +75,76 @@ const Statistics: React.FC = () => {
} }
}; };
// 处理导出数据
const handleExport = () => {
const fileName = `订单统计数据_${moment().format('YYYY-MM-DD')}`;
let header: { title: string; dataIndex: string; }[] = [];
// 根据不同的 Tab 设置不同的表头
switch (activeTab) {
case 'qrcode':
header = [
{ title: '统计时间', dataIndex: 'groupKey' },
{ title: '订单总数', dataIndex: 'totalOrders' },
{ title: '已支付订单数', dataIndex: 'paidOrders' },
{ title: '已完成订单数', dataIndex: 'completedOrders' },
{ title: '已取消订单数', dataIndex: 'canceledOrders' },
{ title: '订单总金额(元)', dataIndex: 'totalAmountYuan' },
{ title: '已支付金额(元)', dataIndex: 'paidAmountYuan' },
{ title: '退款金额(元)', dataIndex: 'refundAmountYuan' },
{ title: '完成率', dataIndex: 'completionRate' },
];
break;
case 'device':
header = [
{ title: '设备SN', dataIndex: 'groupKey' },
{ title: '订单总数', dataIndex: 'totalOrders' },
{ title: '已支付订单数', dataIndex: 'paidOrders' },
{ title: '已完成订单数', dataIndex: 'completedOrders' },
{ title: '已取消订单数', dataIndex: 'canceledOrders' },
{ title: '订单总金额(元)', dataIndex: 'totalAmountYuan' },
{ title: '已支付金额(元)', dataIndex: 'paidAmountYuan' },
{ title: '退款金额(元)', dataIndex: 'refundAmountYuan' },
{ title: '完成率', dataIndex: 'completionRate' },
];
break;
case 'tourism':
header = [
{ title: '景区ID', dataIndex: 'groupKey' },
{ title: '订单总数', dataIndex: 'totalOrders' },
{ title: '已支付订单数', dataIndex: 'paidOrders' },
{ title: '已完成订单数', dataIndex: 'completedOrders' },
{ title: '已取消订单数', dataIndex: 'canceledOrders' },
{ title: '订单总金额(元)', dataIndex: 'totalAmountYuan' },
{ title: '已支付金额(元)', dataIndex: 'paidAmountYuan' },
{ title: '退款金额(元)', dataIndex: 'refundAmountYuan' },
{ title: '完成率', dataIndex: 'completionRate' },
];
break;
}
// 调用子组件的导出方法
if (activeTab === 'qrcode') {
qrcodeRef.current?.exportData(header, `${fileName}_按二维码`);
} else if (activeTab === 'device') {
deviceRef.current?.exportData(header, `${fileName}_按设备`);
} else if (activeTab === 'tourism') {
tourismRef.current?.exportData(header, `${fileName}_按景区`);
}
};
// 创建组件引用
const qrcodeRef = useRef<any>(null);
const deviceRef = useRef<any>(null);
const tourismRef = useRef<any>(null);
const items: TabsProps['items'] = [ const items: TabsProps['items'] = [
{ {
key: 'qrcode', key: 'qrcode',
label: '按二维码统计', label: '按二维码统计',
children: ( children: (
<QrcodeStatistics <QrcodeStatistics
ref={qrcodeRef}
qrcodeOptions={qrcodeOptions} qrcodeOptions={qrcodeOptions}
qrcodeId={qrcodeId} qrcodeId={qrcodeId}
onQrcodeChange={setQrcodeId} onQrcodeChange={setQrcodeId}
@@ -92,6 +157,7 @@ const Statistics: React.FC = () => {
label: '按设备统计', label: '按设备统计',
children: ( children: (
<DeviceStatistics <DeviceStatistics
ref={deviceRef}
refreshKey={refreshKey} refreshKey={refreshKey}
/> />
), ),
@@ -101,6 +167,7 @@ const Statistics: React.FC = () => {
label: '按景区统计', label: '按景区统计',
children: ( children: (
<TourismStatistics <TourismStatistics
ref={tourismRef}
refreshKey={refreshKey} refreshKey={refreshKey}
/> />
), ),
@@ -129,11 +196,9 @@ const Statistics: React.FC = () => {
key="export" key="export"
type="primary" type="primary"
icon={<DownloadOutlined />} icon={<DownloadOutlined />}
onClick={() => { onClick={handleExport}
// 根据当前选中的 tab 调用相应的导出函数
}}
> >
导出数据 导出{activeTab === 'qrcode' ? '二维码' : activeTab === 'device' ? '设备' : '景区'}统计
</Button>, </Button>,
]} ]}
> >
@@ -141,7 +206,7 @@ const Statistics: React.FC = () => {
type="card" type="card"
items={items} items={items}
onChange={(key) => { onChange={(key) => {
// 切换 tab 时的处理逻辑 setActiveTab(key);
}} }}
/> />
@@ -162,13 +227,13 @@ const Statistics: React.FC = () => {
value={generateTimeRange} value={generateTimeRange}
onChange={(dates) => setGenerateTimeRange(dates as [moment.Moment, moment.Moment])} onChange={(dates) => setGenerateTimeRange(dates as [moment.Moment, moment.Moment])}
ranges={{ ranges={{
'今天': [moment().startOf('day'), moment().endOf('day')], '今天': [moment().startOf('day'), moment().endOf('day')],
'本周': [moment().startOf('week'), moment().endOf('week')], '本周': [moment().startOf('week'), moment().endOf('week')],
'本月': [moment().startOf('month'), moment().endOf('month')], '本月': [moment().startOf('month'), moment().endOf('month')],
'上个月': [ '上个月': [
moment().subtract(1, 'months').startOf('month'), moment().subtract(1, 'months').startOf('month'),
moment().subtract(1, 'months').endOf('month') moment().subtract(1, 'months').endOf('month')
], ],
}} }}
/> />
</div> </div>