fix(统计管理):新增二维码、设备、景区分账统计前后端功能
This commit is contained in:
+281
@@ -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<DeviceStatisticsItem[]>([]);
|
||||
|
||||
// 获取统计数据
|
||||
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 (
|
||||
<>
|
||||
{/* 筛选条件 */}
|
||||
<Card bordered={false} className="statistics-filter-card" style={{ marginBottom: 24 }}>
|
||||
<ProForm
|
||||
layout="horizontal"
|
||||
submitter={false}
|
||||
initialValues={{ timeRange }}
|
||||
>
|
||||
<Row>
|
||||
<Col span={24}>
|
||||
<ProFormDateRangePicker
|
||||
name="timeRange"
|
||||
label="统计时间"
|
||||
fieldProps={{
|
||||
value: timeRange,
|
||||
onChange: setTimeRange,
|
||||
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')],
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</ProForm>
|
||||
</Card>
|
||||
|
||||
{/* 数据概览 */}
|
||||
<Card bordered={false} style={{ marginBottom: 24 }}>
|
||||
<Row gutter={24}>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="设备总数"
|
||||
value={statisticsData.length}
|
||||
suffix="台"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="总订单数"
|
||||
value={getOverviewData()?.totalOrders || 0}
|
||||
suffix="笔"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="总金额"
|
||||
value={getOverviewData()?.totalAmount || 0}
|
||||
precision={2}
|
||||
prefix="¥"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="平均完成率"
|
||||
value={getOverviewData()?.avgCompletionRate || 0}
|
||||
precision={2}
|
||||
suffix="%"
|
||||
valueStyle={{ color: '#3f8600' }}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Card>
|
||||
|
||||
{/* 趋势图表 */}
|
||||
<Card bordered={false} style={{ marginBottom: 24 }}>
|
||||
<div style={{ height: 400 }}>
|
||||
<Column
|
||||
loading={loading}
|
||||
data={getChartData()}
|
||||
xField="groupKey"
|
||||
yField="value"
|
||||
seriesField="type"
|
||||
isGroup={true}
|
||||
columnStyle={{
|
||||
radius: [4, 4, 0, 0],
|
||||
}}
|
||||
meta={{
|
||||
value: {
|
||||
alias: '数量/金额',
|
||||
},
|
||||
}}
|
||||
tooltip={{
|
||||
formatter: (datum) => ({
|
||||
name: datum.type,
|
||||
value: datum.type.includes('金额') ? `¥${datum.value}` : datum.value,
|
||||
}),
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 详细数据表格 */}
|
||||
<Card bordered={false}>
|
||||
<ProTable
|
||||
dataSource={statisticsData}
|
||||
columns={columns}
|
||||
loading={loading}
|
||||
search={false}
|
||||
options={false}
|
||||
pagination={false}
|
||||
scroll={{ x: 1500 }}
|
||||
size="small"
|
||||
/>
|
||||
</Card>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default DeviceStatistics;
|
||||
+290
@@ -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<QrcodeStatisticsProps> = ({
|
||||
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<any[]>([]);
|
||||
|
||||
// 获取统计数据
|
||||
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 (
|
||||
<>
|
||||
{/* 筛选条件 */}
|
||||
<Card bordered={false} className="statistics-filter-card" style={{ marginBottom: 24 }}>
|
||||
<ProForm
|
||||
layout="horizontal"
|
||||
submitter={false}
|
||||
initialValues={{ timeRange, qrcodeId }}
|
||||
>
|
||||
<Row gutter={24}>
|
||||
<Col span={8}>
|
||||
<ProFormSelect
|
||||
name="qrcodeId"
|
||||
label="二维码"
|
||||
width="xl"
|
||||
options={qrcodeOptions}
|
||||
placeholder="请选择二维码"
|
||||
rules={[{ required: true, message: '请选择二维码' }]}
|
||||
fieldProps={{
|
||||
value: qrcodeId,
|
||||
onChange: onQrcodeChange,
|
||||
showSearch: true,
|
||||
filterOption: (input, option) =>
|
||||
(option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
|
||||
optionLabelProp: "label",
|
||||
}}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={8}>
|
||||
<ProFormDateRangePicker
|
||||
name="timeRange"
|
||||
label="统计时间"
|
||||
fieldProps={{
|
||||
value: timeRange,
|
||||
onChange: setTimeRange,
|
||||
}}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={8}>
|
||||
<Radio.Group
|
||||
value={timeType}
|
||||
onChange={e => setTimeType(e.target.value)}
|
||||
optionType="button"
|
||||
buttonStyle="solid"
|
||||
>
|
||||
<Radio.Button value="hour">按小时</Radio.Button>
|
||||
<Radio.Button value="day">按天</Radio.Button>
|
||||
<Radio.Button value="month">按月</Radio.Button>
|
||||
</Radio.Group>
|
||||
</Col>
|
||||
</Row>
|
||||
</ProForm>
|
||||
</Card>
|
||||
|
||||
{/* 数据概览 */}
|
||||
<Card bordered={false} style={{ marginBottom: 24 }}>
|
||||
<Row gutter={24}>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="总订单数"
|
||||
value={getOverviewData()?.totalOrders || 0}
|
||||
suffix="笔"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="已支付订单"
|
||||
value={getOverviewData()?.paidOrders || 0}
|
||||
suffix="笔"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="总金额"
|
||||
value={getOverviewData()?.totalAmount || 0}
|
||||
precision={2}
|
||||
prefix="¥"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="平均完成率"
|
||||
value={getOverviewData()?.avgCompletionRate || 0}
|
||||
precision={2}
|
||||
suffix="%"
|
||||
valueStyle={{ color: '#3f8600' }}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Card>
|
||||
|
||||
{/* 趋势图表 */}
|
||||
<Card bordered={false} style={{ marginBottom: 24 }}>
|
||||
<div style={{ height: 400 }}>
|
||||
<Line
|
||||
loading={loading}
|
||||
data={getChartData()}
|
||||
xField="groupKey"
|
||||
yField="value"
|
||||
seriesField="type"
|
||||
smooth
|
||||
meta={{
|
||||
value: {
|
||||
alias: '数量/金额',
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 详细数据表格 */}
|
||||
<Card bordered={false}>
|
||||
<ProTable
|
||||
dataSource={statisticsData}
|
||||
columns={columns}
|
||||
loading={loading}
|
||||
search={false}
|
||||
options={false}
|
||||
pagination={false}
|
||||
scroll={{ x: 1500 }}
|
||||
size="small"
|
||||
/>
|
||||
</Card>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default QrcodeStatistics;
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<string>();
|
||||
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<StatisticsItem[]>([]);
|
||||
const [refreshKey, setRefreshKey] = useState(0); // 用于触发数据刷新
|
||||
|
||||
// 自动刷新定时器
|
||||
const refreshTimer = useRef<NodeJS.Timeout>();
|
||||
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: (
|
||||
<QrcodeStatistics
|
||||
qrcodeOptions={qrcodeOptions}
|
||||
qrcodeId={qrcodeId}
|
||||
onQrcodeChange={setQrcodeId}
|
||||
refreshKey={refreshKey}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
key: 'device',
|
||||
label: '按设备统计',
|
||||
children: (
|
||||
<DeviceStatistics
|
||||
refreshKey={refreshKey}
|
||||
/>
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<PageContainer
|
||||
@@ -265,121 +74,21 @@ const Statistics: React.FC = () => {
|
||||
key="export"
|
||||
type="primary"
|
||||
icon={<DownloadOutlined />}
|
||||
onClick={handleExport}
|
||||
onClick={() => {
|
||||
// 根据当前选中的 tab 调用相应的导出函数
|
||||
}}
|
||||
>
|
||||
导出数据
|
||||
</Button>,
|
||||
]}
|
||||
>
|
||||
{/* 筛选条件 */}
|
||||
<Card bordered={false} className="statistics-filter-card" style={{ marginBottom: 24 }}>
|
||||
<ProForm
|
||||
layout="horizontal"
|
||||
submitter={false}
|
||||
initialValues={{ timeRange, qrcodeId }}
|
||||
>
|
||||
<Row gutter={24}>
|
||||
<Col span={8}>
|
||||
<ProFormSelect
|
||||
name="qrcodeId"
|
||||
label="二维码"
|
||||
width="xl"
|
||||
options={qrcodeOptions}
|
||||
placeholder="请选择二维码"
|
||||
rules={[{ required: true, message: '请选择二维码' }]}
|
||||
fieldProps={{
|
||||
value: qrcodeId,
|
||||
onChange: setQrcodeId,
|
||||
showSearch: true,
|
||||
filterOption: (input, option) =>
|
||||
(option?.label ?? '').toLowerCase().includes(input.toLowerCase()),
|
||||
optionLabelProp: "label",
|
||||
}}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={8}>
|
||||
<ProFormDateRangePicker
|
||||
name="timeRange"
|
||||
label="统计时间"
|
||||
fieldProps={{
|
||||
value: timeRange,
|
||||
onChange: setTimeRange,
|
||||
}}
|
||||
/>
|
||||
</Col>
|
||||
<Col span={8}>
|
||||
<Radio.Group
|
||||
value={timeType}
|
||||
onChange={e => setTimeType(e.target.value)}
|
||||
optionType="button"
|
||||
buttonStyle="solid"
|
||||
>
|
||||
<Radio.Button value="hour">按小时</Radio.Button>
|
||||
<Radio.Button value="day">按天</Radio.Button>
|
||||
<Radio.Button value="month">按月</Radio.Button>
|
||||
</Radio.Group>
|
||||
</Col>
|
||||
</Row>
|
||||
</ProForm>
|
||||
</Card>
|
||||
|
||||
{/* 数据概览 */}
|
||||
<Card bordered={false} style={{ marginBottom: 24 }}>
|
||||
<Row gutter={24}>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="总订单数"
|
||||
value={getOverviewData()?.totalOrders || 0}
|
||||
suffix="笔"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="已支付订单"
|
||||
value={getOverviewData()?.paidOrders || 0}
|
||||
suffix="笔"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="总金额"
|
||||
value={getOverviewData()?.totalAmount || 0}
|
||||
precision={2}
|
||||
prefix="¥"
|
||||
/>
|
||||
</Col>
|
||||
<Col span={6}>
|
||||
<Statistic
|
||||
title="平均完成率"
|
||||
value={getOverviewData()?.avgCompletionRate || 0}
|
||||
precision={2}
|
||||
suffix="%"
|
||||
valueStyle={{ color: '#3f8600' }}
|
||||
/>
|
||||
</Col>
|
||||
</Row>
|
||||
</Card>
|
||||
|
||||
{/* 趋势图表 */}
|
||||
<Card bordered={false} style={{ marginBottom: 24 }}>
|
||||
<div style={{ height: 400 }}>
|
||||
<Line {...chartConfig} data={getChartData()} />
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
{/* 详细数据表格 */}
|
||||
<Card bordered={false}>
|
||||
<ProTable
|
||||
dataSource={statisticsData}
|
||||
columns={columns}
|
||||
loading={loading}
|
||||
search={false}
|
||||
options={false}
|
||||
pagination={false}
|
||||
scroll={{ x: 1500 }}
|
||||
size="small"
|
||||
/>
|
||||
</Card>
|
||||
<Tabs
|
||||
type="card"
|
||||
items={items}
|
||||
onChange={(key) => {
|
||||
// 切换 tab 时的处理逻辑
|
||||
}}
|
||||
/>
|
||||
</PageContainer>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user