浏览代码

sku、bom对账单导出

24282 1 年之前
父节点
当前提交
9e48d0c20e

+ 21 - 4
sd-business/src/main/java/com/sd/business/controller/statement/StatementOfAccountController.java

@@ -1,6 +1,7 @@
 package com.sd.business.controller.statement;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import com.ruoyi.common.annotation.NonInterception;
 import com.ruoyi.common.core.domain.BaseSelectDto;
 import com.sd.business.entity.statement.dto.FileUploadDto;
 import com.sd.business.entity.statement.dto.StatementOfAccountDto;
@@ -9,11 +10,9 @@ import com.sd.business.entity.statement.vo.*;
 import com.sd.business.service.statement.StatementOfAccountService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
-import org.springframework.web.bind.annotation.PostMapping;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 
 
@@ -112,4 +111,22 @@ public class StatementOfAccountController {
         return statementOfAccountService.getDocumentByOrder(dto.getId());
     }
 
+    /**
+     * 导出sku对账单
+     */
+    @NonInterception
+    @GetMapping("/exportDocumentBySku")
+    public void exportDocumentBySku(HttpServletResponse response, @RequestBody BaseSelectDto dto) {
+        statementOfAccountService.exportDocumentBySku(response, dto.getId());
+    }
+
+    /**
+     * 导出bom对账单
+     */
+    @NonInterception
+    @GetMapping("/exportDocumentByBom")
+    public void exportDocumentByBom(HttpServletResponse response, @RequestBody BaseSelectDto dto) {
+        statementOfAccountService.exportDocumentByBom(response, dto.getId());
+    }
+
 }

+ 48 - 0
sd-business/src/main/java/com/sd/business/controller/statement/Test.java

@@ -0,0 +1,48 @@
+package com.sd.business.controller.statement;
+
+import com.sd.business.entity.statement.vo.DocumentBySkuVo;
+import com.sd.framework.util.TemplateExcelUtil;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/open")
+public class Test {
+
+
+    @GetMapping("/a")
+    public void test(HttpServletResponse response) throws IOException {
+        Map<String, Object> map = new HashMap<>();
+
+        List<DocumentBySkuVo> list = new ArrayList<>();
+        DocumentBySkuVo documentBySkuVo1 = DocumentBySkuVo.builder()
+                .skuSpecCode("test01")
+                .skuSpecName("测试1")
+                .quantity(new BigDecimal("102"))
+                .build();
+        DocumentBySkuVo documentBySkuVo2 = DocumentBySkuVo.builder()
+                .skuSpecCode("test02")
+                .skuSpecName("测试2")
+                .quantity(new BigDecimal("107"))
+                .build();
+        list.add(documentBySkuVo1);
+        list.add(documentBySkuVo2);
+        list.add(documentBySkuVo1);
+
+        map.put("totalQuantity", BigDecimal.TEN);
+        map.put("totalSubtotal", new BigDecimal("124.65"));
+        map.put("department", "宝恒晟B1");
+
+        TemplateExcelUtil.writeBrowser("skuDocument.xlsx", "sku对账单", response, list, map);
+    }
+
+}

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/statement/vo/DocumentByBomVo.java

@@ -71,4 +71,9 @@ public class DocumentByBomVo {
      */
     private BigDecimal total;
 
+    /**
+     * 占位符
+     */
+    private Integer placeholder;
+
 }

+ 5 - 0
sd-business/src/main/java/com/sd/business/entity/statement/vo/DocumentBySkuVo.java

@@ -46,4 +46,9 @@ public class DocumentBySkuVo {
      */
     private BigDecimal total;
 
+    /**
+     * 占位符
+     */
+    private Integer placeholder;
+
 }

+ 1 - 2
sd-business/src/main/java/com/sd/business/service/order/impl/OrderServiceImpl.java

@@ -89,8 +89,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderInfo> implem
         wrapper.isNull(Objects.equals(dto.getLinkedStatementOfAccount(), 0), "o.statement_of_account_id");
         wrapper.isNotNull(Objects.equals(dto.getLinkedStatementOfAccount(), 1), "o.statement_of_account_id");
 
