linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
src/main/java/com/ots/project/exam/controller/TQuestionController.java
@@ -1,4 +1,5 @@
package com.ots.project.exam.controller;
import com.alibaba.fastjson.JSONObject;
import com.ots.common.utils.MessageUtils;
import com.ots.common.utils.StringUtils;
@@ -33,35 +34,53 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import static com.ots.common.enums.ExamPaperQuestion.permanentIdDuplicate;
/**
 * 题目列表Controller
 *
 * @author Shawn
 * @date 2019-11-20
 */
@Controller
@RequestMapping("/exam/question")
public class TQuestionController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(TQuestionController.class);
    private String prefix = "exam/question";
    @Autowired
    private ITQuestionService tQuestionService;
    @Autowired
    private ITSubjectService subjectService;
    @Autowired
    private ITExamLevelService itExamLevelService;
    @Autowired
    private IDictDataService dictDataService;
    @Autowired
    private DictService dictService;
    @RequiresPermissions("exam:question:view")
    @GetMapping()
    public String question() {
        return prefix + "/question";
    }
    
    /**
     * 查询题目列表列表
     */
    @RequiresPermissions("exam:question:list")
    @PostMapping("/list")
    @ResponseBody
@@ -71,6 +90,10 @@
        return getDataTable(list);
    }
    
    /**
     * 专门为product的add_question.html 页面提供查询
     */
    @RequiresPermissions("exam:question:list")
    @PostMapping("/listForProductAddQuestion")
    @ResponseBody
@@ -83,19 +106,29 @@
        return getDataTable(list);
    }
    
    /**
     * 新增题目列表
     * 参数 id是 t_subject 表的id
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        // 这句代码为了提示传过来的id是levelid
        /*
         * 去掉题目维度t_subject后只查询t_exam_level 表,得到字典里面的3类问卷模板,分别跳转到3个不同的模板页面,分别是
         * add-job.html,add-personality.html,add-intelligence.html
         */
        // 这句代码为了提示传过来的id是levelId
        Long levelId = id;
        TExamLevel tExamLevel = itExamLevelService.selectTExamLevelById(levelId);
        // 语言选项显示当前国际化的那种
        mmap.put("tExamLevel", tExamLevel);
        mmap.put("langType", dictService.getLangType(LocaleContextHolder.getLocale().toString()));
        return prefix + "/add-" + tExamLevel.getQuestionTemplateId();
    }
    
    /**
     * 新增保存题目列表
     */
    @RequiresPermissions("exam:question:add")
    @Log(title = "题目列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
@@ -108,6 +141,9 @@
        return toAjax(resultNum);
    }
    
    /**
     * 修改题目列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TQuestion tQuestion = tQuestionService.selectTQuestionById(id);
@@ -117,35 +153,45 @@
        tQuestion.setLangName(lanTypeMap.get(tQuestion.getLangType()));
        return prefix + "/edit-" + tExamLevel.getQuestionTemplateId();
    }
    private TExamLevel getEditQuestion(ModelMap mmap, TQuestion tQuestion) {
        QuestionObject questionObject = JSONObject.parseObject(tQuestion.getContent(), QuestionObject.class);
        if (ExamUtil.isListNotEmpty(questionObject.getQuestionItemObjects())) {
            setChioceAndSelectInput(tQuestion, questionObject);
        }
        tQuestion.setAnalyze(questionObject.getAnalyze());
        
        tQuestion.setAnalyze(questionObject.getAnalyze());
        // 多语言版本
        Map<String, String> titleMap = JsonUtil.toJsonObject(questionObject.getTitleContent(), HashMap.class);
        tQuestion.setTitle(titleMap.get(tQuestion.getLangType()));
        TExamLevel tExamLevel = itExamLevelService.selectTExamLevelById(tQuestion.getLevelId());
        mmap.put("tExamLevel", tExamLevel);
        mmap.put("tQuestion", tQuestion);
        return tExamLevel;
    }
    private void setChioceAndSelectInput(TQuestion tQuestion, QuestionObject questionObject) {
        
    private void setChioceAndSelectInput(TQuestion tQuestion, QuestionObject questionObject) {
        //选项的第一个输入框的内容
        List<String> choiceInputList = questionObject.getQuestionItemObjects().stream().map(i -> {
            return i.getPrefix();
        }).collect(Collectors.toList());
        
        //选项的第二个输入框的内容
        List<String> selectInput = questionObject.getQuestionItemObjects().stream().map(i -> {
            // 多语言版本展示
            Map<String, String> content = JsonUtil.toJsonObject(i.getContent(), HashMap.class);
            return content.get(tQuestion.getLangType());
        }).collect(Collectors.toList());
        tQuestion.setChoiceInput(choiceInputList);
        tQuestion.setSelectInput(selectInput);
    }
    
    /**
     * 修改保存题目列表
     */
    @RequiresPermissions("exam:question:edit")
    @Log(title = "题目列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
@@ -154,6 +200,9 @@
        return toAjax(tQuestionService.updateTQuestion(tQuestion));
    }
    
    /**
     * 删除题目列表
     */
    @RequiresPermissions("exam:question:remove")
    @Log(title = "题目列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
@@ -162,6 +211,9 @@
        return toAjax(tQuestionService.deleteTQuestionByIds(ids));
    }
    
    /**
     * 批量导入测试人员名单
     */
    @RequiresPermissions("enterprise:question:import")
    @PostMapping("/importData")
    @ResponseBody
