diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/DeviceStatistics.tsx b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/DeviceStatistics.tsx index f178a854..397df538 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/DeviceStatistics.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/DeviceStatistics.tsx @@ -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 }) => { ); -}; +}); export default DeviceStatistics; \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/QrcodeStatistics.tsx b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/QrcodeStatistics.tsx index 6a69eb08..4ea59f5b 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/QrcodeStatistics.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/QrcodeStatistics.tsx @@ -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 = ({ +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 = ({ }); }; + // 暴露导出方法 + useImperativeHandle(ref, () => ({ + exportData: (header: any[], fileName: string) => { + exportExcel({ + data: statisticsData, + header, + fileName, + }); + }, + })); + return ( <> {/* 筛选条件 */} @@ -286,6 +298,6 @@ const QrcodeStatistics: React.FC = ({ ); -}; +}); export default QrcodeStatistics; \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/TourismStatistics.tsx b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/TourismStatistics.tsx index ea01fa78..9164f833 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/TourismStatistics.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/TourismStatistics.tsx @@ -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 }) => ); -}; +}); export default TourismStatistics; \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.tsx b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.tsx index 40a5e581..363e18aa 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.tsx +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.tsx @@ -21,6 +21,7 @@ const Statistics: React.FC = () => { moment().endOf('month') ]); const [generating, setGenerating] = useState(false); + const [activeTab, setActiveTab] = useState('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(null); + const deviceRef = useRef(null); + const tourismRef = useRef(null); + const items: TabsProps['items'] = [ { key: 'qrcode', label: '按二维码统计', children: ( { label: '按设备统计', children: ( ), @@ -101,6 +167,7 @@ const Statistics: React.FC = () => { label: '按景区统计', children: ( ), @@ -129,11 +196,9 @@ const Statistics: React.FC = () => { key="export" type="primary" icon={} - onClick={() => { - // 根据当前选中的 tab 调用相应的导出函数 - }} + onClick={handleExport} > - 导出数据 + 导出{activeTab === 'qrcode' ? '二维码' : activeTab === 'device' ? '设备' : '景区'}统计 , ]} > @@ -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') + ], }} />