-        Page<OrderInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
-        return page;
+        return this.baseMapper.getPage(dto.getPage(), wrapper);
     }
 
     @Override

+ 11 - 0
sd-business/src/main/java/com/sd/business/service/statement/StatementOfAccountService.java

@@ -8,6 +8,7 @@ import com.sd.business.entity.statement.dto.StatementOfAccountSelectDto;
 import com.sd.business.entity.statement.po.StatementOfAccount;
 import com.sd.business.entity.statement.vo.*;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.List;
 
 
@@ -66,4 +67,14 @@ public interface StatementOfAccountService extends BaseService<StatementOfAccoun
      */
     List<DocumentByOrderVo> getDocumentByOrder(Long statementOfAccountId);
 
+    /**
+     * 导出sku对账单
+     */
+    void exportDocumentBySku(HttpServletResponse response, Long id);
+
+    /**
+     * 导出bom对账单
+     */
+    void exportDocumentByBom(HttpServletResponse response, Long id);
+
 }

+ 50 - 0
sd-business/src/main/java/com/sd/business/service/statement/impl/StatementOfAccountServiceImpl.java

@@ -1,8 +1,10 @@
 package com.sd.business.service.statement.impl;
 
 import cn.hutool.core.convert.Convert;
+import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -10,6 +12,7 @@ import com.fjhx.file.entity.FileInfoVo;
 import com.fjhx.file.utils.ObsFileUtil;
 import com.ruoyi.common.core.domain.BaseIdPo;
 import com.ruoyi.common.utils.wrapper.IWrapper;
+import com.sd.business.entity.department.po.Department;
 import com.sd.business.entity.order.po.OrderInfo;
 import com.sd.business.entity.order.po.OrderSku;
 import com.sd.business.entity.order.po.OrderSkuBom;
@@ -28,10 +31,12 @@ import com.sd.business.service.sku.SkuSpecService;
 import com.sd.business.service.statement.StatementOfAccountService;
 import com.sd.business.util.CodeEnum;
 import com.sd.framework.util.Assert;
+import com.sd.framework.util.TemplateExcelUtil;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
+import javax.servlet.http.HttpServletResponse;
 import java.math.BigDecimal;
 import java.util.*;
 import java.util.function.Function;
@@ -403,6 +408,51 @@ public class StatementOfAccountServiceImpl extends ServiceImpl<StatementOfAccoun
         return result;
     }
 
