linzhijie
2021-03-11 93af1c6ffb9ae0e894689ad3a37b548e57d54cff
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
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
 *
 * @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
    public TableDataInfo list(TQuestion tQuestion) {
        startPage();
        List<TQuestion> list = tQuestionService.selectTQuestionList(tQuestion);
        return getDataTable(list);
    }
 
 
    /**
     * 专门为product的add_question.html 页面提供查询
     */
    @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);
    }
 
    /**
     * 新增题目列表
     * 参数 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")
    @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) {
        // 多语言的时候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) {
                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) {
        // 多语言的时候uuid就是id
        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) {
        // 多语言的时候uuid就是id
        TQuestion tQuestion = tQuestionService.selectTQuestionById(uuid);
        tQuestion.setLangType(langType);
        return toAjax(tQuestionService.deleteQuestionContent(tQuestion));
    }
}