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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
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
 *
 * @author ots
 * @date 2019-11-20
 */
@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<TExamPaper> list = tExamPaperService.selectTExamPaperList(tExamPaper);
        return getDataTable(list);
    }
 
    /**
     * 导出产品包列表列表
     */
    @RequiresPermissions("exam:product:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TExamPaper tExamPaper) {
        List<TExamPaper> list = tExamPaperService.selectTExamPaperList(tExamPaper);
        ExcelUtil<TExamPaper> util = new ExcelUtil<TExamPaper>(TExamPaper.class);
        return util.exportExcel(list, "product");
    }
 
    /**
     * 新增产品包列表
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap) {
        // 这句代码为了提示传过来的id是levelId
        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<ExamPaperTitleItem> 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));
    }
 
    /**
     * 选题目,ABCD的题目
     */
    @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";
    }
 
    /**
     * 选题目,ABCD的题目
     */
    @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<Ztree> treeData(@PathVariable("langType") String langType,@PathVariable("testType") String testType) {
        System.out.println(langType);
        List<Ztree> 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<QuestionJobImport> util = new ExcelUtil<>(QuestionJobImport.class);
        QuestionJobImport questionJobImport = util.importJobExcel(file.getInputStream(),levelId, leveltName, langType);
 
        List<TQuestion> questionList = questionJobImport.getQuestionList();
 
        // 大题目要job_title
        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<QuestionJobImport> util = new ExcelUtil<>(QuestionJobImport.class);
        return util.importTemplateQuestionExcel("JAQ题目模板.xlsx");
    }
 
    /**
     * 批量导入测试人员名单模板下载
     */
    @RequiresPermissions("exam:product:view")
    @GetMapping("/importMAQTemplate")
    @ResponseBody
    public AjaxResult importMQATemplate() {
        ExcelUtil<QuestionPersonalityImport> 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<QuestionPersonalityImport> util = new ExcelUtil<>(QuestionPersonalityImport.class);
        QuestionPersonalityImport questionPersonalityImport = util.importPersonalityExcel(file.getInputStream(), levelId, leveltName, langType);
        if (StringUtils.isEmpty(questionPersonalityImport.getLangType())) {
            return AjaxResult.success(questionPersonalityImport);
        }
 
        List<TQuestion> 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<TQuestion> questionList) {
        tExamPaperService.updateInsertQuestion(questionList, multilinguag);
 
        for (TQuestion tQuestion : questionList) {
            tQuestion.setTitle(examUtilService.getLangOrLocalLangString(langType, tQuestion.getTitle()));
            List<String> selectInput = tQuestion.getSelectInput();
            if (ExamUtil.isListNotEmpty(selectInput)) {
                List<String> 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));
    }
 
    /**
     * 产品包新增语言入口,产品包多语言添加入口
     * @param uuid
     * @param mmap
     * @return
     */
    @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<ExamPaperTitleItem> 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);
        // 批量导入的时候展示题目的维度,Job才需要,维度都是一样的
        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<ExamPaperTitleItem> examPaperTitleItemList = getExamPaperTitleItems(mmap, tExamPaper);
 
        // 批量导入的时候展示题目的维度,Job才需要,维度都是一样的
        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<ExamPaperTitleItem> getExamPaperTitleItems(ModelMap mmap, TExamPaper tExamPaper) {
        //获取题目的内容展示
        List<ExamPaperTitleItem> examPaperTitleItemList = tExamPaperService.getExamPaperTitleContent(tExamPaper);
        // begin 重复题目
        List<ExamPaperTitleItem> contentOriginalList = getExamDuplicateTitleItems(tExamPaper, examPaperTitleItemList);
        // end 重复题目
        mmap.put("examPaperTitleItemList", contentOriginalList);
 
        nmapPutQuestionItem(mmap, examPaperTitleItemList);
 
        return examPaperTitleItemList;
    }
 
    private List<ExamPaperTitleItem> getExamDuplicateTitleItems(TExamPaper tExamPaper, List<ExamPaperTitleItem> examPaperTitleItemList) {
        List<ExamPaperTitleItem> contentOriginalList = JSONArray.parseArray(tExamPaper.getContent(), ExamPaperTitleItem.class);
 
        Map<Integer, QuestionItem> questionItemMap = new HashMap<>();
        for (ExamPaperTitleItem examPaperTitleItem : examPaperTitleItemList) {
            List<QuestionItem> questionItems = examPaperTitleItem.getQuestionItems();
            for (QuestionItem questionItem : questionItems) {
                questionItemMap.put(questionItem.getQuestionId(), questionItem);
            }
        }
 
        for (ExamPaperTitleItem examPaperTitleItem : contentOriginalList) {
            List<QuestionItem> 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<ExamPaperTitleItem> examPaperTitleItemList) {
        // 批量导入的时候展示题目的维度,Job才需要,维度都是一样的
        List<QuestionItem> 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) {
        // 多语言的时候uuid就是id
        TExamPaper tExamPaper = tExamPaperService.selectTExamPaperById(uuid);
        tExamPaper.setLangType(langType);
        return toAjax(tExamPaperService.deleteProductContent(tExamPaper));
    }
 
    private String getDitcByType(String type, String dictType) {
        List<DictData> langTypeList = dictDataService.selectDictDataByType(type);
        Map<String, String> 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<DictData> dictDataList = tExamPaperService.getType(id);
        mmap.put("dictDatas", dictDataList);
        return prefix + "/product::langTypeDiv";
    }
}