+    @Override
+    public void exportDocumentBySku(HttpServletResponse response, Long id) {
+        List<DocumentBySkuVo> list = getDocumentBySku(id);
+        StatementOfAccount statementOfAccount = getById(id);
+        Assert.notNull(statementOfAccount, "没有找到对账单");
+        Department department = departmentService.getById(statementOfAccount.getDepartmentId());
+        Assert.notNull(department, "没有找到事业部");
+
+        BigDecimal totalQuantity = list.stream().map(DocumentBySkuVo::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+        BigDecimal totalSubtotal = list.stream().map(DocumentBySkuVo::getSubtotal).reduce(BigDecimal.ZERO, BigDecimal::add);
+        Date timePeriodBegin = statementOfAccount.getTimePeriodBegin();
+        Date timePeriodEnd = statementOfAccount.getTimePeriodEnd();
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("department", department.getName());
+        map.put("beginDate", timePeriodBegin != null ? DateUtil.formatDate(timePeriodBegin) : StringPool.EMPTY);
+        map.put("endDate", timePeriodEnd != null ? DateUtil.formatDate(timePeriodEnd) : StringPool.EMPTY);
+        map.put("totalQuantity", totalQuantity);
+        map.put("totalSubtotal", totalSubtotal);
+
+        TemplateExcelUtil.writeBrowser("skuDocument.xlsx", "事业部sku对账单", response, list, map);
+    }
+
+    @Override
+    public void exportDocumentByBom(HttpServletResponse response, Long id) {
+        List<DocumentByBomVo> list = getDocumentByBom(id);
+        StatementOfAccount statementOfAccount = getById(id);
+        Assert.notNull(statementOfAccount, "没有找到对账单");
+        Department department = departmentService.getById(statementOfAccount.getDepartmentId());
+        Assert.notNull(department, "没有找到事业部");
+
+        BigDecimal totalQuantity = list.stream().map(DocumentByBomVo::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
+        BigDecimal totalSubtotal = list.stream().map(DocumentByBomVo::getSubtotal).reduce(BigDecimal.ZERO, BigDecimal::add);
+        Date timePeriodBegin = statementOfAccount.getTimePeriodBegin();
+        Date timePeriodEnd = statementOfAccount.getTimePeriodEnd();
+
+        Map<String, Object> map = new HashMap<>();
+        map.put("department", department.getName());
+        map.put("beginDate", timePeriodBegin != null ? DateUtil.formatDate(timePeriodBegin) : StringPool.EMPTY);
+        map.put("endDate", timePeriodEnd != null ? DateUtil.formatDate(timePeriodEnd) : StringPool.EMPTY);
+        map.put("totalQuantity", totalQuantity);
+        map.put("totalSubtotal", totalSubtotal);
+
+        TemplateExcelUtil.writeBrowser("bomDocument.xlsx", "事业部sku对账单", response, list, map);
+    }
 
     /**
      * 根据对账单id获取对账订单id列表

+ 0 - 2
sd-business/src/main/resources/mapper/statement/StatementOfAccountMapper.xml

@@ -5,10 +5,8 @@
         select soa.id,
                soa.code,
                soa.department_id,
-               soa.amount,
                soa.time_period_begin,
                soa.time_period_end,
-               soa.order_id_join,
                soa.create_user,
                soa.create_time,
                soa.update_user,

+ 6 - 0
sd-framework/pom.xml

@@ -31,6 +31,12 @@
             <version>4.5.14</version>
         </dependency>
 
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>3.3.2</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 77 - 0
sd-framework/src/main/java/com/sd/framework/util/ExcelFillCellMergeStrategy.java

@@ -0,0 +1,77 @@
+package com.sd.framework.util;
+
+import com.alibaba.excel.metadata.Head;
+import com.alibaba.excel.metadata.data.WriteCellData;
+import com.alibaba.excel.write.handler.CellWriteHandler;
+import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
+import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellType;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.util.CellRangeAddress;
+
+import java.util.List;
+
+public class ExcelFillCellMergeStrategy implements CellWriteHandler {
+
+    private int[] mergeColumnIndex;
+    private int mergeRowIndex;
+
+    public ExcelFillCellMergeStrategy() {
+    }
+
+    public ExcelFillCellMergeStrategy(int mergeRowIndex, int[] mergeColumnIndex) {
+        this.mergeRowIndex = mergeRowIndex;
+        this.mergeColumnIndex = mergeColumnIndex;
+    }
+
+    @Override
+    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
+        int curRowIndex = cell.getRowIndex();
+        int curColIndex = cell.getColumnIndex();
+        if (curRowIndex <= mergeRowIndex) {
+            return;
+        }
+        for (int columnIndex : mergeColumnIndex) {
+            if (curColIndex == columnIndex) {
+                mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
+                break;
+            }
+        }
+    }
+
+
+    /**
+     * 当前单元格向上合并
+     */
+    private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
+        Object curData = CellType.STRING.equals(cell.getCellType()) ? cell.getStringCellValue() : cell.getNumericCellValue();
+        Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
+        Object preData = CellType.STRING.equals(preCell.getCellType()) ? preCell.getStringCellValue() : preCell.getNumericCellValue();
+        // 将当前单元格数据与上一个单元格数据比较
+        Boolean dataBool = preData.equals(curData);
+        // 此处需要注意:因为我是按照序号确定是否需要合并的,所以获取每一行第一列数据和上一行第一列数据进行比较,如果相等合并
+        Boolean bool = cell.getRow().getCell(0).getNumericCellValue() == cell.getSheet().getRow(curRowIndex - 1).getCell(0).getNumericCellValue();
+        if (dataBool && bool) {
+            Sheet sheet = writeSheetHolder.getSheet();
+            List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
+            boolean isMerged = false;
+            for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
+                CellRangeAddress cellRangeAddr = mergeRegions.get(i);
+                // 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
+                if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
+                    sheet.removeMergedRegion(i);
+                    cellRangeAddr.setLastRow(curRowIndex);
+                    sheet.addMergedRegion(cellRangeAddr);
+                    isMerged = true;
+                }
+            }
+            // 若上一个单元格未被合并,则新增合并单元
+            if (!isMerged) {
+                CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
+                sheet.addMergedRegion(cellRangeAddress);
+            }
+        }
+    }
+
+}

