郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.Controller;
 
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gk.hotwork.Controller.Base.BaseController;
import com.gk.hotwork.Domain.EmergencyPlan;
import com.gk.hotwork.Domain.Utils.FilterObject;
import com.gk.hotwork.Domain.Utils.Msg;
import com.gk.hotwork.Domain.Utils.UploadUtil;
import com.gk.hotwork.Service.EmergencyPlanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
 
 
@Api(tags = "应急预案")
@RestController
@RequestMapping("/emergencyPlan")
public class EmergencyPlanController extends BaseController {
 
    @Autowired
    private EmergencyPlanService emergencyPlanService;
 
    @Value("${emergencyPlanPath}")
    private String emergencyPlanPath;
    @Value("${emergencyPlanUrl}")
    private String emergencyPlanUrl;
    @Value("${emergencyPlan}")
    private String emergencyPlan;
 
    @ApiOperation("分页")
    @PostMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex",value = "当前页码"),
            @ApiImplicitParam(name = "pageSize",value = "每页行数"),
            @ApiImplicitParam(name = "filter.name",value = "{}"),
    })
    public Msg selectPage(@RequestBody FilterObject filterObject) {
        Integer pageIndex = filterObject.getPageIndex();
        Integer pageSize = filterObject.getPageSize();
        IPage page = emergencyPlanService.selectPage(new Page<>(pageIndex, pageSize), filterObject.getFilter(), getUser());
        return success(page);
    }
 
    @ApiOperation("/图片+文件上传")
    @PostMapping("/upload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",value = "文件"),
    })
    public Msg upload(@RequestParam(required = false) MultipartFile file) throws Exception {
        String name= UploadUtil.uploadFile(file,emergencyPlanPath);
        String url= emergencyPlan+name;
        Map<String,String> map=new HashMap<>();
        map.put("fileName",name);
        map.put("fileUrl",url);
        return success(map);
    }
 
    @ApiOperation("/下载")
    @PostMapping("/download")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "path",value = "文件路径"),
    })
    public Msg upload(HttpServletRequest request, HttpServletResponse response, String path) throws Exception {
        try {
            response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");
 
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            File file = new File(emergencyPlanPath,path);
            String fileName = file.getName();
            long fileLength = file.length();
 
            response.setContentType("application/octet-stream");
 
            String header = request.getHeader("User-Agent").toUpperCase();
            if (header.contains("MSIE") || header.contains("TRIDENT") || header.contains("EDGE")) {
                fileName = URLEncoder.encode(fileName, "utf-8");
                fileName = fileName.replace("+", "%20");    //IE下载文件名空格变+号问题
            } else {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            }
 
            response.setHeader("Content-disposition", "attachment; filename=\"" + fileName + "\"");
            response.setHeader("Content-Length", String.valueOf(fileLength));
 
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new BufferedOutputStream(response.getOutputStream());
 
            byte[] buff = new byte[2048];
            int bytesRead;
 
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
            bis.close();
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success();
    }
 
    @ApiOperation("/新增")
    @PostMapping("/add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "应急预案名称"),
            @ApiImplicitParam(name = "fileType",value = "应急预案文件类型"),
            @ApiImplicitParam(name = "fileList",value = "应急预案文件"),
            @ApiImplicitParam(name = "remark",value = "应急预案备注"),
 
    })
    public Msg add(@RequestBody EmergencyPlan param) {
        emergencyPlanService.addOne(param, getUser());
        return success();
    }
 
    @ApiOperation("/修改")
    @PostMapping("/mod")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "name",value = "应急预案名称"),
            @ApiImplicitParam(name = "fileType",value = "应急预案文件类型"),
            @ApiImplicitParam(name = "fileList",value = "应急预案文件"),
            @ApiImplicitParam(name = "remark",value = "应急预案备注"),
    })
    public Msg mod(@RequestBody EmergencyPlan param) {
        emergencyPlanService.modOne(param, getUser());
        return success();
    }
 
    @ApiOperation("/删除")
    @PostMapping("/del")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
    })
    public Msg mod(@RequestBody JSONObject jsonObject) {
        Long id = jsonObject.getLong("id");
        emergencyPlanService.delOne(id, getUser());
        return success();
    }
}