Commit 68bd9a7ac4dff216fd412a30c3c31fcff9c3806e

Authored by 房远帅
1 parent edb08d2e

采购-入库:买断入库核算单导出

1 package com.lframework.xingyun.sc.procurement.controller.buyoutAccount; 1 package com.lframework.xingyun.sc.procurement.controller.buyoutAccount;
2 2
  3 +import cn.hutool.core.io.resource.ClassPathResource;
3 import com.lframework.starter.web.core.annotations.security.HasPermission; 4 import com.lframework.starter.web.core.annotations.security.HasPermission;
4 import com.lframework.starter.web.core.components.resp.InvokeResult; 5 import com.lframework.starter.web.core.components.resp.InvokeResult;
5 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; 6 import com.lframework.starter.web.core.components.resp.InvokeResultBuilder;
@@ -13,14 +14,32 @@ import com.lframework.xingyun.sc.procurement.service.buyoutAccount.ProcurementBu @@ -13,14 +14,32 @@ import com.lframework.xingyun.sc.procurement.service.buyoutAccount.ProcurementBu
13 import com.lframework.xingyun.sc.procurement.vo.buyoutAccount.CreateBuyoutAccountOrderVo; 14 import com.lframework.xingyun.sc.procurement.vo.buyoutAccount.CreateBuyoutAccountOrderVo;
14 import com.lframework.xingyun.sc.procurement.vo.buyoutAccount.QueryBuyoutAccountOrderVo; 15 import com.lframework.xingyun.sc.procurement.vo.buyoutAccount.QueryBuyoutAccountOrderVo;
15 import com.lframework.xingyun.sc.procurement.vo.buyoutAccount.UpdateBuyoutAccountOrderVo; 16 import com.lframework.xingyun.sc.procurement.vo.buyoutAccount.UpdateBuyoutAccountOrderVo;
  17 +import com.lframework.xingyun.sc.utils.ExcelUtil;
  18 +import com.lframework.xingyun.sc.utils.ResponseUtil;
16 import io.swagger.annotations.Api; 19 import io.swagger.annotations.Api;
17 import io.swagger.annotations.ApiImplicitParam; 20 import io.swagger.annotations.ApiImplicitParam;
18 import io.swagger.annotations.ApiOperation; 21 import io.swagger.annotations.ApiOperation;
  22 +
  23 +import java.io.File;
  24 +import java.io.FileInputStream;
  25 +import java.io.FileNotFoundException;
  26 +import java.io.FileOutputStream;
  27 +import java.io.IOException;
  28 +import java.io.InputStream;
  29 +import java.math.BigDecimal;
  30 +import java.nio.file.Files;
  31 +import java.util.HashMap;
19 import java.util.List; 32 import java.util.List;
  33 +import java.util.Map;
20 import java.util.stream.Collectors; 34 import java.util.stream.Collectors;
21 import javax.annotation.Resource; 35 import javax.annotation.Resource;
  36 +import javax.servlet.http.HttpServletResponse;
22 import javax.validation.Valid; 37 import javax.validation.Valid;
23 import javax.validation.constraints.NotBlank; 38 import javax.validation.constraints.NotBlank;
  39 +
  40 +import org.apache.poi.ss.usermodel.Sheet;
  41 +import org.apache.poi.ss.usermodel.Workbook;
  42 +import org.apache.poi.xssf.usermodel.XSSFWorkbook;
