diff --git a/cweb/src/main/java/com/cweb/controller/CommonController.java b/cweb/src/main/java/com/cweb/controller/CommonController.java index dc60b0b..82f9f74 100644 --- a/cweb/src/main/java/com/cweb/controller/CommonController.java +++ b/cweb/src/main/java/com/cweb/controller/CommonController.java @@ -22,6 +22,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import java.math.BigDecimal; import java.util.*; import java.util.stream.Collectors; @@ -46,8 +47,22 @@ public class CommonController { @ApiOperation(value = "test") public ResponseData test() { try { - - JSONObject refundObj = HuiLianTongUnionCardService.refund("R240805090735776", "240803201355633339"); + /* String goodsDesc = ""; + String tranDesc = ""; + String businessType = "HiSen_phg_goods"; + String instCode = "11101608"; + // 工会卡支付 + JSONObject consumption = HuiLianTongUnionCardService.consumption("240803201355633339_1", "8800030115015087807", new BigDecimal("27.40"), businessType, instCode, goodsDesc, tranDesc); + if (!consumption.getString("respCode").equals("0000")) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, consumption.getString("respMessage")); + } + // 响应参数 + JSONObject consumptionResult = HuiLianTongUnionCardService.resolveResponse(consumption.getString("data")); + if (consumptionResult.getBoolean("success") != true) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, consumptionResult.getString("message")); + } +*/ + JSONObject refundObj = HuiLianTongUnionCardService.refund("R240816093659997", "240808134614258487"); if (!refundObj.getString("respCode").equals("0000")) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, refundObj.getString("respMessage")); } diff --git a/order/src/main/java/com/order/controller/business/OrderCouponController.java b/order/src/main/java/com/order/controller/business/OrderCouponController.java index cb3cb98..c5071e0 100644 --- a/order/src/main/java/com/order/controller/business/OrderCouponController.java +++ b/order/src/main/java/com/order/controller/business/OrderCouponController.java @@ -20,6 +20,7 @@ import com.hfkj.sysenum.order.OrderChildStatusEnum; import com.hfkj.sysenum.order.OrderCouponNoStatusEnum; import com.hfkj.sysenum.order.OrderPayChannelEnum; import com.hfkj.sysenum.order.OrderPayTypeEnum; +import com.order.utils.excel.OrderCouponExcelMergeHandler; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; @@ -185,15 +186,15 @@ public class OrderCouponController { if (!file.exists()) { file.mkdirs(); } - //设置第几列开始合并 - int[] mergeColumnIndex = {0,1,2,17}; - //设置第几行开始合并 + // 需要合并的列 + int[] mergeColumnIndex = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}; + // 需要从第几行开始合并 int mergeRowIndex = 1; EasyExcel.write(fileUrl+pathName) .head(OrderCouponModel.class) .sheet("虚拟商品订单") - //.registerWriteHandler(new ExcelFillCellMergeStrategyUtils(mergeRowIndex, mergeColumnIndex)) + .registerWriteHandler(new OrderCouponExcelMergeHandler(mergeRowIndex, mergeColumnIndex)) .doWrite(orderList); return ResponseMsgUtil.success(pathName); diff --git a/order/src/main/java/com/order/utils/excel/OrderCouponExcelMergeHandler.java b/order/src/main/java/com/order/utils/excel/OrderCouponExcelMergeHandler.java new file mode 100644 index 0000000..c6f6ebe --- /dev/null +++ b/order/src/main/java/com/order/utils/excel/OrderCouponExcelMergeHandler.java @@ -0,0 +1,120 @@ +package com.order.utils.excel; + +import com.alibaba.excel.metadata.CellData; +import com.alibaba.excel.metadata.Head; +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.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.util.CellRangeAddress; + +import java.util.List; + +/** + * @className: ExcelMergeHandler + * @author: HuRui + * @date: 2024/8/17 + **/ +public class OrderCouponExcelMergeHandler implements CellWriteHandler { + + private int[] mergeColumnIndex; + private int mergeRowIndex; + + public OrderCouponExcelMergeHandler() { + } + + /** + * 构造函数 + * + * @param mergeRowIndex 合并开始的行索引 + * @param mergeColumnIndex 要合并的列索引数组 + */ + public OrderCouponExcelMergeHandler(int mergeRowIndex, int[] mergeColumnIndex) { + this.mergeRowIndex = mergeRowIndex; + this.mergeColumnIndex = mergeColumnIndex; + } + + @Override + public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) { + + } + + @Override + public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) { + + } + + + /** + * 当前单元格向上合并 + * + * @param writeSheetHolder 当前工作表持有者 + * @param cell 当前单元格 + * @param curRowIndex 当前行索引 + * @param curColIndex 当前列索引 + */ + private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) { + // 获取当前行的当前列的数据和上一行的当前列列数据,通过上一行数据是否相同进行合并 + Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue(); + // 获取前一个单元格的数据 + Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex); + Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue(); + + // 当前交易单号 + Cell curOrderNo = cell.getSheet().getRow(curRowIndex).getCell(0); + // 上一条交易单号 + Cell preOrderNo = cell.getSheet().getRow(curRowIndex - 1).getCell(0); + + // 判断当前单元格和前一个单元格的数据以及主键是否相同 + if (curData.equals(preData) && curOrderNo.getStringCellValue().equals(preOrderNo.getStringCellValue())) { + // 获取工作表 + Sheet sheet = writeSheetHolder.getSheet(); + // 获取已合并的区域 + List 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); + } + } + } + + @Override + public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer integer, Boolean aBoolean) { + + } + + @Override + public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List list, Cell cell, Head head, Integer integer, Boolean aBoolean) { + // 当前行索引 + int curRowIndex = cell.getRowIndex(); + // 当前列索引 + int curColIndex = cell.getColumnIndex(); + // 如果当前行大于合并开始行 + if (curRowIndex > mergeRowIndex) { + // 当前列在需要合并的列中 + for (int columnIndex : mergeColumnIndex) { + if (curColIndex == columnIndex) { + // 进行合并操作 + mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex); + break; + } + } + } + } +}