Files
web-Iot/src/components/SystemSetting/StationManage.tsx

441 lines
12 KiB
TypeScript
Raw Normal View History

2026-04-29 09:47:01 +08:00
import React, { useState, useEffect } from 'react';
2026-04-24 15:28:16 +08:00
import {
2026-04-29 09:47:01 +08:00
Card, Table, Button, Space, Modal, Form, Input, Popconfirm, Select,
Switch, Divider, Row, Col, Tag, message
2026-04-24 15:28:16 +08:00
} from 'antd';
import {
2026-04-29 09:47:01 +08:00
PlusOutlined, EditOutlined, DeleteOutlined, BgColorsOutlined
2026-04-24 15:28:16 +08:00
} from '@ant-design/icons';
2026-04-29 09:47:01 +08:00
import {
getStationList, saveSite, deleteSite,
getStationRegions, saveRegion, deleteRegion
2026-05-07 14:13:26 +08:00
} from '../../api/stationManage';
import { getOrgList } from '../../api/organization';
2026-04-30 15:02:08 +08:00
import { useSelector } from 'react-redux';
2026-05-07 14:13:26 +08:00
import { RootState } from '../../store';
2026-04-24 15:28:16 +08:00
const { Option } = Select;
export default function StationManage() {
2026-04-29 09:47:01 +08:00
const [stationList, setStationList] = useState([]);
2026-04-24 15:28:16 +08:00
const [currentStation, setCurrentStation] = useState(null);
2026-04-29 09:47:01 +08:00
const [regionList, setRegionList] = useState([]);
2026-04-24 15:28:16 +08:00
2026-04-30 15:02:08 +08:00
// 组织列表
const [orgList, setOrgList] = useState([]);
// 🔥 选中的组织ID(用于筛选)
2026-05-07 20:25:14 +08:00
const [selectedOrgId, setSelectedOrgId] = useState<number | undefined>(undefined);
2026-04-30 15:02:08 +08:00
// 获取当前登录用户信息
const { userInfo } = useSelector((state: RootState) => state.user);
2026-04-29 09:47:01 +08:00
// 场站弹窗
2026-04-24 15:28:16 +08:00
const [modalVisible, setModalVisible] = useState(false);
const [form] = Form.useForm();
const [editRecord, setEditRecord] = useState(null);
// 分区弹窗
const [areaVisible, setAreaVisible] = useState(false);
2026-04-29 09:47:01 +08:00
const [regionForm] = Form.useForm();
const [editRegion, setEditRegion] = useState(null);
// 地图弹窗
2026-04-24 15:28:16 +08:00
const [mapVisible, setMapVisible] = useState(false);
2026-04-29 09:47:01 +08:00
const [messageApi, contextHolder] = message.useMessage();
2026-04-29 10:00:24 +08:00
// 分页状态
const [pagination, setPagination] = useState({
pageNum: 1,
pageSize: 10,
total: 0
});
// ====================== 获取组织列表 ======================
2026-04-30 15:02:08 +08:00
const getOrgData = () => {
const params = {
pageNum: 1,
pageSize: 9999,
userId: userInfo?.userId
};
getOrgList(params).then(res => {
if (res.code === 200) {
setOrgList(res.rows || []);
}
});
};
// ====================== 获取场站列表 ======================
2026-04-29 09:47:01 +08:00
const getStationData = () => {
2026-04-29 10:00:24 +08:00
const params = {
pageNum: pagination.pageNum,
2026-05-06 14:04:01 +08:00
pageSize: pagination.pageSize,
orgId: selectedOrgId, // 🔥 最终使用的筛选 orgId
2026-04-29 10:00:24 +08:00
};
getStationList(params).then(res => {
2026-04-29 09:47:01 +08:00
if (res.code === 200) {
setStationList(res.rows || []);
2026-04-29 10:00:24 +08:00
setPagination({
...pagination,
total: res.total || res.rows?.length || 0
});
2026-04-29 09:47:01 +08:00
}
});
};
2026-04-24 15:28:16 +08:00
2026-04-29 10:00:24 +08:00
// 切换分页
const handleTableChange = (newPagination) => {
setPagination({
...pagination,
pageNum: newPagination.current,
pageSize: newPagination.pageSize
});
};
// 组织筛选变化
2026-05-07 20:25:14 +08:00
const handleOrgChange = (value: number) => {
setSelectedOrgId(value);
setPagination({ ...pagination, pageNum: 1 });
2026-05-07 20:25:14 +08:00
};
// 重置筛选
2026-05-07 20:25:14 +08:00
const handleReset = () => {
setSelectedOrgId(undefined);
setPagination({ ...pagination, pageNum: 1 });
};
// ====================== 初始化加载 ======================
2026-04-29 09:47:01 +08:00
useEffect(() => {
2026-05-06 16:16:29 +08:00
getOrgData();
}, []);
// 🔥 关键:如果 userInfo.orgId 存在,则默认选中
useEffect(() => {
if (userInfo?.orgId) {
setSelectedOrgId(userInfo.orgId);
}
}, [userInfo]);
// 加载数据
useEffect(() => {
getStationData();
2026-05-07 20:25:14 +08:00
}, [
pagination.pageNum,
pagination.pageSize,
selectedOrgId
2026-05-07 20:25:14 +08:00
]);
2026-04-29 09:47:01 +08:00
// ====================== 新增 / 编辑 场站 ======================
2026-04-24 15:28:16 +08:00
const handleAdd = () => {
form.resetFields();
setEditRecord(null);
2026-04-29 09:47:01 +08:00
form.setFieldsValue({ status: 1 });
2026-04-24 15:28:16 +08:00
setModalVisible(true);
};
const handleEdit = (record) => {
setEditRecord(record);
2026-04-30 15:02:08 +08:00
form.setFieldsValue({
...record,
status: record.status === 1
});
2026-04-24 15:28:16 +08:00
setModalVisible(true);
};
const handleSave = () => {
form.validateFields().then(values => {
2026-04-29 09:47:01 +08:00
const params = {
...values,
status: values.status ? 1 : 0
};
2026-04-24 15:28:16 +08:00
if (editRecord) {
2026-04-29 09:47:01 +08:00
params.id = editRecord.id;
}
saveSite(params).then(res => {
if (res.code === 200) {
messageApi.success(editRecord ? '编辑成功' : '新增成功');
getStationData();
setModalVisible(false);
} else {
messageApi.error(res.msg || '保存失败');
}
});
});
};
// ====================== 删除场站 ======================
const handleDelete = (id) => {
deleteSite([id]).then(res => {
if (res.code === 200) {
messageApi.success('删除成功');
getStationData();
2026-04-24 15:28:16 +08:00
} else {
2026-04-29 09:47:01 +08:00
messageApi.error(res.msg || '删除失败');
2026-04-24 15:28:16 +08:00
}
});
};
2026-04-29 09:47:01 +08:00
// ====================== 分区管理 ======================
const handleOpenArea = async (record) => {
2026-04-24 15:28:16 +08:00
setCurrentStation(record);
setAreaVisible(true);
2026-04-29 09:47:01 +08:00
setEditRegion(null);
regionForm.resetFields();
const res = await getStationRegions({ id: record.id });
if (res.code === 200) setRegionList(res.data || []);
};
const handleSaveRegion = () => {
regionForm.validateFields().then(values => {
const params = {
...values,
siteId: currentStation.id,
status: values.status ? 1 : 0
};
if (editRegion) params.id = editRegion.id;
saveRegion(params).then(res => {
if (res.code === 200) {
messageApi.success('保存成功');
getStationRegions({ id: currentStation.id }).then(r => {
setRegionList(r.data || []);
});
setEditRegion(null);
regionForm.resetFields();
}
});
});
2026-04-24 15:28:16 +08:00
};
2026-04-29 09:47:01 +08:00
const handleDeleteRegion = (id) => {
deleteRegion([id]).then(res => {
if (res.code === 200) {
messageApi.success('删除成功');
setRegionList(regionList.filter(x => x.id !== id));
}
});
};
// ====================== 边界绘制 ======================
2026-04-24 15:28:16 +08:00
const handleOpenMap = (record) => {
setCurrentStation(record);
setMapVisible(true);
};
2026-04-29 09:47:01 +08:00
// ====================== 表格列 ======================
2026-04-24 15:28:16 +08:00
const columns = [
2026-04-29 09:47:01 +08:00
{ title: '场站名称', dataIndex: 'siteName' },
2026-05-18 14:28:42 +08:00
//{ title: '场站编号', dataIndex: 'siteCode' },
2026-05-06 10:09:28 +08:00
{
title: '所属组织',
dataIndex: 'orgId',
render: (orgId) => {
const org = orgList.find(item => item.id == orgId);
return org ? org.orgName : '—';
}
},
2026-04-24 15:28:16 +08:00
{ title: '地址', dataIndex: 'address' },
{
title: '操作',
width: 400,
render: (_, record) => (
<Space wrap size="small">
2026-04-29 09:47:01 +08:00
<Button icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑</Button>
<Button icon={<BgColorsOutlined />} onClick={() => handleOpenArea(record)}>分区设置</Button>
<Button onClick={() => handleOpenMap(record)}>边界绘制</Button>
2026-04-24 15:28:16 +08:00
<Popconfirm title="确定删除?" onConfirm={() => handleDelete(record.id)}>
2026-04-29 09:47:01 +08:00
<Button danger>删除</Button>
2026-04-24 15:28:16 +08:00
</Popconfirm>
</Space>
)
}
];
return (
2026-05-15 15:39:28 +08:00
<div style={{ padding: '8px 0px' }}>
2026-04-29 09:47:01 +08:00
{contextHolder}
<Card title="场站管理" style={{ borderRadius: 8 }}>
{/* 🔥 筛选栏:只有 userInfo 没有 orgId 时才显示 */}
{!userInfo?.orgId && (
<div style={{ marginBottom: 16, display: 'flex', alignItems: 'center', gap: 16 }}>
<Select
placeholder="请选择组织筛选"
style={{ width: 220 }}
value={selectedOrgId}
onChange={handleOrgChange}
allowClear
>
{orgList.map(item => (
<Option key={item.id} value={item.id}>
{item.orgName}
</Option>
))}
</Select>
<Button onClick={handleReset}>重置</Button>
2026-05-07 20:25:14 +08:00
</div>
)}
<div style={{ display: 'flex', justifyContent: 'end', marginBottom: 16 }}>
<Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
新增场站
</Button>
2026-04-29 09:47:01 +08:00
</div>
2026-04-24 15:28:16 +08:00
<Table
rowKey="id"
columns={columns}
dataSource={stationList}
2026-04-29 10:00:24 +08:00
pagination={{
current: pagination.pageNum,
pageSize: pagination.pageSize,
total: pagination.total,
showSizeChanger: true,
showTotal: (total) => `共 ${total} 条`
}}
onChange={handleTableChange}
2026-04-29 09:47:01 +08:00
bordered
size="middle"
2026-04-24 15:28:16 +08:00
/>
</Card>
2026-04-29 09:47:01 +08:00
{/* ====================== 场站弹窗 ====================== */}
2026-04-24 15:28:16 +08:00
<Modal
title={editRecord ? '编辑场站' : '新增场站'}
open={modalVisible}
2026-04-29 09:47:01 +08:00
width={650}
2026-04-24 15:28:16 +08:00
onCancel={() => setModalVisible(false)}
onOk={handleSave}
destroyOnClose
>
<Form form={form} layout="vertical">
<Row gutter={16}>
<Col span={12}>
2026-04-29 09:47:01 +08:00
<Form.Item name="siteName" label="场站名称" rules={[{ required: true }]}>
<Input placeholder="请输入" />
2026-04-24 15:28:16 +08:00
</Form.Item>
</Col>
<Col span={12}>
2026-05-18 14:28:42 +08:00
<Form.Item name="orgId" label="所属组织" rules={[{ required: true }]}>
<Select placeholder="请选择所属组织" allowClear>
{orgList.map(item => (
<Option key={item.id} value={item.id}>
{item.orgName}
</Option>
))}
</Select>
2026-04-24 15:28:16 +08:00
</Form.Item>
</Col>
</Row>
2026-05-18 14:28:42 +08:00
2026-04-24 15:28:16 +08:00
<Row gutter={16}>
<Col span={12}>
2026-04-29 09:47:01 +08:00
<Form.Item name="longitude" label="经度">
<Input placeholder="经度" />
2026-04-24 15:28:16 +08:00
</Form.Item>
</Col>
<Col span={12}>
2026-04-29 09:47:01 +08:00
<Form.Item name="latitude" label="纬度">
<Input placeholder="纬度" />
2026-04-24 15:28:16 +08:00
</Form.Item>
</Col>
</Row>
2026-04-29 09:47:01 +08:00
<Form.Item name="address" label="详细地址">
<Input placeholder="地址" />
2026-04-24 15:28:16 +08:00
</Form.Item>
2026-04-29 09:47:01 +08:00
<Form.Item name="remark" label="备注">
<Input.TextArea rows={3} />
</Form.Item>
2026-04-24 15:28:16 +08:00
2026-04-29 09:47:01 +08:00
<Form.Item name="status" label="状态" valuePropName="checked">
2026-04-24 15:28:16 +08:00
<Switch checkedChildren="启用" unCheckedChildren="停用" />
</Form.Item>
</Form>
</Modal>
2026-04-29 09:47:01 +08:00
{/* ====================== 分区弹窗 ====================== */}
2026-04-24 15:28:16 +08:00
<Modal
2026-04-29 09:47:01 +08:00
title={`【${currentStation?.siteName}】分区设置`}
2026-04-24 15:28:16 +08:00
open={areaVisible}
2026-04-29 09:47:01 +08:00
width={650}
2026-04-24 15:28:16 +08:00
onCancel={() => setAreaVisible(false)}
footer={null}
>
2026-04-29 09:47:01 +08:00
<Form form={regionForm} layout="vertical" style={{ marginBottom: 16 }}>
<Row gutter={16}>
<Col span={12}>
<Form.Item name="regionName" label="分区名称" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Col>
<Col span={12}>
<Form.Item name="regionCode" label="分区编码" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Col>
</Row>
<Row gutter={16}>
<Col span={12}>
<Form.Item name="type" label="分区类型">
<Select><Option value="清扫区">清扫区</Option><Option value="巡检区">巡检区</Option></Select>
</Form.Item>
</Col>
<Col span={12}>
<Form.Item name="status" label="状态" valuePropName="checked">
<Switch checkedChildren="启用" unCheckedChildren="停用" />
</Form.Item>
</Col>
</Row>
<Form.Item name="remark" label="备注"><Input.TextArea rows={2} /></Form.Item>
<Button type="primary" onClick={handleSaveRegion}>保存分区</Button>
</Form>
2026-04-24 15:28:16 +08:00
<Table
2026-04-29 09:47:01 +08:00
size="middle"
2026-04-24 15:28:16 +08:00
rowKey="id"
pagination={false}
2026-04-29 09:47:01 +08:00
bordered
2026-04-24 15:28:16 +08:00
columns={[
2026-04-29 09:47:01 +08:00
{ title: '分区名称', dataIndex: 'regionName' },
{ title: '编码', dataIndex: 'regionCode' },
2026-04-24 15:28:16 +08:00
{ title: '类型', dataIndex: 'type' },
{
2026-04-29 09:47:01 +08:00
title: '状态', render: r => r.status === 1 ? '启用' : '停用'
2026-04-24 15:28:16 +08:00
},
{
2026-04-29 09:47:01 +08:00
title: '操作', render: (_, r) => (
<Space>
<Button onClick={() => { setEditRegion(r); regionForm.setFieldsValue(r); }}>编辑</Button>
<Button danger onClick={() => handleDeleteRegion(r.id)}>删除</Button>
2026-04-24 15:28:16 +08:00
</Space>
)
}
]}
2026-04-29 09:47:01 +08:00
dataSource={regionList}
2026-04-24 15:28:16 +08:00
/>
</Modal>
2026-04-29 09:47:01 +08:00
{/* ====================== 地图绘制 ====================== */}
2026-04-24 15:28:16 +08:00
<Modal
2026-04-29 09:47:01 +08:00
title={`【${currentStation?.siteName}】边界绘制`}
2026-04-24 15:28:16 +08:00
open={mapVisible}
width={800}
onCancel={() => setMapVisible(false)}
footer={null}
>
2026-04-29 09:47:01 +08:00
<div style={{ height: 400, background: '#f5f5f5', borderRadius: 8, display: 'flex', alignItems: 'center', justifyContent: 'center', flexDirection: 'column' }}>
地图区域绘制(可自行接入高德/百度地图)
<Space style={{ marginTop: 20 }}>
2026-04-24 15:28:16 +08:00
<Button type="primary">开始绘制</Button>
2026-04-29 09:47:01 +08:00
<Button>保存边界</Button>
2026-04-24 15:28:16 +08:00
</Space>
</div>
</Modal>
</div>
);
}