pages_xunlian_codes.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. import React, { useState } from 'react';
  2. import { useQueryClient } from '@tanstack/react-query';
  3. import {
  4. Button, Table, Space,
  5. Form, Input, message, Modal,
  6. Card, Row, Col,
  7. Popconfirm, Tag, DatePicker
  8. } from 'antd';
  9. import {
  10. useQuery,
  11. } from '@tanstack/react-query';
  12. import dayjs from 'dayjs';
  13. import weekday from 'dayjs/plugin/weekday';
  14. import localeData from 'dayjs/plugin/localeData';
  15. import 'dayjs/locale/zh-cn';
  16. import type {
  17. XunlianCode
  18. } from '../share/types_stock.ts';
  19. import {
  20. XunlianCodeAPI
  21. } from './api/xunlian_codes.ts';
  22. // 配置 dayjs 插件
  23. dayjs.extend(weekday);
  24. dayjs.extend(localeData);
  25. // 设置 dayjs 语言
  26. dayjs.locale('zh-cn');
  27. // 训练代码管理页面组件
  28. export const XunlianCodePage = () => {
  29. const queryClient = useQueryClient();
  30. const [modalVisible, setModalVisible] = useState(false);
  31. const [formMode, setFormMode] = useState<'create' | 'edit'>('create');
  32. const [editingId, setEditingId] = useState<number | null>(null);
  33. const [form] = Form.useForm();
  34. const [searchForm] = Form.useForm();
  35. const [searchParams, setSearchParams] = useState({
  36. code: '',
  37. page: 1,
  38. limit: 10,
  39. });
  40. // 使用React Query获取训练代码列表
  41. const { data: codeDatas, isLoading: isListLoading, refetch } = useQuery({
  42. queryKey: ['xunlianCodes', searchParams],
  43. queryFn: async () => {
  44. const response = await XunlianCodeAPI.getXunlianCodes({
  45. page: searchParams.page,
  46. pageSize: searchParams.limit,
  47. code: searchParams.code,
  48. });
  49. return response;
  50. }
  51. });
  52. const { data: codes, pagination } = codeDatas || {
  53. data: [],
  54. pagination: {
  55. current: 1,
  56. pageSize: 10,
  57. total: 0,
  58. totalPages: 1
  59. }
  60. };
  61. // 获取单个训练代码
  62. const fetchCode = async (id: number) => {
  63. try {
  64. const response = await XunlianCodeAPI.getXunlianCode(id);
  65. return response.data;
  66. } catch (error) {
  67. message.error('获取训练代码详情失败');
  68. return null;
  69. }
  70. };
  71. // 处理表单提交
  72. const handleSubmit = async (values: Partial<XunlianCode>) => {
  73. try {
  74. // 转换交易日期格式为YYYY-MM-DD格式,确保不为null
  75. const submitValues = {
  76. ...values,
  77. ...(values.trade_date && {
  78. trade_date: dayjs(values.trade_date).format('YYYY-MM-DD')
  79. })
  80. };
  81. const response = formMode === 'create'
  82. ? await XunlianCodeAPI.createXunlianCode(submitValues)
  83. : await XunlianCodeAPI.updateXunlianCode(editingId!, submitValues);
  84. message.success(formMode === 'create' ? '创建训练代码成功' : '更新训练代码成功');
  85. setModalVisible(false);
  86. form.resetFields();
  87. refetch();
  88. } catch (error) {
  89. message.error((error as Error).message);
  90. }
  91. };
  92. // 处理编辑
  93. const handleEdit = async (id: number) => {
  94. const code = await fetchCode(id);
  95. if (code) {
  96. setFormMode('edit');
  97. setEditingId(id);
  98. // 转换交易日期格式为dayjs对象
  99. form.setFieldsValue({
  100. ...code,
  101. trade_date: code.trade_date ? dayjs(code.trade_date) : null
  102. });
  103. setModalVisible(true);
  104. }
  105. };
  106. // 处理删除
  107. const handleDelete = async (id: number) => {
  108. try {
  109. await XunlianCodeAPI.deleteXunlianCode(id);
  110. message.success('删除训练代码成功');
  111. refetch();
  112. } catch (error) {
  113. message.error((error as Error).message);
  114. }
  115. };
  116. // 处理搜索
  117. const handleSearch = async (values: any) => {
  118. try {
  119. queryClient.removeQueries({ queryKey: ['xunlianCodes'] });
  120. setSearchParams({
  121. code: values.code || '',
  122. page: 1,
  123. limit: searchParams.limit,
  124. });
  125. } catch (error) {
  126. message.error('搜索失败');
  127. }
  128. };
  129. // 处理分页
  130. const handlePageChange = (page: number, pageSize?: number) => {
  131. setSearchParams(prev => ({
  132. ...prev,
  133. page,
  134. limit: pageSize || prev.limit,
  135. }));
  136. };
  137. // 处理添加
  138. const handleAdd = () => {
  139. setFormMode('create');
  140. setEditingId(null);
  141. form.resetFields();
  142. setModalVisible(true);
  143. };
  144. // 表格列定义
  145. const columns = [
  146. {
  147. title: 'ID',
  148. dataIndex: 'id',
  149. key: 'id',
  150. width: 80,
  151. },
  152. {
  153. title: '股票代码',
  154. dataIndex: 'code',
  155. key: 'code',
  156. },
  157. {
  158. title: '股票名称',
  159. dataIndex: 'stock_name',
  160. key: 'stock_name',
  161. },
  162. {
  163. title: '案例名称',
  164. dataIndex: 'name',
  165. key: 'name',
  166. },
  167. {
  168. title: '案例类型',
  169. dataIndex: 'type',
  170. key: 'type',
  171. },
  172. {
  173. title: '交易日期',
  174. dataIndex: 'trade_date',
  175. key: 'trade_date',
  176. render: (date: string) => dayjs(date).format('YYYY-MM-DD'),
  177. },
  178. {
  179. title: '提交用户',
  180. dataIndex: 'nickname',
  181. key: 'nickname',
  182. },
  183. {
  184. title: '创建时间',
  185. dataIndex: 'created_at',
  186. key: 'created_at',
  187. render: (date: string) => dayjs(date).format('YYYY-MM-DD HH:mm:ss'),
  188. },
  189. {
  190. title: '操作',
  191. key: 'action',
  192. render: (_: any, record: XunlianCode) => (
  193. <Space size="middle">
  194. <Button type="link" onClick={() => handleEdit(record.id)}>编辑</Button>
  195. <Popconfirm
  196. title="确定要删除这条训练代码吗?"
  197. onConfirm={() => handleDelete(record.id)}
  198. okText="确定"
  199. cancelText="取消"
  200. >
  201. <Button type="link" danger>删除</Button>
  202. </Popconfirm>
  203. </Space>
  204. ),
  205. },
  206. ];
  207. return (
  208. <div>
  209. <Card title="训练代码管理" className="mb-4">
  210. <Form
  211. form={searchForm}
  212. layout="inline"
  213. onFinish={handleSearch}
  214. style={{ marginBottom: '16px' }}
  215. >
  216. <Form.Item name="code" label="股票代码">
  217. <Input placeholder="要搜索的股票代码" />
  218. </Form.Item>
  219. <Form.Item>
  220. <Space>
  221. <Button type="primary" htmlType="submit">
  222. 搜索
  223. </Button>
  224. <Button htmlType="reset" onClick={() => {
  225. searchForm.resetFields();
  226. setSearchParams({
  227. code: '',
  228. page: 1,
  229. limit: 10,
  230. });
  231. }}>
  232. 重置
  233. </Button>
  234. <Button type="primary" onClick={handleAdd}>
  235. 添加代码
  236. </Button>
  237. </Space>
  238. </Form.Item>
  239. </Form>
  240. <Table
  241. columns={columns}
  242. dataSource={codes}
  243. rowKey="id"
  244. loading={{
  245. spinning: isListLoading,
  246. tip: '正在加载数据...',
  247. }}
  248. pagination={{
  249. current: pagination.current,
  250. pageSize: pagination.pageSize,
  251. total: pagination.total,
  252. onChange: handlePageChange,
  253. showSizeChanger: true,
  254. showTotal: (total) => `共 ${total} 条`,
  255. }}
  256. />
  257. </Card>
  258. <Modal
  259. title={formMode === 'create' ? '添加训练代码' : '编辑训练代码'}
  260. open={modalVisible}
  261. onOk={() => {
  262. form.validateFields()
  263. .then(values => {
  264. handleSubmit(values);
  265. })
  266. .catch(info => {
  267. console.log('表单验证失败:', info);
  268. });
  269. }}
  270. onCancel={() => setModalVisible(false)}
  271. width={800}
  272. okText="确定"
  273. cancelText="取消"
  274. destroyOnClose
  275. >
  276. <Form
  277. form={form}
  278. layout="vertical"
  279. >
  280. <Row gutter={16}>
  281. <Col span={12}>
  282. <Form.Item
  283. name="code"
  284. label="股票代码"
  285. rules={[{ required: true, message: '请输入股票代码' }]}
  286. >
  287. <Input placeholder="请输入股票代码" />
  288. </Form.Item>
  289. </Col>
  290. <Col span={12}>
  291. <Form.Item
  292. name="stock_name"
  293. label="股票名称"
  294. rules={[{ required: true, message: '请输入股票名称' }]}
  295. >
  296. <Input placeholder="请输入股票名称" />
  297. </Form.Item>
  298. </Col>
  299. </Row>
  300. <Row gutter={16}>
  301. <Col span={12}>
  302. <Form.Item
  303. name="name"
  304. label="案例名称"
  305. rules={[{ required: true, message: '请输入案例名称' }]}
  306. >
  307. <Input placeholder="请输入案例名称" />
  308. </Form.Item>
  309. </Col>
  310. <Col span={12}>
  311. <Form.Item
  312. name="type"
  313. label="案例类型"
  314. >
  315. <Input placeholder="请输入案例类型" />
  316. </Form.Item>
  317. </Col>
  318. </Row>
  319. <Row gutter={16}>
  320. <Col span={12}>
  321. <Form.Item
  322. name="trade_date"
  323. label="交易日期"
  324. rules={[{ required: true, message: '请选择交易日期' }]}
  325. >
  326. <DatePicker style={{ width: '100%' }} format="YYYY-MM-DD" />
  327. </Form.Item>
  328. </Col>
  329. <Col span={12}>
  330. <Form.Item
  331. name="description"
  332. label="案例描述"
  333. >
  334. <Input.TextArea rows={1} placeholder="请输入案例描述" />
  335. </Form.Item>
  336. </Col>
  337. </Row>
  338. </Form>
  339. </Modal>
  340. </div>
  341. );
  342. };