diff --git a/packages/examination/src/api/examPaper/index.tsx b/packages/examination/src/api/examPaper/index.tsx index 1d247e3..53b50e3 100644 --- a/packages/examination/src/api/examPaper/index.tsx +++ b/packages/examination/src/api/examPaper/index.tsx @@ -51,6 +51,7 @@ export function getExamPaperDetail(id: any) { * 随机获取试题 */ export function getRandomQuestions(data: any) { + return axios({ url: "/ex/examPaper/getRandomQuestions", method: 'post', @@ -61,7 +62,10 @@ export function getRandomQuestions(data: any) { /** * 获取监管行业下的所有试题 */ -export function geIndustryQuestions(data: any) { +export function geIndustryQuestions(industryId: string) { + const data = { + industryId: industryId + }; return axios({ url: "/ex/examPaper/geIndustryQuestions", method: 'post', diff --git a/packages/examination/src/views/examPaper/examPaperAdd.tsx b/packages/examination/src/views/examPaper/examPaperAdd.tsx index 5b0c06f..ca83f9d 100644 --- a/packages/examination/src/views/examPaper/examPaperAdd.tsx +++ b/packages/examination/src/views/examPaper/examPaperAdd.tsx @@ -3,7 +3,6 @@ import { Form, Input, Button, Radio, Checkbox, Select, message, Modal, Table } f import { getRandomQuestions, addExamPaper, editExamPaper, getExamPaperDetail, geIndustryQuestions} from 'api/examPaper'; import { getList ,findIndustry} from 'api/question'; import { dictionary } from "../../api/dict"; -import TextArea from "antd/es/input/TextArea"; const { Option } = Select; @@ -14,6 +13,7 @@ interface optionsState { interface QuestionState { id: string; + industryId: string; serviceTypeId: string; questionTypes: string; questionContent: string; @@ -35,7 +35,9 @@ interface States { selectedParams: any; selectedQuestionList: QuestionState[]; questionSelectList: QuestionState[]; - selectedRowKeys: string[]; // 修改为存储 id 的数组 + selectedRowKeys: string[]; + skipValidation: boolean, + questionCountSum: number } class ExamPaperAdd extends Component { @@ -59,7 +61,9 @@ class ExamPaperAdd extends Component { selectedParams: {}, selectedQuestionList: [], questionSelectList: [], - selectedRowKeys: [] + selectedRowKeys: [], + skipValidation: false, + questionCountSum: 0 }; } @@ -101,10 +105,11 @@ class ExamPaperAdd extends Component { } // 试卷详情 - handleGetDetail = (id: any) => { - getExamPaperDetail(id).then((res: any) => { + handleGetDetail = async (id: any) => { + try { + const res = await getExamPaperDetail(id); if (res.data) { - const newQuestions: QuestionState[] = res.data.data.map((questionData: any) => { + const newQuestions = res.data.data.map((questionData: any) => { const options = questionData.options.split(','); const answerOptions = options.map((option: any) => { const [value, label] = option.split('.'); @@ -112,6 +117,7 @@ class ExamPaperAdd extends Component { }); return { id: String(questionData.id), + industryId: String(questionData.industryId), serviceTypeId: String(questionData.serviceTypeId), questionTypes: String(questionData.questionTypes), questionContent: String(questionData.questionContent), @@ -120,6 +126,7 @@ class ExamPaperAdd extends Component { }; }); this.setState({ questions: newQuestions }); + const formValues = {}; formValues['paperName'] = res.data.head.paperName; formValues['industryId'] = String(res.data.head.industryId); @@ -130,15 +137,29 @@ class ExamPaperAdd extends Component { formValues['paperContent'] = res.data.head.paperContent.trim() === '' ? '' : res.data.head.paperContent; this.formRef.current.setFieldsValue(formValues); } - }).catch(() => { + + let res1; + if (res.data?.head?.industryId) { + [res1] = await Promise.all([ + geIndustryQuestions(res.data.head.industryId) + ]); + if (res1.data) { + this.setState({ questionCountSum: res1.data }); + } else { + this.setState({ questionCountSum: 0 }); + } + } + } catch (error) { message.error('获取题目详情失败,请重试'); - }); + } }; // 随机生成 handleRandomQuestion = () => { + this.setState({ skipValidation: true }); this.formRef.current.validateFields(['industryId', 'questionCount']) .then((values: any) => { + this.setState({ skipValidation: false }); const data = { industryId: values.industryId, questionCount: values.questionCount @@ -159,6 +180,7 @@ class ExamPaperAdd extends Component { }); return { id: String(questionData.id), + industryId: String(questionData.industryId), serviceTypeId: String(questionData.serviceTypeId), questionTypes: String(questionData.questionTypes), questionContent: String(questionData.questionContent), @@ -176,7 +198,9 @@ class ExamPaperAdd extends Component { message.error('获取随机题目失败'); }); }) - .catch(() => { }); + .catch(() => { + this.setState({ skipValidation: false }); + }); }; // 手动选题 @@ -251,6 +275,7 @@ class ExamPaperAdd extends Component { }); return { id: String(questionData.id), + industryId: String(questionData.industryId), serviceTypeId: String(questionData.serviceTypeId), questionTypes: String(questionData.questionTypes), questionContent: String(questionData.questionContent), @@ -292,67 +317,42 @@ class ExamPaperAdd extends Component { }; // 保存试卷 - handleSaveExamPaper = () => { + handleSaveExamPaper = async () => { const { isEdit, id } = this.state; - this.formRef.current.validateFields() - .then((values: any) => { - const { questions } = this.state; - if (questions === null || questions.length === 0) { - message.error('请选择题目'); - return; + try { + const values = await this.formRef.current.validateFields(); + const { questions } = this.state; + if (questions === null || questions.length === 0) { + message.error('请选择题目'); + return; + } + const questionIds = questions.map(question => question.id); + const data = { + ...values, + id, + questionIds + }; + if (isEdit === 'true') { + const res = await editExamPaper(data); + const success = res["success"]; + if (success) { + message.success('试卷更新成功'); + this.props.history.push('/examPaperList'); + } else { + message.error('试卷更新失败'); } - const questionIds = questions.map(question => question.id); - const data = { - ...values, - id, - questionIds - }; - if (isEdit === 'true') { - editExamPaper(data) - .then((res) => { - const success = res["success"]; - if (success) { - message.success('试卷更新成功'); - this.props.history.push('/examPaperList'); - } else { - message.error('试卷更新失败'); - } - }) - .catch(() => { - message.error('试卷更新时出错,请稍后重试'); - }) + } else { + const res = await addExamPaper(data); + const success = res["success"]; + if (success) { + message.success('试卷保存成功'); + this.props.history.push('/examPaperList'); } else { - addExamPaper(data) - .then((res) => { - const success = res["success"]; - if (success) { - message.success('试卷保存成功'); - this.props.history.push('/examPaperList'); - } else { - message.error('试卷保存失败'); - } - }) - .catch(() => { - message.error('试卷保存时出错,请稍后重试'); - }) + message.error('试卷保存失败'); } - }); - }; - - // 题目数量 - compareQuestionCount = async (industryId: string, questionCount: number) => { - if (industryId === null || industryId === undefined || questionCount === null || questionCount === undefined) { - return true; - } - const data = { - industryId: industryId - }; - const res = await geIndustryQuestions(data); - if (res.data) { - const dbCount = res.data; - return questionCount <= dbCount; + } + } catch (error) { } - return true; }; render() { @@ -364,8 +364,11 @@ class ExamPaperAdd extends Component { isModalVisible, selectedQuestionList, questionSelectList, - selectedRowKeys + selectedRowKeys, + skipValidation, + questionCountSum } = this.state; + const changePage = (current: number, pageSize: number) => { setTimeout(() => { this.setState({ page: current, num: pageSize }); @@ -375,6 +378,76 @@ class ExamPaperAdd extends Component { const handleListQuestion = () => { this.props.history.push('/examPaperList'); }; + + // 监管行业cahnge + const handleIndustryChange = async (industryId: string) => { + let flag = true; + let dbCount = 0; + const errorMessages = []; + const { questionCount } = this.formRef.current.getFieldsValue(); + if (industryId) { + const res = await geIndustryQuestions(industryId); + if (res.data) { + dbCount = res.data; + } + this.setState({ questionCountSum: dbCount }); + + if (questionCount) { + if (questionCount > dbCount){ + errorMessages.push('输入的题目数量大于该监管行业可获取的题目数量'); + flag = false; + } + } + } + + const { questions } = this.state + if (questions && questions.length !== 0) { + if (Number(questionCount) !== questions.length) { + errorMessages.push('输入的题目数量和试题详情数量不一致'); + } + flag = false; + } + + if (flag){ + this.formRef.current.setFields([{ + name: 'questionCount', + errors: [] + }]); + } else { + this.formRef.current.setFields([{ + name: 'questionCount', + errors: errorMessages + }]); + } + }; + + // 数量cahnge + const handleQuestionCountChange = async () => { + let flag = true; + const errorMessages = []; + const { industryId } = this.formRef.current.getFieldsValue(); + const { questions } = this.state + if(industryId){ + if (questions && questions.length !== 0 ) { + if (industryId !== questions[0].industryId) { + errorMessages.push('监管行业ID和题目列表中的监管行业ID不一致'); + } + flag = false; + } + if (flag){ + this.formRef.current.setFields([{ + name: 'industryId', + errors: [] + }]); + } else { + this.formRef.current.setFields([{ + name: 'industryId', + errors: errorMessages + }]); + } + } + }; + const columns: any = [ { title: '序号', dataIndex: 'index', align: 'center', width: 60, render: (_: number, __: number, index: number) => index + 1 @@ -421,9 +494,33 @@ class ExamPaperAdd extends Component { { + if (!skipValidation) { + try { + const { questions } = this.state + if (questions && questions.length !== 0 ) { + if (value !== questions[0].industryId) { + return Promise.reject(new Error('监管行业ID和题目列表中的监管行业ID不一致')); + } + return Promise.resolve(); + } + } catch (error) { + return Promise.reject(new Error('校验监管行业ID时出错,请稍后重试')); + } + } + } + } + ]} > - {industryDict && industryDict.length > 0? ( (() => { let rows = []; @@ -449,17 +546,40 @@ class ExamPaperAdd extends Component { { message: '输入的题目数量大于该监管行业可获取的题目数量', validator: async (_, value) => { - const { industryId } = this.formRef.current.getFieldsValue(); - const isValid= await this.compareQuestionCount(industryId, value); - if (!isValid) { - return Promise.reject(new Error('输入的题目数量大于该监管行业可获取的题目数量')); + try { + const { industryId } = this.formRef.current.getFieldsValue(); + if (industryId) { + if (value > questionCountSum) { + return Promise.reject(new Error('输入的题目数量大于该监管行业可获取的题目数量')); + } + return Promise.resolve(); + } + } catch (error) { + return Promise.reject(new Error('校验题目数量时出错,请稍后重试')); + } + } + }, + { + message: '输入的题目数量和试题详情数量不一致', + validator: async (_, value) => { + try { + if (!skipValidation) { + const { questions } = this.state + if (questions && questions.length !== 0 ) { + if (Number(value) !== questions.length) { + return Promise.reject(new Error('输入的题目数量和题目列表数量不一致')); + } + return Promise.resolve(); + } + } + } catch (error) { + return Promise.reject(new Error('校验题目数量时出错,请稍后重试')); } - return Promise.resolve(); } } ]} > - + { align: 'center', width: 450, render: (questionContent: any, record: any) => { - return `${questionContent} ${record.options}`; + const combinedContent = `${questionContent}\n${record.options}`; + return
{combinedContent}
; } }, {