@@ -175,6 +227,10 @@
        return AjaxResult.success(message);
    }
    
    /**
     * 批量导入测试人员名单模板下载
     */
    @RequiresPermissions("enterprise:question:view")
    @GetMapping("/importTemplate")
    @ResponseBody
@@ -182,16 +238,18 @@
        ExcelUtil<QuestionJobImport> util = new ExcelUtil<QuestionJobImport>(QuestionJobImport.class);
        return util.importTemplateExcel("题目模版");
    }
    @GetMapping("/addLang/{uuid}")
    public String addLang(@PathVariable("uuid") Long uuid, ModelMap mmap) {
        // 多语言的时候uuid就是id
        TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid);
        tQuestion.setUuid(uuid);
        TExamLevel tExamLevel = getEditQuestion(mmap, tQuestion);
        
        // 调用的是修改页面进行修改,所以不需要填写主要的选项
        tQuestion.setTitle("");
        List<String> selectInputList = tQuestion.getSelectInput();
        // job工作分析问卷没有input
        if (ExamUtil.isListNotEmpty(selectInputList)) {
            List<String> selectInputs = new ArrayList<>();
            for (String selectInput : selectInputList) {
@@ -202,11 +260,14 @@
        return prefix + "/edit-" + tExamLevel.getQuestionTemplateId();
    }
    
    /**
     *  根据语言修改题目, 页面上的新增语言调用
     */
    @GetMapping("/editLang/{uuid}/{langType}")
    public String editLang(@PathVariable("uuid") Long uuid, @PathVariable("langType") String langType,ModelMap mmap) {
        // 多语言的时候uuid就是id
        TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid);
        // 多语言版本
        tQuestion.setUuid(uuid);
        tQuestion.setLangType(langType);
        TExamLevel tExamLevel = getEditQuestion(mmap, tQuestion);
@@ -215,10 +276,11 @@
        tQuestion.setLangName(lanTypeMap.get(langType));
        return prefix + "/edit-" + tExamLevel.getQuestionTemplateId();
    }
    @GetMapping("/deleteLang/{uuid}/{langType}")
    @ResponseBody
    public AjaxResult deleteLang(@PathVariable("uuid") Long uuid, @PathVariable("langType") String langType) {
        // 多语言的时候uuid就是id
        TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid);
        tQuestion.setLangType(langType);
        return toAjax(tQuestionService.deleteQuestionContent(tQuestion));