[测评系统]--测评系统核心代码库
林致杰
2022-06-26 99f7f9adac08f3a3fa3211f7a6062c26d62fa224
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
package com.ots.framework.web.controller;
import cn.hutool.core.util.URLUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ots.common.utils.DateUtils;
import com.ots.common.utils.StringUtils;
import com.ots.common.utils.security.ShiroUtils;
import com.ots.common.utils.sql.SqlUtil;
import com.ots.framework.web.domain.AjaxResult;
import com.ots.framework.web.domain.AjaxResult.Type;
import com.ots.framework.web.page.PageDomain;
import com.ots.framework.web.page.TableDataInfo;
import com.ots.framework.web.page.TableSupport;
import com.ots.project.system.user.domain.User;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.PropertyEditorSupport;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class BaseController {
    
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtils.parseDate(text));
            }
        });
    }
    
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected TableDataInfo getDataTable(List<?> list) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }
    
    protected AjaxResult toAjax(int rows) {
        return rows > 0 ? success() : error();
    }
    
    protected AjaxResult toAjax(boolean result) {
        return result ? success() : error();
    }
    
    public AjaxResult success() {
        return AjaxResult.success();
    }
    
    public AjaxResult error() {
        return AjaxResult.error();
    }
    
    public AjaxResult success(String message) {
        return AjaxResult.success(message);
    }
    
    public AjaxResult error(String message) {
        return AjaxResult.error(message);
    }
    
    public AjaxResult error(Type type, String message) {
        return new AjaxResult(type, message);
    }
    
    public String redirect(String url) {
        return StringUtils.format("redirect:{}", url);
    }
    public User getSysUser() {
        return ShiroUtils.getSysUser();
    }
    public void setSysUser(User user) {
        ShiroUtils.setSysUser(user);
    }
    public Long getUserId() {
        return getSysUser().getUserId();
    }
    public String getLoginName() {
        return getSysUser().getLoginName();
    }
    public static void main(String[] args) {
        String line = "xxxxx";
        System.out.println(getJson(line, "time_slot_list"));
    }
    public static String getJson(String json, String key) {
        String pattern = key + "\": \\{[^}]*\\}";
        
        Pattern r = Pattern.compile(pattern);
        
        Matcher m = r.matcher(json);
        if (m.find()) {
            String group = m.group(0);
            group = group.replace(key + "\":", key + "\":[");
            group += "]";
            return json.replaceAll(pattern, group);
        }
        return json;
    }
 
    public void downDeviceManagementTemplate(String fileName, String path , HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
        // 读取服务器端模板文件
        // ClassPathResource类的构造方法接收路径名称,自动去classpath路径下找文件
        ClassPathResource classPathResource = new ClassPathResource(path);
 
        //创建临时文件
        XSSFWorkbook workbook = null;
        try (InputStream inputStream = classPathResource.getInputStream()) {
            // 转为POI的Workbook输出流,这样下载后 打开不报错,直接输出  流 的话,打开excel可能提示错误
            workbook = new XSSFWorkbook(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("模板下载异常");
        }
        String userAgent = req.getHeader("User-Agent");
        // 针对IE或者以IE为内核的浏览器:
        if(userAgent.contains("MSIE")||userAgent.contains("Trident")) {
            fileName = URLUtil.encode(fileName, Charset.forName("UTF-8"));
        } else {
            // 非IE浏览器的处理:
            fileName = new String(fileName.getBytes("UTF-8"),"ISO-8859-1");
        }
        res.setHeader("Content-disposition", "attachment; filename=" + fileName);
        res.setContentType("application/vnd.ms-excel; charset=utf-8");
        res.setCharacterEncoding("UTF-8");
        // 将流中内容写出去
        try (OutputStream outputStream = res.getOutputStream()) {
            workbook.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("模板下载异常");
        }
    }
}