Commit 70b1067617ba43ba86365aad6cac9c1789d82fb6

Authored by yeqianyong
1 parent 0d31273a

楚江erp:台账报表导出相关接口开发

1 1 package com.lframework.xingyun.sc.controller.ledger;
2 2
  3 +import com.lframework.starter.mq.core.utils.ExportTaskUtil;
3 4 import com.lframework.xingyun.sc.bo.ledger.receipt.GetReceiptLedgerInfoBo;
4 5 import com.lframework.xingyun.sc.bo.ledger.receipt.QueryReceiptLedgerInfoBo;
5 6 import com.lframework.xingyun.sc.bo.ledger.receipt.ReceiptLedgerReportBo;
  7 +import com.lframework.xingyun.sc.enums.ExportType;
  8 +import com.lframework.xingyun.sc.excel.ledger.LedgerReportExportTaskWorker;
6 9 import com.lframework.xingyun.sc.utils.CommonUtil;
7 10 import com.lframework.xingyun.sc.vo.ledger.receipt.*;
8 11 import com.lframework.xingyun.sc.service.ledger.ReceiptLedgerInfoService;
... ... @@ -139,4 +142,16 @@ public class ReceiptLedgerInfoController extends DefaultBaseController {
139 142 PageResult<ReceiptLedgerReportBo> pageResult = receiptLedgerInfoService.report(getPageIndex(vo), getPageSize(vo), vo);
140 143 return InvokeResultBuilder.success(pageResult);
141 144 }
  145 +
  146 + /**
  147 + * 台账报表导出
  148 + */
  149 + @ApiOperation("台账报表导出")
  150 + @HasPermission({"customer-dev-manage:customer-dev-plan:export"})
  151 + @GetMapping("/report/export")
  152 + public InvokeResult<Void> export(@Valid ReceiptLedgerReportVo vo) {
  153 + vo.setExportType(ExportType.RECEIVABLE_LEDGER_REPORT.getCode());
  154 + ExportTaskUtil.exportTask("应收款台账报表信息", LedgerReportExportTaskWorker.class, vo);
  155 + return InvokeResultBuilder.success();
  156 + }
