futian.liu
2023-12-01 79a57e0844bdd3b5f06ebaac807a0896624be70f
consum-base/src/main/java/com/consum/base/controller/LWhFormOutputController.java
@@ -9,6 +9,7 @@
import com.consum.base.core.utils.MapperUtil;
import com.consum.base.core.utils.PageUtil;
import com.consum.base.pojo.LWhFormOutputParam;
import com.consum.base.pojo.excel.OutputExcelTemplate;
import com.consum.base.pojo.query.LWhFormOutputQry;
import com.consum.base.pojo.response.FormOutputGoodsVO;
import com.consum.base.pojo.response.FormOutputTemplateInfoVO;
@@ -23,20 +24,18 @@
import com.iplatform.model.po.S_user_core;
import com.walker.db.page.GenericPager;
import com.walker.infrastructure.utils.CollectionUtils;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.web.ResponseValue;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.compress.utils.Lists;
@@ -209,41 +208,45 @@
    @ApiOperation(value = "出库单导出", notes = "出库单导出")
    @ApiImplicitParam(name = "id", value = "出库单id", required = true, dataType = "Long", paramType = "query")
    @GetMapping("/list/export")
    public void export(Long id, HttpServletResponse response) throws IOException {
    public void export(Long id, HttpServletResponse response) throws Exception {
        FinSysTenantUser sysInfo = this.getSysInfo();
        if (sysInfo == null) {
            throw new RuntimeException("登录用户信息不存在");
        }
        ClassPathResource classPathResource = new ClassPathResource("import/采购退货单.xls");
        // 获取文件输入流
        InputStream inputStream = classPathResource.getInputStream();
        Workbook wb = new HSSFWorkbook(inputStream);
        TemplateExportParams params = new TemplateExportParams();
        params.setTemplateWb(wb);
        params.setHeadingStartRow(2);
        FinSysTenantUser sysInfo = this.getSysInfo();
        String userName = sysInfo.getUserName();
        Map<String, Object> map = lWhFormOutputService.getExportList(id, userName);
        if (CollectionUtils.isEmpty(map)) {
        List<OutputExcelTemplate> exportList = lWhFormOutputService.getExportList(id);
        if (CollectionUtils.isEmpty(exportList)) {
            throw new RuntimeException("数据为空");
        }
        int countNum = exportList.stream().filter(item -> item.getNum() != null).mapToInt(OutputExcelTemplate::getNum).sum();
        int totalAmount = exportList.stream().filter(item -> item.getTotalAmount() != null).mapToInt(OutputExcelTemplate::getTotalAmount).sum();
        Optional<OutputExcelTemplate> first = exportList.stream().findFirst();
        OutputExcelTemplate templateExcelExport = first.get();
        String businessFormCode = templateExcelExport.getBusinessFormCode();
        Long createTime = templateExcelExport.getCreateTime();
        String operatorName = templateExcelExport.getOperatorName();
        Workbook workbook = ExcelExportUtil.exportExcel(params, map);
        Map<String, Object> map = new HashMap<>();
        map.put("code", businessFormCode);
        map.put("date", DateUtils.toShowDate(createTime));
        map.put("name", operatorName);
        map.put("countNum", countNum);
        map.put("totalAmount", totalAmount);
        Workbook workbook = ExcelExportUtil.exportExcel(params, OutputExcelTemplate.class, exportList, map);
        downLoadExcel("采购退货单", response, workbook);
    }
    private void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try (OutputStream out = response.getOutputStream()) {
            response.setCharacterEncoding("UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            workbook.write(baos);
            response.setHeader("Content-Length", String.valueOf(baos.size()));
            out.write(baos.toByteArray());
        } catch (Exception e) {
            logger.error("导出文件失败", e);
        }
    }
}