Showing
3 changed files
with
292 additions
and
1 deletions
| 1 | 1 | package com.lframework.xingyun.sc.procurement.controller.settle; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.io.resource.ClassPathResource; | |
| 4 | +import com.lframework.starter.common.utils.StringUtil; | |
| 3 | 5 | import com.lframework.starter.web.core.annotations.security.HasPermission; |
| 4 | 6 | import com.lframework.starter.web.core.components.resp.InvokeResult; |
| 5 | 7 | import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; |
| ... | ... | @@ -16,14 +18,40 @@ import com.lframework.xingyun.sc.procurement.vo.settle.CreateProcurementMaterial |
| 16 | 18 | import com.lframework.xingyun.sc.procurement.vo.settle.QueryMaterialSettleSelectableTicketVo; |
| 17 | 19 | import com.lframework.xingyun.sc.procurement.vo.settle.QueryProcurementMaterialSettleDetailVo; |
| 18 | 20 | import com.lframework.xingyun.sc.procurement.vo.settle.UpdateProcurementMaterialSettleDetailVo; |
| 21 | +import com.lframework.xingyun.sc.utils.ExcelUtil; | |
| 22 | +import com.lframework.xingyun.sc.utils.ResponseUtil; | |
| 19 | 23 | import io.swagger.annotations.Api; |
| 20 | 24 | import io.swagger.annotations.ApiImplicitParam; |
| 21 | 25 | import io.swagger.annotations.ApiOperation; |
| 26 | + | |
| 27 | +import java.io.File; | |
| 28 | +import java.io.FileInputStream; | |
| 29 | +import java.io.FileNotFoundException; | |
| 30 | +import java.io.FileOutputStream; | |
| 31 | +import java.io.IOException; | |
| 32 | +import java.io.InputStream; | |
| 33 | +import java.math.BigDecimal; | |
| 34 | +import java.nio.file.Files; | |
| 35 | +import java.time.LocalDate; | |
| 36 | +import java.util.HashMap; | |
| 22 | 37 | import java.util.List; |
| 38 | +import java.util.Map; | |
| 23 | 39 | import java.util.stream.Collectors; |
| 24 | 40 | import javax.annotation.Resource; |
| 41 | +import javax.servlet.http.HttpServletResponse; | |
| 25 | 42 | import javax.validation.Valid; |
| 26 | 43 | import javax.validation.constraints.NotBlank; |
| 44 | + | |
| 45 | +import org.apache.poi.ss.usermodel.Cell; | |
| 46 | +import org.apache.poi.ss.usermodel.CellStyle; | |
| 47 | +import org.apache.poi.ss.usermodel.CellType; | |
| 48 | +import org.apache.poi.ss.usermodel.HorizontalAlignment; | |
| 49 | +import org.apache.poi.ss.usermodel.Row; | |
| 50 | +import org.apache.poi.ss.usermodel.Sheet; | |
| 51 | +import org.apache.poi.ss.usermodel.VerticalAlignment; | |
| 52 | +import org.apache.poi.ss.usermodel.Workbook; | |
| 53 | +import org.apache.poi.ss.util.CellRangeAddress; | |
| 54 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
| 27 | 55 | import lombok.extern.slf4j.Slf4j; |
| 28 | 56 | import org.springframework.util.CollectionUtils; |
| 29 | 57 | import org.springframework.validation.annotation.Validated; |
| ... | ... | @@ -44,6 +72,23 @@ import org.springframework.web.bind.annotation.RestController; |
| 44 | 72 | @RequestMapping("/procurement/material/settle") |
| 45 | 73 | public class ProcurementMaterialSettleDetailController extends DefaultBaseController { |
| 46 | 74 | |
| 75 | + /** | |
| 76 | + * 申请净重明细行(Excel第4行):模板预留1行,B:E=类别、F:I=数量,超出1条按条数向下插行 | |
| 77 | + */ | |
| 78 | + private static final int APPLY_ROW = 3; | |
| 79 | + /** | |
| 80 | + * 结算明细首行(Excel第6行),模板固定14行(第6~19行) | |
| 81 | + */ | |
| 82 | + private static final int SETTLE_LINE_START_ROW = 5; | |
| 83 | + /** | |
| 84 | + * 结算明细固定行数上限 | |
| 85 | + */ | |
| 86 | + private static final int SETTLE_LINE_MAX = 14; | |
| 87 | + /** | |
| 88 | + * 说明行(Excel第21行):1经销/2双经销/3买断复选框 | |
| 89 | + */ | |
| 90 | + private static final int NOTE_ROW = 20; | |
| 91 | + | |
| 47 | 92 | @Resource |
| 48 | 93 | private ProcurementMaterialSettleDetailService procurementMaterialSettleDetailService; |
| 49 | 94 | |
| ... | ... | @@ -140,4 +185,249 @@ public class ProcurementMaterialSettleDetailController extends DefaultBaseContro |
| 140 | 185 | procurementMaterialSettleDetailService.cancel(id); |
| 141 | 186 | return InvokeResultBuilder.success(); |
| 142 | 187 | } |
| 188 | + | |
| 189 | + /** | |
| 190 | + * 打印原料结算详情单 | |
| 191 | + * | |
| 192 | + * @param exportType 导出类型 PDF-导出PDF文件,其他/默认-导出Excel文件 | |
| 193 | + */ | |
| 194 | + @ApiOperation("打印原料结算详情单(exportType=PDF导出PDF文件,默认Excel)") | |
| 195 | + @HasPermission({"warehouse-manage:material-settlement:print"}) | |
| 196 | + @ApiImplicitParam(value = "ID", name = "id", paramType = "query", required = true) | |
| 197 | + @GetMapping("/export") | |
| 198 | + public void export(@NotBlank(message = "ID不能为空!") String id, String exportType, | |
| 199 | + HttpServletResponse response) throws IOException { | |
| 200 | + | |
| 201 | + GetProcurementMaterialSettleDetailBo data = procurementMaterialSettleDetailService.findDetail(id); | |
| 202 | + | |
| 203 | + String templateFile = "templates/materialSettleDetailTemplate.xlsx"; | |
| 204 | + try { | |
| 205 | + try (InputStream inputStream = new ClassPathResource(templateFile).getStream(); | |
| 206 | + Workbook workbook = new XSSFWorkbook(inputStream)) { | |
| 207 | + Sheet sheet = workbook.getSheetAt(0); | |
| 208 | + | |
| 209 | + // 申请净重明细:动态行,模板预留1行(Excel第4行,B:E类别、F:I数量),超出1条按条数向下插行, | |
| 210 | + // 下方明细表整体随之下移 | |
| 211 | + List<GetProcurementMaterialSettleDetailBo.ApplyLineBo> applyLines = data.getApplyLines(); | |
| 212 | + int applySize = applyLines == null ? 0 : applyLines.size(); | |
| 213 | + int insertCount = Math.max(0, applySize - 1); | |
| 214 | + if (insertCount > 0) { | |
| 215 | + int shiftStart = APPLY_ROW + 1; | |
| 216 | + int shiftEnd = sheet.getLastRowNum(); | |
| 217 | + if (shiftEnd >= shiftStart) { | |
| 218 | + sheet.shiftRows(shiftStart, shiftEnd, insertCount, true, true); | |
| 219 | + } | |
| 220 | + // 复制模板申请净重行样式到新插入的每一行 | |
| 221 | + Row templateRow = sheet.getRow(APPLY_ROW); | |
| 222 | + for (int k = 1; k <= insertCount; k++) { | |
| 223 | + int newRowIdx = APPLY_ROW + k; | |
| 224 | + Row newRow = sheet.getRow(newRowIdx); | |
| 225 | + if (newRow == null) { | |
| 226 | + newRow = sheet.createRow(newRowIdx); | |
| 227 | + } | |
| 228 | + if (templateRow != null) { | |
| 229 | + newRow.setHeight(templateRow.getHeight()); | |
| 230 | + for (int c = 0; c < 10; c++) { | |
| 231 | + Cell tc = templateRow.getCell(c); | |
| 232 | + Cell nc = newRow.getCell(c); | |
| 233 | + if (nc == null) { | |
| 234 | + nc = newRow.createCell(c); | |
| 235 | + } | |
| 236 | + if (tc != null) { | |
| 237 | + nc.setCellStyle(tc.getCellStyle()); | |
| 238 | + } | |
| 239 | + } | |
| 240 | + // 新行补充合并单元格,与模板申请净重行一致:B:E(类别)、F:I(数量) | |
| 241 | + sheet.addMergedRegion(new CellRangeAddress(newRowIdx, newRowIdx, 1, 4)); | |
| 242 | + sheet.addMergedRegion(new CellRangeAddress(newRowIdx, newRowIdx, 5, 8)); | |
| 243 | + } | |
| 244 | + } | |
| 245 | + // "申请净重"标签(A列)纵向合并到最后一行申请行,合并后设置水平/垂直居中(克隆原样式避免破坏边框) | |
| 246 | + sheet.addMergedRegion(new CellRangeAddress(APPLY_ROW, APPLY_ROW + applySize - 1, 0, 0)); | |
| 247 | + Cell aCell = sheet.getRow(APPLY_ROW).getCell(0); | |
| 248 | + if (aCell != null) { | |
| 249 | + CellStyle centerStyle = workbook.createCellStyle(); | |
| 250 | + centerStyle.cloneStyleFrom(aCell.getCellStyle()); | |
| 251 | + centerStyle.setAlignment(HorizontalAlignment.CENTER); | |
| 252 | + centerStyle.setVerticalAlignment(VerticalAlignment.CENTER); | |
| 253 | + for (int r = APPLY_ROW; r <= APPLY_ROW + applySize - 1; r++) { | |
| 254 | + Cell ac = sheet.getRow(r).getCell(0); | |
| 255 | + if (ac != null) { | |
| 256 | + ac.setCellStyle(centerStyle); | |
| 257 | + } | |
| 258 | + } | |
| 259 | + } | |
| 260 | + } | |
| 261 | + | |
| 262 | + // 填充申请净重明细(每条一行:B:E类别、F:I数量) | |
| 263 | + for (int i = 0; i < applySize; i++) { | |
| 264 | + GetProcurementMaterialSettleDetailBo.ApplyLineBo apply = applyLines.get(i); | |
| 265 | + int rowIdx = APPLY_ROW + i; | |
| 266 | + if (apply.getCategory() != null) { | |
| 267 | + ExcelUtil.setCellValue(sheet, rowIdx, 1, apply.getCategory()); | |
| 268 | + } | |
| 269 | + if (apply.getQuantity() != null) { | |
| 270 | + ExcelUtil.setCellValue(sheet, rowIdx, 5, apply.getQuantity()); | |
| 271 | + } | |
| 272 | + } | |
| 273 | + | |
| 274 | + // 结算明细:固定14行(模板Excel第6~19行,随申请净重插行下移),最多14条(超出取前14条),不足留空 | |
| 275 | + List<GetProcurementMaterialSettleDetailBo.LineBo> lines = data.getLines(); | |
| 276 | + int lineSize = lines == null ? 0 : Math.min(lines.size(), SETTLE_LINE_MAX); | |
| 277 | + int lineStart = SETTLE_LINE_START_ROW + insertCount; | |
| 278 | + for (int i = 0; i < lineSize; i++) { | |
| 279 | + fillSettleLineRow(sheet, lineStart + i, lines.get(i)); | |
| 280 | + } | |
| 281 | + | |
| 282 | + // 主表占位符替换(${字段} / #{字段},模板里用到哪个替换哪个) | |
| 283 | + // 日期(码单日期,年月日):模板用 ${year}年${month}月${day}日 拆分填充,也支持 ${ticketDate} 整体填充 | |
| 284 | + LocalDate ticketDate = data.getTicketDate(); | |
| 285 | + Map<String, Object> dataMap = new HashMap<>(); | |
| 286 | + dataMap.put("year", ticketDate == null ? "" : String.valueOf(ticketDate.getYear())); | |
| 287 | + dataMap.put("month", ticketDate == null ? "" : String.valueOf(ticketDate.getMonthValue())); | |
| 288 | + dataMap.put("day", ticketDate == null ? "" : String.valueOf(ticketDate.getDayOfMonth())); | |
| 289 | + dataMap.put("ticketDate", ticketDate == null ? "" | |
| 290 | + : ticketDate.getYear() + "年" + ticketDate.getMonthValue() | |
| 291 | + + "月" + ticketDate.getDayOfMonth() + "日"); | |
| 292 | + dataMap.put("planCodes", CollectionUtils.isEmpty(data.getPlanCode()) ? "" | |
| 293 | + : String.join(",", data.getPlanCode())); | |
| 294 | + dataMap.put("supplierName", nvl(data.getSupplierName())); | |
| 295 | + dataMap.put("totalWeighQuantity", formatDecimal(data.getTotalWeighQuantity())); | |
| 296 | + dataMap.put("totalFoldWeight", formatDecimal(data.getTotalFoldWeight())); | |
| 297 | + dataMap.put("totalReturnWeight", formatDecimal(data.getTotalReturnWeight())); | |
| 298 | + dataMap.put("totalNetWeight", formatDecimal(data.getTotalNetWeight())); | |
| 299 | + dataMap.put("remark", nvl(data.getRemark())); | |
| 300 | + dataMap.put("ckzgName", nvl(data.getCkzgName())); | |
| 301 | + dataMap.put("gyglkzjyName", nvl(data.getGyglkzjyName())); | |
| 302 | + dataMap.put("createBy", nvl(data.getCreateBy())); | |
| 303 | + ExcelUtil.processTemplateSheet(sheet, dataMap); | |
| 304 | + | |
| 305 | + // 说明区勾选:按结算类型把对应行的 □ 改为 ■(1经销/2双经销/3买断) | |
| 306 | + markSettleType(sheet, NOTE_ROW + insertCount, data.getSettleType()); | |
| 307 | + | |
| 308 | + if ("PDF".equals(exportType)) { | |
| 309 | + // 导出 PDF:先写临时 .xlsx 文件,再调 LibreOffice 转换输出 | |
| 310 | + ResponseUtil.setPDFResponseHead(response, | |
| 311 | + nvl(data.getWeighingTicketCode()) + "-原料结算详情单.pdf"); | |
| 312 | + File tempExcel = File.createTempFile( | |
| 313 | + "material_settle_detail_" + nvl(data.getWeighingTicketCode()), ".xlsx"); | |
| 314 | + try { | |
| 315 | + try (FileOutputStream fos = new FileOutputStream(tempExcel)) { | |
| 316 | + workbook.write(fos); | |
| 317 | + } | |
| 318 | + File pdfFile = ExcelUtil.convertExcelToPdf(tempExcel, | |
| 319 | + "/usr/bin/libreoffice --headless --convert-to pdf --outdir %s %s"); | |
| 320 | + try (InputStream pdfIn = new FileInputStream(pdfFile)) { | |
| 321 | + byte[] buffer = new byte[8192]; | |
| 322 | + int len; | |
| 323 | + while ((len = pdfIn.read(buffer)) != -1) { | |
| 324 | + response.getOutputStream().write(buffer, 0, len); | |
| 325 | + } | |
| 326 | + response.getOutputStream().flush(); | |
| 327 | + } finally { | |
| 328 | + if (pdfFile != null && pdfFile.exists()) { | |
| 329 | + Files.delete(pdfFile.toPath()); | |
| 330 | + } | |
| 331 | + } | |
| 332 | + } finally { | |
| 333 | + if (tempExcel != null && tempExcel.exists()) { | |
| 334 | + Files.delete(tempExcel.toPath()); | |
| 335 | + } | |
| 336 | + } | |
| 337 | + } else { | |
| 338 | + // 默认导出 Excel | |
| 339 | + ResponseUtil.setExcelResponseHead(response, | |
| 340 | + nvl(data.getWeighingTicketCode()) + "-原料结算详情单.xlsx"); | |
| 341 | + workbook.write(response.getOutputStream()); | |
| 342 | + response.getOutputStream().flush(); | |
| 343 | + } | |
| 344 | + } catch (FileNotFoundException e) { | |
| 345 | + throw new RuntimeException("模板文件不存在: " + templateFile, e); | |
| 346 | + } catch (IOException e) { | |
| 347 | + throw new RuntimeException("无法读取模板文件: " + templateFile, e); | |
| 348 | + } | |
| 349 | + } catch (Exception e) { | |
| 350 | + log.error("打印原料结算详情单失败, id={}, {}", id, e.getMessage(), e); | |
| 351 | + } | |
| 352 | + } | |
| 353 | + | |
| 354 | + /** | |
| 355 | + * 填充一行结算明细(列布局固定:A类别、B名称、D过磅数、E杂率%、F折斤、G退料、H入库净重;B:C、H:I为模板合并列,值填主格) | |
| 356 | + */ | |
| 357 | + private void fillSettleLineRow(Sheet sheet, int rowIdx, | |
| 358 | + GetProcurementMaterialSettleDetailBo.LineBo line) { | |
| 359 | + if (line == null) { | |
| 360 | + return; | |
| 361 | + } | |
| 362 | + if (line.getCategory() != null) { | |
| 363 | + ExcelUtil.setCellValue(sheet, rowIdx, 0, line.getCategory()); | |
| 364 | + } | |
| 365 | + if (line.getName() != null) { | |
| 366 | + ExcelUtil.setCellValue(sheet, rowIdx, 1, line.getName()); | |
| 367 | + } | |
| 368 | + if (line.getWeighQuantity() != null) { | |
| 369 | + ExcelUtil.setCellValue(sheet, rowIdx, 3, line.getWeighQuantity()); | |
| 370 | + } | |
| 371 | + if (StringUtil.isNotBlank(line.getImpurityRate())) { | |
| 372 | + ExcelUtil.setCellValue(sheet, rowIdx, 4, line.getImpurityRate()); | |
| 373 | + } | |
| 374 | + if (line.getFoldWeight() != null) { | |
| 375 | + ExcelUtil.setCellValue(sheet, rowIdx, 5, line.getFoldWeight()); | |
| 376 | + } | |
| 377 | + if (line.getReturnWeight() != null) { | |
| 378 | + ExcelUtil.setCellValue(sheet, rowIdx, 6, line.getReturnWeight()); | |
| 379 | + } | |
| 380 | + if (line.getNetWeight() != null) { | |
| 381 | + ExcelUtil.setCellValue(sheet, rowIdx, 7, line.getNetWeight()); | |
| 382 | + } | |
| 383 | + } | |
| 384 | + | |
| 385 | + /** | |
| 386 | + * 说明区勾选(B列):按结算类型把对应行的 □ 改为 ■ | |
| 387 | + * (结算类型存枚举代码:经销DISTRIBUTION/双经销DUAL_DISTRIBUTION/买断BUYOUT;模板行文本为中文且含空格,需去空白后匹配) | |
| 388 | + */ | |
| 389 | + private void markSettleType(Sheet sheet, int noteRowIdx, String settleType) { | |
| 390 | + if (StringUtil.isBlank(settleType)) { | |
| 391 | + return; | |
| 392 | + } | |
| 393 | + Row row = sheet.getRow(noteRowIdx); | |
| 394 | + if (row == null) { | |
| 395 | + return; | |
| 396 | + } | |
| 397 | + Cell cell = row.getCell(1); | |
| 398 | + if (cell == null || cell.getCellTypeEnum() != CellType.STRING | |
| 399 | + || StringUtil.isBlank(cell.getStringCellValue())) { | |
| 400 | + return; | |
| 401 | + } | |
| 402 | + String[] texts = cell.getStringCellValue().split("\n"); | |
| 403 | + for (int i = 0; i < texts.length; i++) { | |
| 404 | + // 行文本去空白后匹配(模板中"经 销""买 断"中间有空格) | |
| 405 | + String line = texts[i].replaceAll("\\s+", ""); | |
| 406 | + boolean hit; | |
| 407 | + if ("DUAL_DISTRIBUTION".equals(settleType)) { | |
| 408 | + hit = line.contains("双经销"); | |
| 409 | + } else if ("DISTRIBUTION".equals(settleType)) { | |
| 410 | + hit = line.contains("经销") && !line.contains("双经销"); | |
| 411 | + } else if ("BUYOUT".equals(settleType)) { | |
| 412 | + hit = line.contains("买断"); | |
| 413 | + } else { | |
| 414 | + hit = false; | |
| 415 | + } | |
| 416 | + if (hit) { | |
| 417 | + texts[i] = texts[i].replaceFirst("□", "■"); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + cell.setCellValue(String.join("\n", texts)); | |
| 421 | + } | |
| 422 | + | |
| 423 | + private String formatDecimal(BigDecimal bd) { | |
| 424 | + if (bd == null) { | |
| 425 | + return ""; | |
| 426 | + } | |
| 427 | + return bd.stripTrailingZeros().toPlainString(); | |
| 428 | + } | |
| 429 | + | |
| 430 | + private String nvl(Object o) { | |
| 431 | + return o == null ? "" : o.toString(); | |
| 432 | + } | |
| 143 | 433 | } | ... | ... |
| ... | ... | @@ -263,7 +263,8 @@ public class ProcurementMaterialSettleDetailServiceImpl |
| 263 | 263 | throw new InputErrorException("ID不能为空!"); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - ProcurementMaterialSettleDetail entity = this.getById(id); | |
| 266 | + // 用 XML 联表查询(带出 planCodes/planIds/workshopCode 等 exist=false 字段,getById 查不出) | |
| 267 | + ProcurementMaterialSettleDetail entity = settleDetailMapper.findById(id); | |
| 267 | 268 | if (entity == null) { |
| 268 | 269 | throw new DefaultClientException("原料结算详情单不存在!"); |
| 269 | 270 | } | ... | ... |
No preview for this file type