142 157 }
... ...
... ... @@ -9,6 +9,7 @@ public enum ExportType implements BaseEnum<String> {
9 9 CUSTOMER_DEVELOP("CUSTOMER_DEVELOP", "客户开发"),
10 10 CUSTOMER_CREDIT("CUSTOMER_CREDIT", "客户资信"),
11 11 CONTRACT_FRAMEWORK("CONTRACT_FRAMEWORK", "合同框架"),
  12 + RECEIVABLE_LEDGER_REPORT("RECEIVABLE_LEDGER_REPORT", "应收款台账报表"),
12 13
13 14
14 15 ;
... ...
  1 +package com.lframework.xingyun.sc.excel.ledger;
  2 +
  3 +
  4 +import cn.hutool.core.io.resource.ClassPathResource;
  5 +import com.lframework.starter.common.exceptions.impl.DefaultClientException;
  6 +import com.lframework.starter.mq.core.components.export.ExportHandler;
  7 +import com.lframework.starter.web.core.utils.ApplicationUtil;
  8 +import com.lframework.starter.web.core.utils.JsonUtil;
  9 +import com.lframework.starter.web.inner.entity.SysDataDicItem;
  10 +import com.lframework.starter.web.inner.service.system.SysDataDicItemService;
  11 +import com.lframework.xingyun.sc.bo.ledger.receipt.ReceiptLedgerReportDetail;
  12 +import com.lframework.xingyun.sc.utils.ExcelUtil;
  13 +import com.lframework.xingyun.sc.vo.ledger.receipt.ReceiptLedgerReportVo;
  14 +import lombok.extern.slf4j.Slf4j;
  15 +import org.apache.commons.collections4.CollectionUtils;
  16 +import org.apache.commons.lang3.StringUtils;
  17 +import org.apache.poi.ss.usermodel.*;
  18 +import org.apache.poi.ss.util.CellRangeAddress;
  19 +import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  20 +import org.springframework.stereotype.Component;
  21 +
  22 +import java.io.File;
  23 +import java.io.FileOutputStream;
  24 +import java.io.InputStream;
  25 +import java.util.HashMap;
  26 +import java.util.List;
  27 +import java.util.Map;
  28 +
  29 +
  30 +@Slf4j
  31 +@Component
  32 +public class LedgerReportExportHandler implements ExportHandler {
  33 +
  34 + @Override
  35 + public void exportLedgerReport(Object params, Object dataList, File xlsxFile) {
  36 + List<LedgerReportExportModel> ledgerReportList = JsonUtil.parseList(JsonUtil.toJsonString(dataList), LedgerReportExportModel.class);
  37 + // 读取模板文件
  38 + ClassPathResource templateResource = new ClassPathResource("templates/ledgerReportExportTemplate.xlsx");
  39 + try (InputStream inputStream = templateResource.getStream();
  40 + Workbook workbook = new XSSFWorkbook(inputStream)) {
  41 + Sheet sheet = workbook.getSheetAt(0);
  42 + // 数据填充起始行
  43 + int rowIndex = 2;
  44 + for (int i = 0; i < ledgerReportList.size(); i++) {
  45 + LedgerReportExportModel model = ledgerReportList.get(i);
  46 + ExcelUtil.setCellValue(sheet, rowIndex, 0, model.getQuota()
  47 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  48 + ExcelUtil.setCellValue(sheet, rowIndex, 1, model.getSettleTerm()
  49 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  50 + ExcelUtil.setCellValue(sheet, rowIndex, 2, model.getDeptName()
  51 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  52 + ExcelUtil.setCellValue(sheet, rowIndex, 3, model.getRegionName()
  53 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  54 + ExcelUtil.setCellValue(sheet, rowIndex, 4, model.getCustomerType()
  55 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  56 + ExcelUtil.setCellValue(sheet, rowIndex, 5, model.getCustomerShortName()
  57 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  58 +
  59 + List<ReceiptLedgerReportDetail> detailList = model.getDetailList();
  60 + if (CollectionUtils.isEmpty(detailList)) {
  61 + rowIndex++;
  62 + continue;
  63 + }
  64 + // 合并起始行
  65 + int startMergeIndex = rowIndex;
  66 + // 合并结束行
  67 + int endMergeIndex = rowIndex + detailList.size();
  68 + for (int j = 0; j < detailList.size(); j++) {
  69 + ReceiptLedgerReportDetail detail = detailList.get(j);
  70 + ExcelUtil.setCellValue(sheet, rowIndex, 6, detail.getCustomerName()
  71 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  72 + ExcelUtil.setCellValue(sheet, rowIndex, 7, detail.getDebtTotal()
  73 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  74 + ExcelUtil.setCellValue(sheet, rowIndex, 8, detail.getAgreement()
  75 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  76 + ExcelUtil.setCellValue(sheet, rowIndex, 9, detail.getFirstCoordinate()
  77 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  78 + ExcelUtil.setCellValue(sheet, rowIndex, 10, detail.getSecondCoordinate()
  79 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  80 + ExcelUtil.setCellValue(sheet, rowIndex, 11, detail.getClearDebt()
  81 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  82 + ExcelUtil.setCellValue(sheet, rowIndex, 12, detail.getCreditStatus()
  83 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  84 + ExcelUtil.setCellValue(sheet, rowIndex, 13, detail.getTimingStatus()
  85 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  86 + ExcelUtil.setCellValue(sheet, rowIndex, 14, detail.getWaitDeliveredOrder()
  87 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  88 + rowIndex++;
  89 + }
  90 + if (detailList.size() > 1) {
  91 + ExcelUtil.setCellValue(sheet, rowIndex, 6, "小计"
  92 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  93 + ExcelUtil.setCellValue(sheet, rowIndex, 7, model.getDebtTotal()
  94 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  95 + ExcelUtil.setCellValue(sheet, rowIndex, 8, model.getAgreementTotal()
  96 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  97 + ExcelUtil.setCellValue(sheet, rowIndex, 9, model.getFirstCoordinateTotal()
  98 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  99 + ExcelUtil.setCellValue(sheet, rowIndex, 10, model.getSecondCoordinateTotal()
  100 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  101 + ExcelUtil.setCellValue(sheet, rowIndex, 11, model.getClearDebtTotal()
  102 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  103 + ExcelUtil.setCellValue(sheet, rowIndex, 12, model.getCreditStatus()
  104 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  105 + ExcelUtil.setCellValue(sheet, rowIndex, 13, model.getTimingStatus()
  106 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  107 + ExcelUtil.setCellValue(sheet, rowIndex, 14, model.getWaitDeliveredOrderTotal()
  108 + , setCellStyle(workbook, VerticalAlignment.CENTER, false));
  109 + // 合并单位格
  110 + CellRangeAddress cellRangeAddress1 = new CellRangeAddress(
  111 + startMergeIndex, endMergeIndex, 0, 0);
  112 + sheet.addMergedRegion(cellRangeAddress1);
  113 + CellRangeAddress cellRangeAddress2 = new CellRangeAddress(
  114 + startMergeIndex, endMergeIndex, 1, 1);
  115 + sheet.addMergedRegion(cellRangeAddress2);
  116 + CellRangeAddress cellRangeAddress3 = new CellRangeAddress(
  117 + startMergeIndex, endMergeIndex, 2, 2);
  118 + sheet.addMergedRegion(cellRangeAddress3);
  119 + CellRangeAddress cellRangeAddress4 = new CellRangeAddress(
  120 + startMergeIndex, endMergeIndex, 3, 3);
  121 + sheet.addMergedRegion(cellRangeAddress4);
  122 + CellRangeAddress cellRangeAddress5 = new CellRangeAddress(
  123 + startMergeIndex, endMergeIndex, 4, 4);
  124 + sheet.addMergedRegion(cellRangeAddress5);
  125 + CellRangeAddress cellRangeAddress6 = new CellRangeAddress(
  126 + startMergeIndex, endMergeIndex, 5, 5);
  127 + sheet.addMergedRegion(cellRangeAddress6);
  128 + rowIndex++;
  129 + }
  130 + }
  131 + // ========== 填充标题和表头 ==========
  132 + Map<String, Object> dataMap = new HashMap<>();
  133 + ReceiptLedgerReportVo reportVo = JsonUtil.parseObject(JsonUtil.toJsonString(params), ReceiptLedgerReportVo.class);
  134 + StringBuilder builder = new StringBuilder();
  135 + if (reportVo != null) {
  136 + SysDataDicItemService sysDataDicItemService = ApplicationUtil.getBean(SysDataDicItemService.class);
  137 + if (StringUtils.isNotBlank(reportVo.getDeptName())) {
  138 + builder.append(reportVo.getDeptName()).append(",");
  139 + }
  140 + if (StringUtils.isNotBlank(reportVo.getDebtAmountScope())) {
  141 + SysDataDicItem dicItem = sysDataDicItemService.findByCode("DEBT_AMOUNT_SCOPE", reportVo.getDebtAmountScope());
  142 + builder.append(dicItem.getName()).append(",");
  143 + }
  144 + if (StringUtils.isNotBlank(reportVo.getCustomerType())) {
  145 + SysDataDicItem dicItem = sysDataDicItemService.findByCode("ENTERPRISE_TYPE", reportVo.getCustomerType());
  146 + builder.append(dicItem.getName()).append(",");
  147 + }
  148 + if (StringUtils.isNotBlank(reportVo.getDebtStatus())) {
  149 + SysDataDicItem dicItem = sysDataDicItemService.findByCode("DEBT_STATUS", reportVo.getDebtStatus());
  150 + builder.append(dicItem.getName()).append(",");
  151 + }
  152 + if (builder.length() > 0) {
  153 + builder.deleteCharAt(builder.length() - 1);
  154 + }
  155 + }
  156 + dataMap.put("titleSuffix", "——" + builder.toString());
  157 + ExcelUtil.processTemplate(workbook, dataMap);
  158 + // 将workbook写入文件
  159 + try (FileOutputStream outputStream = new FileOutputStream(xlsxFile)) {
  160 + workbook.write(outputStream);
  161 + }
  162 + } catch (Exception e) {
  163 + log.error("台账报表导出失败:", e);
  164 + throw new DefaultClientException("台账报表导出失败:" + e.getMessage());
  165 + }
  166 + }
  167 +
  168 + /**
  169 + * 设置单元格样式
  170 + *
  171 + * @param workbook 工作簿
  172 + * @param alignmentType 对齐方式
  173 + * @param setBorder 是否设置边框
  174 + * @return CellStyle
  175 + */
  176 + private CellStyle setCellStyle(Workbook workbook, VerticalAlignment alignmentType, boolean setBorder) {
  177 + // 创建字体
  178 + Font font = workbook.createFont();
  179 + font.setFontName("宋体");
  180 + font.setFontHeightInPoints((short) 11);
  181 + // 创建样式
  182 + CellStyle style = workbook.createCellStyle();
  183 + style.setFont(font);
  184 + // 对齐方式
  185 + style.setVerticalAlignment(alignmentType);
  186 + if (setBorder) {
  187 + // 设置边框
  188 + style.setBorderTop(BorderStyle.THIN);
  189 + style.setBorderRight(BorderStyle.THIN);
  190 + style.setBorderBottom(BorderStyle.THIN);
  191 + style.setBorderLeft(BorderStyle.THIN);
  192 + }
  193 + return style;
  194 + }
  195 +}
... ...
  1 +package com.lframework.xingyun.sc.excel.ledger;
  2 +
  3 +import com.alibaba.excel.annotation.ExcelIgnore;
  4 +import com.alibaba.excel.annotation.ExcelProperty;
  5 +import com.lframework.starter.web.core.components.excel.ExcelModel;
  6 +import com.lframework.xingyun.sc.bo.ledger.receipt.ReceiptLedgerReportDetail;
  7 +import lombok.Data;
  8 +
  9 +import java.math.BigDecimal;
  10 +import java.util.List;
  11 +
  12 +
  13 +@Data
  14 +public class LedgerReportExportModel implements ExcelModel {
  15 +
  16 + /**
  17 + * 额度
  18 + */
  19 + @ExcelProperty("额度")
  20 + private BigDecimal quota;
  21 +
  22 + /**
  23 + * 结算期限
  24 + */
  25 + @ExcelProperty("结算期限")
  26 + private String settleTerm;
  27 +
  28 + /**
  29 + * 办事处名称
  30 + */
  31 + @ExcelProperty("办")
  32 + private String deptName;
  33 +
  34 + /**
  35 + * 区域名称
  36 + */
  37 + @ExcelProperty("区域")
  38 + private String regionName;
  39 +
  40 + /**
  41 + * 客户类型
  42 + */
  43 + @ExcelProperty("客户类型")
  44 + private String customerType;
  45 +
  46 + /**
  47 + * 客户简称
  48 + */
  49 + @ExcelProperty("客户简称")
  50 + private String customerShortName;
  51 +
  52 + /**
  53 + * 报表明细
  54 + */
  55 + @ExcelIgnore
  56 + private List<ReceiptLedgerReportDetail> detailList;
  57 +
  58 + /**
  59 + * 总欠款
  60 + */
  61 + @ExcelProperty("总欠款")
  62 + private BigDecimal debtTotal;
  63 +
  64 + /**
  65 + * 约定内总欠款
  66 + */
  67 + @ExcelProperty("约定内总欠款")
  68 + private BigDecimal agreementTotal;
  69 +
  70 + /**
  71 + * 一次协调总欠款
  72 + */
  73 + @ExcelProperty("一次协调总欠款")
  74 + private BigDecimal firstCoordinateTotal;
  75 +
  76 + /**
  77 + * 二次协调总欠款
  78 + */
  79 + @ExcelProperty("二次协调总欠款")
  80 + private BigDecimal secondCoordinateTotal;
  81 +
  82 + /**
  83 + * 清欠总欠款
  84 + */
  85 + @ExcelProperty("清欠总欠款")
  86 + private BigDecimal clearDebtTotal;
  87 +
  88 + /**
  89 + * 授信状态
  90 + */
  91 + @ExcelProperty("授信状态")
  92 + private String creditStatus;
  93 +
  94 + /**
  95 + * 授时状态
  96 + */
  97 + @ExcelProperty("总欠款")
  98 + private String timingStatus;
  99 +
  100 + /**
  101 + * 待交付订单总数
  102 + */
  103 + @ExcelProperty("待交付订单总数")
  104 + private Integer waitDeliveredOrderTotal;
  105 +}
... ...
  1 +package com.lframework.xingyun.sc.excel.ledger;
  2 +
  3 +import com.lframework.starter.mq.core.components.export.ExportTaskWorker;
  4 +import com.lframework.starter.web.core.components.resp.PageResult;
  5 +import com.lframework.starter.web.core.utils.ApplicationUtil;
  6 +import com.lframework.starter.web.core.utils.JsonUtil;
  7 +import com.lframework.xingyun.sc.bo.ledger.receipt.ReceiptLedgerReportBo;
  8 +import com.lframework.xingyun.sc.service.ledger.ReceiptLedgerInfoService;
  9 +import com.lframework.xingyun.sc.vo.ledger.receipt.ReceiptLedgerReportVo;
  10 +
  11 +
  12 +public class LedgerReportExportTaskWorker implements
  13 + ExportTaskWorker<ReceiptLedgerReportVo, ReceiptLedgerReportBo, LedgerReportExportModel> {
  14 +
  15 +
  16 + @Override
  17 + public ReceiptLedgerReportVo parseParams(String json) {
  18 + return JsonUtil.parseObject(json, ReceiptLedgerReportVo.class);
  19 + }
  20 +
  21 + @Override
  22 + public PageResult<ReceiptLedgerReportBo> getDataList(int pageIndex, int pageSize, ReceiptLedgerReportVo params) {
  23 + ReceiptLedgerInfoService receiptLedgerInfoService = ApplicationUtil.getBean(ReceiptLedgerInfoService.class);
  24 + return receiptLedgerInfoService.report(pageIndex, pageSize, params);
  25 + }
  26 +
  27 + @Override
  28 + public LedgerReportExportModel exportData(ReceiptLedgerReportBo data) {
  29 + return JsonUtil.parseObject(JsonUtil.toJsonString(data), LedgerReportExportModel.class);
  30 + }
  31 +
  32 + @Override
  33 + public Class<LedgerReportExportModel> getModelClass() {
  34 + return LedgerReportExportModel.class;
  35 + }
  36 +}
... ...
... ... @@ -210,4 +210,34 @@ public class ExcelUtil {
210 210 cell.setCellValue(((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
211 211 }
212 212 }
  213 +
  214 +
  215 + /**
  216 + * 设置单元格值和样式
  217 + */
  218 + public static void setCellValue(Sheet sheet, int rowNum, int colNum, Object value, CellStyle style) {
  219 + Row row = sheet.getRow(rowNum);
  220 + if (row == null) {
  221 + row = sheet.createRow(rowNum);
  222 + }
  223 + Cell cell = row.getCell(colNum);
  224 + if (cell == null) {
  225 + cell = row.createCell(colNum);
  226 + }
  227 + if (value == null) {
  228 + cell.setCellValue("");
  229 + } else if (value instanceof String) {
  230 + cell.setCellValue((String) value);
  231 + } else if (value instanceof Integer) {
  232 + cell.setCellValue((Integer) value);
  233 + } else if (value instanceof Double) {
  234 + cell.setCellValue((Double) value);
  235 + } else if (value instanceof BigDecimal) {
  236 + cell.setCellValue(((BigDecimal) value).doubleValue());
  237 + } else if (value instanceof LocalDate) {
  238 + cell.setCellValue(((LocalDate) value).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
  239 + }
  240 + // 设置样式
  241 + cell.setCellStyle(style);
  242 + }
213 243 }
... ...
... ... @@ -48,4 +48,10 @@ public class ReceiptLedgerReportVo extends PageVo implements BaseVo, Serializabl
48 48 */
49 49 @ApiModelProperty("欠款状态")
50 50 private String debtStatus;
  51 +
  52 + /**
  53 + * 导出类型
  54 + */
  55 + @ApiModelProperty("导出类型")
  56 + private String exportType;
51 57 }
... ...