+ 116 - 0
sd-framework/src/main/java/com/sd/framework/util/TemplateExcelUtil.java

@@ -0,0 +1,116 @@
+package com.sd.framework.util;
+
+import cn.hutool.core.io.IoUtil;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.handler.WriteHandler;
+import com.alibaba.excel.write.metadata.WriteSheet;
+import com.alibaba.excel.write.metadata.fill.FillConfig;
+import com.ruoyi.common.exception.ServiceException;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.core.io.ClassPathResource;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+
+@Slf4j
+public class TemplateExcelUtil {
+
+    /**
+     * 自定义模板excel写入浏览器
+     *
+     * @param templateName 模板名称
+     * @param fileName     文件名称
+     * @param response     response
+     * @param data         模板字段替换数据
+     */
+    public static void writeBrowser(String templateName, String fileName, HttpServletResponse response, Object... data) {
+
+        ExcelWriter excelWriter = null;
+        InputStream is = null;
+        ServletOutputStream os = null;
+        try {
+            String fileNameCn = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
+
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            response.setHeader("Content-disposition", "attachment;filename=" + fileNameCn + ".xlsx");
+
+            ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + templateName);
+            is = classPathResource.getInputStream();
+            os = response.getOutputStream();
+
+            excelWriter = EasyExcel.write(os).withTemplate(is).build();
+            WriteSheet writeSheet = EasyExcel.writerSheet().build();
+            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
+
+            for (Object item : data) {
+                excelWriter.fill(item, fillConfig, writeSheet);
+            }
+
+        } catch (IOException e) {
+            log.error("excel导出失败", e);
+            throw new ServiceException("excel导出失败");
+        } finally {
+            if (excelWriter != null) {
+                excelWriter.finish();
+            }
+            IoUtil.close(excelWriter);
+            IoUtil.close(is);
+            IoUtil.close(os);
+        }
+
+    }
+
+    /**
+     * 自定义模板excel写入浏览器
+     *
+     * @param templateName 模板名称
+     * @param fileName     文件名称
+     * @param writeHandler 合并策略
+     * @param response     response
+     * @param data         模板字段替换数据
+     */
+    public static void writeBrowser(String templateName, String fileName, WriteHandler writeHandler, HttpServletResponse response, Object... data) {
+
+        ExcelWriter excelWriter = null;
+        InputStream is = null;
+        ServletOutputStream os = null;
+        try {
+            String fileNameCn = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
+
+            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+            response.setCharacterEncoding("utf-8");
+            response.setHeader("Content-disposition", "attachment;filename=" + fileNameCn + ".xlsx");
+
+            ClassPathResource classPathResource = new ClassPathResource("template" + File.separator + templateName);
+            is = classPathResource.getInputStream();
+            os = response.getOutputStream();
+
+            excelWriter = EasyExcel.write(os).withTemplate(is).build();
+            WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(writeHandler).build();
+            FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
+
+            for (Object item : data) {
+                excelWriter.fill(item, fillConfig, writeSheet);
+            }
+
+        } catch (IOException e) {
+            log.error("excel导出失败", e);
+            throw new ServiceException("excel导出失败");
+        } finally {
+            if (excelWriter != null) {
+                excelWriter.finish();
+            }
+            IoUtil.close(excelWriter);
+            IoUtil.close(is);
+            IoUtil.close(os);
+        }
+
+    }
+
+}

二进制
sd-starter/src/main/resources/template/bomDocument.xlsx


二进制
sd-starter/src/main/resources/template/skuDocument.xlsx