自动化立体仓库 - WMS系统
zhangchao
2024-11-08 940cab832331ab054d2a4c74f13aacc6847dd79d
src/main/java/com/zy/asrs/controller/ReportDownloadController.java
New file
@@ -0,0 +1,235 @@
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 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;
@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) ViewWorkInBean 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(bean);
            // 这里需要设置不关闭流
            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) ViewWorkInBean 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.queryViewWorkInList(bean);
            // 这里需要设置不关闭流
            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));
        }
    }
}