项目实训第三周02:后端逻辑

编写试卷管理相关的数据库服务接口

	//获取试卷列表
	public List<Map<String, Object>> getPapersList();
	//获取课程选项
    public List<Map<String, Object>> getCourOptions();
    //查询试卷列表
    public List<Map<String, Object>> searchPapersList(String paperName, Integer langId, Integer paperType);
    //通过试卷id获取试卷分数
    public int selectPaperScoreByPaperId(Integer paperId);
    //删除试卷
    public int deletePaper(Integer paperId);
    //通过试卷id获取试卷问题详情
    public Map<String, Object> getPaperQueDetailByPaperId(Integer paperId, Integer totalNum);
    //随机组卷
    public int randomInsertPaperInfo(Map<String, Object> obj);
    //通过课程id获取试卷问题详情
    public Map<String, Object> getPaperQueDetailByCourId(Integer langId);
    //手动组卷
    public int fixedInsertPaperInfo(Map<String, Object> obj);

接口实现

这里只展示自己负责的部分功能
获取试卷列表:

	@Override
    public List<Map<String, Object>> getPapersList() {
        List<Map<String, Object>> resultList = new ArrayList<>();
        PaperExample paperExample = new PaperExample();
        paperExample.setOrderByClause("paper_id asc");
        List<Paper> paperList = paperMapper.selectByExample(paperExample);
        for (Paper paper : paperList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", paperList.indexOf(paper) + 1);
            map.put("paperId", paper.getPaperId());
            map.put("paperName", paper.getPaperName());

            map.put("langId", paper.getLangId());
            ProgramingLanguage programingLanguage = programingLanguageMapper.selectByPrimaryKey(paper.getLangId());
            map.put("langName", programingLanguage.getLangName());
            map.put("langImgSrc", programingLanguage.getLangImgSrc());

            map.put("paperDuration", paper.getPaperDuration());
            map.put("paperDifficulty", paper.getPaperDifficulty());
            map.put("paperAttention", paper.getPaperAttention());
            map.put("paperType", paper.getPaperType());
            map.put("singleScore", paper.getSingleScore());
            map.put("multipleScore", paper.getMultipleScore());
            map.put("judgeScore", paper.getJudgeScore());
            map.put("fillScore", paper.getFillScore());

            Map<String, Integer> queNum = studentHomeService.getPaperQueNumByPaperId(paper.getPaperId());
            int totalScore = queNum.get("singleNum")*paper.getSingleScore()
                    + queNum.get("multipleNum")*paper.getMultipleScore()
                    + queNum.get("judgeNum")*paper.getJudgeScore()
                    + queNum.get("fillNum")*paper.getFillScore();
            map.put("totalScore", totalScore);
            map.put("singleNum", queNum.get("singleNum"));
            map.put("multipleNum", queNum.get("multipleNum"));
            map.put("judgeNum", queNum.get("judgeNum"));
            map.put("fillNum", queNum.get("fillNum"));
            map.put("totalNum", queNum.get("totalNum"));

            map.put("participateNum", paper.getParticipateNum());
            map.put("paperCreateTime", paper.getPaperCreateTime());
            resultList.add(map);
        }
        return resultList;
    }

