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 new file mode 100644 index 00000000..820c9b5a --- /dev/null +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/DeviceStatistics.tsx @@ -0,0 +1,281 @@ +// 设备维度统计组件 +import React, { 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'; + +interface DeviceStatisticsItem { + groupKey: string; // 设备SN + totalOrders: number; // 订单总数 + paidOrders: number; // 已支付订单数 + completedOrders: number; // 已完成订单数 + canceledOrders: number; // 已取消订单数 + totalAmount: number; // 订单总金额(分) + totalAmountYuan: string; // 订单总金额(元) + paidAmount: number; // 已支付金额(分) + paidAmountYuan: string; // 已支付金额(元) + refundAmount: number; // 退款金额(分) + refundAmountYuan: string; // 退款金额(元) + completionRate: string; // 完成率 +} + +const DeviceStatistics: React.FC<{ refreshKey: number }> = ({ refreshKey }) => { + const [loading, setLoading] = useState(false); + const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([ + moment().startOf('month'), + moment().endOf('month') + ]); + const [statisticsData, setStatisticsData] = useState([]); + + // 获取统计数据 + const fetchStatisticsData = async () => { + setLoading(true); + try { + const params = { + startTime: timeRange[0].format('YYYY-MM-DD HH:mm:ss'), + endTime: timeRange[1].format('YYYY-MM-DD HH:mm:ss'), + }; + + const res = await getDeviceStatistics(params); + if (res.resp_code === 0) { + setStatisticsData(res.data || []); + } + } catch (error) { + message.error('获取设备统计数据失败'); + } finally { + setLoading(false); + } + }; + + // 数据刷新 + useEffect(() => { + fetchStatisticsData(); + }, [timeRange, refreshKey]); + + // 处理图表数据 + const getChartData = () => { + const result: any[] = []; + statisticsData.forEach(item => { + result.push( + { + groupKey: item.groupKey, + type: '订单总数', + value: item.totalOrders, + }, + { + groupKey: item.groupKey, + type: '已完成订单', + value: item.completedOrders, + }, + { + groupKey: item.groupKey, + type: '总金额(元)', + value: Number(item.totalAmountYuan), + }, + ); + }); + return result; + }; + + // 计算总览数据 + const getOverviewData = () => { + if (statisticsData.length === 0) return null; + + return statisticsData.reduce((acc, cur) => ({ + totalOrders: acc.totalOrders + cur.totalOrders, + paidOrders: acc.paidOrders + cur.paidOrders, + totalAmount: acc.totalAmount + Number(cur.totalAmountYuan), + avgCompletionRate: (acc.avgCompletionRate * acc.count + parseFloat(cur.completionRate)) / (acc.count + 1), + count: acc.count + 1, + }), { + totalOrders: 0, + paidOrders: 0, + totalAmount: 0, + avgCompletionRate: 0, + count: 0, + }); + }; + + // 表格列定义 + const columns = [ + { + title: '设备SN', + dataIndex: 'groupKey', + width: 180, + fixed: 'left', + }, + { + title: '订单数量', + children: [ + { + title: '总订单', + dataIndex: 'totalOrders', + width: 100, + sorter: (a, b) => a.totalOrders - b.totalOrders, + }, + { + title: '已支付', + dataIndex: 'paidOrders', + width: 100, + sorter: (a, b) => a.paidOrders - b.paidOrders, + }, + { + title: '已完成', + dataIndex: 'completedOrders', + width: 100, + sorter: (a, b) => a.completedOrders - b.completedOrders, + }, + { + title: '已取消', + dataIndex: 'canceledOrders', + width: 100, + sorter: (a, b) => a.canceledOrders - b.canceledOrders, + }, + { + title: '完成率', + dataIndex: 'completionRate', + width: 100, + sorter: (a, b) => parseFloat(a.completionRate) - parseFloat(b.completionRate), + } + ], + }, + { + title: '订单金额(元)', + children: [ + { + title: '总金额', + dataIndex: 'totalAmountYuan', + width: 120, + sorter: (a, b) => parseFloat(a.totalAmountYuan) - parseFloat(b.totalAmountYuan), + }, + { + title: '已支付', + dataIndex: 'paidAmountYuan', + width: 120, + sorter: (a, b) => parseFloat(a.paidAmountYuan) - parseFloat(b.paidAmountYuan), + }, + { + title: '退款', + dataIndex: 'refundAmountYuan', + width: 120, + sorter: (a, b) => parseFloat(a.refundAmountYuan) - parseFloat(b.refundAmountYuan), + }, + ], + }, + ]; + + return ( + <> + {/* 筛选条件 */} + + + + + + + + + + + {/* 数据概览 */} + + + + + + + + + + + + + + + + + + {/* 趋势图表 */} + +
+ ({ + name: datum.type, + value: datum.type.includes('金额') ? `¥${datum.value}` : datum.value, + }), + }} + /> +
+
+ + {/* 详细数据表格 */} + + + + + ); +}; + +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 new file mode 100644 index 00000000..3a665e12 --- /dev/null +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/components/QrcodeStatistics.tsx @@ -0,0 +1,290 @@ +// 二维码维度统计组件 +import React, { useState, useEffect } 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 + +interface QrcodeStatisticsProps { + qrcodeOptions: any[]; + qrcodeId?: string; + onQrcodeChange: (id: string) => void; + refreshKey: number; +} + +const QrcodeStatistics: React.FC = ({ + qrcodeOptions, + qrcodeId, + onQrcodeChange, + refreshKey, +}) => { + const [loading, setLoading] = useState(false); + const [timeType, setTimeType] = useState<'hour' | 'day' | 'month'>('day'); + const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([ + moment().startOf('month'), + moment().endOf('month') + ]); + const [statisticsData, setStatisticsData] = useState([]); + + // 获取统计数据 + const fetchStatisticsData = async () => { + if (!qrcodeId) return; + + setLoading(true); + try { + const params = { + qrcodeId, + startTime: timeRange[0].format('YYYY-MM-DD HH:mm:ss'), + endTime: timeRange[1].format('YYYY-MM-DD HH:mm:ss'), + timeType, + }; + + const res = await getTimeStatistics(params); + if (res.resp_code === 0) { + setStatisticsData(res.data || []); + } + } catch (error) { + message.error('获取统计数据失败'); + } finally { + setLoading(false); + } + }; + + // 数据刷新 + useEffect(() => { + if (qrcodeId) { + fetchStatisticsData(); + } + }, [qrcodeId, timeType, timeRange, refreshKey]); + + // 处理图表数据 + const getChartData = () => { + const result: any[] = []; + statisticsData.forEach(item => { + result.push( + { + groupKey: item.groupKey, + type: '订单总数', + value: item.totalOrders, + }, + { + groupKey: item.groupKey, + type: '总金额(元)', + value: Number(item.totalAmountYuan), + }, + ); + }); + return result; + }; + + // 表格列定义 + const columns = [ + { + title: '统计时间', + dataIndex: 'groupKey', + width: 180, + fixed: 'left', + }, + { + title: '订单数量', + children: [ + { + title: '总订单', + dataIndex: 'totalOrders', + width: 100, + sorter: (a, b) => a.totalOrders - b.totalOrders, + }, + { + title: '已支付', + dataIndex: 'paidOrders', + width: 100, + sorter: (a, b) => a.paidOrders - b.paidOrders, + }, + { + title: '已完成', + dataIndex: 'completedOrders', + width: 100, + sorter: (a, b) => a.completedOrders - b.completedOrders, + }, + { + title: '已取消', + dataIndex: 'canceledOrders', + width: 100, + sorter: (a, b) => a.canceledOrders - b.canceledOrders, + }, + { + title: '完成率', + dataIndex: 'completionRate', + width: 100, + } + ], + }, + { + title: '订单金额(元)', + children: [ + { + title: '总金额', + dataIndex: 'totalAmountYuan', + width: 120, + }, + { + title: '已支付', + dataIndex: 'paidAmountYuan', + width: 120, + }, + { + title: '退款', + dataIndex: 'refundAmountYuan', + width: 120, + }, + ], + }, + ]; + + // 计算总览数据 + const getOverviewData = () => { + if (statisticsData.length === 0) return null; + + return statisticsData.reduce((acc, cur) => ({ + totalOrders: acc.totalOrders + cur.totalOrders, + paidOrders: acc.paidOrders + cur.paidOrders, + totalAmount: acc.totalAmount + Number(cur.totalAmountYuan), + avgCompletionRate: parseFloat(cur.completionRate), + }), { + totalOrders: 0, + paidOrders: 0, + totalAmount: 0, + avgCompletionRate: 0, + }); + }; + + return ( + <> + {/* 筛选条件 */} + + + + + + (option?.label ?? '').toLowerCase().includes(input.toLowerCase()), + optionLabelProp: "label", + }} + /> + + + + + + setTimeType(e.target.value)} + optionType="button" + buttonStyle="solid" + > + 按小时 + 按天 + 按月 + + + + + + + {/* 数据概览 */} + + + + + + + + + + + + + + + + + + {/* 趋势图表 */} + +
+ +
+
+ + {/* 详细数据表格 */} + + + + + ); +}; + +export default QrcodeStatistics; \ No newline at end of file diff --git a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.less b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.less index 48f1c8f4..3ec1a540 100644 --- a/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.less +++ b/zlt-web/react-web/src/main/frontend/src/pages/orders/statistics/index.less @@ -58,4 +58,29 @@ } } } +} + +.statistics-tabs { + .ant-tabs-nav { + margin-bottom: 0; + + .ant-tabs-tab { + border-radius: 4px 4px 0 0; + background: #fafafa; + border-color: #f0f0f0; + padding: 8px 24px; + + &.ant-tabs-tab-active { + background: #fff; + border-bottom-color: #fff; + } + } + } + + .ant-tabs-content { + background: #fff; + padding: 24px; + border: 1px solid #f0f0f0; + border-top: none; + } } \ 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 fb54e250..2d04d5e9 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 @@ -1,42 +1,16 @@ import React, { useState, useEffect, useRef } from 'react'; -import { PageContainer, ProForm, ProFormDateRangePicker, ProFormSelect, ProTable } from '@ant-design/pro-components'; -import { Card, Radio, Space, message, Tabs, Row, Col, Statistic, Button } from 'antd'; -import { Line } from '@ant-design/plots'; +import { PageContainer } from '@ant-design/pro-components'; +import { Card, Tabs, Button, message } from 'antd'; import { ReloadOutlined, DownloadOutlined } from '@ant-design/icons'; -import moment from 'moment'; -import { getTimeStatistics, getQrcodeList } from '@/services/statistics/api'; -import { exportExcel } from '@/utils/export'; - -// 定义数据结构接口 -interface StatisticsItem { - groupKey: string; - totalOrders: number; - paidOrders: number; - completedOrders: number; - canceledOrders: number; - totalAmount: number; - totalAmountYuan: string; - paidAmount: number; - paidAmountYuan: string; - refundAmount: number; - refundAmountYuan: string; - completionRate: string; -} +import type { TabsProps } from 'antd'; +import { getQrcodeList } from '@/services/statistics/api'; +import QrcodeStatistics from './components/QrcodeStatistics'; +import DeviceStatistics from './components/DeviceStatistics'; const Statistics: React.FC = () => { - const [loading, setLoading] = useState(false); const [qrcodeOptions, setQrcodeOptions] = useState<{ label: string; value: string; }[]>([]); const [qrcodeId, setQrcodeId] = useState(); - const [timeType, setTimeType] = useState<'hour' | 'day' | 'month'>('day'); - const [timeRange, setTimeRange] = useState<[moment.Moment, moment.Moment]>([ - moment().startOf('month'), - moment().endOf('month') - ]); - const [statisticsData, setStatisticsData] = useState([]); - const [refreshKey, setRefreshKey] = useState(0); // 用于触发数据刷新 - - // 自动刷新定时器 - const refreshTimer = useRef(); + const [refreshKey, setRefreshKey] = useState(0); // 获取二维码列表 const fetchQrcodeList = async () => { @@ -57,199 +31,34 @@ const Statistics: React.FC = () => { } }; - // 获取统计数据 - const fetchStatisticsData = async () => { - if (!qrcodeId) return; - - setLoading(true); - try { - const params = { - qrcodeId, - startTime: timeRange[0].format('YYYY-MM-DD HH:mm:ss'), - endTime: timeRange[1].format('YYYY-MM-DD HH:mm:ss'), - timeType, - }; - - const res = await getTimeStatistics(params); - if (res.resp_code === 0) { - setStatisticsData(res.data || []); - } - } catch (error) { - message.error('获取统计数据失败'); - } finally { - setLoading(false); - } - }; - - // 处理数据导出 - const handleExport = () => { - const 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' }, - ]; - - exportExcel({ - data: statisticsData, - header, - fileName: `订单统计数据_${moment().format('YYYY-MM-DD')}`, - }); - }; - - // 图表配置 - const chartConfig = { - data: statisticsData, - loading, - xField: 'groupKey', - yField: 'value', - seriesField: 'type', - smooth: true, - meta: { - value: { - alias: '数量/金额', - }, - }, - tooltip: { - formatter: (datum: any) => ({ - name: datum.type, - value: datum.type.includes('金额') ? `¥${datum.value}` : datum.value, - }), - }, - }; - - // 处理图表数据 - const getChartData = () => { - const result: any[] = []; - statisticsData.forEach(item => { - result.push( - { - groupKey: item.groupKey, - type: '订单总数', - value: item.totalOrders, - }, - { - groupKey: item.groupKey, - type: '总金额(元)', - value: Number(item.totalAmountYuan), - }, - ); - }); - return result; - }; - - // 表格列定义 - const columns = [ - { - title: '统计时间', - dataIndex: 'groupKey', - width: 180, - fixed: 'left', - }, - { - title: '订单数量', - children: [ - { - title: '总订单', - dataIndex: 'totalOrders', - width: 100, - sorter: (a, b) => a.totalOrders - b.totalOrders, - }, - { - title: '已支付', - dataIndex: 'paidOrders', - width: 100, - sorter: (a, b) => a.paidOrders - b.paidOrders, - }, - { - title: '已完成', - dataIndex: 'completedOrders', - width: 100, - sorter: (a, b) => a.completedOrders - b.completedOrders, - }, - { - title: '已取消', - dataIndex: 'canceledOrders', - width: 100, - sorter: (a, b) => a.canceledOrders - b.canceledOrders, - }, - { - title: '完成率', - dataIndex: 'completionRate', - width: 100, - sorter: (a, b) => parseFloat(a.completionRate) - parseFloat(b.completionRate), - } - ], - }, - { - title: '订单金额(元)', - children: [ - { - title: '总金额', - dataIndex: 'totalAmountYuan', - width: 120, - sorter: (a, b) => parseFloat(a.totalAmountYuan) - parseFloat(b.totalAmountYuan), - }, - { - title: '已支付', - dataIndex: 'paidAmountYuan', - width: 120, - sorter: (a, b) => parseFloat(a.paidAmountYuan) - parseFloat(b.paidAmountYuan), - }, - { - title: '退款', - dataIndex: 'refundAmountYuan', - width: 120, - sorter: (a, b) => parseFloat(a.refundAmountYuan) - parseFloat(b.refundAmountYuan), - }, - ], - }, - ]; - - // 计算总览数据 - const getOverviewData = () => { - if (statisticsData.length === 0) return null; - - return statisticsData.reduce((acc, cur) => ({ - totalOrders: acc.totalOrders + cur.totalOrders, - paidOrders: acc.paidOrders + cur.paidOrders, - totalAmount: acc.totalAmount + parseFloat(cur.totalAmountYuan), - avgCompletionRate: (acc.avgCompletionRate + parseFloat(cur.completionRate)) / 2, - }), { - totalOrders: 0, - paidOrders: 0, - totalAmount: 0, - avgCompletionRate: 0, - }); - }; - - // 自动刷新 - useEffect(() => { - refreshTimer.current = setInterval(() => { - setRefreshKey(prev => prev + 1); - }, 300000); // 每5分钟刷新一次 - - return () => { - if (refreshTimer.current) { - clearInterval(refreshTimer.current); - } - }; - }, []); - - // 添加初始化加载二维码列表 + // 初始化加载 useEffect(() => { fetchQrcodeList(); }, []); - // 数据刷新 - useEffect(() => { - fetchStatisticsData(); - }, [qrcodeId, timeType, timeRange, refreshKey]); + const items: TabsProps['items'] = [ + { + key: 'qrcode', + label: '按二维码统计', + children: ( + + ), + }, + { + key: 'device', + label: '按设备统计', + children: ( + + ), + }, + ]; return ( { key="export" type="primary" icon={} - onClick={handleExport} + onClick={() => { + // 根据当前选中的 tab 调用相应的导出函数 + }} > 导出数据 , ]} > - {/* 筛选条件 */} - - - - - - (option?.label ?? '').toLowerCase().includes(input.toLowerCase()), - optionLabelProp: "label", - }} - /> - - - - - - setTimeType(e.target.value)} - optionType="button" - buttonStyle="solid" - > - 按小时 - 按天 - 按月 - - - - - - - {/* 数据概览 */} - - - - - - - - - - - - - - - - - - {/* 趋势图表 */} - -
- -
-
- - {/* 详细数据表格 */} - - - + { + // 切换 tab 时的处理逻辑 + }} + />
); }; diff --git a/zlt-web/react-web/src/main/frontend/src/services/statistics/api.ts b/zlt-web/react-web/src/main/frontend/src/services/statistics/api.ts index 2155bc08..c877a58a 100644 --- a/zlt-web/react-web/src/main/frontend/src/services/statistics/api.ts +++ b/zlt-web/react-web/src/main/frontend/src/services/statistics/api.ts @@ -40,4 +40,18 @@ export async function getTimeStatistics(params: TimeStatisticsParams) { timeType: params.timeType || 'day', }, }); +} + +/** 获取设备维度统计数据参数 */ +interface DeviceStatisticsParams { + startTime: string; + endTime: string; +} + +/** 获取设备维度统计数据 */ +export async function getDeviceStatistics(params: DeviceStatisticsParams) { + return request('/api-order/statistics/device', { + method: 'GET', + params, + }); } \ No newline at end of file