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 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 map = new HashMap<>(); map.put("status", "failure"); map.put("message", "下载文件失败" + e.getMessage()); response.getWriter().println(JSON.toJSONString(map)); } } private List getInData(Map 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 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 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 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 map = new HashMap<>(); map.put("status", "failure"); map.put("message", "下载文件失败" + e.getMessage()); response.getWriter().println(JSON.toJSONString(map)); } } private List getOutData(Map 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 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 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 map = new HashMap<>(); map.put("status", "failure"); map.put("message", "下载文件失败" + e.getMessage()); response.getWriter().println(JSON.toJSONString(map)); } } private List getWrkMastLogData(Map param) { excludeTrash(param); EntityWrapper wrapper = new EntityWrapper<>(); convert(param, wrapper); return wrkMastLogService.selectList(wrapper); } private void convert(Map map, EntityWrapper wrapper) { for (Map.Entry 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 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 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 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 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 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 map = new HashMap<>(); map.put("status", "failure"); map.put("message", "下载文件失败" + e.getMessage()); response.getWriter().println(JSON.toJSONString(map)); } } }