24 import lombok.extern.slf4j.Slf4j; 43 import lombok.extern.slf4j.Slf4j;
25 import org.springframework.util.CollectionUtils; 44 import org.springframework.util.CollectionUtils;
26 import org.springframework.validation.annotation.Validated; 45 import org.springframework.validation.annotation.Validated;
@@ -41,6 +60,15 @@ import org.springframework.web.bind.annotation.RestController; @@ -41,6 +60,15 @@ import org.springframework.web.bind.annotation.RestController;
41 @RequestMapping("/procurement/buyout/account") 60 @RequestMapping("/procurement/buyout/account")
42 public class ProcurementBuyoutAccountOrderController extends DefaultBaseController { 61 public class ProcurementBuyoutAccountOrderController extends DefaultBaseController {
43 62
  63 + /**
  64 + * 核算明细首行(Excel第5行),模板固定10行(第5~14行)
  65 + */
  66 + private static final int BUYOUT_LINE_START_ROW = 4;
  67 + /**
  68 + * 核算明细固定行数上限
  69 + */
  70 + private static final int BUYOUT_LINE_MAX = 10;
  71 +
44 @Resource 72 @Resource
45 private ProcurementBuyoutAccountOrderService buyoutAccountOrderService; 73 private ProcurementBuyoutAccountOrderService buyoutAccountOrderService;
46 74
@@ -121,6 +149,155 @@ public class ProcurementBuyoutAccountOrderController extends DefaultBaseControll @@ -121,6 +149,155 @@ public class ProcurementBuyoutAccountOrderController extends DefaultBaseControll
121 return InvokeResultBuilder.success(); 149 return InvokeResultBuilder.success();
122 } 150 }
123 151
  152 + /**
  153 + * 打印买断入库核算单
  154 + *
  155 + * @param exportType 导出类型 PDF-导出PDF文件,其他/默认-导出Excel文件
  156 + */
  157 + @ApiOperation("打印买断入库核算单(exportType=PDF导出PDF文件,默认Excel)")
  158 + @HasPermission({"warehouse-manage:outright-purchase:print"})
  159 + @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true)
  160 + @GetMapping("/export")
  161 + public void export(@NotBlank(message = "ID不能为空!") String id, String exportType,
  162 + HttpServletResponse response) throws IOException {
  163 +
  164 + GetBuyoutAccountOrderBo data = buyoutAccountOrderService.findDetail(id);
  165 +
  166 + String templateFile = "templates/buyoutAccountOrderTemplate.xlsx";
  167 + try {
  168 + try (InputStream inputStream = new ClassPathResource(templateFile).getStream();
  169 + Workbook workbook = new XSSFWorkbook(inputStream)) {
  170 + Sheet sheet = workbook.getSheetAt(0);
  171 +
  172 + // 核算明细:固定10行(模板Excel第5~14行),最多10条(超出取前10条),不足留空
  173 + List<GetBuyoutAccountOrderBo.LineBo> lines = data.getLines();
  174 + int lineSize = lines == null ? 0 : Math.min(lines.size(), BUYOUT_LINE_MAX);
  175 + for (int i = 0; i < lineSize; i++) {
  176 + fillBuyoutLineRow(sheet, BUYOUT_LINE_START_ROW + i, lines.get(i));
  177 + }
  178 +
  179 + // 主表占位符替换(${字段} / #{字段},模板里用到哪个替换哪个)
  180 + Map<String, Object> dataMap = new HashMap<>();
  181 + dataMap.put("incomingUnit", nvl(data.getIncomingUnit()));
  182 + dataMap.put("planCodes", CollectionUtils.isEmpty(data.getPlanCodes()) ? ""
  183 + : String.join(",", data.getPlanCodes()));
  184 + // 磅差情况
  185 + dataMap.put("weighDiff", formatDecimal(data.getWeighDiff()));
  186 + // 合计
  187 + dataMap.put("totalBuyoutQuantity", formatDecimal(data.getTotalBuyoutQuantity()));
  188 + dataMap.put("totalBuyoutAmount", formatDecimal(data.getTotalBuyoutAmount()));
  189 + dataMap.put("totalInstockQuantity", formatDecimal(data.getTotalInstockQuantity()));
  190 + dataMap.put("totalInstockAmount", formatDecimal(data.getTotalInstockAmount()));
  191 + dataMap.put("totalDiffAmount", formatDecimal(data.getTotalDiffAmount()));
  192 + // 审核签名区(总经理行模板无占位符,保持空白)
  193 + dataMap.put("fcckzgName", nvl(data.getFcckzgName()));
  194 + dataMap.put("fczlfgName", nvl(data.getFczlfgName()));
  195 + dataMap.put("gyglkzgName", nvl(data.getGyglkzgName()));
  196 + dataMap.put("fccwkzgName", nvl(data.getFccwkzgName()));
  197 + dataMap.put("gybzgName", nvl(data.getGybzgName()));
  198 + dataMap.put("createBy", nvl(data.getCreateBy()));
  199 + ExcelUtil.processTemplateSheet(sheet, dataMap);
  200 +
  201 + if ("PDF".equals(exportType)) {
  202 + // 导出 PDF:先写临时 .xlsx 文件,再调 LibreOffice 转换输出
  203 + ResponseUtil.setPDFResponseHead(response,
  204 + nvl(data.getWeighingTicketCode()) + "-买断入库核算单.pdf");
  205 + File tempExcel = File.createTempFile(
  206 + "buyout_account_order_" + nvl(data.getWeighingTicketCode()), ".xlsx");
  207 + try {
  208 + try (FileOutputStream fos = new FileOutputStream(tempExcel)) {
  209 + workbook.write(fos);
  210 + }
  211 + File pdfFile = ExcelUtil.convertExcelToPdf(tempExcel,
  212 + "/usr/bin/libreoffice --headless --convert-to pdf --outdir %s %s");
  213 + try (InputStream pdfIn = new FileInputStream(pdfFile)) {
  214 + byte[] buffer = new byte[8192];
  215 + int len;
  216 + while ((len = pdfIn.read(buffer)) != -1) {
  217 + response.getOutputStream().write(buffer, 0, len);
  218 + }
  219 + response.getOutputStream().flush();
  220 + } finally {
  221 + if (pdfFile != null && pdfFile.exists()) {
  222 + Files.delete(pdfFile.toPath());
  223 + }
  224 + }
  225 + } finally {
  226 + if (tempExcel != null && tempExcel.exists()) {
  227 + Files.delete(tempExcel.toPath());
  228 + }
  229 + }
  230 + } else {
  231 + // 默认导出 Excel
  232 + ResponseUtil.setExcelResponseHead(response,
  233 + nvl(data.getWeighingTicketCode()) + "-买断入库核算单.xlsx");
  234 + workbook.write(response.getOutputStream());
  235 + response.getOutputStream().flush();
  236 + }
  237 + } catch (FileNotFoundException e) {
  238 + throw new RuntimeException("模板文件不存在: " + templateFile, e);
  239 + } catch (IOException e) {
  240 + throw new RuntimeException("无法读取模板文件: " + templateFile, e);
  241 + }
  242 + } catch (Exception e) {
  243 + log.error("打印买断入库核算单失败, id={}, {}", id, e.getMessage(), e);
  244 + }
  245 + }
  246 +
  247 + /**
  248 + * 填充一行核算明细(列布局固定:A买断品种、B买断数量、C买断单价、D买断金额、E入库数量、F入库单价、
  249 + * G入库金额、H预计吨损耗、I实际吨损耗、J吨损耗差异、K差异金额)
  250 + */
  251 + private void fillBuyoutLineRow(Sheet sheet, int rowIdx, GetBuyoutAccountOrderBo.LineBo line) {
  252 + if (line == null) {
  253 + return;
  254 + }
  255 + if (line.getBuyoutCategory() != null) {
  256 + ExcelUtil.setCellValue(sheet, rowIdx, 0, line.getBuyoutCategory());
  257 + }
  258 + if (line.getBuyoutQuantity() != null) {
  259 + ExcelUtil.setCellValue(sheet, rowIdx, 1, line.getBuyoutQuantity());
  260 + }
  261 + if (line.getBuyoutPrice() != null) {
  262 + ExcelUtil.setCellValue(sheet, rowIdx, 2, line.getBuyoutPrice());
  263 + }
  264 + if (line.getBuyoutAmount() != null) {
  265 + ExcelUtil.setCellValue(sheet, rowIdx, 3, line.getBuyoutAmount());
  266 + }
  267 + if (line.getInstockQuantity() != null) {
  268 + ExcelUtil.setCellValue(sheet, rowIdx, 4, line.getInstockQuantity());
  269 + }
  270 + if (line.getInstockPrice() != null) {
  271 + ExcelUtil.setCellValue(sheet, rowIdx, 5, line.getInstockPrice());
  272 + }
  273 + if (line.getInstockAmount() != null) {
  274 + ExcelUtil.setCellValue(sheet, rowIdx, 6, line.getInstockAmount());
  275 + }
  276 + if (line.getExpectTonLoss() != null) {
  277 + ExcelUtil.setCellValue(sheet, rowIdx, 7, line.getExpectTonLoss());
  278 + }
  279 + if (line.getActualTonLoss() != null) {
  280 + ExcelUtil.setCellValue(sheet, rowIdx, 8, line.getActualTonLoss());
  281 + }
  282 + if (line.getTonLossDiff() != null) {
  283 + ExcelUtil.setCellValue(sheet, rowIdx, 9, line.getTonLossDiff());
  284 + }
  285 + if (line.getDiffAmount() != null) {
  286 + ExcelUtil.setCellValue(sheet, rowIdx, 10, line.getDiffAmount());
  287 + }
  288 + }
  289 +
  290 + private String formatDecimal(BigDecimal bd) {
  291 + if (bd == null) {
  292 + return "";
  293 + }
  294 + return bd.stripTrailingZeros().toPlainString();
  295 + }
  296 +
  297 + private String nvl(Object o) {
  298 + return o == null ? "" : o.toString();
  299 + }
  300 +
124 private List<String> splitPlanCodes(String planCodes) { 301 private List<String> splitPlanCodes(String planCodes) {
125 if (planCodes == null || planCodes.isEmpty()) { 302 if (planCodes == null || planCodes.isEmpty()) {
126 return java.util.Collections.emptyList(); 303 return java.util.Collections.emptyList();