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 { Card, Row, Col, Statistic, message } from 'antd';
import { Column } from '@ant-design/plots';
import moment from 'moment';
import { getDeviceStatistics } from '@/services/statistics/api';
import { exportExcel } from '@/utils/export';
interface DeviceStatisticsItem {
groupKey: string; // 设备SN
@@ -21,7 +22,11 @@ interface DeviceStatisticsItem {
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 [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([
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 (
<>
{/* 筛选条件 */}
@@ -277,6 +293,6 @@ const DeviceStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) => {
</Card>
</>
);
};
});
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 { Card, Radio, Row, Col, Statistic, message } from 'antd';
import { Line } from '@ant-design/plots';
import moment from 'moment';
import { getTimeStatistics } from '@/services/statistics/api';
import { exportExcel } from '@/utils/export';
// ... 其他 import
interface QrcodeStatisticsProps {
@@ -14,12 +15,12 @@ interface QrcodeStatisticsProps {
refreshKey: number;
}
const QrcodeStatistics: React.FC<QrcodeStatisticsProps> = ({
const QrcodeStatistics = forwardRef<{ exportData: (header: any[], fileName: string) => void }, QrcodeStatisticsProps>(({
qrcodeOptions,
qrcodeId,
onQrcodeChange,
refreshKey,
}) => {
}, ref) => {
const [loading, setLoading] = useState(false);
const [timeType, setTimeType] = useState<'hour' | 'day' | 'month'>('day');
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 (
<>
{/* 筛选条件 */}
@@ -286,6 +298,6 @@ const QrcodeStatistics: React.FC<QrcodeStatisticsProps> = ({
</Card>
</>
);
};
});
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 { Card, Row, Col, Statistic, message } from 'antd';
import { Column } from '@ant-design/plots';
import moment from 'moment';
import { getTourismStatistics } from '@/services/statistics/api';
import { exportExcel } from '@/utils/export';
interface TourismStatisticsItem {
groupKey: number; // 景区ID
@@ -20,7 +21,11 @@ interface TourismStatisticsItem {
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 [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([
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 (
<>
{/* 筛选条件 */}
@@ -277,6 +293,6 @@ const TourismStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) =>
</Card>
</>
);
};
});
export default TourismStatistics;
@@ -21,6 +21,7 @@ const Statistics: React.FC = () => {
moment().endOf('month')
]);
const [generating, setGenerating] = useState(false);
const [activeTab, setActiveTab] = useState<string>('qrcode');
// 获取二维码列表
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'] = [
{
key: 'qrcode',
label: '按二维码统计',
children: (
<QrcodeStatistics
ref={qrcodeRef}
qrcodeOptions={qrcodeOptions}
qrcodeId={qrcodeId}
onQrcodeChange={setQrcodeId}
@@ -92,6 +157,7 @@ const Statistics: React.FC = () => {
label: '按设备统计',
children: (
<DeviceStatistics
ref={deviceRef}
refreshKey={refreshKey}
/>
),
@@ -101,6 +167,7 @@ const Statistics: React.FC = () => {
label: '按景区统计',
children: (
<TourismStatistics
ref={tourismRef}
refreshKey={refreshKey}
/>
),
@@ -129,11 +196,9 @@ const Statistics: React.FC = () => {
key="export"
type="primary"
icon={<DownloadOutlined />}
onClick={() => {
// 根据当前选中的 tab 调用相应的导出函数
}}
onClick={handleExport}
>
导出数据
导出{activeTab === 'qrcode' ? '二维码' : activeTab === 'device' ? '设备' : '景区'}统计
</Button>,
]}
>
@@ -141,7 +206,7 @@ const Statistics: React.FC = () => {
type="card"
items={items}
onChange={(key) => {
// 切换 tab 时的处理逻辑
setActiveTab(key);
}}
/>
@@ -162,13 +227,13 @@ const Statistics: React.FC = () => {
value={generateTimeRange}
onChange={(dates) => setGenerateTimeRange(dates as [moment.Moment, moment.Moment])}
ranges={{
'今天': [moment().startOf('day'), moment().endOf('day')],
'本周': [moment().startOf('week'), moment().endOf('week')],
'本月': [moment().startOf('month'), moment().endOf('month')],
'上个月': [
moment().subtract(1, 'months').startOf('month'),
moment().subtract(1, 'months').endOf('month')
],
'今天': [moment().startOf('day'), moment().endOf('day')],
'本周': [moment().startOf('week'), moment().endOf('week')],
'本月': [moment().startOf('month'), moment().endOf('month')],
'上个月': [
moment().subtract(1, 'months').startOf('month'),
moment().subtract(1, 'months').endOf('month')
],
}}
/>
</div>