import { DataTableColumn, FormInst, FormItemRule, FormRules, NButton, NCascader, NDataTable, NDrawer, NDrawerContent, NForm, NFormItem, NIcon, NInput, NInputNumber, NModal, NPageHeader, NSelect, NSpace, NSpin, NTabPane, NTabs, useDialog, useMessage } from 'naive-ui' import { computed, defineComponent, onMounted, reactive, ref, watch } from 'vue' import { useTabsViewStore } from '@/store/modules/tabsView' import { useRoute, useRouter } from 'vue-router' import { getLessonType, lessonType } from '@/views/knowledge-manage/knowledgeTypeData' import AddQuestionList from '../model/addQuestionList' import { difficultyCoefficients, questionTypeCode } from '../question-bank' import styles from '../index.module.less' import { examinationKnowledgePointCategoryPage, unitExaminationDetail, unitExaminationRandomSave, unitExaminationSave } from '../../api' import { useUserStore } from '@/store/modules/user' import { DeleteColumnOutlined, DeleteFilled } from '@vicons/antd' import PreviewUnit from './previewUnit' import { filterPointCategory } from '..' type IUnitype = 'normal' | 'random' interface IQuestion { /**分值 */ score: null | any /**题目类型多个逗号分割 */ questionTypeCodes: string /**难度,可用值:ONE,TWO,THREE */ difficultyCoefficient: string /** 条件名称(例如考点一) */ name: string /**题目数量 */ questionNum: null | any /** */ maxScore: number /**考点编号 */ categoryId: null | any /**课程类型 */ courseTypeCode: string } function createUnitItem(): IQuestion { return { score: null, //分值 questionTypeCodes: '', //题目类型多个逗号分割 difficultyCoefficient: '', // 难度,可用值:ONE,TWO,THREE name: '', // 条件名称(例如考点一) questionNum: null, //题目数量 maxScore: 0, categoryId: null, // 考点编号 courseTypeCode: '' // 课程类型 } } export default defineComponent({ name: 'unit-test-index-editAndUpdate', emits: ['handleSuccess'], setup(props, { emit }) { const dialog = useDialog() const route = useRoute() const query = route.query const router = useRouter() /**题目类型 */ const questions = Object.entries(questionTypeCode).map(([value, label]) => ({ label, value })) const tabsViewStore = useTabsViewStore() const gotoBack = () => { tabsViewStore.closeCurrentTab(route) router.push({ path: '/educationalManage/unitExamination' }) } /**获取考点类型 */ const categorys = ref([]) const getType = async () => { try { const res: any = await examinationKnowledgePointCategoryPage({ page: 1, rows: 1000 }) if (Array.isArray(res?.data?.rows)) { categorys.value = filterPointCategory(res.data.rows, 'children') } } catch (error) {} } const message = useMessage() const loading = ref(false) const formRef = ref(null) const userStore = useUserStore() const unitType = ref('normal') const saveModel = reactive({ operatorId: (userStore.getUserInfo as any)?.id || '', id: query.unitExaminationId || '', name: query.name || '', //测验名称 courseTypeCode: query.courseTypeCode || (null as any), // 课程类型 passScore: Number(query.passScore) || (null as any), // 达标分数 timeMinutes: Number(query.timeMinutes) || (null as any) // 测验时长(分钟 }) const pointRef = ref() const pointList = reactive({ questionList: [createUnitItem()] as IQuestion[] }) const questionListKey = 'questionList' const setPoint = () => { const questionListStr = localStorage.getItem(questionListKey) || '' try { pointList.questionList = JSON.parse(questionListStr) } catch (error) {} } setPoint() watch(pointList, () => { localStorage.setItem(questionListKey, JSON.stringify(pointList.questionList)) }) const rules: FormRules = { name: [{ required: true, message: '请填写类型名称', trigger: ['input', 'blur'] }], courseTypeCode: [{ required: true, message: '请选择课程类型', trigger: 'change' }], passScore: [ { type: 'number', required: true, message: '请填写达标分数', trigger: ['input', 'blur'] } ], timeMinutes: [ { type: 'number', required: true, message: '请填写测验时长', trigger: ['input', 'blur'] } ] } // 获取详情 const getDetail = async () => { if (!saveModel.id) return loading.value = true try { const res: any = await unitExaminationDetail(saveModel.id) if (Array.isArray(res?.data)) { res.data.forEach((n: any) => { let item = { ...n?.question, answers: n?.answers || [] } // console.log('🚀 ~ item', item) modalData.selectList.push(item) }) } } catch (error) {} loading.value = false } onMounted(() => { getType() getDetail() }) const submit = () => { // console.log(saveModel) formRef.value?.validate(async (err) => { if (!err) { if (!modalData.selectList.length) { message.error('请添加题目') return } if (saveModel.passScore > totalScore.value) { message.error('阶段自测的合格分数高于题目的总分值') return } const params: any = { ...saveModel, questionList: modalData.selectList } let res: any = null console.log(params) if (saveModel.id) { res = await unitExaminationSave(params) } else { res = await unitExaminationSave(params) } if (res?.code == 200) { message.success('保存成功') gotoBack() // emit('handleSuccess') } else { message.warning('保存失败') } } }) } const modalData = reactive({ open: false, selectList: [] as any[], checkList: [] as any[], previewOpen: false }) /**已选题目的总分数 */ const totalScore = computed(() => { return modalData.selectList.reduce((total: Number, n: any) => total + n.totalScore, 0) }) /** * 删除随机考点 */ const handleDeleteItem = (index: number) => { // if (pointList.questionList.length === 1) return message.error('最少保留一个考点') pointList.questionList.splice(index, 1) } const columns = (): DataTableColumn[] => { return [ { type: 'selection' }, { title: '题目名称', key: 'name', width: 230, render(row: any) { return (
{row.name}
) } }, { title: '题目类型', key: 'questionTypeCode', render(row: any) { return questionTypeCode[row.questionTypeCode] } }, { title: '考点', key: 'examinationKnowledgePointCategoryName' }, // { // title: '课程类型', // key: 'courseTypeCode', // render(row: any) { // return getLessonType(row.courseTypeCode) || '通用类型' // } // }, { title: '难度', key: 'difficultyCoefficient', render(row: any) { return ( difficultyCoefficients.find( (n: any) => n.value?.toLocaleUpperCase() === row.difficultyCoefficient )?.label || row.difficultyCoefficient ) } }, { title: '分值', key: 'totalScore' }, { title: '操作', key: 'action', width: 180, render(row: any, rowIndex: number) { return ( handleRowMove('up', rowIndex)} > 上移 handleRowMove('down', rowIndex)} > 下移 ) } } ] } /**删除题目 */ const handleDeleteQuestion = () => { dialog.warning({ title: '警告', content: '是否确认删除选中的题目?', positiveText: '确定', negativeText: '取消', onPositiveClick: async () => { modalData.selectList = modalData.selectList.filter( (n: any) => !modalData.checkList.includes(n.id) ) } }) } /**表格行移动 */ const handleRowMove = (type: 'up' | 'down', index: number) => { if (type === 'up') { modalData.selectList[index] = modalData.selectList.splice( index - 1, 1, modalData.selectList[index] )[0] } else { modalData.selectList[index] = modalData.selectList.splice( index + 1, 1, modalData.selectList[index] )[0] } } const courseTypeCodeRef = ref() /** 随机生成题目 */ const checkRamdom = () => { // courseTypeCodeRef.value.validate() // if (!saveModel.courseTypeCode) { // message.error('请选择课程类型') // return // } if (!pointList.questionList.length) { message.error('请先添加考点') return } if (modalData.selectList.length) { dialog.warning({ title: '警告', content: '已有选择的题目,重新生成会清空当前已选的题目,是否继续?', positiveText: '继续', negativeText: '取消', onPositiveClick: async () => { createUnitTest() } }) return } createUnitTest() } const createUnitTest = async () => { modalData.selectList = [] pointRef.value?.validate(async (err) => { if (!err) { const params: any = pointList.questionList.map((n: any, i: number) => { return { ...n, name: `考点${i + 1}`, questionTypeCodes: n.questionTypeCodes?.join(',') || '' } }) let res: any = await unitExaminationRandomSave(params) if (Array.isArray(res?.data)) { modalData.selectList = res.data message.success('随机生成题目成功') } } }) } return () => (
gotoBack()} title={query.name ? (query.name as any) : '新增阶段自测'} >
{{ suffix: () => '分' }} {{ suffix: () => '分钟' }}
{pointList.questionList.map((item: IQuestion, index: number) => { return (
{{ default: () => ( ), label: () => (

* 考点{index + 1}

) }}
{{ default: () => ( {{ suffix: () => '题' }} ), label: () => (

* 题目数量

) }}
{{ default: () => ( ), label: () => (

* 难度

) }}
{{ default: () => ( {{ suffix: () => '分' }} ), label: () => (

* 总分值

) }}
{{ default: () => ( ), label: () => (

* 题型

) }}
handleDeleteItem(index)} > }>
) })}
{ pointList.questionList.push(createUnitItem()) }} > + 添加考点 {modalData.selectList.length ? '根据考点重新生成题目' : '根据考点生成测验题目'} (modalData.open = true)} disabled={query.isLock ? true : false} > 手动选择题目添加
已选题目列表
{modalData.selectList.length} 道题目
总分: {totalScore.value}
批量删除
row.id} onUpdateCheckedRowKeys={(list) => (modalData.checkList = list)} >
{modalData.selectList.length ? ( (modalData.previewOpen = true)}>预览题目 ) : null} 保存测验
{ // console.log(row) if (list) { modalData.selectList = modalData.selectList.concat(list) } modalData.open = false }} />
) } })