linzhijie
2021-03-11 c33914ba0a98c823c4b4d7da21cdd476906c9924
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
package com.ots.project.exam.controller;
import com.ots.common.utils.file.FileUploadUtils;
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.config.EssConfig;
import com.ots.framework.web.controller.BaseController;
import com.ots.framework.web.domain.AjaxResult;
import com.ots.framework.web.page.TableDataInfo;
import com.ots.project.exam.domain.TLibraryCode;
import com.ots.project.exam.service.ITLibraryCodeService;
import com.ots.project.tool.CacheServiceFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
@Controller
@Slf4j
@RequestMapping("/exam/librarycode")
public class TLibraryCodeController extends BaseController {
    private String prefix = "exam/librarycode";
    @Autowired
    private ITLibraryCodeService tLibraryCodeService;
    @RequiresPermissions("exam:librarycode:view")
    @GetMapping()
    public String librarycode() {
        return prefix + "/librarycode";
    }
    
    @RequiresPermissions("exam:librarycode:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TLibraryCode tLibraryCode) {
        startPage();
        List<TLibraryCode> list = tLibraryCodeService.selectTLibraryCodeList(tLibraryCode);
        
        for (TLibraryCode tLibraryCodeTemp : list) {
            if (StringUtils.isNotEmpty(tLibraryCodeTemp.getIcon())) {
                tLibraryCodeTemp.setIcon(EssConfig.getHttpProfilePath() +"/upload" + tLibraryCodeTemp.getIcon());
            }
        }
        return getDataTable(list);
    }
    
    @RequiresPermissions("exam:librarycode:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TLibraryCode tLibraryCode) {
        List<TLibraryCode> list = tLibraryCodeService.selectTLibraryCodeList(tLibraryCode);
        ExcelUtil<TLibraryCode> util = new ExcelUtil<TLibraryCode>(TLibraryCode.class);
        return util.exportExcel(list, "librarycode");
    }
    
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }
    
    @RequiresPermissions("exam:librarycode:add")
    @Log(title = "报告字典", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TLibraryCode tLibraryCode) throws IOException {
        if (Objects.equals(tLibraryCode.getDictType(), "2") && !tLibraryCode.getIconFile().isEmpty()) {
            String ufi = FileUploadUtils.upload(EssConfig.getUploadPath(), tLibraryCode.getIconFile());
            tLibraryCode.setIcon(ufi.split("profile/upload")[1]);
        }
        AjaxResult result = toAjax(tLibraryCodeService.insertTLibraryCode(tLibraryCode));
        CacheServiceFactory.getInstance().reLoad();
        return result;
    }
    
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TLibraryCode tLibraryCode = tLibraryCodeService.selectTLibraryCodeById(id);
        tLibraryCode.setIcon(EssConfig.getHttpProfilePath() + "/upload" + tLibraryCode.getIcon());
        mmap.put("tLibraryCode", tLibraryCode);
        return prefix + "/edit";
    }
    
    @RequiresPermissions("exam:librarycode:edit")
    @Log(title = "报告字典", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TLibraryCode tLibraryCode) throws IOException {
        if (Objects.equals(tLibraryCode.getDictType(), "2") && !tLibraryCode.getIconFile().isEmpty()) {
            String ufi = FileUploadUtils.upload(EssConfig.getUploadPath(), tLibraryCode.getIconFile());
            tLibraryCode.setIcon(ufi.split("profile/upload")[1]);
        }
        AjaxResult result = toAjax(tLibraryCodeService.updateTLibraryCode(tLibraryCode));
        CacheServiceFactory.getInstance().reLoad();
        return result;
    }
    
    @RequiresPermissions("exam:librarycode:remove")
    @Log(title = "报告字典", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tLibraryCodeService.deleteTLibraryCodeByIds(ids));
    }
    @Log(title = "报告字典导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("exam:librarycode:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport, String importReportType) throws Exception {
        if (Objects.isNull(importReportType)) {
            return AjaxResult.error("报告类型不能为空", null);
        }
        ExcelUtil<TLibraryCode> util = new ExcelUtil<>(TLibraryCode.class);
        List<TLibraryCode> libraryCodes;
        try {
            libraryCodes = util.importExcel(file.getInputStream());
        } catch (NullPointerException npe) {
            log.error("模板解析错误:{}", npe.getMessage(), npe);
            return AjaxResult.error("导入数据解析错误,请按模板格式导入!");
        }
        libraryCodes.stream().forEach(item -> {
            item.setReportType(importReportType);
        });
        List<TLibraryCode> list = libraryCodes.stream().filter(item -> {
            return StringUtils.isNotBlank(item.getLibraryCode());
        }).collect(Collectors.toList());
        String message = tLibraryCodeService.importLibraryCodes(list, updateSupport);
        CacheServiceFactory.getInstance().reLoad();
        return AjaxResult.success(message);
    }
    @RequiresPermissions("exam:librarycode:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<TLibraryCode> util = new ExcelUtil<>(TLibraryCode.class);
        AjaxResult result = util.importTemplateExcel("报告字典内容(Report Library Entries)");
        CacheServiceFactory.getInstance().reLoad();
        return result;
    }
}