ExamResultList.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { CardStepTitle, FileIcon } from "@/components";
  2. import ExamScoreImportController from "@/services/apis/ExamScoreImportController";
  3. import { SendOutlined, UploadOutlined } from "@ant-design/icons";
  4. import { ProCard } from "@ant-design/pro-components";
  5. import { App, Button, Card, Space, Typography, Upload, UploadFile, UploadProps, theme } from "antd";
  6. import { RcFile } from "antd/es/upload";
  7. import { useCallback, useState } from 'react';
  8. /** 监测结果管理 */
  9. const ExamResultList: React.FC<{ examPlanId: number, semesterId: number }> = ({ examPlanId, semesterId }) => {
  10. const { token } = theme.useToken();
  11. const { modal, message } = App.useApp();
  12. const [fileList, setFileList] = useState<UploadFile[]>([]);
  13. const [uploading, setUploading] = useState(false);
  14. const [uploadStatus, setUploadStatus] = useState<{ success: boolean; message: string }>();
  15. const uploadProps: UploadProps = {
  16. onRemove: () => {
  17. setFileList([]);
  18. },
  19. beforeUpload: (file) => {
  20. setFileList([file]);
  21. return false;
  22. },
  23. listType: 'picture',
  24. disabled: uploading,
  25. fileList,
  26. accept: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel',
  27. iconRender: () => <FileIcon type=".xlsx" />
  28. };
  29. const handleUploadImportWithoutStudentTotalScore = useCallback(async () => {
  30. const onConfirm = () => new Promise((resolve) => {
  31. modal.confirm({
  32. title: '导入提示',
  33. content: '初始成绩导入会删除当前计划内所有已存在学生和成绩数据,删除后不能恢复,确定开始导入吗?',
  34. okText: '确定',
  35. cancelText: '取消',
  36. centered: true,
  37. onOk: () => resolve(true),
  38. onCancel: () => resolve(false),
  39. });
  40. });
  41. const confirm = await onConfirm();
  42. if (!confirm) { return; }
  43. try {
  44. setUploading(true);
  45. const formData = new FormData();
  46. formData.append('examPlanId', `${examPlanId}`);
  47. formData.append('file', fileList[0] as RcFile);
  48. await ExamScoreImportController.uploadImportWithoutStudentTotalScore(formData, { timeout: 600000 });
  49. message.success('已导入');
  50. setFileList([]);
  51. setUploadStatus({ success: true, message: '导入成功' });
  52. }
  53. catch (ex) {
  54. setUploadStatus({ success: false, message: `导入失败(${JSON.stringify(ex)})` });
  55. }
  56. finally {
  57. setUploading(false);
  58. }
  59. }, [fileList]);
  60. if (semesterId < 20232) {
  61. return (
  62. <ProCard
  63. style={{ marginTop: token.margin }}
  64. title={<CardStepTitle>结果管理</CardStepTitle>}
  65. >
  66. <Typography.Paragraph>
  67. <Typography.Title level={5}>初始成绩导入</Typography.Title>
  68. <Typography.Text>适用于前未上报学生和抽样的批量学生成绩导入。</Typography.Text>
  69. </Typography.Paragraph>
  70. <Card>
  71. <Space direction="vertical" size="large" style={{ width: '100%' }}>
  72. <Upload {...uploadProps}>
  73. <Button icon={<UploadOutlined />} disabled={uploading}>选择文件...</Button>
  74. </Upload>
  75. <Space>
  76. <Button
  77. type="primary"
  78. disabled={fileList.length === 0}
  79. loading={uploading}
  80. icon={<SendOutlined />}
  81. onClick={handleUploadImportWithoutStudentTotalScore}
  82. >{uploading ? '正在导入,请稍候' : '开始导入'}</Button>
  83. {uploadStatus && <Typography.Text type={uploadStatus.success ? 'success' : 'danger'}>{uploadStatus?.message}</Typography.Text>}
  84. </Space>
  85. </Space>
  86. </Card>
  87. </ProCard>
  88. );
  89. }
  90. return null;
  91. // return (
  92. // <ProCard
  93. // style={{ marginTop: token.margin }}
  94. // title={<CardStepTitle>结果管理</CardStepTitle>}
  95. // >
  96. // <Typography.Paragraph>
  97. // <Typography.Title level={5}>成绩导入</Typography.Title>
  98. // <Typography.Text>适用于前未上报学生和抽样的批量学生成绩导入。</Typography.Text>
  99. // </Typography.Paragraph>
  100. // <Card>
  101. // <Space direction="vertical" size="large" style={{ width: '100%' }}>
  102. // <Upload {...uploadProps}>
  103. // <Button icon={<UploadOutlined />} disabled={uploading}>选择文件...</Button>
  104. // </Upload>
  105. // <Space>
  106. // <Button
  107. // type="primary"
  108. // disabled={fileList.length === 0}
  109. // loading={uploading}
  110. // icon={<SendOutlined />}
  111. // onClick={handleUploadImportWithoutStudentTotalScore}
  112. // >{uploading ? '正在导入,请稍候' : '开始导入'}</Button>
  113. // {uploadStatus && <Typography.Text type={uploadStatus.success ? 'success' : 'danger'}>{uploadStatus?.message}</Typography.Text>}
  114. // </Space>
  115. // </Space>
  116. // </Card>
  117. // </ProCard>
  118. // );
  119. }
  120. export default ExamResultList;