自动化立体仓库 - WMS系统
zhangchao
2024-11-08 b0936f893a4712eebf55030bbdab159db8fe9d58
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
package com.zy.asrs.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.core.common.Cools;
import com.core.common.DateUtils;
import com.core.common.R;
import com.zy.asrs.entity.*;
import com.zy.asrs.mapper.ReportQueryMapper;
import com.zy.asrs.service.AgvWrkMastLogService;
import com.zy.common.web.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.SAAJResult;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Slf4j
@RestController
@RequestMapping("/report/download")
public class ReportDownloadController extends BaseController {
 
 
    @Autowired
    private ReportQueryMapper reportQueryMapper;
 
 
    @Autowired
    private AgvWrkMastLogService wrkMastLogService;
 
 
    /**
     * 文件下载并且失败的时候返回json(默认失败了会返回一个有部分数据的Excel)
     * 日入库汇总查询
     *
     * @since 2.1.1
     */
    @GetMapping("/countIn")
    public void download1(HttpServletResponse response, @RequestParam Map<String, Object> param) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("日入库汇总", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ViewWorkCountInView.class).autoCloseStream(Boolean.FALSE).sheet("sheet1").doWrite(getInData(param));
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
 
 
    private List<ViewWorkCountInView> getInData(Map<String, Object> param) {
        String startTime = "1970.1.2";
        String endTime = "2099.1.2";
        if (!Cools.isEmpty(param.get("query_date"))) {
            String queryDate = (String) param.get("query_date");
            String[] split = queryDate.split(" - ");
            startTime = split[0].split(" ")[0].replace("-", ".");
            endTime = split[1].split(" ")[0].replace("-", ".");
        }
        List<ViewWorkCountInView> allCountIn = reportQueryMapper.selectWorkCountIn(null, null, (String) param.get("matnr"), startTime, endTime);
        return allCountIn;
    }
 
    /**
     * 文件下载并且失败的时候返回json(默认失败了会返回一个有部分数据的Excel)
     * 日入库汇总查询
     *
     * @since 2.1.1
     */
    @RequestMapping("/countOut")
    public void download2(HttpServletResponse response, @RequestParam Map<String, Object> param) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("日出库汇总", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            List<ViewWorkCountInView> outData = getOutData(param);
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ViewWorkCountInView.class).autoCloseStream(Boolean.FALSE).sheet("sheet1").doWrite(outData);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
 
 
    private List<ViewWorkCountInView> getOutData(Map<String, Object> param) {
        String startTime = "1970.1.2";
        String endTime = "2099.1.2";
        if (!Cools.isEmpty(param.get("query_date"))) {
            String queryDate = (String) param.get("query_date");
            String[] split = queryDate.split(" - ");
            startTime = split[0].split(" ")[0].replace("-", ".");
            endTime = split[1].split(" ")[0].replace("-", ".");
        }
        List<ViewWorkCountInView> allCountIn = reportQueryMapper.selectWorkCountOut(null,null, (String) param.get("matnr"), startTime, endTime);
        return allCountIn;
    }
 
 
    /**
     * 文件下载并且失败的时候返回json(默认失败了会返回一个有部分数据的Excel)
     * 日入库汇总查询
     *
     * @since 2.1.1
     */
    @RequestMapping("/wrkMastLog")
    public void wrkMastLog(HttpServletResponse response, @RequestParam Map<String, Object> param) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("工作档维护日志", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ViewWorkCountInView.class).autoCloseStream(Boolean.FALSE).sheet("sheet1").doWrite(getWrkMastLogData(param));
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
 
 
    private List<AgvWrkMastLog> getWrkMastLogData(Map<String, Object> param) {
        excludeTrash(param);
        EntityWrapper<AgvWrkMastLog> wrapper = new EntityWrapper<>();
        convert(param, wrapper);
        return wrkMastLogService.selectList(wrapper);
    }
 
    private <T> void convert(Map<String, Object> map, EntityWrapper<T> wrapper) {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String val = String.valueOf(entry.getValue());
            if (val.contains(RANGE_TIME_LINK)) {
                String[] dates = val.split(RANGE_TIME_LINK);
                wrapper.ge(entry.getKey(), DateUtils.convert(dates[0]));
                wrapper.le(entry.getKey(), DateUtils.convert(dates[1]));
            } else {
                if (entry.getKey().equals("manu_type")) {
                    wrapper.like(entry.getKey(), val);
                } else {
                    wrapper.eq(entry.getKey(), val);
                }
            }
        }
    }
 
 
    /**
     * 文件下载并且失败的时候返回json(默认失败了会返回一个有部分数据的Excel)
     * 日出库明细统计
     *
     * @since 2.1.1
     */
    @RequestMapping("/out")
    public void out(HttpServletResponse response, @RequestParam(required = false) Map<String, Object> bean) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("日出库明细统计", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            List<ViewWorkInBean> list = reportQueryMapper.queryViewWorkOutList(JSON.parseObject(JSON.toJSONString(bean), ViewWorkInBean.class));
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ViewWorkInBean.class).autoCloseStream(Boolean.FALSE).sheet("sheet1").doWrite(list);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
 
    /**
     * 文件下载并且失败的时候返回json(默认失败了会返回一个有部分数据的Excel)
     * 日入库明细统计
     *
     * @since 2.1.1
     */
    @RequestMapping("/in")
    public void in(HttpServletResponse response, @RequestParam(required = false) Map<String, Object> bean) throws IOException {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("日入库明细统计", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            ViewWorkInBean viewWorkInBean = JSON.parseObject(JSON.toJSONString(bean), ViewWorkInBean.class);
            List<ViewWorkInBean> list = reportQueryMapper.queryViewWorkInList(viewWorkInBean);
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), ViewWorkInBean.class).autoCloseStream(Boolean.FALSE).sheet("sheet1").doWrite(list);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            Map<String, String> map = new HashMap<>();
            map.put("status", "failure");
            map.put("message", "下载文件失败" + e.getMessage());
            response.getWriter().println(JSON.toJSONString(map));
        }
    }
}