pages_rack_server.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import React, { useState } from 'react';
  2. import {
  3. Button, Table, Space,
  4. Form, Input, Select, message, Modal,
  5. Card, Row, Col, Typography, Badge
  6. } from 'antd';
  7. import {
  8. useQuery
  9. } from '@tanstack/react-query';
  10. import dayjs from 'dayjs';
  11. import 'dayjs/locale/zh-cn';
  12. import type {
  13. ZichanInfo, RackInfo, RackServer
  14. } from '../share/monitorTypes.ts';
  15. import {
  16. ServerType, ServerTypeNameMap
  17. } from '../share/monitorTypes.ts';
  18. import { EnableStatus } from '../share/types.ts'
  19. import { RackAPI, RackServerAPI , ZichanAPI} from './api/index.ts';
  20. const { Title } = Typography;
  21. // 机柜服务器管理页面
  22. export const RackServerPage = () => {
  23. const [form] = Form.useForm();
  24. const [formMode, setFormMode] = useState<'create' | 'edit'>('create');
  25. const [editingId, setEditingId] = useState<number | null>(null);
  26. const [modalVisible, setModalVisible] = useState(false);
  27. const [isLoading, setIsLoading] = useState(false);
  28. const [searchParams, setSearchParams] = useState({
  29. page: 1,
  30. limit: 10,
  31. rack_id: undefined as number | undefined,
  32. server_type: undefined as string | undefined
  33. });
  34. // 获取机柜列表用于下拉选择
  35. const { data: rackOptions = [] } = useQuery({
  36. queryKey: ['rackOptions'],
  37. queryFn: async () => {
  38. try {
  39. const response = await RackAPI.getRackList({ limit: 100 });
  40. return response.data.map((rack: RackInfo) => ({
  41. label: `${rack.rack_name || ''} (${rack.rack_code || ''})`,
  42. value: rack.id
  43. }));
  44. } catch (error) {
  45. console.error('获取机柜列表失败:', error);
  46. return [];
  47. }
  48. }
  49. });
  50. // 获取资产列表用于下拉选择
  51. const { data: assetOptions = [] } = useQuery({
  52. queryKey: ['assetOptions'],
  53. queryFn: async () => {
  54. try {
  55. const response = await ZichanAPI.getZichanList({ limit: 100 });
  56. return response.data.map((asset: ZichanInfo) => ({
  57. label: `${asset.asset_name || ''} (ID:${asset.id})`,
  58. value: asset.id
  59. }));
  60. } catch (error) {
  61. console.error('获取资产列表失败:', error);
  62. return [];
  63. }
  64. }
  65. });
  66. // 服务器类型选项
  67. const serverTypeOptions = [
  68. { label: ServerTypeNameMap[ServerType.STANDARD], value: ServerType.STANDARD },
  69. { label: ServerTypeNameMap[ServerType.NETWORK], value: ServerType.NETWORK },
  70. { label: ServerTypeNameMap[ServerType.STORAGE], value: ServerType.STORAGE },
  71. { label: ServerTypeNameMap[ServerType.SPECIAL], value: ServerType.SPECIAL }
  72. ];
  73. // 查询机柜服务器列表
  74. const {
  75. data: rackServerResult = { data: [], pagination: { total: 0, current: 1, pageSize: 10 } },
  76. isLoading: isFetching,
  77. refetch
  78. } = useQuery({
  79. queryKey: ['rackServers', searchParams],
  80. queryFn: () => RackServerAPI.getRackServerList(searchParams),
  81. });
  82. // 提取数据和分页信息
  83. const rackServerList = rackServerResult.data || [];
  84. const pagination = rackServerResult.pagination || { total: 0, current: 1, pageSize: 10 };
  85. // 处理表单提交
  86. const handleSubmit = async (values: Partial<RackServer>) => {
  87. try {
  88. setIsLoading(true);
  89. if (formMode === 'create') {
  90. await RackServerAPI.createRackServer(values);
  91. message.success('机柜服务器创建成功');
  92. } else {
  93. if (editingId) {
  94. await RackServerAPI.updateRackServer(editingId, values);
  95. message.success('机柜服务器更新成功');
  96. }
  97. }
  98. setModalVisible(false);
  99. refetch();
  100. } catch (error: any) {
  101. message.error(error.response?.data?.error || '操作失败');
  102. } finally {
  103. setIsLoading(false);
  104. }
  105. };
  106. // 处理编辑
  107. const handleEdit = async (id: number) => {
  108. try {
  109. setIsLoading(true);
  110. const data = await RackServerAPI.getRackServer(id);
  111. form.setFieldsValue(data);
  112. setEditingId(id);
  113. setFormMode('edit');
  114. setModalVisible(true);
  115. } catch (error: any) {
  116. message.error(error.response?.data?.error || '获取机柜服务器详情失败');
  117. } finally {
  118. setIsLoading(false);
  119. }
  120. };
  121. // 处理删除
  122. const handleDelete = async (id: number) => {
  123. try {
  124. await RackServerAPI.deleteRackServer(id);
  125. message.success('机柜服务器删除成功');
  126. refetch();
  127. } catch (error: any) {
  128. message.error(error.response?.data?.error || '删除机柜服务器失败');
  129. }
  130. };
  131. // 处理搜索
  132. const handleSearch = (values: any) => {
  133. setSearchParams({
  134. ...searchParams,
  135. page: 1, // 重置为第一页
  136. rack_id: values.rack_id,
  137. server_type: values.server_type
  138. });
  139. };
  140. // 处理页码变化
  141. const handlePageChange = (page: number, pageSize?: number) => {
  142. setSearchParams({
  143. ...searchParams,
  144. page,
  145. limit: pageSize || 10
  146. });
  147. };
  148. // 处理新增
  149. const handleAdd = () => {
  150. form.resetFields();
  151. setFormMode('create');
  152. setEditingId(null);
  153. setModalVisible(true);
  154. };
  155. // 表格列定义
  156. const columns = [
  157. {
  158. title: 'ID',
  159. dataIndex: 'id',
  160. key: 'id',
  161. width: 60
  162. },
  163. {
  164. title: '机柜',
  165. dataIndex: 'rack_id',
  166. key: 'rack_id',
  167. render: (rackId: number) => {
  168. const rack = rackOptions.find((r: any) => r.value === rackId);
  169. return rack ? rack.label : `ID:${rackId}`;
  170. }
  171. },
  172. {
  173. title: '资产',
  174. dataIndex: 'asset_id',
  175. key: 'asset_id',
  176. render: (assetId: number) => {
  177. const asset = assetOptions.find((a: any) => a.value === assetId);
  178. return asset ? asset.label : `ID:${assetId}`;
  179. }
  180. },
  181. {
  182. title: '起始U位',
  183. dataIndex: 'start_position',
  184. key: 'start_position'
  185. },
  186. {
  187. title: '占用U数',
  188. dataIndex: 'size',
  189. key: 'size'
  190. },
  191. {
  192. title: '服务器类型',
  193. dataIndex: 'server_type',
  194. key: 'server_type',
  195. render: (type: ServerType) => ServerTypeNameMap[type] || '未知类型'
  196. },
  197. {
  198. title: '状态',
  199. dataIndex: 'is_disabled',
  200. key: 'is_disabled',
  201. render: (status: EnableStatus) => (
  202. <Badge
  203. status={status === EnableStatus.ENABLED ? 'success' : 'error'}
  204. text={status === EnableStatus.ENABLED ? '正常' : '禁用'}
  205. />
  206. )
  207. },
  208. {
  209. title: '创建时间',
  210. dataIndex: 'created_at',
  211. key: 'created_at',
  212. render: (date: string) => date ? dayjs(date).format('YYYY-MM-DD HH:mm:ss') : '-'
  213. },
  214. {
  215. title: '操作',
  216. key: 'action',
  217. width: 200,
  218. render: (_: any, record: RackServer) => (
  219. <Space>
  220. <Button size="small" type="primary" onClick={() => handleEdit(record.id)}>编辑</Button>
  221. <Button size="small" danger onClick={() =>
  222. Modal.confirm({
  223. title: '确认删除',
  224. content: '确定要删除此机柜服务器记录吗?',
  225. onOk: () => handleDelete(record.id)
  226. })
  227. }>删除</Button>
  228. </Space>
  229. )
  230. }
  231. ];
  232. return (
  233. <div>
  234. <Title level={2}>机柜服务器管理</Title>
  235. <Card>
  236. <Form layout="inline" onFinish={handleSearch} style={{ marginBottom: 16 }}>
  237. <Form.Item name="rack_id" label="机柜">
  238. <Select
  239. placeholder="请选择机柜"
  240. style={{ width: 200 }}
  241. allowClear
  242. options={rackOptions}
  243. showSearch
  244. filterOption={(input, option) =>
  245. (String(option?.label ?? '')).toLowerCase().includes(input.toLowerCase())
  246. }
  247. />
  248. </Form.Item>
  249. <Form.Item name="server_type" label="服务器类型">
  250. <Select
  251. placeholder="请选择服务器类型"
  252. style={{ width: 160 }}
  253. allowClear
  254. options={serverTypeOptions}
  255. />
  256. </Form.Item>
  257. <Form.Item>
  258. <Button type="primary" htmlType="submit">查询</Button>
  259. </Form.Item>
  260. <Form.Item>
  261. <Button type="primary" onClick={handleAdd}>添加服务器</Button>
  262. </Form.Item>
  263. </Form>
  264. <Table
  265. columns={columns}
  266. dataSource={rackServerList}
  267. rowKey="id"
  268. loading={isFetching}
  269. pagination={{
  270. current: pagination.current,
  271. pageSize: pagination.pageSize,
  272. total: pagination.total,
  273. onChange: handlePageChange,
  274. showSizeChanger: true,
  275. showTotal: (total) => `共 ${total} 条记录`
  276. }}
  277. />
  278. <Modal
  279. title={formMode === 'create' ? '添加机柜服务器' : '编辑机柜服务器'}
  280. open={modalVisible}
  281. onCancel={() => setModalVisible(false)}
  282. footer={null}
  283. width={700}
  284. >
  285. <Form
  286. form={form}
  287. layout="vertical"
  288. onFinish={handleSubmit}
  289. >
  290. <Row gutter={16}>
  291. <Col span={12}>
  292. <Form.Item
  293. name="rack_id"
  294. label="选择机柜"
  295. rules={[{ required: true, message: '请选择机柜' }]}
  296. >
  297. <Select
  298. placeholder="请选择机柜"
  299. options={rackOptions}
  300. showSearch
  301. filterOption={(input, option) =>
  302. (String(option?.label ?? '')).toLowerCase().includes(input.toLowerCase())
  303. }
  304. />
  305. </Form.Item>
  306. </Col>
  307. <Col span={12}>
  308. <Form.Item
  309. name="asset_id"
  310. label="选择资产"
  311. rules={[{ required: true, message: '请选择资产' }]}
  312. >
  313. <Select
  314. placeholder="请选择资产"
  315. options={assetOptions}
  316. showSearch
  317. filterOption={(input, option) =>
  318. (String(option?.label ?? '')).toLowerCase().includes(input.toLowerCase())
  319. }
  320. />
  321. </Form.Item>
  322. </Col>
  323. </Row>
  324. <Row gutter={16}>
  325. <Col span={12}>
  326. <Form.Item
  327. name="start_position"
  328. label="起始U位"
  329. rules={[{ required: true, message: '请输入起始U位' }]}
  330. >
  331. <Input type="number" placeholder="请输入起始U位" />
  332. </Form.Item>
  333. </Col>
  334. <Col span={12}>
  335. <Form.Item
  336. name="size"
  337. label="占用U数"
  338. initialValue={1}
  339. rules={[{ required: true, message: '请输入占用U数' }]}
  340. >
  341. <Input type="number" placeholder="请输入占用U数" />
  342. </Form.Item>
  343. </Col>
  344. </Row>
  345. <Row gutter={16}>
  346. <Col span={12}>
  347. <Form.Item
  348. name="server_type"
  349. label="服务器类型"
  350. initialValue={ServerType.STANDARD}
  351. >
  352. <Select
  353. placeholder="请选择服务器类型"
  354. options={serverTypeOptions}
  355. />
  356. </Form.Item>
  357. </Col>
  358. <Col span={12}>
  359. <Form.Item
  360. name="is_disabled"
  361. label="状态"
  362. initialValue={EnableStatus.ENABLED}
  363. >
  364. <Select>
  365. <Select.Option value={EnableStatus.ENABLED}>正常</Select.Option>
  366. <Select.Option value={EnableStatus.DISABLED}>禁用</Select.Option>
  367. </Select>
  368. </Form.Item>
  369. </Col>
  370. </Row>
  371. <Form.Item
  372. name="remark"
  373. label="备注信息"
  374. >
  375. <Input.TextArea rows={3} placeholder="请输入备注信息" />
  376. </Form.Item>
  377. <Form.Item>
  378. <Space>
  379. <Button type="primary" htmlType="submit" loading={isLoading}>
  380. {formMode === 'create' ? '创建' : '保存'}
  381. </Button>
  382. <Button onClick={() => setModalVisible(false)}>取消</Button>
  383. </Space>
  384. </Form.Item>
  385. </Form>
  386. </Modal>
  387. </Card>
  388. </div>
  389. );
  390. };