linzhijie
2021-04-12 c632636e2f5b4188b430f5efc9d9f68c8dbe3d6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
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<TQuestion> 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<TQuestion> 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<DictData> langTypeList = dictDataService.selectDictDataByType("lang_type");
        Map<String, String> 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<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) {
        
        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")
    @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<QuestionJobImport> util = new ExcelUtil<>(QuestionJobImport.class);
        List<Map<Integer,String >> 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<QuestionJobImport> util = new ExcelUtil<QuestionJobImport>(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<String> selectInputList = tQuestion.getSelectInput();
        
        if (ExamUtil.isListNotEmpty(selectInputList)) {
            List<String> 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<DictData> langTypeList = dictDataService.selectDictDataByType("lang_type");
        Map<String, String> 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));
    }
}