package com.ots.project.exam.controller; import com.alibaba.fastjson.JSONArray; import com.ots.common.utils.StringUtils; import com.ots.common.utils.bean.BeanUtils; import com.ots.common.utils.poi.ExcelUtil; import com.ots.common.utils.security.ShiroUtils; 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.domain.Ztree; import com.ots.framework.web.page.TableDataInfo; import com.ots.project.exam.domain.TExamLevel; import com.ots.project.exam.domain.TExamPaper; import com.ots.project.exam.domain.TQuestion; import com.ots.project.exam.dto.ExamPaperTitleItem; import com.ots.project.exam.dto.QuestionItem; import com.ots.project.exam.dto.QuestionJobImport; import com.ots.project.exam.dto.QuestionPersonalityImport; import com.ots.project.exam.service.ITExamLevelService; import com.ots.project.exam.service.ITExamPaperService; import com.ots.project.exam.service.ITReportTemplateService; import com.ots.project.exam.service.ITTextContentService; 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 com.ots.project.tool.exam.ModelMapperSingle; import com.ots.project.tool.gen.service.IExamUtilService; import org.apache.shiro.authz.annotation.RequiresPermissions; import org.modelmapper.ModelMapper; import org.springframework.beans.factory.annotation.Autowired; 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; @Controller @RequestMapping("/exam/product") public class TExamPaperController extends BaseController { private String prefix = "exam/product"; @Autowired private ITExamPaperService tExamPaperService; @Autowired private ITTextContentService tTextContentService; @Autowired private ITExamLevelService itExamLevelService; @Autowired private IDictDataService dictDataService; @Autowired private IExamUtilService examUtilService; @Autowired private ITReportTemplateService reportTemplateService; protected final static ModelMapper modelMapper = ModelMapperSingle.Instance(); @RequiresPermissions("exam:product:view") @GetMapping() public String product() { return prefix + "/product"; } @RequiresPermissions("exam:product:list") @PostMapping("/list") @ResponseBody public TableDataInfo list(TExamPaper tExamPaper) { startPage(); List list = tExamPaperService.selectTExamPaperList(tExamPaper); return getDataTable(list); } @RequiresPermissions("exam:product:export") @PostMapping("/export") @ResponseBody public AjaxResult export(TExamPaper tExamPaper) { List list = tExamPaperService.selectTExamPaperList(tExamPaper); ExcelUtil util = new ExcelUtil(TExamPaper.class); return util.exportExcel(list, "product"); } @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("templateList", reportTemplateService.getReportTemplates(ShiroUtils.getSysUser())); return prefix + "/add-" + tExamLevel.getQuestionTemplateId(); } @RequiresPermissions("exam:product:add") @Log(title = "产品包列表", businessType = BusinessType.INSERT) @PostMapping("/add") @ResponseBody public AjaxResult addSave(TExamPaper tExamPaper) { return toAjax(tExamPaperService.insertTExamPaper(tExamPaper)); } @GetMapping("/edit/{id}") public String edit(@PathVariable("id") Long id, ModelMap mmap) { TExamPaper tExamPaper = tExamPaperService.selectTExamPaperById(id); List examPaperTitleItemList = tExamPaperService.getExamPaperTitleContent(tExamPaper); mmap.put("examPaperTitleItemList", examPaperTitleItemList); mmap.put("templateList", reportTemplateService.getReportTemplates(ShiroUtils.getSysUser())); mmap.put("tExamPaper", tExamPaper); TExamLevel tExamLevel = itExamLevelService.selectTExamLevelById(tExamPaper.getLevelId()); mmap.put("tExamLevel", tExamLevel); return prefix + "/edit-" + tExamLevel.getQuestionTemplateId(); } @RequiresPermissions("exam:product:edit") @Log(title = "产品包列表", businessType = BusinessType.UPDATE) @PostMapping("/edit") @ResponseBody public AjaxResult editSave(TExamPaper tExamPaper) { return toAjax(tExamPaperService.updateTExamPaper(tExamPaper)); } @RequiresPermissions("exam:product:remove") @Log(title = "产品包列表", businessType = BusinessType.DELETE) @PostMapping("/remove") @ResponseBody public AjaxResult remove(String ids) { return toAjax(tExamPaperService.deleteTExamPaperByIds(ids)); } @GetMapping("/addQuestion/{levelId}/{langType}") public String addQuestion(@PathVariable("levelId") Long levelId, @PathVariable("langType") String langType, ModelMap mmap) { mmap.put("level", levelId); mmap.put("langType", langType); return prefix + "/add_question"; } @GetMapping("/addIntelligenceTitleQuestion/{levelId}/{langType}") public String addIntelligenceTitleQuestion(@PathVariable("levelId") Long levelId, @PathVariable("langType") String langType, ModelMap mmap) { mmap.put("level", levelId); mmap.put("langType", langType); mmap.put("template", "intelligence_title"); return prefix + "/add_question"; } @GetMapping("/addIntelligenceQuestion/{levelId}/{langType}") public String addIntelligenceQuestion(@PathVariable("levelId") Long levelId, @PathVariable("langType") String langType, ModelMap mmap) { mmap.put("level", levelId); mmap.put("langType", langType); mmap.put("template", "intelligence"); return prefix + "/add_question"; } @GetMapping("/addTitle/{questionTemplateId}/{langType}") public String addTitle(@PathVariable("questionTemplateId") String questionTemplateId, @PathVariable("langType") String langType, ModelMap mmap) { mmap.put("questionTemplateId", questionTemplateId); mmap.put("langType", langType); return prefix + "/add_title"; } @GetMapping("/getProdList") public String getProdList(String langType,String testType,ModelMap mmap) { mmap.put("langType", langType); mmap.put("testType", testType); return prefix + "/prodtree"; } @GetMapping("/treeData/{langType}/{testType}") @ResponseBody public List treeData(@PathVariable("langType") String langType,@PathVariable("testType") String testType) { System.out.println(langType); List ztrees = tExamPaperService.selectProdTree(testType); ztrees.stream().forEach(ztree -> { ztree.setName(ExamUtil.getLangString(langType, ztree.getName())); }); return ztrees; } @RequiresPermissions("exam:product:import") @PostMapping("/importJobData/{langType}/{levelId}/{levelName}") @ResponseBody public AjaxResult importJobData(MultipartFile file, @PathVariable("langType") String langType, @PathVariable("levelId") Long levelId, @PathVariable("levelName") String leveltName, boolean updateSupport) throws Exception { ExcelUtil util = new ExcelUtil<>(QuestionJobImport.class); QuestionJobImport questionJobImport = util.importJobExcel(file.getInputStream(),levelId, leveltName, langType); List questionList = questionJobImport.getQuestionList(); for (TQuestion tQuestion : questionList) { tQuestion.setTemplate("job_title"); } updateBatchImportData(langType, JsonUtil.toJsonStr(questionJobImport.getMultilingualList()), questionList); if (StringUtils.isEmpty(questionJobImport.getLangType())) { return AjaxResult.success(questionJobImport); } if (Objects.isNull(questionJobImport) || Objects.isNull(questionJobImport.getQuestionList()) || questionJobImport.getQuestionList().isEmpty()) { return AjaxResult.error("导入失败"); } return AjaxResult.success(questionJobImport); } @RequiresPermissions("exam:product:view") @GetMapping("/importTemplate") @ResponseBody public AjaxResult importTemplate() { ExcelUtil util = new ExcelUtil<>(QuestionJobImport.class); return util.importTemplateQuestionExcel("JAQ题目模板.xlsx"); } @RequiresPermissions("exam:product:view") @GetMapping("/importMAQTemplate") @ResponseBody public AjaxResult importMQATemplate() { ExcelUtil util = new ExcelUtil<>(QuestionPersonalityImport.class); return util.importTemplateQuestionExcel("MAQ题目模板.xlsx"); } @RequiresPermissions("exam:product:import") @PostMapping("/importMAQData/{langType}") @ResponseBody public AjaxResult importMAQData(MultipartFile file, boolean updateSupport, Long levelId, String leveltName, @PathVariable String langType) throws Exception { ExcelUtil util = new ExcelUtil<>(QuestionPersonalityImport.class); QuestionPersonalityImport questionPersonalityImport = util.importPersonalityExcel(file.getInputStream(), levelId, leveltName, langType); if (StringUtils.isEmpty(questionPersonalityImport.getLangType())) { return AjaxResult.success(questionPersonalityImport); } List questionList = questionPersonalityImport.getQuestionList(); if (Objects.isNull(questionPersonalityImport) || Objects.isNull(questionList) || questionList.isEmpty()) { return AjaxResult.error("导入失败"); } updateBatchImportData(langType, JsonUtil.toJsonStr(questionPersonalityImport.getMultilingualList()), questionList); return AjaxResult.success(questionPersonalityImport); } private void updateBatchImportData(@PathVariable String langType, String multilinguag, List questionList) { tExamPaperService.updateInsertQuestion(questionList, multilinguag); for (TQuestion tQuestion : questionList) { tQuestion.setTitle(examUtilService.getLangOrLocalLangString(langType, tQuestion.getTitle())); List selectInput = tQuestion.getSelectInput(); if (ExamUtil.isListNotEmpty(selectInput)) { List selectInputResult = new ArrayList<>(); for (String value : selectInput) { selectInputResult.add(examUtilService.getLangOrLocalLangString(langType, value)); } tQuestion.setSelectInput(selectInputResult); } } } @RequiresPermissions("exam:product:changeDeleted") @PostMapping("/changeDeleted") @ResponseBody public AjaxResult changeDeleted(TExamPaper tExamPaper) { return toAjax(tExamPaperService.updateDeleted(tExamPaper)); } @GetMapping("/addLang/{uuid}/{langType}") public String addLang(@PathVariable("uuid") Long uuid, @PathVariable("langType") String langType, ModelMap mmap) { TExamPaper tExamPaper = tExamPaperService.selectTExamPaperById(uuid); tExamPaper.setLangType(langType); tExamPaper.setLangName(getDitcByType("lang_type", langType)); tExamPaper.setUuid(uuid); tExamPaper.setOperatingLanguage("addLang"); List examPaperTitleItemList = getExamPaperTitleItems(mmap, tExamPaper); mmap.put("templateList", reportTemplateService.getReportTemplates(ShiroUtils.getSysUser())); mmap.put("tExamPaper", tExamPaper); TExamLevel tExamLevel = itExamLevelService.selectTExamLevelById(tExamPaper.getLevelId()); mmap.put("tExamLevel", tExamLevel); nmapPutQuestionItem(mmap, examPaperTitleItemList); tExamPaper.setName(""); tExamPaper.setGuide(""); return prefix + "/edit-" + tExamLevel.getQuestionTemplateId(); } @GetMapping("/editLang/{uuid}/{langType}") public String editLang(@PathVariable("uuid") Long uuid, @PathVariable("langType") String langType, ModelMap mmap) { TExamPaper tExamPaper = tExamPaperService.selectTExamPaperById(uuid); tExamPaper.setUuid(uuid); tExamPaper.setId(uuid); tExamPaper.setLangType(langType); tExamPaper.setOperatingLanguage("editLang"); tExamPaper.setName(ExamUtil.getStringByLang(tExamPaper.getLangType(), tExamPaper.getName())); tExamPaper.setGuide(ExamUtil.getStringByLang(tExamPaper.getLangType(), tExamPaper.getGuide())); tExamPaper.setLangName(getDitcByType("lang_type", langType)); List examPaperTitleItemList = getExamPaperTitleItems(mmap, tExamPaper); nmapPutQuestionItem(mmap, examPaperTitleItemList); mmap.put("tExamPaper", tExamPaper); mmap.put("templateList", reportTemplateService.getReportTemplates(ShiroUtils.getSysUser())); TExamLevel tExamLevel = itExamLevelService.selectTExamLevelById(tExamPaper.getLevelId()); mmap.put("tExamLevel", tExamLevel); return prefix + "/edit-" + tExamLevel.getQuestionTemplateId(); } private List getExamPaperTitleItems(ModelMap mmap, TExamPaper tExamPaper) { List examPaperTitleItemList = tExamPaperService.getExamPaperTitleContent(tExamPaper); List contentOriginalList = getExamDuplicateTitleItems(tExamPaper, examPaperTitleItemList); mmap.put("examPaperTitleItemList", contentOriginalList); nmapPutQuestionItem(mmap, examPaperTitleItemList); return examPaperTitleItemList; } private List getExamDuplicateTitleItems(TExamPaper tExamPaper, List examPaperTitleItemList) { List contentOriginalList = JSONArray.parseArray(tExamPaper.getContent(), ExamPaperTitleItem.class); Map questionItemMap = new HashMap<>(); for (ExamPaperTitleItem examPaperTitleItem : examPaperTitleItemList) { List questionItems = examPaperTitleItem.getQuestionItems(); for (QuestionItem questionItem : questionItems) { questionItemMap.put(questionItem.getQuestionId(), questionItem); } } for (ExamPaperTitleItem examPaperTitleItem : contentOriginalList) { List questionItems = examPaperTitleItem.getQuestionItems(); for (QuestionItem questionItem : questionItems) { Integer partOrder = questionItem.getPartOrder(); QuestionItem questionItemSource = questionItemMap.get(questionItem.getId()); questionItemSource.setTemplate(questionItem.getTemplate()); questionItemSource.setParentQuestionId(questionItem.getParentQuestionId()); BeanUtils.copyProperties(questionItemSource, questionItem); questionItem.setPartOrder(partOrder); } } return contentOriginalList; } private void nmapPutQuestionItem(ModelMap mmap, List examPaperTitleItemList) { List questionItems = examPaperTitleItemList.get(0).getQuestionItems(); mmap.put("questionItems", questionItems); mmap.put("parts", JsonUtil.toJsonStr(examPaperTitleItemList.get(0).getParts())); } @GetMapping("/deleteLang/{uuid}/{langType}") @ResponseBody public AjaxResult deleteLang(@PathVariable("uuid") Long uuid, @PathVariable("langType") String langType) { TExamPaper tExamPaper = tExamPaperService.selectTExamPaperById(uuid); tExamPaper.setLangType(langType); return toAjax(tExamPaperService.deleteProductContent(tExamPaper)); } private String getDitcByType(String type, String dictType) { List langTypeList = dictDataService.selectDictDataByType(type); Map lanTypeMap = langTypeList.stream().collect(Collectors.toMap(DictData::getDictValue, DictData::getDictLabel)); return lanTypeMap.get(dictType); } @GetMapping("/langType/{id}") public String getLangType(@PathVariable("id") Long id, ModelMap mmap) { List dictDataList = tExamPaperService.getType(id); mmap.put("dictDatas", dictDataList); return prefix + "/product::langTypeDiv"; } }