查询试卷列表:

	@Override
    public List<Map<String, Object>> searchPapersList(String paperName, Integer langId, Integer paperType) {
        List<Map<String, Object>> resultList = new ArrayList<>();
        PaperExample paperExample = new PaperExample();
        PaperExample.Criteria criteria = paperExample.createCriteria();
        if(!paperName.equals("undefined")) {
            criteria.andPaperNameLike("%" + paperName + "%");
        }
        if(langId != 0){
            criteria.andLangIdEqualTo(langId);
        }
        if(paperType != 0){
            criteria.andPaperTypeEqualTo(paperType);
        }
        paperExample.setOrderByClause("paper_id asc");
        List<Paper> paperList = paperMapper.selectByExample(paperExample);
        for (Paper paper : paperList) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", paperList.indexOf(paper) + 1);
            map.put("paperId", paper.getPaperId());
            map.put("paperName", paper.getPaperName());

            map.put("langId", paper.getLangId());
            ProgramingLanguage programingLanguage = programingLanguageMapper.selectByPrimaryKey(paper.getLangId());
            map.put("langName", programingLanguage.getLangName());
            map.put("langImgSrc", programingLanguage.getLangImgSrc());

            map.put("paperDuration", paper.getPaperDuration());
            map.put("paperDifficulty", paper.getPaperDifficulty());
            map.put("paperType", paper.getPaperType());
            map.put("paperAttention", paper.getPaperAttention());
            map.put("singleScore", paper.getSingleScore());
            map.put("multipleScore", paper.getMultipleScore());
            map.put("judgeScore", paper.getJudgeScore());
            map.put("fillScore", paper.getFillScore());

            Map<String, Integer> queNum = studentHomeService.getPaperQueNumByPaperId(paper.getPaperId());
            int totalScore = queNum.get("singleNum")*paper.getSingleScore()
                    + queNum.get("multipleNum")*paper.getMultipleScore()
                    + queNum.get("judgeNum")*paper.getJudgeScore()
                    + queNum.get("fillNum")*paper.getFillScore();
            map.put("totalScore", totalScore);
            map.put("singleNum", queNum.get("singleNum"));
            map.put("multipleNum", queNum.get("multipleNum"));
            map.put("judgeNum", queNum.get("judgeNum"));
            map.put("fillNum", queNum.get("fillNum"));
            map.put("totalNum", queNum.get("totalNum"));

            map.put("participateNum", paper.getParticipateNum());
            map.put("paperCreateTime", paper.getPaperCreateTime());
            resultList.add(map);
        }
        return resultList;
    }

