Showing
2 changed files
with
353 additions
and
11 deletions
xingyun-sc/src/main/java/com/lframework/xingyun/sc/controller/order/PurchaseOrderInfoController.java
| 1 | 1 | package com.lframework.xingyun.sc.controller.order; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.io.resource.ClassPathResource; | |
| 4 | +import com.baomidou.mybatisplus.core.conditions.Wrapper; | |
| 5 | +import com.baomidou.mybatisplus.core.toolkit.Wrappers; | |
| 6 | +import com.lframework.starter.common.exceptions.impl.DefaultClientException; | |
| 7 | +import com.lframework.starter.common.utils.CollectionUtil; | |
| 3 | 8 | import com.lframework.starter.web.core.annotations.security.HasPermission; |
| 9 | +import com.lframework.starter.web.core.components.resp.InvokeResult; | |
| 10 | +import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; | |
| 11 | +import com.lframework.starter.web.core.components.resp.PageResult; | |
| 4 | 12 | import com.lframework.starter.web.core.controller.DefaultBaseController; |
| 5 | 13 | import com.lframework.starter.web.core.utils.PageResultUtil; |
| 6 | -import com.lframework.starter.web.core.components.resp.PageResult; | |
| 7 | -import com.lframework.starter.web.core.components.resp.InvokeResult; | |
| 8 | - | |
| 9 | -import javax.annotation.Resource; | |
| 10 | -import javax.validation.constraints.NotBlank; | |
| 14 | +import com.lframework.starter.web.inner.entity.SysUser; | |
| 15 | +import com.lframework.starter.web.inner.service.system.SysUserService; | |
| 11 | 16 | import com.lframework.xingyun.sc.bo.order.GetPurchaseOrderInfoBo; |
| 12 | 17 | import com.lframework.xingyun.sc.entity.PurchaseOrderInfo; |
| 13 | 18 | import com.lframework.xingyun.sc.entity.PurchaseOrderLine; |
| ... | ... | @@ -17,20 +22,37 @@ import com.lframework.xingyun.sc.vo.order.CreatePurchaseOrderInfoVo; |
| 17 | 22 | import com.lframework.xingyun.sc.vo.order.QueryPurchaseOrderInfoVo; |
| 18 | 23 | import com.lframework.xingyun.sc.vo.order.QueryPurchaseOrderLineVo; |
| 19 | 24 | import com.lframework.xingyun.sc.vo.order.UpdatePurchaseOrderInfoVo; |
| 25 | +import io.swagger.annotations.Api; | |
| 20 | 26 | import io.swagger.annotations.ApiImplicitParam; |
| 21 | -import com.lframework.starter.web.core.components.resp.InvokeResultBuilder; | |
| 22 | -import com.lframework.starter.common.exceptions.impl.DefaultClientException; | |
| 23 | 27 | import io.swagger.annotations.ApiOperation; |
| 24 | -import com.lframework.starter.common.utils.CollectionUtil; | |
| 25 | -import io.swagger.annotations.Api; | |
| 26 | -import org.springframework.security.core.parameters.P; | |
| 27 | -import org.springframework.web.bind.annotation.DeleteMapping; | |
| 28 | +import lombok.extern.slf4j.Slf4j; | |
| 29 | +import org.apache.commons.collections4.CollectionUtils; | |
| 30 | +import org.apache.poi.hssf.usermodel.HSSFWorkbook; | |
| 31 | +import org.apache.poi.ss.usermodel.*; | |
| 32 | +import org.apache.poi.ss.util.CellRangeAddress; | |
| 33 | +import org.apache.poi.util.IOUtils; | |
| 34 | +import org.apache.poi.xssf.usermodel.XSSFWorkbook; | |
| 28 | 35 | import org.springframework.beans.factory.annotation.Autowired; |
| 29 | 36 | import org.springframework.validation.annotation.Validated; |
| 30 | 37 | import org.springframework.web.bind.annotation.*; |
| 31 | 38 | |
| 39 | +import javax.annotation.Resource; | |
| 40 | +import javax.servlet.http.HttpServletResponse; | |
| 32 | 41 | import javax.validation.Valid; |
| 42 | +import javax.validation.constraints.NotBlank; | |
| 43 | +import java.io.FileNotFoundException; | |
| 44 | +import java.io.FileOutputStream; | |
| 45 | +import java.io.IOException; | |
| 46 | +import java.io.InputStream; | |
| 47 | +import java.math.BigDecimal; | |
| 48 | +import java.math.RoundingMode; | |
| 49 | +import java.net.URLEncoder; | |
| 50 | +import java.time.LocalDate; | |
| 51 | +import java.time.format.DateTimeFormatter; | |
| 52 | +import java.util.ArrayList; | |
| 53 | +import java.util.HashMap; | |
| 33 | 54 | import java.util.List; |
| 55 | +import java.util.Map; | |
| 34 | 56 | import java.util.stream.Collectors; |
| 35 | 57 | |
| 36 | 58 | /** |
| ... | ... | @@ -38,6 +60,7 @@ import java.util.stream.Collectors; |
| 38 | 60 | * |
| 39 | 61 | */ |
| 40 | 62 | @Api(tags = "订货单表") |
| 63 | +@Slf4j | |
| 41 | 64 | @Validated |
| 42 | 65 | @RestController |
| 43 | 66 | @RequestMapping("/purchaseOrderInfo") |
| ... | ... | @@ -47,6 +70,9 @@ public class PurchaseOrderInfoController extends DefaultBaseController { |
| 47 | 70 | private PurchaseOrderInfoService purchaseOrderInfoService; |
| 48 | 71 | @Resource |
| 49 | 72 | private PurchaseOrderLineService purchaseOrderLineService; |
| 73 | + @Autowired | |
| 74 | + private SysUserService sysUserService; | |
| 75 | + | |
| 50 | 76 | /** |
| 51 | 77 | * 查询列表 |
| 52 | 78 | */ |
| ... | ... | @@ -152,4 +178,320 @@ public class PurchaseOrderInfoController extends DefaultBaseController { |
| 152 | 178 | |
| 153 | 179 | return InvokeResultBuilder.success(); |
| 154 | 180 | } |
| 181 | + | |
| 182 | + @ApiOperation("订购单打印") | |
| 183 | + @GetMapping("/printPurchaseOrder") | |
| 184 | + public void printPurchaseOrder(@NotBlank(message = "id不能为空") String id, HttpServletResponse response) throws IOException { | |
| 185 | + PurchaseOrderInfo data = purchaseOrderInfoService.findById(id); | |
| 186 | + | |
| 187 | + // 设置响应头 | |
| 188 | + setupResponse(response, data.getOrderNo() + "-订购单打印.xls"); | |
| 189 | + | |
| 190 | + Wrapper<PurchaseOrderLine> purchaseOrderLineWrapper = Wrappers.lambdaQuery(PurchaseOrderLine.class) | |
| 191 | + .eq(PurchaseOrderLine::getPurchaseOrderId, id); | |
| 192 | + List<PurchaseOrderLine> purchaseOrderLineList = purchaseOrderLineService.list(purchaseOrderLineWrapper); | |
| 193 | + | |
| 194 | + | |
| 195 | + try { | |
| 196 | + // 加载模板文件 | |
| 197 | + ClassPathResource templateResource = new ClassPathResource("templates/purchaseOrderTemplate.xlsx"); | |
| 198 | + try (InputStream inputStream = templateResource.getStream(); | |
| 199 | + Workbook workbook = new XSSFWorkbook(inputStream)) { | |
| 200 | + try { | |
| 201 | + Sheet sheet = workbook.getSheetAt(0); | |
| 202 | + DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | |
| 203 | + int startRow = 7; // 产品开始行 | |
| 204 | + BigDecimal totalQuantity=BigDecimal.ZERO; | |
| 205 | + if (CollectionUtils.isNotEmpty(purchaseOrderLineList)) { | |
| 206 | + | |
| 207 | + for (int i = startRow + 1; i < startRow + purchaseOrderLineList.size(); i++) { | |
| 208 | + copyRow(workbook, sheet, startRow, i); | |
| 209 | + } | |
| 210 | + | |
| 211 | + for (PurchaseOrderLine currentOrderLine : purchaseOrderLineList) { | |
| 212 | + setCellValue(sheet, startRow, 1, currentOrderLine.getIndustry()); | |
| 213 | + setCellValue(sheet, startRow, 2, currentOrderLine.getQuality()); | |
| 214 | + setCellValue(sheet, startRow, 3, currentOrderLine.getBrand()); | |
| 215 | + | |
| 216 | + //todo 规格 | |
| 217 | + setCellValue(sheet, startRow, 11, currentOrderLine.getStatus()); | |
| 218 | + setCellValue(sheet, startRow, 13, currentOrderLine.getQuantity()); | |
| 219 | + setCellValue(sheet, startRow, 14, currentOrderLine.getSalesPrice()); | |
| 220 | + setCellValue(sheet, startRow, 16, currentOrderLine.getDeliveryDate()); | |
| 221 | + setCellValue(sheet, startRow, 17, currentOrderLine.getAssessmentExceedsAgreement()); | |
| 222 | + totalQuantity = totalQuantity.add(currentOrderLine.getQuantity()); | |
| 223 | + startRow++; | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + Map<String, Object> dataMap = new HashMap<>(); | |
| 228 | + dataMap.put("supplyUnit", data.getSupplyUnit()); | |
| 229 | + dataMap.put("orderNo", data.getOrderNo()); | |
| 230 | + dataMap.put("orderingUnitName", data.getOrderingUnitName()); | |
| 231 | + dataMap.put("orderDate", data.getOrderDate() == null ? "" : data.getOrderDate().format(dateFormatter)); | |
| 232 | + | |
| 233 | + dataMap.put("totalQuantity", totalQuantity.setScale(1, RoundingMode.HALF_UP)); | |
| 234 | + dataMap.put("executionStandard", data.getExecutionStandard()); | |
| 235 | + dataMap.put("deliveryMethod", data.getDeliveryMethod()); | |
| 236 | + dataMap.put("invoicingStatus", data.getInvoicingStatus()); | |
| 237 | + dataMap.put("pieceWeightHeader", data.getPieceWeightHeader()); | |
| 238 | + dataMap.put("surface", data.getSurface()); | |
| 239 | + dataMap.put("tolerance", data.getTolerance()); | |
| 240 | + dataMap.put("performanceance", data.getPerformance()); | |
| 241 | + dataMap.put("packaging", data.getPackaging()); | |
| 242 | + dataMap.put("shippingCost", data.getShippingCost()); | |
| 243 | + dataMap.put("remarks", data.getRemarks()); | |
| 244 | + dataMap.put("createUser", data.getCreateBy()); | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + processTemplate(workbook, dataMap); | |
| 249 | + | |
| 250 | + // 写入响应流 | |
| 251 | + workbook.write(response.getOutputStream()); | |
| 252 | + response.getOutputStream().flush(); | |
| 253 | + } finally { | |
| 254 | + IOUtils.closeQuietly(workbook); | |
| 255 | + } | |
| 256 | + | |
| 257 | + } catch (FileNotFoundException e) { | |
| 258 | + throw new RuntimeException("模板文件不存在: templates/purchaseOrderTemplate.xlsx", e); | |
| 259 | + } catch (IOException e) { | |
| 260 | + throw new RuntimeException("无法读取模板文件: templates/purchaseOrderTemplate.xlsx", e); | |
| 261 | + } | |
| 262 | + } catch (Exception e) { | |
| 263 | + log.error("标准合同模版打印: {}", e.getMessage(), e); | |
| 264 | + throw e; | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | + /** | |
| 269 | + * 设置HTTP响应头 | |
| 270 | + */ | |
| 271 | + private void setupResponse(HttpServletResponse response, String fileName) throws IOException { | |
| 272 | + String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20"); | |
| 273 | + | |
| 274 | + response.setContentType("application/vnd.ms-excel"); | |
| 275 | + response.setCharacterEncoding("UTF-8"); | |
| 276 | + response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName); | |
| 277 | + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * 复制行并处理合并单元格 | |
| 282 | + */ | |
| 283 | + public static void copyRow(Workbook workbook, Sheet sheet, | |
| 284 | + int sourceRowIndex, int targetRowIndex) { | |
| 285 | + | |
| 286 | + // 获取源行的所有合并区域 | |
| 287 | + List<CellRangeAddress> mergedRegions = getMergedRegionsInRow(sheet, sourceRowIndex); | |
| 288 | + | |
| 289 | + // 移动目标行及之后的行 | |
| 290 | + if (targetRowIndex <= sheet.getLastRowNum()) { | |
| 291 | + sheet.shiftRows(targetRowIndex, sheet.getLastRowNum(), 1, true, false); | |
| 292 | + } | |
| 293 | + | |
| 294 | + // 复制行内容 | |
| 295 | + Row sourceRow = sheet.getRow(sourceRowIndex); | |
| 296 | + Row newRow = sheet.createRow(targetRowIndex); | |
| 297 | + newRow.setHeight(sourceRow.getHeight()); | |
| 298 | + | |
| 299 | + // 复制单元格 | |
| 300 | + for (int i = 0; i < sourceRow.getLastCellNum(); i++) { | |
| 301 | + Cell oldCell = sourceRow.getCell(i); | |
| 302 | + Cell newCell = newRow.createCell(i); | |
| 303 | + | |
| 304 | + if (oldCell != null) { | |
| 305 | + copyCell(workbook, oldCell, newCell); | |
| 306 | + } | |
| 307 | + } | |
| 308 | + | |
| 309 | + // 处理合并单元格 | |
| 310 | + handleMergedRegions(sheet, mergedRegions, sourceRowIndex, targetRowIndex); | |
| 311 | + } | |
| 312 | + | |
| 313 | + /** | |
| 314 | + * 获取行中涉及的合并区域 | |
| 315 | + */ | |
| 316 | + private static List<CellRangeAddress> getMergedRegionsInRow(Sheet sheet, int rowIndex) { | |
| 317 | + List<CellRangeAddress> result = new ArrayList<>(); | |
| 318 | + for (int i = 0; i < sheet.getNumMergedRegions(); i++) { | |
| 319 | + CellRangeAddress mergedRegion = sheet.getMergedRegion(i); | |
| 320 | + if (mergedRegion.getFirstRow() == rowIndex) { | |
| 321 | + result.add(mergedRegion); | |
| 322 | + } | |
| 323 | + } | |
| 324 | + return result; | |
| 325 | + } | |
| 326 | + | |
| 327 | + /** | |
| 328 | + * 处理合并单元格的复制 | |
| 329 | + */ | |
| 330 | + private static void handleMergedRegions(Sheet sheet, List<CellRangeAddress> mergedRegions, | |
| 331 | + int sourceRowIndex, int targetRowIndex) { | |
| 332 | + | |
| 333 | + for (CellRangeAddress oldRegion : mergedRegions) { | |
| 334 | + // 创建新的合并区域 | |
| 335 | + CellRangeAddress newRegion = new CellRangeAddress( | |
| 336 | + targetRowIndex, | |
| 337 | + targetRowIndex + (oldRegion.getLastRow() - oldRegion.getFirstRow()), | |
| 338 | + oldRegion.getFirstColumn(), | |
| 339 | + oldRegion.getLastColumn() | |
| 340 | + ); | |
| 341 | + | |
| 342 | + sheet.addMergedRegion(newRegion); | |
| 343 | + | |
| 344 | + // 设置合并区域的边框样式 | |
| 345 | + setMergedRegionBorderStyle(sheet, newRegion); | |
| 346 | + } | |
| 347 | + } | |
| 348 | + | |
| 349 | + /** | |
| 350 | + * 设置合并单元格的边框样式 | |
| 351 | + */ | |
| 352 | + private static void setMergedRegionBorderStyle(Sheet sheet, CellRangeAddress region) { | |
| 353 | + Row firstRow = sheet.getRow(region.getFirstRow()); | |
| 354 | + if (firstRow != null) { | |
| 355 | + Cell firstCell = firstRow.getCell(region.getFirstColumn()); | |
| 356 | + if (firstCell != null && firstCell.getCellStyle() != null) { | |
| 357 | + CellStyle style = firstCell.getCellStyle(); | |
| 358 | + | |
| 359 | + // 为合并区域的所有单元格设置样式 | |
| 360 | + for (int i = region.getFirstRow(); i <= region.getLastRow(); i++) { | |
| 361 | + Row row = sheet.getRow(i); | |
| 362 | + if (row == null) { | |
| 363 | + row = sheet.createRow(i); | |
| 364 | + } | |
| 365 | + for (int j = region.getFirstColumn(); j <= region.getLastColumn(); j++) { | |
| 366 | + Cell cell = row.getCell(j); | |
| 367 | + if (cell == null) { | |
| 368 | + cell = row.createCell(j); | |
| 369 | + } | |
| 370 | + cell.setCellStyle(style); | |
| 371 | + } | |
| 372 | + } | |
| 373 | + } | |
| 374 | + } | |
| 375 | + } | |
| 376 | + | |
| 377 | + /** | |
| 378 | + * 复制单元格内容和样式 | |
| 379 | + */ | |
| 380 | + private static void copyCell(Workbook workbook, Cell oldCell, Cell newCell) { | |
| 381 | + if (oldCell.getCellStyle() != null) { | |
| 382 | + newCell.setCellStyle(oldCell.getCellStyle()); | |
| 383 | + } | |
| 384 | + | |
| 385 | + switch (oldCell.getCellType()) { | |
| 386 | + case 1: | |
| 387 | + //STRING | |
| 388 | + newCell.setCellValue(oldCell.getStringCellValue()); | |
| 389 | + break; | |
| 390 | + case 0: | |
| 391 | + //NUMERIC | |
| 392 | + newCell.setCellValue(oldCell.getNumericCellValue()); | |
| 393 | + break; | |
| 394 | + case 4: | |
| 395 | + //BOOLEAN | |
| 396 | + newCell.setCellValue(oldCell.getBooleanCellValue()); | |
| 397 | + break; | |
| 398 | + case 2: | |
| 399 | + //FORMULA | |
| 400 | + newCell.setCellFormula(oldCell.getCellFormula()); | |
| 401 | + break; | |
| 402 | + default: | |
| 403 | + newCell.setCellValue(oldCell.getStringCellValue()); | |
| 404 | + break; | |
| 405 | + } | |
| 406 | + } | |
| 407 | + | |
| 408 | + /** | |
| 409 | + * 检查指定区域是否存在合并单元格 | |
| 410 | + */ | |
| 411 | + public static boolean hasMergedRegionsInRange(Sheet sheet, int firstRow, int lastRow, | |
| 412 | + int firstCol, int lastCol) { | |
| 413 | + | |
| 414 | + for (int i = 0; i < sheet.getNumMergedRegions(); i++) { | |
| 415 | + CellRangeAddress mergedRegion = sheet.getMergedRegion(i); | |
| 416 | + if (mergedRegion.getFirstRow() >= firstRow && | |
| 417 | + mergedRegion.getLastRow() <= lastRow && | |
| 418 | + mergedRegion.getFirstColumn() >= firstCol && | |
| 419 | + mergedRegion.getLastColumn() <= lastCol) { | |
| 420 | + return true; | |
| 421 | + } | |
| 422 | + } | |
| 423 | + return false; | |
| 424 | + } | |
| 425 | + | |
| 426 | + /** | |
| 427 | + * 设置单元格值 | |
| 428 | + */ | |
| 429 | + private static void setCellValue(Sheet sheet, int rowNum, int colNum, Object value) { | |
| 430 | + Row row = sheet.getRow(rowNum); | |
| 431 | + if (row == null) { | |
| 432 | + row = sheet.createRow(rowNum); | |
| 433 | + } | |
| 434 | + | |
| 435 | + Cell cell = row.getCell(colNum); | |
| 436 | + if (cell == null) { | |
| 437 | + cell = row.createCell(colNum); | |
| 438 | + } | |
| 439 | + | |
| 440 | + if (value == null) { | |
| 441 | + cell.setCellValue(""); | |
| 442 | + } else if (value instanceof String) { | |
| 443 | + cell.setCellValue((String) value); | |
| 444 | + } else if (value instanceof Integer) { | |
| 445 | + cell.setCellValue((Integer) value); | |
| 446 | + } else if (value instanceof Double) { | |
| 447 | + cell.setCellValue((Double) value); | |
| 448 | + } else if (value instanceof BigDecimal) { | |
| 449 | + cell.setCellValue(((BigDecimal) value).doubleValue()); | |
| 450 | + } else if (value instanceof LocalDate) { | |
| 451 | + cell.setCellValue(((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); | |
| 452 | + } | |
| 453 | + } | |
| 454 | + | |
| 455 | + private void processTemplate(Workbook workbook, Map<String, Object> dataMap) { | |
| 456 | + Sheet sheet = workbook.getSheetAt(0); | |
| 457 | + | |
| 458 | + for (Row row : sheet) { | |
| 459 | + for (Cell cell : row) { | |
| 460 | + if (cell.getCellTypeEnum() == CellType.STRING) { | |
| 461 | + String cellValue = cell.getStringCellValue(); | |
| 462 | + String newValue = replacePlaceholders(cellValue, dataMap); | |
| 463 | + if (!cellValue.equals(newValue)) { | |
| 464 | + cell.setCellValue(newValue); | |
| 465 | + } | |
| 466 | + } | |
| 467 | + } | |
| 468 | + } | |
| 469 | + } | |
| 470 | + | |
| 471 | + private String replacePlaceholders(String text, Map<String, Object> dataMap) { | |
| 472 | + if (text == null || text.isEmpty()) { | |
| 473 | + return text; | |
| 474 | + } | |
| 475 | + | |
| 476 | + String result = text; | |
| 477 | + for (Map.Entry<String, Object> entry : dataMap.entrySet()) { | |
| 478 | + String placeholder = "${" + entry.getKey() + "}"; | |
| 479 | + String value = entry.getValue() != null ? entry.getValue().toString() : ""; | |
| 480 | + // 将数据库中的 \n 转换为 Excel 识别的换行符 | |
| 481 | + value = value.replace("\\n", "\n"); | |
| 482 | + | |
| 483 | + result = result.replace(placeholder, value); | |
| 484 | + } | |
| 485 | + | |
| 486 | + for (Map.Entry<String, Object> entry : dataMap.entrySet()) { | |
| 487 | + String placeholder = "#{" + entry.getKey() + "}"; | |
| 488 | + String value = entry.getValue() != null ? entry.getValue().toString() : ""; | |
| 489 | + // 将数据库中的 \n 转换为 Excel 识别的换行符 | |
| 490 | + value = value.replace("\\n", "\n"); | |
| 491 | + | |
| 492 | + result = result.replace(placeholder, value); | |
| 493 | + } | |
| 494 | + | |
| 495 | + return result; | |
| 496 | + } | |
| 155 | 497 | } | ... | ... |
No preview for this file type