package com.ots.project.exam.controller; import com.alibaba.fastjson.JSONObject; import com.ots.common.utils.MessageUtils; import com.ots.common.utils.StringUtils; import com.ots.common.utils.poi.ExcelUtil; import com.ots.framework.aspectj.lang.annotation.Log; import com.ots.framework.aspectj.lang.enums.BusinessType; import com.ots.framework.web.controller.BaseController; import com.ots.framework.web.domain.AjaxResult; import com.ots.framework.web.page.TableDataInfo; import com.ots.framework.web.service.DictService; import com.ots.project.exam.domain.TExamLevel; import com.ots.project.exam.domain.TQuestion; import com.ots.project.exam.dto.QuestionJobImport; import com.ots.project.exam.dto.QuestionObject; import com.ots.project.exam.service.ITExamLevelService; import com.ots.project.exam.service.ITQuestionService; import com.ots.project.exam.service.ITSubjectService; import com.ots.project.system.dict.domain.DictData; import com.ots.project.system.dict.service.IDictDataService; import com.ots.project.tool.exam.ExamUtil; import com.ots.project.tool.exam.JsonUtil; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; 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 @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 public TableDataInfo list(TQuestion tQuestion) { startPage(); List list = tQuestionService.selectTQuestionList(tQuestion); return getDataTable(list); } @RequiresPermissions("exam:question:list") @PostMapping("/listForProductAddQuestion") @ResponseBody public TableDataInfo listForProductAddQuestion(TQuestion tQuestion) { startPage(); if (StringUtils.equals(tQuestion.getLangType(), "ALL")) { tQuestion.setLangType(dictService.getLangType(LocaleContextHolder.getLocale().toString())); } List list = tQuestionService.selectTQuestionsForProductAddQuestion(tQuestion); return getDataTable(list); } @GetMapping("/add/{id}") public String add(@PathVariable("id") Long id, ModelMap mmap) { 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") @ResponseBody public AjaxResult addSave(TQuestion tQuestion) { int resultNum = tQuestionService.insertTQuestion(tQuestion); if (permanentIdDuplicate.ordinal() == resultNum) { return AjaxResult.error(MessageUtils.message("jsp.exam.quesiton.permanentidDuplicate")); } return toAjax(resultNum); } @GetMapping("/edit/{id}") public String edit(@PathVariable("id") Long id, ModelMap mmap) { TQuestion tQuestion = tQuestionService.selectTQuestionById(id); TExamLevel tExamLevel = getEditQuestion(mmap, tQuestion); List langTypeList = dictDataService.selectDictDataByType("lang_type"); Map lanTypeMap = langTypeList.stream().collect(Collectors.toMap(DictData::getDictValue,DictData::getDictLabel)); 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()); Map 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) { List choiceInputList = questionObject.getQuestionItemObjects().stream().map(i -> { return i.getPrefix(); }).collect(Collectors.toList()); List selectInput = questionObject.getQuestionItemObjects().stream().map(i -> { Map 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") @ResponseBody public AjaxResult editSave(TQuestion tQuestion) { return toAjax(tQuestionService.updateTQuestion(tQuestion)); } @RequiresPermissions("exam:question:remove") @Log(title = "题目列表", businessType = BusinessType.DELETE) @PostMapping("/remove") @ResponseBody public AjaxResult remove(String ids) { return toAjax(tQuestionService.deleteTQuestionByIds(ids)); } @RequiresPermissions("enterprise:question:import") @PostMapping("/importData") @ResponseBody public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { ExcelUtil util = new ExcelUtil<>(QuestionJobImport.class); List> excelList = util.importDynamicExcel(file.getInputStream()); if (excelList.size() <= 0 && Objects.isNull(excelList)) { return AjaxResult.success("导入失败,模版格式不正确。"); } String message = tQuestionService.insertImportQuestion(excelList); return AjaxResult.success(message); } @RequiresPermissions("enterprise:question:view") @GetMapping("/importTemplate") @ResponseBody public AjaxResult importTemplate() { ExcelUtil util = new ExcelUtil(QuestionJobImport.class); return util.importTemplateExcel("题目模版"); } @GetMapping("/addLang/{uuid}") public String addLang(@PathVariable("uuid") Long uuid, ModelMap mmap) { TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid); tQuestion.setUuid(uuid); TExamLevel tExamLevel = getEditQuestion(mmap, tQuestion); tQuestion.setTitle(""); List selectInputList = tQuestion.getSelectInput(); if (ExamUtil.isListNotEmpty(selectInputList)) { List selectInputs = new ArrayList<>(); for (String selectInput : selectInputList) { selectInputs.add(""); } tQuestion.setSelectInput(selectInputs); } return prefix + "/edit-" + tExamLevel.getQuestionTemplateId(); } @GetMapping("/editLang/{uuid}/{langType}") public String editLang(@PathVariable("uuid") Long uuid, @PathVariable("langType") String langType,ModelMap mmap) { TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid); tQuestion.setUuid(uuid); tQuestion.setLangType(langType); TExamLevel tExamLevel = getEditQuestion(mmap, tQuestion); List langTypeList = dictDataService.selectDictDataByType("lang_type"); Map lanTypeMap = langTypeList.stream().collect(Collectors.toMap(DictData::getDictValue,DictData::getDictLabel)); 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) { TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid); tQuestion.setLangType(langType); return toAjax(tQuestionService.deleteQuestionContent(tQuestion)); } }