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