| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | 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.file.Files; |
| | | import java.nio.file.Path; |
| | | import java.nio.file.Paths; |
| | |
| | | ); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void exportWithProducts(HttpServletResponse response, SalesLedgerDto salesLedgerDto) { |
| | | try { |
| | | // 1. æ¥è¯¢éå®å°è´¦å表ï¼å¯¼åºä½¿ç¨ååºæåºï¼ |
| | | Page<SalesLedger> page = new Page<>(-1, -1); |
| | | // ä½¿ç¨ Wrappers æå»ºååºæ¥è¯¢ |
| | | LambdaQueryWrapper<SalesLedger> queryWrapper = Wrappers.<SalesLedger>lambdaQuery() |
| | | .orderByAsc(SalesLedger::getEntryDate) |
| | | .orderByAsc(SalesLedger::getId); |
| | | |
| | | // æ·»å æ¥è¯¢æ¡ä»¶ |
| | | if (salesLedgerDto.getCustomerName() != null && !salesLedgerDto.getCustomerName().isEmpty()) { |
| | | queryWrapper.like(SalesLedger::getCustomerName, salesLedgerDto.getCustomerName()); |
| | | } |
| | | if (salesLedgerDto.getSalesContractNo() != null && !salesLedgerDto.getSalesContractNo().isEmpty()) { |
| | | queryWrapper.like(SalesLedger::getSalesContractNo, salesLedgerDto.getSalesContractNo()); |
| | | } |
| | | if (salesLedgerDto.getProjectName() != null && !salesLedgerDto.getProjectName().isEmpty()) { |
| | | queryWrapper.like(SalesLedger::getProjectName, salesLedgerDto.getProjectName()); |
| | | } |
| | | if (salesLedgerDto.getEntryDateStart() != null && !salesLedgerDto.getEntryDateStart().isEmpty()) { |
| | | queryWrapper.ge(SalesLedger::getEntryDate, salesLedgerDto.getEntryDateStart()); |
| | | } |
| | | if (salesLedgerDto.getEntryDateEnd() != null && !salesLedgerDto.getEntryDateEnd().isEmpty()) { |
| | | queryWrapper.le(SalesLedger::getEntryDate, salesLedgerDto.getEntryDateEnd()); |
| | | } |
| | | if (salesLedgerDto.getDeliveryStatus() != null) { |
| | | queryWrapper.eq(SalesLedger::getDeliveryStatus, salesLedgerDto.getDeliveryStatus()); |
| | | } |
| | | if (salesLedgerDto.getStockStatus() != null) { |
| | | queryWrapper.eq(SalesLedger::getStockStatus, salesLedgerDto.getStockStatus()); |
| | | } |
| | | if (salesLedgerDto.getReviewStatus() != null) { |
| | | queryWrapper.eq(SalesLedger::getReviewStatus, salesLedgerDto.getReviewStatus()); |
| | | } |
| | | if (salesLedgerDto.getOrderStatus() != null) { |
| | | queryWrapper.eq(SalesLedger::getOrderStatus, salesLedgerDto.getOrderStatus()); |
| | | } |
| | | if (salesLedgerDto.getReviewStatusList() != null && !salesLedgerDto.getReviewStatusList().isEmpty()) { |
| | | queryWrapper.and(w -> w.in(SalesLedger::getReviewStatus, salesLedgerDto.getReviewStatusList()) |
| | | .or().isNull(SalesLedger::getReviewStatus)); |
| | | } |
| | | |
| | | List<SalesLedger> ledgerList = salesLedgerMapper.selectList(page, queryWrapper); |
| | | |
| | | // 2. æ¶éæ°æ® |
| | | List<SalesLedgerExportDto> ledgerExportList = new ArrayList<>(); |
| | | List<SalesLedgerProductExportDto> productExportList = new ArrayList<>(); |
| | | |
| | | for (SalesLedger ledger : ledgerList) { |
| | | // 转æ¢å°è´¦æ°æ® |
| | | SalesLedgerExportDto ledgerDto = new SalesLedgerExportDto(); |
| | | ledgerDto.setSalesContractNo(ledger.getSalesContractNo()); |
| | | ledgerDto.setCustomerContractNo(ledger.getCustomerContractNo()); |
| | | ledgerDto.setProjectName(ledger.getProjectName()); |
| | | ledgerDto.setCustomerName(ledger.getCustomerName()); |
| | | ledgerDto.setSalesman(ledger.getSalesman()); |
| | | ledgerDto.setEntryPersonName(ledger.getEntryPersonName()); |
| | | ledgerDto.setEntryDate(ledger.getEntryDate()); |
| | | ledgerDto.setExecutionDate(ledger.getExecutionDate() != null ? |
| | | java.sql.Date.valueOf(ledger.getExecutionDate()) : null); |
| | | ledgerDto.setDeliveryDate(ledger.getDeliveryDate() != null ? |
| | | java.sql.Date.valueOf(ledger.getDeliveryDate()) : null); |
| | | ledgerDto.setContractAmount(ledger.getContractAmount()); |
| | | ledgerDto.setRemarks(ledger.getRemarks()); |
| | | ledgerDto.setCustomerRemarks(ledger.getCustomerRemarks()); |
| | | ledgerDto.setDeliveryStatusText(getDeliveryStatusText(ledger.getDeliveryStatus())); |
| | | ledgerDto.setStockStatusText(getStockStatusText(ledger.getStockStatus())); |
| | | ledgerDto.setReviewStatusText(getReviewStatusText(ledger.getReviewStatus())); |
| | | ledgerDto.setOrderStatusText(getOrderStatusText(ledger.getOrderStatus())); |
| | | ledgerExportList.add(ledgerDto); |
| | | |
| | | // æ¥è¯¢è¯¥å°è´¦ç产åå表 |
| | | List<SalesLedgerProduct> products = salesLedgerProductMapper.selectList( |
| | | Wrappers.<SalesLedgerProduct>lambdaQuery() |
| | | .eq(SalesLedgerProduct::getSalesLedgerId, ledger.getId()) |
| | | .eq(SalesLedgerProduct::getType, 1) |
| | | ); |
| | | |
| | | for (SalesLedgerProduct product : products) { |
| | | SalesLedgerProductExportDto productDto = new SalesLedgerProductExportDto(); |
| | | productDto.setSalesContractNo(ledger.getSalesContractNo()); |
| | | productDto.setProductCategory(product.getProductCategory()); |
| | | productDto.setSpecificationModel(product.getSpecificationModel()); |
| | | productDto.setThickness(product.getThickness()); |
| | | productDto.setFloorCode(product.getFloorCode()); |
| | | productDto.setWidth(product.getWidth()); |
| | | productDto.setHeight(product.getHeight()); |
| | | productDto.setQuantity(product.getQuantity()); |
| | | productDto.setSettlePieceArea(product.getSettlePieceArea()); |
| | | productDto.setSettleTotalArea(product.getSettleTotalArea()); |
| | | productDto.setTaxInclusiveUnitPrice(product.getTaxInclusiveUnitPrice()); |
| | | productDto.setTaxRate(product.getTaxRate()); |
| | | productDto.setTaxInclusiveTotalPrice(product.getTaxInclusiveTotalPrice()); |
| | | productDto.setTaxExclusiveTotalPrice(product.getTaxExclusiveTotalPrice()); |
| | | productDto.setInvoiceType(product.getInvoiceType()); |
| | | productDto.setProcessRequirement(product.getProcessRequirement()); |
| | | productDto.setRemark(product.getRemark()); |
| | | productExportList.add(productDto); |
| | | } |
| | | } |
| | | |
| | | // 3. 使ç¨ExcelUtil导åºï¼å
é¨ä¼å建Workbookå¹¶åå
¥ååºï¼ |
| | | // å
åå»ºä¸´æ¶æä»¶ï¼åå并两个sheet |
| | | response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
| | | response.setCharacterEncoding("utf-8"); |
| | | String fileName = URLEncoder.encode("éå®å°è´¦.xlsx", "utf-8").replaceAll("\\+", "%20"); |
| | | response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName); |
| | | |
| | | org.apache.poi.xssf.usermodel.XSSFWorkbook workbook = new org.apache.poi.xssf.usermodel.XSSFWorkbook(); |
| | | |
| | | // Sheet1: éå®å°è´¦ - æå¨å¡«å
|
| | | fillSheetWithData(workbook, "éå®å°è´¦", SalesLedgerExportDto.class, ledgerExportList); |
| | | |
| | | // Sheet2: 产åæç» - æå¨å¡«å
|
| | | fillSheetWithData(workbook, "产åæç»", SalesLedgerProductExportDto.class, productExportList); |
| | | |
| | | workbook.write(response.getOutputStream()); |
| | | workbook.close(); |
| | | |
| | | } catch (Exception e) { |
| | | log.error("导åºéå®å°è´¦å¤±è´¥", e); |
| | | throw new ServiceException("导åºå¤±è´¥ï¼" + e.getMessage()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æå¨å¡«å
Sheetæ°æ® |
| | | */ |
| | | private <T> void fillSheetWithData(org.apache.poi.xssf.usermodel.XSSFWorkbook workbook, String sheetName, Class<T> clazz, List<T> dataList) throws Exception { |
| | | org.apache.poi.ss.usermodel.Sheet sheet = workbook.createSheet(sheetName); |
| | | |
| | | // è·ååæ®µä¸ç@Excel注解 |
| | | java.lang.reflect.Field[] fields = clazz.getDeclaredFields(); |
| | | List<java.lang.reflect.Field> excelFields = new ArrayList<>(); |
| | | for (java.lang.reflect.Field field : fields) { |
| | | com.ruoyi.framework.aspectj.lang.annotation.Excel excel = field.getAnnotation(com.ruoyi.framework.aspectj.lang.annotation.Excel.class); |
| | | if (excel != null) { |
| | | excelFields.add(field); |
| | | } |
| | | } |
| | | |
| | | // å建表头 |
| | | org.apache.poi.ss.usermodel.Row headerRow = sheet.createRow(0); |
| | | org.apache.poi.ss.usermodel.CellStyle headerStyle = workbook.createCellStyle(); |
| | | headerStyle.setFillForegroundColor(org.apache.poi.ss.usermodel.IndexedColors.GREY_50_PERCENT.getIndex()); |
| | | headerStyle.setFillPattern(org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND); |
| | | headerStyle.setAlignment(org.apache.poi.ss.usermodel.HorizontalAlignment.CENTER); |
| | | org.apache.poi.ss.usermodel.Font headerFont = workbook.createFont(); |
| | | headerFont.setBold(true); |
| | | headerStyle.setFont(headerFont); |
| | | |
| | | for (int i = 0; i < excelFields.size(); i++) { |
| | | java.lang.reflect.Field field = excelFields.get(i); |
| | | field.setAccessible(true); |
| | | com.ruoyi.framework.aspectj.lang.annotation.Excel excel = field.getAnnotation(com.ruoyi.framework.aspectj.lang.annotation.Excel.class); |
| | | org.apache.poi.ss.usermodel.Cell cell = headerRow.createCell(i); |
| | | cell.setCellValue(excel.name()); |
| | | cell.setCellStyle(headerStyle); |
| | | sheet.setColumnWidth(i, 20 * 256); |
| | | } |
| | | |
| | | // åå»ºæ°æ®è¡ |
| | | org.apache.poi.ss.usermodel.CellStyle dataStyle = workbook.createCellStyle(); |
| | | dataStyle.setAlignment(org.apache.poi.ss.usermodel.HorizontalAlignment.CENTER); |
| | | |
| | | for (int rowIndex = 0; rowIndex < dataList.size(); rowIndex++) { |
| | | T data = dataList.get(rowIndex); |
| | | org.apache.poi.ss.usermodel.Row row = sheet.createRow(rowIndex + 1); |
| | | for (int colIndex = 0; colIndex < excelFields.size(); colIndex++) { |
| | | java.lang.reflect.Field field = excelFields.get(colIndex); |
| | | field.setAccessible(true); |
| | | Object value = field.get(data); |
| | | org.apache.poi.ss.usermodel.Cell cell = row.createCell(colIndex); |
| | | if (value == null) { |
| | | cell.setCellValue(""); |
| | | } else if (value instanceof Number) { |
| | | cell.setCellValue(((Number) value).doubleValue()); |
| | | } else if (value instanceof Date) { |
| | | cell.setCellValue(new java.text.SimpleDateFormat("yyyy-MM-dd").format((Date) value)); |
| | | } else { |
| | | cell.setCellValue(value.toString()); |
| | | } |
| | | cell.setCellStyle(dataStyle); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private String getDeliveryStatusText(Integer status) { |
| | | if (status == null) return "æªç¥"; |
| | | switch (status) { |
| | | case 1: return "æªåè´§"; |
| | | case 2: return "审æ¹ä¸"; |
| | | case 3: return "审æ¹ä¸éè¿"; |
| | | case 4: return "审æ¹éè¿"; |
| | | case 5: return "å·²åè´§"; |
| | | case 6: return "é¨ååè´§"; |
| | | default: return "æªç¥"; |
| | | } |
| | | } |
| | | |
| | | private String getStockStatusText(Integer status) { |
| | | if (status == null) return "æªç¥"; |
| | | switch (status) { |
| | | case 0: return "æªå
¥åº"; |
| | | case 1: return "é¨åå
¥åº"; |
| | | case 2: return "å·²å
¥åº"; |
| | | case 3: return "审æ¹ä¸"; |
| | | default: return "æªç¥"; |
| | | } |
| | | } |
| | | |
| | | private String getReviewStatusText(Integer status) { |
| | | if (status == null) return "å¾
å®¡æ ¸"; |
| | | switch (status) { |
| | | case 0: return "å¾
å®¡æ ¸"; |
| | | case 1: return "å·²å®¡æ ¸"; |
| | | case 2: return "å·²å审"; |
| | | default: return "å¾
å®¡æ ¸"; |
| | | } |
| | | } |
| | | |
| | | private String getOrderStatusText(Integer status) { |
| | | if (status == null || status == 0) return "è¿è¡ä¸"; |
| | | switch (status) { |
| | | case 0: return "è¿è¡ä¸"; |
| | | case 1: return "已宿"; |
| | | default: return "è¿è¡ä¸"; |
| | | } |
| | | } |
| | | } |