| | |
| | | package com.ruoyi.sales.service.impl; |
| | | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.deepoove.poi.XWPFTemplate; |
| | | import com.deepoove.poi.config.Configure; |
| | | import com.ruoyi.account.pojo.AccountIncome; |
| | | import com.ruoyi.account.service.AccountIncomeService; |
| | | import com.ruoyi.basic.mapper.CustomerMapper; |
| | | import com.ruoyi.basic.mapper.ProductMapper; |
| | |
| | | import com.ruoyi.common.enums.FileNameType; |
| | | import com.ruoyi.common.exception.base.BaseException; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.HackLoopTableRenderPolicy; |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.device.execl.DeviceMaintenanceExeclDto; |
| | | import com.ruoyi.framework.security.LoginUser; |
| | | import com.ruoyi.framework.web.domain.AjaxResult; |
| | | import com.ruoyi.other.mapper.TempFileMapper; |
| | |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.FilenameUtils; |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.xssf.usermodel.XSSFSheet; |
| | | import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.nio.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.YearMonth; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | | * 销售台账Service业务层处理 |
| | |
| | | |
| | | @Override |
| | | public void exportShippingNote(HttpServletResponse response, List<Long> ids) { |
| | | log.info("开始导出送货单,台账ID列表: {}", ids); |
| | | try { |
| | | List<SalesLedger> ledgerList = salesLedgerMapper.selectBatchIds(ids); |
| | | String fileName = "送货单.xlsx"; |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); |
| | | response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | | |
| | | // 参数校验 |
| | | if (ids == null || ids.isEmpty()) { |
| | | throw new BaseException("请选择要导出的销售台账"); |
| | | } |
| | | XSSFWorkbook workbook = new XSSFWorkbook(); |
| | | |
| | | List<SalesLedger> salesLedgerList = salesLedgerMapper.selectList( |
| | | Wrappers.<SalesLedger>lambdaQuery() |
| | | .in(SalesLedger::getId, ids) |
| | | ); |
| | | for (SalesLedger ledger : ledgerList) { |
| | | XSSFSheet sheet = workbook.createSheet(ledger.getSalesContractNo()); |
| | | |
| | | if (salesLedgerList.isEmpty()) { |
| | | throw new BaseException("未找到对应销售台账数据"); |
| | | } |
| | | // 设置页边距(单位:英寸,1英寸=2.54厘米) |
| | | sheet.getPrintSetup().setLandscape(false); // 纵向打印 |
| | | sheet.setMargin(Sheet.TopMargin, 0.54 / 2.54); // 上页边距:0.54厘米 |
| | | sheet.setMargin(Sheet.BottomMargin, 0.54 / 2.54); // 下页边距:0.54厘米 |
| | | sheet.setMargin(Sheet.LeftMargin, 0.41 / 2.54); // 左页边距:0.41厘米 |
| | | sheet.setMargin(Sheet.RightMargin, 0.41 / 2.54); // 右页边距:0.41厘米 |
| | | sheet.setMargin(Sheet.HeaderMargin, 0.77 / 2.54); // 页眉:0.77厘米 |
| | | sheet.setMargin(Sheet.FooterMargin, 0.27 / 2.54); // 页脚:0.27厘米 |
| | | |
| | | response.setContentType("application/zip"); |
| | | response.setHeader("Content-Disposition", "attachment;filename=送货单.zip"); |
| | | response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
| | | sheet.setColumnWidth(0, 1310); |
| | | sheet.setColumnWidth(1, 4000); |
| | | sheet.setColumnWidth(2, 8980); |
| | | sheet.setColumnWidth(3, 1820); |
| | | sheet.setColumnWidth(4, 2880); |
| | | sheet.setColumnWidth(5, 5090); |
| | | sheet.setColumnWidth(6, 1410); |
| | | |
| | | // 直接内存打包,抛弃临时文件 |
| | | try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) { |
| | | CellStyle companyTitle = createBaseStyle(workbook, "宋体", (short) 20, true, true); |
| | | CellStyle billTitle = createBaseStyle(workbook, "宋体", (short) 18, false, true); |
| | | CellStyle headerStyle = createBorderStyle(workbook, "宋体", (short) 12, true, true); |
| | | CellStyle dataCenterStyle = createBorderStyle(workbook, "宋体", (short) 11, false, true); |
| | | |
| | | for (SalesLedger ledger : salesLedgerList) { |
| | | log.info("处理合同号: {}", ledger.getSalesContractNo()); |
| | | // 样式1:带虚线 + 居中 |
| | | CellStyle dashStyle = createDashBottomStyle(workbook); |
| | | dashStyle.setAlignment(HorizontalAlignment.CENTER); |
| | | dashStyle.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | |
| | | // 查询明细 |
| | | // 样式2:无边框 + 居中(给仓库用) |
| | | CellStyle centerNoBorder = createNoBorderStyle(workbook); |
| | | centerNoBorder.setAlignment(HorizontalAlignment.CENTER); |
| | | centerNoBorder.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | |
| | | // 标题 |
| | | Row row0 = sheet.createRow(0); |
| | | row0.createCell(0).setCellValue("湖南鹏创电子有限公司"); |
| | | sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6)); |
| | | fillMergeRange(sheet, 0, 0, 0, 6, companyTitle); |
| | | |
| | | Row row1 = sheet.createRow(1); |
| | | row1.createCell(0).setCellValue("送 货 单"); |
| | | sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6)); |
| | | fillMergeRange(sheet, 1, 1, 0, 6, billTitle); |
| | | |
| | | Customer customer = customerMapper.selectById(ledger.getCustomerId()); |
| | | |
| | | Row row2 = sheet.createRow(2); |
| | | row2.setHeightInPoints(13); |
| | | row2.createCell(0).setCellValue("客户名称:" + ledger.getCustomerName()); |
| | | row2.createCell(4).setCellValue("单据编号:" + ledger.getSalesContractNo()); |
| | | |
| | | Row row3 = sheet.createRow(3); |
| | | row3.setHeightInPoints(13); |
| | | row3.createCell(0).setCellValue("送货地址:" + customer.getCompanyAddress()); |
| | | row3.createCell(4).setCellValue("送货日期:" + formatDate(LocalDate.now())); |
| | | |
| | | // 空行 |
| | | Row row4 = sheet.createRow(4); |
| | | row4.setHeightInPoints(13); |
| | | row4.createCell(4).setCellValue("联系电话:" + customer.getCompanyPhone()); |
| | | |
| | | Row row5 = sheet.createRow(5); |
| | | row5.createCell(0).setCellValue("联系方式:" + customer.getContactPerson() + " " + customer.getContactPhone()); |
| | | Row row6 = sheet.createRow(6); |
| | | row6.createCell(0).setCellValue("送货单位:湖南鹏创电子有限公司"); |
| | | Row row7 = sheet.createRow(7); |
| | | row7.createCell(0).setCellValue("地址:湖南省耒阳市创新创业园A1栋"); |
| | | Row row8 = sheet.createRow(8); |
| | | row8.setHeightInPoints((short) 3); // 行高3磅 |
| | | Row row9 = sheet.createRow(9); |
| | | row9.createCell(0).setCellValue("货物详细信息:"); |
| | | |
| | | int tableStart = 10; |
| | | Row headerRow = sheet.createRow(tableStart); |
| | | headerRow.setHeightInPoints((short) 18); |
| | | |
| | | String[] headers = {"序号", "物料编号", "品名/规格", "单位", "数量", "订单号", "共四联"}; |
| | | for (int i = 0; i < headers.length; i++) { |
| | | Cell hc = headerRow.createCell(i); |
| | | hc.setCellValue(headers[i]); |
| | | if (i == 6) { |
| | | hc.setCellStyle(dashStyle); |
| | | } else { |
| | | hc.setCellStyle(headerStyle); |
| | | } |
| | | } |
| | | |
| | | // 查询产品 |
| | | SalesLedgerProduct cond = new SalesLedgerProduct(); |
| | | cond.setSalesLedgerId(ledger.getId()); |
| | | List<SalesLedgerProduct> productList = salesLedgerProductServiceImpl.selectSalesLedgerProductList(cond); |
| | | |
| | | // 自动序号 |
| | | BigDecimal totalQuantity = BigDecimal.ZERO; |
| | | for (SalesLedgerProduct p : productList) { |
| | | if (p.getQuantity() != null) totalQuantity = totalQuantity.add(p.getQuantity()); |
| | | } |
| | | |
| | | int currentRow = tableStart + 1; |
| | | String[] fourNames = {"存根", "回单", "客户", "仓库"}; |
| | | |
| | | // 1. 所有产品 |
| | | for (int i = 0; i < productList.size(); i++) { |
| | | productList.get(i).setSerialNumber(i + 1); |
| | | SalesLedgerProduct p = productList.get(i); |
| | | Row dataRow = sheet.createRow(currentRow++); |
| | | dataRow.setHeightInPoints((short) 18); |
| | | |
| | | String materialCode = ""; |
| | | if (p.getProductModelId() != null) { |
| | | ProductModel m = productModelMapper.selectById(p.getProductModelId()); |
| | | if (m != null) materialCode = m.getMaterialCode(); |
| | | } |
| | | |
| | | dataRow.createCell(0).setCellValue(i + 1); |
| | | dataRow.createCell(1).setCellValue(materialCode); |
| | | dataRow.createCell(2).setCellValue(p.getProductCategory() + "/" + p.getSpecificationModel()); |
| | | dataRow.createCell(3).setCellValue(p.getUnit()); |
| | | dataRow.createCell(4).setCellValue(p.getQuantity() == null ? 0 : p.getQuantity().doubleValue()); |
| | | dataRow.createCell(5).setCellValue(ledger.getSalesContractNo()); |
| | | |
| | | for (int c = 0; c <= 5; c++) dataRow.getCell(c).setCellStyle(dataCenterStyle); |
| | | |
| | | Cell fourCell = dataRow.createCell(6); |
| | | if (i < fourNames.length) { |
| | | fourCell.setCellValue(fourNames[i]); |
| | | if (i <= 2) { |
| | | fourCell.setCellStyle(dashStyle); |
| | | } else { |
| | | fourCell.setCellStyle(centerNoBorder); |
| | | } |
| | | } else { |
| | | fourCell.setCellStyle(centerNoBorder); |
| | | } |
| | | } |
| | | |
| | | // 关联查询客户信息 |
| | | Customer customer = customerMapper.selectById(ledger.getCustomerId()); |
| | | // 组装数据 |
| | | Map<String, Object> dataMap = new HashMap<>(); |
| | | dataMap.put("customerName", ledger.getCustomerName()); |
| | | dataMap.put("companyAddress", customer.getCompanyAddress()); |
| | | dataMap.put("contactPerson", customer.getContactPerson()); |
| | | dataMap.put("contactPhone", customer.getContactPhone()); |
| | | dataMap.put("companyPhone", customer.getCompanyPhone()); |
| | | dataMap.put("salesContractNo", ledger.getSalesContractNo()); |
| | | dataMap.put("deliveryDate", formatDate(LocalDate.now())); |
| | | // 放入商品集合 |
| | | dataMap.put("products", productList); |
| | | // 2. 不足6行补空 |
| | | int minRow = 6; |
| | | int needEmpty = Math.max(0, minRow - productList.size()); |
| | | for (int i = 0; i < needEmpty; i++) { |
| | | int seq = productList.size() + i; |
| | | Row dataRow = sheet.createRow(currentRow++); |
| | | dataRow.setHeightInPoints((short) 18); |
| | | |
| | | // 每次循环全新读取模板,彻底解决流问题 |
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| | | try (InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx")) { |
| | | if (templateIs == null) throw new BaseException("模板文件不存在"); |
| | | for (int c = 0; c <= 5; c++) dataRow.createCell(c).setCellStyle(dataCenterStyle); |
| | | |
| | | // EasyExcel填充 |
| | | ExcelWriter writer = EasyExcel.write(bos).withTemplate(templateIs).build(); |
| | | WriteSheet sheet = EasyExcel.writerSheet().build(); |
| | | writer.fill(dataMap, sheet); |
| | | writer.finish(); |
| | | Cell fourCell = dataRow.createCell(6); |
| | | if (seq < fourNames.length) { |
| | | fourCell.setCellValue(fourNames[seq]); |
| | | if (seq <= 2) { |
| | | fourCell.setCellStyle(dashStyle); |
| | | } else { |
| | | fourCell.setCellStyle(centerNoBorder); |
| | | } |
| | | } else { |
| | | fourCell.setCellStyle(centerNoBorder); |
| | | } |
| | | } |
| | | |
| | | // 打包进ZIP |
| | | String excelName = String.format("送货单_%s.xlsx", |
| | | Optional.ofNullable(ledger.getSalesContractNo()).orElse(String.valueOf(ledger.getId()))); |
| | | ZipEntry entry = new ZipEntry(excelName); |
| | | zos.putNextEntry(entry); |
| | | zos.write(bos.toByteArray()); |
| | | zos.closeEntry(); |
| | | bos.close(); |
| | | // 合计 |
| | | Row totalRow = sheet.createRow(currentRow); |
| | | totalRow.setHeightInPoints((short) 18); |
| | | |
| | | // 合计放在第二列,不合并单元格 |
| | | totalRow.createCell(1).setCellValue("合计"); |
| | | totalRow.createCell(4).setCellValue(totalQuantity.doubleValue()); |
| | | |
| | | // 设置样式 |
| | | for (int c = 0; c <= 5; c++) { |
| | | Cell cell = totalRow.getCell(c) != null ? totalRow.getCell(c) : totalRow.createCell(c); |
| | | cell.setCellStyle(dataCenterStyle); |
| | | } |
| | | totalRow.createCell(6).setCellStyle(centerNoBorder); |
| | | |
| | | currentRow++; |
| | | Row emptyRow1 = sheet.createRow(currentRow); |
| | | emptyRow1.setHeightInPoints((short) 9); // 行高9磅 |
| | | |
| | | currentRow++; |
| | | Row remarkRow = sheet.createRow(currentRow); |
| | | remarkRow.createCell(0).setCellValue("备注:贵司在收货后请即刻核实数量及品质,若有异议,请在3日内提出,否则视为收妥。"); |
| | | sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 6)); |
| | | |
| | | currentRow++; |
| | | Row emptyRow2 = sheet.createRow(currentRow); |
| | | emptyRow2.setHeightInPoints((short) 9); // 行高9磅 |
| | | |
| | | currentRow++; |
| | | Row sign1 = sheet.createRow(currentRow); |
| | | sign1.createCell(0).setCellValue("送货单位(签章):"); |
| | | sign1.createCell(4).setCellValue("收货单位:"); |
| | | |
| | | currentRow++; |
| | | Row sign2 = sheet.createRow(currentRow); |
| | | sign2.createCell(0).setCellValue("送货人:"); |
| | | sign2.createCell(4).setCellValue("收货人:"); |
| | | } |
| | | |
| | | zos.finish(); |
| | | log.info("批量导出完成,共{}份送货单", salesLedgerList.size()); |
| | | workbook.write(response.getOutputStream()); |
| | | workbook.close(); |
| | | |
| | | } catch (BaseException e) { |
| | | log.error("导出失败:{}", e.getMessage(), e); |
| | | throw e; |
| | | } catch (Exception e) { |
| | | log.error("导出异常", e); |
| | | throw new BaseException("导出失败:" + e.getMessage()); |
| | | log.error("批量导出送货单异常", e); |
| | | throw new RuntimeException("导出失败"); |
| | | } |
| | | } |
| | | |
| | | // 工具类 |
| | | private CellStyle createBaseStyle(Workbook workbook, String fontName, short fontSize, boolean bold, boolean center) { |
| | | CellStyle style = workbook.createCellStyle(); |
| | | Font font = workbook.createFont(); |
| | | font.setFontName(fontName); |
| | | font.setFontHeightInPoints(fontSize); |
| | | font.setBold(bold); |
| | | style.setFont(font); |
| | | if (center) { |
| | | style.setAlignment(HorizontalAlignment.CENTER); |
| | | style.setVerticalAlignment(VerticalAlignment.CENTER); |
| | | } |
| | | return style; |
| | | } |
| | | |
| | | private CellStyle createBorderStyle(Workbook workbook, String fontName, short fontSize, boolean bold, boolean center) { |
| | | CellStyle style = createBaseStyle(workbook, fontName, fontSize, bold, center); |
| | | style.setBorderTop(BorderStyle.THIN); |
| | | style.setBorderBottom(BorderStyle.THIN); |
| | | style.setBorderLeft(BorderStyle.THIN); |
| | | style.setBorderRight(BorderStyle.THIN); |
| | | return style; |
| | | } |
| | | |
| | | private CellStyle createNoBorderStyle(Workbook workbook) { |
| | | return createBaseStyle(workbook, "宋体", (short) 11, false, false); |
| | | } |
| | | |
| | | private CellStyle createDashBottomStyle(Workbook workbook) { |
| | | CellStyle style = createNoBorderStyle(workbook); |
| | | style.setBorderBottom(BorderStyle.DASHED); |
| | | return style; |
| | | } |
| | | |
| | | private void fillMergeRange(Sheet sheet, int startR, int endR, int startC, int endC, CellStyle style) { |
| | | for (int r = startR; r <= endR; r++) { |
| | | Row row = sheet.getRow(r) == null ? sheet.createRow(r) : sheet.getRow(r); |
| | | for (int c = startC; c <= endC; c++) { |
| | | Cell cell = row.getCell(c) == null ? row.createCell(c) : row.getCell(c); |
| | | cell.setCellStyle(style); |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | if (date == null) { |
| | | return null; |
| | | } |
| | | return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); |
| | | return date.format(DateTimeFormatter.ofPattern("yyyy/MM/dd")); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public IPage<SalesLedger> selectSalesLedgerListPage(Page page, SalesLedgerDto salesLedgerDto) { |
| | | public IPage<SalesLedgerDto> selectSalesLedgerListPage(Page page, SalesLedgerDto salesLedgerDto) { |
| | | return salesLedgerMapper.selectSalesLedgerListPage(page, salesLedgerDto); |
| | | } |
| | | |
| | |
| | | // 4. 处理子表数据 |
| | | List<SalesLedgerProduct> productList = salesLedgerDto.getProductData(); |
| | | if (productList != null && !productList.isEmpty()) { |
| | | handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType(), salesLedgerDto.isProduce()); |
| | | handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType(), Boolean.TRUE.equals(salesLedgerDto.getProduce())); |
| | | updateMainContractAmount( |
| | | salesLedger.getId(), |
| | | productList, |
| | |
| | | salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice()); |
| | | salesLedgerProductMapper.insert(salesLedgerProduct); |
| | | |
| | | } |
| | | } |
| | | if (isProduce) { |
| | | for (SalesLedgerProduct salesLedgerProduct : products) { |
| | | // 添加生产数据 |
| | | if (isProduce) { |
| | | salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct); |
| | | } |
| | | salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct); |
| | | } |
| | | } |
| | | } |
| | |
| | | private SalesLedger convertToEntity(SalesLedgerDto dto) { |
| | | SalesLedger entity = new SalesLedger(); |
| | | BeanUtils.copyProperties(dto, entity); |
| | | entity.setProduce(dto.getProduce()); |
| | | return entity; |
| | | } |
| | | |
| | |
| | | throw new RuntimeException("动态更新主表金额失败", e); |
| | | } |
| | | } |
| | | } |
| | | } |