yuan
11 小时以前 0f79bd8c70a6122794aafac0d9373613a75f3e4c
src/main/java/com/ruoyi/quality/service/impl/QualityInspectServiceImpl.java
@@ -33,7 +33,10 @@
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
@@ -207,5 +210,81 @@
    }
    @Override
    public void downOutReport(HttpServletResponse response, QualityInspect qualityInspect) {
        QualityInspect inspect = qualityInspectMapper.selectOneData(qualityInspect.getId());
        // 格式化日期字段
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        inspect.setCheckTimeStr(inspect.getCheckTime() != null ? sdf.format(inspect.getCheckTime()) : "");
        inspect.setProductionDateStr(inspect.getProductionDate() != null ? inspect.getProductionDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) : "");
        inspect.setValidityDateStr(inspect.getValidityDate() != null ? inspect.getValidityDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) : "");
        // 获取检验参数列表
        List<QualityInspectParam> paramList = qualityInspectParamService.list(
                Wrappers.<QualityInspectParam>lambdaQuery().eq(QualityInspectParam::getInspectId, inspect.getId()));
        int index = 1;
        for (QualityInspectParam detail : paramList) {
            detail.setIndex(index);
            index++;
        }
        // 获取产品型号信息(包含有效期)
        ProductModel productModel = null;
        BigDecimal validityPeriod = null;
        if (inspect.getProductModelId() != null) {
            productModel = productModelMapper.selectById(inspect.getProductModelId());
            if (productModel != null) {
                validityPeriod = productModel.getValidityPeriod();
            }
        }
        /*String calculatedValidityDate = "";
        if (inspect.getProductionDate() != null && validityPeriod != null) {
            LocalDate productionDate = inspect.getProductionDate();
            int years = validityPeriod.intValue();
            int months = validityPeriod
                    .subtract(new BigDecimal(years))
                    .multiply(new BigDecimal(12))
                    .setScale(0, RoundingMode.HALF_UP)
                    .intValue();
            LocalDate calculatedDate = productionDate
                    .plusYears(years)
                    .plusMonths(months);
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            calculatedValidityDate = calculatedDate.format(formatter);
        }*/
        // 检验依据:产品名称 + 出厂检验操作流程
        String checkBasis = (inspect.getProductName() != null ? inspect.getProductName() : "") + "出厂检验操作流程";
        InputStream inputStream = this.getClass().getResourceAsStream("/static/out-report-template.docx");
        Configure configure = Configure.builder()
                .bind("paramList", new HackLoopTableRenderPolicy())
                .build();
//        final String finalCalculatedValidityDate = calculatedValidityDate;
        final String finalCheckBasis = checkBasis;
        XWPFTemplate template = XWPFTemplate.compile(inputStream, configure).render(
                new HashMap<String, Object>() {{
                    put("inspect", inspect);
                    put("checkBasis", finalCheckBasis);
//                    put("calculatedValidityDate", finalCalculatedValidityDate);
                    put("paramList", paramList);
                }});
        try {
            response.setContentType("application/msword");
            String fileName = URLEncoder.encode("出库检验报告", "UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".docx");
            OutputStream os = response.getOutputStream();
            template.write(os);
            os.flush();
            os.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("导出失败");
        }
    }
}