From 4cd1cbaace5ca4629e7664b0c3dac113150bae1a Mon Sep 17 00:00:00 2001 From: xiaoCJ <406612557@qq.com> Date: Mon, 15 Jul 2024 14:56:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EAPI=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/TopicResourceController.java | 20 +- .../controller/api/ObjectiveApi.java | 255 +++++++++++++++++- .../mapper/SysObjectiveQuestionsMapper.java | 19 +- .../mapper/SysTopicAndCourseMapper.java | 7 +- .../mapper/SysObjectiveQuestionsMapper.xml | 15 +- .../mapper/SysTopicAndCourseMapper.xml | 12 + 6 files changed, 312 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/sztzjy/resource_center/controller/TopicResourceController.java b/src/main/java/com/sztzjy/resource_center/controller/TopicResourceController.java index e60b8c5..182a2e5 100644 --- a/src/main/java/com/sztzjy/resource_center/controller/TopicResourceController.java +++ b/src/main/java/com/sztzjy/resource_center/controller/TopicResourceController.java @@ -193,12 +193,21 @@ public class TopicResourceController { @RequestParam(required = false) String type, @ApiParam("题干") @RequestParam(required = false) String content) { PageHelper.startPage(index, size); - List list = sysObjectiveQuestionMapper.getTopicByConfig(oneID, twoID, threeID, type, content); + List list = sysObjectiveQuestionMapper.getTopicByConfig(oneID, twoID, threeID, type, null, content); PageInfo pageInfo = new PageInfo(list); return new ResultEntity>(pageInfo); } + @AnonymousAccess + @ApiOperation("客观题单个详情查看") + @PostMapping("getById") + private ResultEntity getById(@RequestParam String id) { + SysObjectiveQuestions sysObjectiveQuestions = sysObjectiveQuestionMapper.selectByPrimaryKey(id); + return new ResultEntity<>(sysObjectiveQuestions); + } + + //老师新增的题目正在使用的话资源中心只能查看,不能编辑和删除, // 如果老师自己删除了这个题目,资源中心题库管理也可以编辑和删除。如果题目正在被使用,则删除的时候提示:题目被xx课程xx章节使用,不能删除。 @ApiOperation("编辑") @@ -249,6 +258,15 @@ public class TopicResourceController { } + @AnonymousAccess + @ApiOperation("批量删除题目绑定关系") + @PostMapping("deleteTopicBindingByList") + private ResultEntity deleteTopicBindingByList(@RequestParam("传topicId和三级ID") @RequestBody List sysTopicAndCourses) { + sysTopicAndCourseMapper.batchDelete(sysTopicAndCourses); + return new ResultEntity<>(HttpStatus.OK, "成功删除绑定关系!"); + } + + //老师新增的题目正在使用的话资源中心只能查看,不能编辑和删除, // 如果老师自己删除了这个题目,资源中心题库管理也可以编辑和删除。如果题目正在被使用,则删除的时候提示:题目被xx课程xx章节使用,不能删除。 @ApiOperation("删除") diff --git a/src/main/java/com/sztzjy/resource_center/controller/api/ObjectiveApi.java b/src/main/java/com/sztzjy/resource_center/controller/api/ObjectiveApi.java index 6b5bba8..ea7d632 100644 --- a/src/main/java/com/sztzjy/resource_center/controller/api/ObjectiveApi.java +++ b/src/main/java/com/sztzjy/resource_center/controller/api/ObjectiveApi.java @@ -1,19 +1,127 @@ package com.sztzjy.resource_center.controller.api; +import cn.hutool.core.util.IdUtil; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.sztzjy.resource_center.annotation.AnonymousAccess; +import com.sztzjy.resource_center.entity.*; +import com.sztzjy.resource_center.entity.dto.SysObjectiveQuestionsDto; +import com.sztzjy.resource_center.mapper.SysObjectiveQuestionsMapper; +import com.sztzjy.resource_center.mapper.SysOneCatalogMapper; +import com.sztzjy.resource_center.mapper.SysTopicAndCourseMapper; +import com.sztzjy.resource_center.util.ResultEntity; +import com.sztzjy.resource_center.util.file.IFileUtil; import io.swagger.annotations.Api; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.ss.usermodel.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; + +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.math.BigDecimal; +import java.util.*; @RestController @Api(tags = "课程方面API") @RequestMapping("api/sys/ObjectiveApi") public class ObjectiveApi { + @Autowired + private SysObjectiveQuestionsMapper sysObjectiveQuestionMapper; + @Autowired + private SysTopicAndCourseMapper sysTopicAndCourseMapper; + @Autowired + SysOneCatalogMapper oneCatalogMapper; + @Autowired + IFileUtil fileUtil; + @Value("${file.path}") + private String filePath; + + + private SysOneCatalog getSysOneCatalogs(String systemOwner) { + SysOneCatalogExample example = new SysOneCatalogExample(); + example.createCriteria().andOneNameEqualTo(systemOwner); + List sysOneCatalogs = oneCatalogMapper.selectByExample(example); + return sysOneCatalogs.get(0); + } + + /** * 客观题新增 * 方法名:insertObjective - * 参数:SysObjectiveQuestions systemOwner schoolId + * 参数:SysObjectiveQuestions systemOwner * return: boolean */ + @AnonymousAccess + @ApiOperation("新增单个试题") //新增 题干不允许重复 + @PostMapping("addTopic") + private Boolean addTopic(@RequestBody SysObjectiveQuestionsDto objectiveQuestion) { + if (StringUtils.isBlank(objectiveQuestion.getContent())) { + return false; + } + if (StringUtils.isBlank(objectiveQuestion.getAnswer())) { + return false; + } + if (objectiveQuestion.getScore() == null) { + return false; + } + if (StringUtils.isBlank(objectiveQuestion.getType())) { + return false; + } + if (StringUtils.isBlank(objectiveQuestion.getObjectiveId())) { //子系统的oneName放在这个字段传过来 + return false; + } + if (objectiveQuestion.getType().equals("1") || objectiveQuestion.getType().equals("0")) { + if (StringUtils.isBlank(objectiveQuestion.getQuestionA()) || StringUtils.isBlank(objectiveQuestion.getQuestionB()) + || StringUtils.isBlank(objectiveQuestion.getQuestionC()) || StringUtils.isBlank(objectiveQuestion.getQuestionD())) + return false; + } + if (objectiveQuestion.getType().equals("2")) { + if (StringUtils.isBlank(objectiveQuestion.getQuestionA()) || StringUtils.isBlank(objectiveQuestion.getQuestionB())) + return false; + } + + SysOneCatalog sysOneCatalogs = getSysOneCatalogs(objectiveQuestion.getObjectiveId()); + String oneId = sysOneCatalogs.getOneId(); + SysObjectiveQuestionsExample example = new SysObjectiveQuestionsExample(); + example.createCriteria().andContentEqualTo(objectiveQuestion.getContent()); + List sysObjectiveQuestions = sysObjectiveQuestionMapper.selectByExample(example); + if (!sysObjectiveQuestions.isEmpty()) { + return false; + } + //新增题库表 + String uuid = IdUtil.randomUUID(); + objectiveQuestion.setObjectiveId(uuid); + objectiveQuestion.setCreateTime(new Date()); + objectiveQuestion.setIsDelete(false); + objectiveQuestion.setOneID(oneId); + sysObjectiveQuestionMapper.insert(objectiveQuestion); + + //新增题库关系表 + SysTopicAndCourse sysTopicAndCourse = new SysTopicAndCourse(); + sysTopicAndCourse.setId(IdUtil.randomUUID()); + sysTopicAndCourse.setTopicId(uuid); + sysTopicAndCourse.setTopicType(objectiveQuestion.getType()); + sysTopicAndCourse.setOneId(oneId); + sysTopicAndCourse.setOneName(objectiveQuestion.getObjectiveId()); //子系统的oneName放在这个字段传过来 + if (StringUtils.isNotBlank(objectiveQuestion.getTwoID())) { + sysTopicAndCourse.setTwoId(objectiveQuestion.getTwoID()); + sysTopicAndCourse.setTwoName(objectiveQuestion.getTwoName()); + } + if (StringUtils.isNotBlank(objectiveQuestion.getThreeID())) { + sysTopicAndCourse.setThreeId(objectiveQuestion.getThreeID()); + sysTopicAndCourse.setThreeName(objectiveQuestion.getThreeName()); + } + sysTopicAndCourseMapper.insert(sysTopicAndCourse); + return true; + } + /** * 客观题批量删除 @@ -21,6 +129,30 @@ public class ObjectiveApi { * 参数:List objIdList schoolId * return: boolean */ + @AnonymousAccess + @ApiOperation("客观题批量删除") + @PostMapping("batchDeleteObjective") + private Boolean batchDeleteObjective(@RequestBody List objIdList, + @RequestParam String schoolId) { + SysObjectiveQuestionsExample example = new SysObjectiveQuestionsExample(); + example.createCriteria().andObjectiveIdIn(objIdList); + List sysObjectiveQuestions = sysObjectiveQuestionMapper.selectByExample(example); + + List ids = new ArrayList<>(); + for (SysObjectiveQuestions sysObjectiveQuestion : sysObjectiveQuestions) { + if (sysObjectiveQuestion.getSource().equals(schoolId)) { + continue; + } else { + ids.add(sysObjectiveQuestion.getObjectiveId()); + } + } + try { + sysObjectiveQuestionMapper.batchDelete(ids); + return true; + } catch (Exception e) { + return false; + } + } /** * 客观题列表查看 @@ -28,6 +160,21 @@ public class ObjectiveApi { * 参数:schoolId courseId type content index size * return:PageInfo */ + @AnonymousAccess + @ApiOperation("课程配置/题目条件查询") + @PostMapping("getTopicByConfig") //todo 关于老师删除的题库如何查询 + private PageInfo getTopicByConfig(@RequestParam Integer index, + @RequestParam Integer size, + @RequestParam(required = false) String courseId, + @RequestParam(required = false) String threeID, + @RequestParam(required = false) String type, + @RequestParam String schoolId, + @ApiParam("题干") @RequestParam(required = false) String content) { + PageHelper.startPage(index, size); + List list = sysObjectiveQuestionMapper.getTopicByConfig(null, courseId, threeID, type, schoolId, content); + PageInfo pageInfo = new PageInfo(list); + return (pageInfo); + } /** * 客观题单个详情查看 @@ -35,6 +182,13 @@ public class ObjectiveApi { * 参数:objectiveId * return:SysObjectiveQuestions */ + @AnonymousAccess + @ApiOperation("客观题单个详情查看") + @PostMapping("selectObjectiveDetails") + private SysObjectiveQuestions getById(@RequestParam String objectiveId) { + SysObjectiveQuestions sysObjectiveQuestions = sysObjectiveQuestionMapper.selectByPrimaryKey(objectiveId); + return sysObjectiveQuestions; + } /** * 客观题导入模板下载 @@ -42,11 +196,104 @@ public class ObjectiveApi { * 参数:response * return:void */ + @GetMapping("downloadResource") + @ApiOperation("客观题导入模板下载") + @AnonymousAccess + public void downloadResource(HttpServletResponse response) { + fileUtil.download(response, "客观题导入模板.xlsx", filePath); + } /** * 客观题批量导入 * 方法名:insertObjectiveByExcel * 参数:MultipartFile schoolId - * return:void + * return:Boolean */ + @AnonymousAccess + @ApiOperation("本地excel导入试题") + @PostMapping("importTopicByLocal") + private Boolean importTopicByLocal(@RequestParam MultipartFile file, + @RequestParam String schoolId) throws IOException, InvalidFormatException { + if (file == null) { + return false; + } + return insertObjectiveByExcel(file, schoolId); + } + + //如果是内置增加 则需要提供课程和章节信息 + //如果是老师增加 则只需要提供课程信息 + public Boolean insertObjectiveByExcel(MultipartFile file, String schoolId) throws IOException, InvalidFormatException { + List objectiveQuestionList = new ArrayList<>(); + Workbook workbook = WorkbookFactory.create(file.getInputStream()); + Sheet sheet = workbook.getSheetAt(0); + + // 迭代每一行 + Iterator iterator = sheet.iterator(); + iterator.next(); + while (iterator.hasNext()) { + Row row = iterator.next(); + + /*解析列,下标从0开始*/ + Cell cell0 = row.getCell(0); + Cell cell1 = row.getCell(1); + Cell cell2 = row.getCell(2); + Cell cell3 = row.getCell(3); + Cell cell4 = row.getCell(4); + Cell cell5 = row.getCell(5); + Cell cell6 = row.getCell(6); + Cell cell7 = row.getCell(7); + Cell cell8 = row.getCell(8); + Cell cell9 = row.getCell(9); + //判断题的BCDE选项和解析可能为空 + if (cell0 == null || cell1 == null || cell2 == null || cell6 == null || cell7 == null || cell9 == null) { + return false; + } + + String content = this.getCellValue(cell0);//题干 + String a = this.getCellValue(cell1); //选项A + String b = this.getCellValue(cell2);//选项B + String type = this.getCellValue(cell6);//题型 + String answer = this.getCellValue(cell7);//答案 + String score = this.getCellValue(cell9);//分值 + String c = cell3 != null ? this.getCellValue(cell3) : null; + String d = cell4 != null ? this.getCellValue(cell4) : null; + String e = cell5 != null ? this.getCellValue(cell5) : null; + String analyze = cell8 != null ? this.getCellValue(cell8) : null; + + + SysObjectiveQuestions obj = new SysObjectiveQuestions(); + obj.setObjectiveId(String.valueOf(UUID.randomUUID())); + obj.setContent(content); + obj.setType(type); + obj.setSource(schoolId); + obj.setQuestionA(a); + obj.setQuestionB(b); + obj.setQuestionC(c); + obj.setQuestionD(d); + obj.setQuestionE(e); + obj.setAnswer(answer); + obj.setAnalysis(analyze); + obj.setScore(BigDecimal.valueOf(Integer.parseInt(score))); + obj.setCreateTime(new Date()); + obj.setIsDelete(false); + objectiveQuestionList.add(obj); + } + int result = sysObjectiveQuestionMapper.insertBatch(objectiveQuestionList); + if (result > 0) { + return true; + } else { + return false; + } + } + + + public String getCellValue(Cell cell) { + if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) { + return String.valueOf(cell.getBooleanCellValue()); + } else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { + Double d = cell.getNumericCellValue(); + return String.valueOf(d.intValue()); + } + return String.valueOf(cell.getStringCellValue()); + } } diff --git a/src/main/java/com/sztzjy/resource_center/mapper/SysObjectiveQuestionsMapper.java b/src/main/java/com/sztzjy/resource_center/mapper/SysObjectiveQuestionsMapper.java index b5f2d73..d04f3b0 100644 --- a/src/main/java/com/sztzjy/resource_center/mapper/SysObjectiveQuestionsMapper.java +++ b/src/main/java/com/sztzjy/resource_center/mapper/SysObjectiveQuestionsMapper.java @@ -2,11 +2,12 @@ package com.sztzjy.resource_center.mapper; import com.sztzjy.resource_center.entity.SysObjectiveQuestions; import com.sztzjy.resource_center.entity.SysObjectiveQuestionsExample; -import java.util.List; - import com.sztzjy.resource_center.entity.dto.SysObjectiveQuestionsDto; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; + +import java.util.List; + @Mapper public interface SysObjectiveQuestionsMapper { long countByExample(SysObjectiveQuestionsExample example); @@ -31,17 +32,21 @@ public interface SysObjectiveQuestionsMapper { int updateByPrimaryKey(SysObjectiveQuestions record); - List selectTopicByConditions(@Param("oneID")String oneID, + List selectTopicByConditions(@Param("oneID") String oneID, @Param("twoID") String twoID, @Param("threeID") String threeID, - @Param("type")String type, + @Param("type") String type, @Param("content") String content); int insertBatch(@Param("objectiveQuestionList") List objectiveQuestionList); - List getTopicByConfig(@Param("oneID")String oneID, + List getTopicByConfig(@Param("oneID") String oneID, @Param("twoID") String twoID, @Param("threeID") String threeID, - @Param("type")String type, - @Param("content") String content); + @Param("type") String type, + @Param("schoolId") String schoolId, + @Param("content") String content + ); + + void batchDelete(@Param("list") List ids); } \ No newline at end of file diff --git a/src/main/java/com/sztzjy/resource_center/mapper/SysTopicAndCourseMapper.java b/src/main/java/com/sztzjy/resource_center/mapper/SysTopicAndCourseMapper.java index 5ce7d83..6174ada 100644 --- a/src/main/java/com/sztzjy/resource_center/mapper/SysTopicAndCourseMapper.java +++ b/src/main/java/com/sztzjy/resource_center/mapper/SysTopicAndCourseMapper.java @@ -2,10 +2,11 @@ package com.sztzjy.resource_center.mapper; import com.sztzjy.resource_center.entity.SysTopicAndCourse; import com.sztzjy.resource_center.entity.SysTopicAndCourseExample; -import java.util.List; - import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; + +import java.util.List; + @Mapper public interface SysTopicAndCourseMapper { long countByExample(SysTopicAndCourseExample example); @@ -31,4 +32,6 @@ public interface SysTopicAndCourseMapper { int updateByPrimaryKey(SysTopicAndCourse record); void batchInsert(@Param("list") List list); + + void batchDelete(@Param("list") List sysTopicAndCourses); } \ No newline at end of file diff --git a/src/main/resources/mapper/SysObjectiveQuestionsMapper.xml b/src/main/resources/mapper/SysObjectiveQuestionsMapper.xml index da26988..79db0e1 100644 --- a/src/main/resources/mapper/SysObjectiveQuestionsMapper.xml +++ b/src/main/resources/mapper/SysObjectiveQuestionsMapper.xml @@ -401,7 +401,7 @@ SELECT/* sb.*,st.one_id,st.two_id,st.three_id,st.id,st.one_name,st.two_name,st.three_name*/ sb.*, st.* FROM sys_objective_questions sb - left JOIN sys_topic_and_course st + left JOIN sys_topic_and_course st on sb.objective_id = st.topic_id @@ -427,7 +427,7 @@ @@ -464,4 +467,12 @@ ) + + + DELETE FROM sys_objective_questions + WHERE objective_id IN + + #{id} + + \ No newline at end of file diff --git a/src/main/resources/mapper/SysTopicAndCourseMapper.xml b/src/main/resources/mapper/SysTopicAndCourseMapper.xml index 3fe8370..46ce5a4 100644 --- a/src/main/resources/mapper/SysTopicAndCourseMapper.xml +++ b/src/main/resources/mapper/SysTopicAndCourseMapper.xml @@ -295,4 +295,16 @@ ) + + + + DELETE FROM sys_topic_and_course + WHERE topic_id IN + + #{item.id} + AND one_id = #{item.oneId} + AND two_id = #{item.twoId} + AND three_id = #{item.threeId} + + \ No newline at end of file