Files
gofly-platform-services/zlt-web/react-web/src/main/frontend/src/pages/Welcome.tsx
T

310 lines
9.0 KiB
TypeScript

import { PageContainer } from '@ant-design/pro-components';
import { Card, Row, Col, Statistic, Progress, Typography, Select, DatePicker, Space, Button } from 'antd';
import React, { useState, useEffect } from 'react';
import { dashboard } from '@/services/orders/api';
import { Area, Column, Pie } from '@ant-design/plots';
import {
ShoppingCartOutlined,
PayCircleOutlined,
RiseOutlined,
FilterOutlined,
ReloadOutlined,
} from '@ant-design/icons';
import styles from './Welcome.less';
const { Title } = Typography;
const { RangePicker } = DatePicker;
interface Statistics {
orderStats: {
totalOrders: number;
todayOrders: number;
orderGrowth: number;
completionRate: number;
// 新增对账维度
totalAmount: number; // 订单总金额(分)
platformAmount: number; // 平台分账金额(分)
tourismAmount: number; // 景区分账金额(分)
refundAmount: number; // 退款金额(分)
netIncome: number; // 净收入(分)
};
// 移除设备和航线统计
chartStats: {
orderTrend: {
date: string;
type: string;
value: number;
}[];
orderDistribution: {
type: string;
value: number;
}[];
incomeBreakdown: {
type: string;
value: number;
}[];
};
// 新增筛选维度数据
filterOptions: {
tourismList: { label: string; value: number; }[];
qrcodeList: { label: string; value: string; }[];
deviceList: { label: string; value: string; }[];
};
}
const Welcome: React.FC = () => {
const [loading, setLoading] = useState<boolean>(true);
const [statistics, setStatistics] = useState<any>({});
// 只保留景区筛选
const [selectedTourism, setSelectedTourism] = useState<number | undefined>();
// 新增平台分账比例,默认70%
const [platformRate, setPlatformRate] = useState<number>(0.7);
// 新增时间区间筛选
const [timeRange, setTimeRange] = useState<[string, string]>(['', '']);
const fetchData = async () => {
try {
setLoading(true);
const params = {
tourismId: selectedTourism,
startTime: timeRange[0],
endTime: timeRange[1],
};
const result = await dashboard(params);
if (result.resp_code === 0) {
setStatistics(result.data);
}
} catch (error) {
console.error('获取统计数据失败:', error);
} finally {
setLoading(false);
}
};
useEffect(() => {
fetchData();
}, [selectedTourism, timeRange]);
// 更新数据访问方式
const orderStats = statistics.orderStats || {};
const chartStats = statistics.chartStats || {};
const filterOptions = statistics.filterOptions || {};
// 动态分账金额计算
const netIncome = orderStats.netIncome || 0;
const platformAmount = Math.round(netIncome * platformRate);
const tourismAmount = netIncome - platformAmount;
// 订单趋势配置
const orderTrendConfig = {
data: chartStats.orderTrend || [],
xField: 'date',
yField: 'value',
seriesField: 'type',
smooth: true,
animation: true,
legend: {
position: 'top' as const,
},
areaStyle: {
fillOpacity: 0.6,
},
};
// 订单分布配置
const orderDistConfig = {
data: chartStats.orderDistribution || [],
angleField: 'value',
colorField: 'type',
radius: 0.8,
label: {
type: 'outer',
content: '{name} {percentage}',
},
};
// 收入构成图表数据
const incomeConfig = {
data: [
{ type: '平台分账', value: platformAmount / 100 },
{ type: '景区分账', value: tourismAmount / 100 },
{ type: '退款金额', value: (orderStats.refundAmount || 0) / 100 },
],
xField: 'type',
yField: 'value',
label: {
position: 'middle' as const,
style: {
fill: '#FFFFFF',
opacity: 0.6,
},
},
};
return (
<PageContainer className={styles.container}>
{/* 筛选条件 */}
<Card style={{ marginBottom: 16 }}>
<Space size="large">
<span>筛选条件:</span>
<Select
placeholder="选择景区"
style={{ width: 200 }}
allowClear
options={filterOptions.tourismList || []}
value={selectedTourism}
onChange={setSelectedTourism}
/>
{/* 新增平台分账比例选择 */}
<span>平台分账比例:</span>
<Select
style={{ width: 120 }}
value={platformRate}
onChange={setPlatformRate}
options={[
{ label: '70%', value: 0.7 },
{ label: '60%', value: 0.6 },
{ label: '50%', value: 0.5 },
]}
/>
{/* 新增时间区间筛选 */}
<RangePicker
placeholder={['开始时间', '结束时间']}
onChange={(dates) => {
if (dates) {
setTimeRange([
dates[0]?.format('YYYY-MM-DD HH:mm:ss') || '',
dates[1]?.format('YYYY-MM-DD HH:mm:ss') || ''
]);
} else {
setTimeRange(['', '']);
}
}}
showTime
style={{ width: 300 }}
/>
<Button
icon={<ReloadOutlined />}
onClick={fetchData}
type="primary"
>
刷新
</Button>
</Space>
</Card>
{/* 核心指标 */}
<Row gutter={[16, 16]}>
<Col xs={24} sm={12} md={8}>
<Card className={styles.statCard} loading={loading}>
<div className={styles.statHeader}>
<ShoppingCartOutlined className={styles.iconOrders} />
<span>订单总览</span>
</div>
<Statistic
title="总订单数"
value={orderStats.totalOrders || 0}
suffix={
<span className={styles.trend}>
<RiseOutlined /> {orderStats.orderGrowth || 0}%
</span>
}
/>
<div className={styles.subStats}>
<Statistic title="今日订单" value={orderStats.todayOrders || 0} />
<Statistic
title="完成率"
value={orderStats.completionRate || 0}
suffix="%"
/>
</div>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card className={styles.statCard} loading={loading}>
<div className={styles.statHeader}>
<PayCircleOutlined className={styles.iconIncome} />
<span>收入统计</span>
</div>
<Statistic
title="订单总金额"
value={(orderStats.totalAmount || 0) / 100}
precision={2}
prefix="¥"
/>
<div className={styles.subStats}>
<Statistic
title="景区净收入"
value={netIncome / 100}
precision={2}
prefix="¥"
/>
<Statistic
title="退款金额"
value={(orderStats.refundAmount || 0) / 100}
precision={2}
prefix="¥"
/>
</div>
</Card>
</Col>
<Col xs={24} sm={12} md={8}>
<Card className={styles.statCard} loading={loading}>
<div className={styles.statHeader}>
<PayCircleOutlined className={styles.iconIncome} />
<span>分账明细</span>
</div>
<Statistic
title="平台分账"
value={platformAmount / 100}
precision={2}
prefix="¥"
/>
<div className={styles.subStats}>
<Statistic
title="景区分账"
value={tourismAmount / 100}
precision={2}
prefix="¥"
/>
<Progress
percent={netIncome > 0 ? (tourismAmount / netIncome * 100) : 0}
size="small"
status="active"
format={(percent) => `分账率 ${percent?.toFixed(1)}%`}
/>
</div>
</Card>
</Col>
</Row>
{/* 图表展示 */}
<Row gutter={[16, 16]} className={styles.chartsRow}>
<Col xs={24} lg={16}>
<Card title="订单趋势" loading={loading}>
<Area {...orderTrendConfig} height={300} />
</Card>
</Col>
<Col xs={24} lg={8}>
<Card title="订单状态分布" loading={loading}>
<Pie {...orderDistConfig} height={300} />
</Card>
</Col>
</Row>
{/* 收入构成 */}
<Row gutter={[16, 16]}>
<Col xs={24}>
<Card title="收入构成分析" loading={loading}>
<Column {...incomeConfig} height={300} />
</Card>
</Col>
</Row>
</PageContainer>
);
};
export default Welcome;