随机组卷:

	@Override
    public int randomInsertPaperInfo(Map<String, Object> obj) {
        String paperName = (String) obj.get("paperName");
        Integer paperDuration = (Integer) obj.get("paperDuration");
        Integer paperDifficulty = (Integer) obj.get("paperDifficulty");
        String paperAttention = (String) obj.get("paperAttention");
        Integer singleScore = (Integer) obj.get("singleScore");
        Integer singleNum = (Integer) obj.get("singleNum");
        Integer multipleScore = (Integer) obj.get("multipleScore");
        Integer multipleNum = (Integer) obj.get("multipleNum");
        Integer judgeScore = (Integer) obj.get("judgeScore");
        Integer judgeNum = (Integer) obj.get("judgeNum");
        Integer fillScore = (Integer) obj.get("fillScore");
        Integer fillNum = (Integer) obj.get("fillNum");
        Integer langId = (Integer) obj.get("langId");
        Paper paper = new Paper();
        paper.setPaperName(paperName);
        paper.setPaperDuration(paperDuration);
        paper.setPaperDifficulty(paperDifficulty);
        paper.setPaperAttention(paperAttention);
        paper.setSingleScore(singleScore);
        paper.setMultipleScore(multipleScore);
        paper.setJudgeScore(judgeScore);
        paper.setFillScore(fillScore);
        paper.setLangId(langId);
        paper.setPaperCreateTime(new Date());
        paper.setPaperType(1);
        int paperId = paperMapper.selectMaxPaperId() + 1;
        paper.setPaperId(paperId);

        //  插入试卷信息到paper表
        int result = paperMapper.insertSelective(paper);

        //  插入试卷问题信息到paper_que表
        //  单选题
        List<BankSingleChoiceQue> bankSingleChoiceQueList = bankSingleChoiceQueMapper.getRandomSingleByCountAndLangId(langId, singleNum);
        for (BankSingleChoiceQue bankSingleChoiceQue : bankSingleChoiceQueList) {
            int singleId = bankSingleChoiceQue.getSingleId();
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(1);
            paperQue.setSingleId(singleId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            bankSingleChoiceQue.setComposeFlag("1");
            bankSingleChoiceQueMapper.updateByPrimaryKeySelective(bankSingleChoiceQue);
        }

        //  多选题
        List<BankMultipleChoiceQue> bankMultipleChoiceQueList = bankMultipleChoiceQueMapper.getRandomMultipleByCountAndLangId(langId, multipleNum);
        for (BankMultipleChoiceQue bankMultipleChoiceQue : bankMultipleChoiceQueList) {
            int multipleId = bankMultipleChoiceQue.getMultipleId();
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(2);
            paperQue.setMultipleId(multipleId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            bankMultipleChoiceQue.setComposeFlag("1");
            bankMultipleChoiceQueMapper.updateByPrimaryKeySelective(bankMultipleChoiceQue);
        }

        //  判断题
        List<BankJudgeQue> bankJudgeQueList = bankJudgeQueMapper.getRandomJudgeByCountAndLangId(langId, judgeNum);
        for (BankJudgeQue bankJudgeQue : bankJudgeQueList) {
            int judgeId = bankJudgeQue.getJudgeId();
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(3);
            paperQue.setJudgeId(judgeId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            bankJudgeQue.setComposeFlag("1");
            bankJudgeQueMapper.updateByPrimaryKeySelective(bankJudgeQue);
        }

        //  填空题
        List<BankFillQue> bankFillQueList = bankFillQueMapper.getRandomFillByCountAndLangId(langId, fillNum);
        for (BankFillQue bankFillQue : bankFillQueList) {
            int fillId = bankFillQue.getFillId();
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(4);
            paperQue.setFillId(fillId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            bankFillQue.setComposeFlag("1");
            bankFillQueMapper.updateByPrimaryKeySelective(bankFillQue);
        }

        return result;
    }

手动组卷:

@Override
    public int fixedInsertPaperInfo(Map<String, Object> obj) {
        String paperName = (String) obj.get("paperName");
        Integer paperDuration = (Integer) obj.get("paperDuration");
        Integer paperDifficulty = (Integer) obj.get("paperDifficulty");
        String paperAttention = (String) obj.get("paperAttention");
        Integer singleScore = (Integer) obj.get("singleScore");
        List<Integer> singleNum = (List<Integer>) obj.get("singleNum");
        Integer multipleScore = (Integer) obj.get("multipleScore");
        List<Integer> multipleNum = (List<Integer>) obj.get("multipleNum");
        Integer judgeScore = (Integer) obj.get("judgeScore");
        List<Integer> judgeNum = (List<Integer>) obj.get("judgeNum");
        Integer fillScore = (Integer) obj.get("fillScore");
        List<Integer> fillNum = (List<Integer>) obj.get("fillNum");
        Integer langId = (Integer) obj.get("langId");
        Paper paper = new Paper();
        paper.setPaperName(paperName);
        paper.setPaperDuration(paperDuration);
        paper.setPaperDifficulty(paperDifficulty);
        paper.setPaperAttention(paperAttention);
        paper.setSingleScore(singleScore);
        paper.setMultipleScore(multipleScore);
        paper.setJudgeScore(judgeScore);
        paper.setFillScore(fillScore);
        paper.setLangId(langId);
        paper.setPaperCreateTime(new Date());
        paper.setPaperType(2);
        int paperId = paperMapper.selectMaxPaperId() + 1;
        paper.setPaperId(paperId);

        //  插入试卷信息到paper表
        int result = paperMapper.insertSelective(paper);

        //单选题
        for (Integer singleId : singleNum) {
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(1);
            paperQue.setSingleId(singleId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            BankSingleChoiceQue bankSingleChoiceQue = new BankSingleChoiceQue();
            bankSingleChoiceQue.setSingleId(singleId);
            bankSingleChoiceQue.setComposeFlag("1");
            bankSingleChoiceQueMapper.updateByPrimaryKeySelective(bankSingleChoiceQue);
        }
        //多选题
        for (Integer multipleId : multipleNum) {
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(2);
            paperQue.setMultipleId(multipleId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            BankMultipleChoiceQue bankMultipleChoiceQue = new BankMultipleChoiceQue();
            bankMultipleChoiceQue.setMultipleId(multipleId);
            bankMultipleChoiceQue.setComposeFlag("1");
            bankMultipleChoiceQueMapper.updateByPrimaryKeySelective(bankMultipleChoiceQue);
        }
        //判断题
        for (Integer judgeId : judgeNum) {
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(3);
            paperQue.setJudgeId(judgeId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            BankJudgeQue bankJudgeQue = new BankJudgeQue();
            bankJudgeQue.setJudgeId(judgeId);
            bankJudgeQue.setComposeFlag("1");
            bankJudgeQueMapper.updateByPrimaryKeySelective(bankJudgeQue);
        }
        //填空题
        for (Integer fillId : fillNum) {
            PaperQue paperQue = new PaperQue();
            paperQue.setQueType(4);
            paperQue.setFillId(fillId);
            paperQue.setPaperId(paperId);
            paperQueMapper.insertSelective(paperQue);
            //更新compose_flag字段
            BankFillQue bankFillQue = new BankFillQue();
            bankFillQue.setFillId(fillId);
            bankFillQue.setComposeFlag("1");
            bankFillQueMapper.updateByPrimaryKeySelective(bankFillQue);
        }

        return result;
    }

TeacherPaperController类实现

主要展示负责部分的代码

//    获取搜索试卷列表信息
    @RequestMapping("/searchPapersList")
    public ServerResponse searchPapersList(@RequestParam("paperName")String paperName,
                                           @RequestParam("langId")Integer langId,
                                           @RequestParam("paperType")Integer paperType){

        List<Map<String, Object>> resultList = teacherPaperService.searchPapersList(paperName, langId, paperType);
        return ServerResponse.createBySuccess("获取搜索试卷信息成功",resultList);
    }
//    获取成绩列表信息
    @RequestMapping("/getTeacherPapersList")
    public ServerResponse getTeacherPapersList(){
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> papersList = teacherPaperService.getPapersList();
        map.put("papersList",papersList);
        List<Map<String, Object>> langOptions = teacherPaperService.getLangOptions();
        map.put("langOptions",langOptions);
        return ServerResponse.createBySuccess("获取全部试卷信息成功",map);
    }
//  删除选中试卷信息
    @RequestMapping(value = "/deletePaper",method = RequestMethod.POST)
    public ServerResponse deletePaper(@RequestBody Map<String, Object> obj){
        Integer paperId = (Integer) obj.get("paperId");
        int selectResult = teacherPaperService.selectPaperScoreByPaperId(paperId);
        if(selectResult > 0) {
            return ServerResponse.createByError("该试卷已有学生参加考试,删除失败");
        } else {
            int deleteResult = teacherPaperService.deletePaper(paperId);
            if(deleteResult > 0){
                return ServerResponse.createBySuccess("删除成功",null);
            } else {
                return ServerResponse.createByError("数据库错误,删除失败");
            }
        }
    }
//  请求随机组卷,插入试卷数据,即发布试卷
    @RequestMapping(value = "/randomInsertPaperInfo",method = RequestMethod.POST)
    public ServerResponse randomInsertPaperInfo(@RequestBody Map<String, Object> obj){
        int langId = (int) obj.get("langId");
        if(langId > 1) {
            return ServerResponse.createByError("且慢,该科目题库不足,目前只有java科目题库比较充沛");
        } else {
            int result = teacherPaperService.randomInsertPaperInfo(obj);
            if (result >0) {
                return ServerResponse.createBySuccess("试卷发布成功",result);
            } else {
                return ServerResponse.createByError("数据库错误,试卷发布失败");
            }
        }
    }
//  请求固定组卷,插入试卷数据,即发布试卷
    @RequestMapping(value = "/fixedInsertPaperInfo",method = RequestMethod.POST)
    public ServerResponse fixedInsertPaperInfo(@RequestBody Map<String, Object> obj){
        int result = teacherPaperService.fixedInsertPaperInfo(obj);
        if (result >0) {
            return ServerResponse.createBySuccess("试卷发布成功",result);
        } else {
            return ServerResponse.createByError("数据库错误,试卷发布失败");
        }
    }
上一篇:[PHP]代码执行和生命周期


下一篇:读取数据库内容,并在页面渲染