Merge branch 'test' into prod

# Conflicts:
#	cash-api/order-server/src/main/java/com/czg/controller/pay/OrderPayController.java
This commit is contained in:
2026-01-30 16:02:25 +08:00
54 changed files with 2065 additions and 112 deletions

View File

@@ -28,5 +28,4 @@ public class ShopUserDTO extends ShopUser {
private String nextMemberLevelName;
private Long nextExperience;
private Long pointBalance;
private boolean isNew;
}

View File

@@ -0,0 +1,116 @@
package com.czg.account.dto.shopuser;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* @author yjjie
* @date 2026/1/28 11:30
*/
@Data
public class ShopUserExportDTO {
@ExcelProperty("手机号")
private String phone;
@ExcelProperty("会员生日")
private String birthDay;
@ExcelProperty("用户昵称")
private String nickName;
@ExcelIgnore
private Integer status;
@ExcelProperty("会员状态")
private String statusRemark;
@ExcelIgnore
private Integer isVip;
@ExcelProperty("是否会员")
private String vipRemark;
@ExcelProperty("会员编号")
private String code;
@ExcelProperty("余额")
private BigDecimal amount;
@ExcelProperty("充值次数")
private Integer rechargeCount;
@ExcelProperty("消费累计")
private BigDecimal consumeAmount;
@ExcelProperty("消费次数")
private Integer consumeCount;
@ExcelProperty("经验值")
private Long experience;
@ExcelIgnore
private String distributionShops;
@ExcelProperty("是否分销员")
private String distributionShopsRemark;
@ExcelProperty("优惠券数量")
private Long couponNum;
@ExcelProperty("订单数量")
private Long orderNumber;
@ExcelProperty("充值金额")
private BigDecimal rechargeAmount;
@ExcelProperty("会员等级")
private String memberLevelName;
@ExcelProperty("下一级会员等级")
private String nextMemberLevelName;
@ExcelProperty("升级所需经验值")
private Long nextExperience;
@ExcelProperty("积分余额")
private Long pointBalance;
@ExcelProperty("加入会员时间")
private LocalDateTime joinTime;
@ExcelProperty("创建时间")
private LocalDateTime createTime;
public String getVipRemark() {
if (isVip == null || isVip == 0) {
return "";
}
return "";
}
public String getStatusRemark() {
if (status == null || status == 0) {
return "禁用";
}
return "正常";
}
public String getDistributionShopsRemark() {
if (StrUtil.isBlank(distributionShops) || !distributionShops.contains("_")) {
return "";
}
String[] split = distributionShops.split("_");
if (split.length < 2) {
return "";
}
if ("0".equals(split[1])) {
return "";
}
return "";
}
}

View File

@@ -4,6 +4,7 @@ import com.czg.account.dto.shopuser.*;
import com.czg.account.entity.ShopUser;
import com.czg.market.entity.SmsPushEventUser;
import com.mybatisflex.core.paginate.Page;
import jakarta.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;
@@ -22,6 +23,8 @@ public interface AShopUserService {
Page<ShopUser> getPushEventUser(SmsPushEventUser smsPushEventUser);
Page<ShopUser> getAcPushEventUser(SmsPushEventUser smsPushEventUser);
void exportUserList(String key, Integer isVip, HttpServletResponse response);
Boolean add(Long shopId, ShopUserAddDTO shopUserAddDTO);

View File

@@ -5,7 +5,6 @@ import com.czg.account.entity.ShopInfo;
import com.czg.exception.CzgException;
import com.mybatisflex.core.service.IService;
import java.io.Serializable;
import java.util.List;
/**
@@ -16,7 +15,7 @@ import java.util.List;
*/
public interface ShopConfigService extends IService<ShopConfig> {
ShopInfo getShopInfoAndConfig(Serializable id) throws CzgException;
ShopInfo getShopInfoAndConfig(Long id) throws CzgException;
void editStatusByShopIdList(Long mainShopId, Integer isEnable, boolean onyUpValid, String name, String useShopType, List<Long> shopIdList);

View File

@@ -0,0 +1,46 @@
package com.czg.market.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serial;
import java.io.Serializable;
/**
* 轮播图配置表 实体类。
*
* @author ww
* @since 2026-01-27
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class MkCarouselDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 店铺Id
*/
private Long shopId;
/**
* 轮播图名称20字内
*/
private String name;
/**
* 是否可分享 1=开启 0=关闭
*/
private Integer isShareable;
/**
* 启用状态 1=启用 0=禁用
*/
private Integer isEnabled;
}

View File

@@ -0,0 +1,97 @@
package com.czg.market.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 轮播图配置表 实体类。
*
* @author ww
* @since 2026-01-27
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("mk_carousel")
public class MkCarousel implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 主键ID
*/
@Id(keyType = KeyType.Auto)
private BigInteger id;
/**
* 店铺Id
*/
private Long shopId;
/**
* 轮播图名称20字内
*/
private String name;
/**
* 轮播图片地址
*/
private String imageUrl;
/**
* 是否可分享 1=开启 0=关闭
*/
private Integer isShareable;
/**
* 跳转页面 tb_mini_app_pages的 id
*/
private Long jumpPageId;
/**
* 跳转页面路径
*/
@Column(ignore = true)
private String jumpPagePath;
/**
* 扩展参数
*/
private String extendParam;
/**
* 排序值,值越大越靠前
*/
private Integer sort;
/**
* 启用状态 1=启用 0=禁用
*/
private Integer isEnabled;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@@ -0,0 +1,67 @@
package com.czg.market.entity;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.io.Serial;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 分销员管理群(全民股东管理) 实体类。
*
* @author ww
* @since 2026-01-28
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("mk_distribution_group")
public class MkDistributionGroup implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Id
private Long shopId;
/**
* 群二维码地址
*/
private String groupUrl;
/**
* 模块标题 15字以内
*/
private String title;
/**
* 模块内容 20字以内
*/
private String content;
/**
* 是否开启
*/
private Integer isEnable;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@@ -1,7 +1,6 @@
package com.czg.market.entity;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
@@ -117,5 +116,9 @@ public class MkDistributionUser implements Serializable {
* 邀请码
*/
private String inviteCode;
/**
* 是否第一次进入全民股东界面
*/
private Integer firstIn;
}

View File

@@ -0,0 +1,114 @@
package com.czg.market.entity;
import com.czg.market.dto.ShopCouponDTO;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 分享奖励基础配置 实体类。
*
* @author ww
* @since 2026-01-27
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table("mk_share_base")
public class MkShareBase implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 店铺id
*/
@Id
private Long shopId;
/**
* 功能开启状态 1=开启 0=关闭
*/
private Integer isEnabled;
/**
* 可获得奖励的分享页面,用逗号分隔,
* 店铺首页 index
* 我的 dine
* 点餐页 eat
* 点餐页 eat-detail
* 套餐推广-列表 pp-list
* 套餐推广-详情 pp-detail
* 商品拼团-列表 gb-list
* 商品拼团-详情 gb-detail
* 全民股东 dis
*/
private String rewardSharePages;
/**
* 分享人奖励的优惠券ID关联优惠券表
*/
private Long sharerCouponId;
/**
* 分享人奖励的优惠券信息
*/
@Column(ignore = true)
private ShopCouponDTO sharerCoupon;
/**
* 分享人奖励的优惠券名称
*/
@Column(ignore = true)
private String sharerCouponName;
/**
* 分享人单次获得优惠券数量
*/
private Integer sharerCouponNum;
/**
* 可获得奖励次数 1=仅1次 2=每次分享成功
*/
private Integer rewardTimesType;
/**
* 被分享人奖励的优惠券ID关联优惠券表可选
*/
private Long sharedUserCouponId;
/**
* 被分享人奖励的优惠券名称
*/
@Column(ignore = true)
private String sharedUserCouponName;
/**
* 被分享人单次获得优惠券数量(可选)
*/
private Integer sharedUserCouponNum;
/**
* 被分享人弹窗开关 1=开启 0=关闭
*/
private Integer isSharedUserPopup;
/**
* 创建时间
*/
@Column(onInsertValue = "now()")
private LocalDateTime createTime;
/**
* 更新时间
*/
@Column(onInsertValue = "now()", onUpdateValue = "now()")
private LocalDateTime updateTime;
}

View File

@@ -0,0 +1,19 @@
package com.czg.market.service;
import com.czg.market.dto.MkCarouselDTO;
import com.mybatisflex.core.service.IService;
import com.czg.market.entity.MkCarousel;
import java.util.List;
/**
* 轮播图配置表 服务层。
*
* @author ww
* @since 2026-01-27
*/
public interface MkCarouselService extends IService<MkCarousel> {
List<MkCarousel> getCarousels(MkCarouselDTO mkCarouselDTO);
}

View File

@@ -0,0 +1,14 @@
package com.czg.market.service;
import com.mybatisflex.core.service.IService;
import com.czg.market.entity.MkDistributionGroup;
/**
* 分销员管理群(全民股东管理) 服务层。
*
* @author ww
* @since 2026-01-28
*/
public interface MkDistributionGroupService extends IService<MkDistributionGroup> {
}

View File

@@ -60,9 +60,18 @@ public interface MkDistributionUserService extends IService<MkDistributionUser>
/**
* 分销员中心-绑定邀请人
* 通过邀请码
*/
void bindInviteUser(MkDistributionUserDTO param) throws CzgException, ValidateException;
/**
* 分销员中心-绑定邀请人
* 通过邀请人id
* @param fromUserId shopUserId 邀请人
* @param toUserId 被邀请人邀请人
*/
void bindInviteUser(Long fromUserId, Long toUserId, Long shopId) throws CzgException, ValidateException;
/**
* 获取分销员分页列表
*/

View File

@@ -0,0 +1,17 @@
package com.czg.market.service;
import com.mybatisflex.core.service.IService;
import com.czg.market.entity.MkShareBase;
/**
* 分享奖励基础配置 服务层。
*
* @author ww
* @since 2026-01-27
*/
public interface MkShareBaseService extends IService<MkShareBase> {
MkShareBase getShareBase(Long shopId);
void shareClaim(String tagType, Long shopId, Long fromUserId, Long toUserId);
}

View File

@@ -0,0 +1,104 @@
package com.czg.product.dto;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalTime;
/**
* 商品导出
* @author yjjie
* @date 2026/1/28 14:30
*/
@Data
public class ProductExportDTO {
@ExcelProperty("商品名称")
@ColumnWidth(20)
private String name;
@ExcelProperty("商品分类")
@ColumnWidth(15)
private String categoryName;
@ExcelProperty("条码")
@ColumnWidth(20)
private String barCode;
@ExcelProperty("商品规格")
@ColumnWidth(20)
private String specFullName;
@ExcelProperty("售价")
@ColumnWidth(10)
private BigDecimal price;
@ExcelProperty("会员价")
@ColumnWidth(10)
private BigDecimal memberPrice;
@ExcelProperty("成本价")
@ColumnWidth(10)
private BigDecimal costPrice;
@ExcelProperty("商品单位")
@ColumnWidth(10)
private String unitName;
/**
* 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
*/
@ExcelProperty("商品类型")
@ColumnWidth(15)
private String type;
/**
* 可用开始时间
*/
@ExcelProperty("可用开始时间")
@ColumnWidth(16)
private LocalTime startTime;
/**
* 可用结束时间
*/
@ExcelProperty("可用结束时间")
@ColumnWidth(16)
private LocalTime endTime;
/**
* 商品级库存数量
*/
@ExcelProperty("库存数量")
@ColumnWidth(10)
private Integer stockNumber;
/**
* 是否上架
*/
@ExcelIgnore
private Integer isSale;
@ExcelProperty("是否上架")
@ColumnWidth(10)
private String isSaleRemark;
public String getType() {
return switch (type) {
case "single" -> "单规格商品";
case "sku" -> "多规格商品";
case "package" -> "套餐商品";
case "weight" -> "称重商品";
case "coupon" -> "团购券";
case null, default -> "未知类型";
};
}
public String getIsSaleRemark() {
return switch (isSale) {
case 0 -> "下架";
case 1 -> "上架";
case null, default -> "未知状态";
};
}
}

View File

@@ -0,0 +1,101 @@
package com.czg.product.dto;
import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import lombok.Data;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
/**
* 套餐商品导出
* @author yjjie
* @date 2026/1/30 10:26
*/
@Data
@Accessors(chain = true)
public class ProductPackageExportDTO {
@ExcelProperty("套餐名称")
@ColumnWidth(20)
private String name;
@ExcelProperty("套餐分类")
@ColumnWidth(15)
private String categoryName;
@ExcelProperty("售价")
@ColumnWidth(10)
private BigDecimal price;
@ExcelProperty("会员价")
@ColumnWidth(10)
private BigDecimal memberPrice;
/**
* 商品类型 single-单规格商品 sku-多规格商品 package-套餐商品 weight-称重商品 coupon-团购券
*/
@ExcelIgnore()
private String type;
/**
* 套餐类型 0 固定套餐 1可选套餐
*/
@ExcelIgnore
private Integer groupType;
@ExcelProperty("套餐类型")
@ColumnWidth(15)
private String groupTypeRemark;
@ExcelProperty("组名称")
@ColumnWidth(15)
private String groupTitleName;
@ExcelProperty("商品名称")
@ColumnWidth(21)
private String groupProductName;
@ExcelProperty("商品单位")
@ColumnWidth(10)
private String unitName;
@ExcelProperty("套餐内选择数量")
@ColumnWidth(10)
private String groupProductNumber;
/**
* 商品级库存数量
*/
@ExcelProperty("库存数量")
@ColumnWidth(10)
private Integer stockNumber;
/**
* 是否上架
*/
@ExcelIgnore
private Integer isSale;
@ExcelProperty("是否上架")
@ColumnWidth(10)
private String isSaleRemark;
public String getGroupTypeRemark() {
if (!"package".equals(type)) {
return "";
}
return switch (groupType) {
case 0 -> "固定套餐";
case 1 -> "可选套餐";
case null, default -> "未知类型";
};
}
public String getIsSaleRemark() {
return switch (isSale) {
case 0 -> "下架";
case 1 -> "上架";
case null, default -> "未知状态";
};
}
}

View File

@@ -1,13 +1,13 @@
package com.czg.product.service;
import com.czg.product.dto.ProductDTO;
import com.czg.product.dto.RelatedProductDTO;
import com.czg.product.entity.Product;
import com.czg.product.entity.ProductStockFlow;
import com.czg.product.param.*;
import com.czg.product.vo.ProductStatisticsVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.service.IService;
import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
@@ -34,6 +34,8 @@ public interface ProductService extends IService<Product> {
*/
List<ProductDTO> getProductList(ProductDTO param);
void exportProductList(ProductDTO param, HttpServletResponse response);
/**
* 从缓存里面获取商品列表
*

View File

@@ -95,6 +95,11 @@
<artifactId>mybatis-flex-processor</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -0,0 +1,21 @@
package com.czg.excel;
import java.util.List;
/**
* 数据提供者接口(用于分批获取数据)
* @author yjjie
* @date 2026/1/28 10:51
*/
@FunctionalInterface
public interface DataSupplier<T> {
/**
* 获取指定页的数据
*
* @param pageNum 页码
* @param pageSize 每页大小
* @return 数据列表
*/
List<T> getData(int pageNum, int pageSize);
}

View File

@@ -0,0 +1,40 @@
package com.czg.excel;
import lombok.Data;
/**
* Excel导出配置类
* @author yjjie
* @date 2026/1/28 10:47
*/
@Data
public class ExcelExportConfig {
/**
* 默认工作表名称
*/
private String defaultSheetName = "Sheet1";
/**
* 默认文件名
*/
private String defaultFileName = "export_data";
/**
* 是否自动关闭流
*/
private boolean autoCloseStream = true;
/**
* 响应头编码
*/
private String charset = "UTF-8";
public ExcelExportConfig() {}
public ExcelExportConfig(String defaultSheetName, String defaultFileName) {
this.defaultSheetName = defaultSheetName;
this.defaultFileName = defaultFileName;
}
}

View File

@@ -0,0 +1,318 @@
package com.czg.excel;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.czg.exception.CzgException;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.List;
/**
* EasyExcel导出工具类
* @author yjjie
* @date 2026/1/28 10:48
*/
@Slf4j
public class ExcelExportUtil {
private static final ExcelExportConfig DEFAULT_CONFIG = new ExcelExportConfig();
/**
* 导出Excel到HttpServletResponse
*
* @param data 数据列表
* @param clazz 数据类型
* @param fileName 文件名(不含扩展名)
* @param response HttpServletResponse
* @param <T> 数据类型
*/
public static <T> void exportToResponse(List<T> data, Class<T> clazz,
String fileName, HttpServletResponse response) {
exportToResponse(data, clazz, fileName, DEFAULT_CONFIG, response);
}
/**
* 导出Excel到HttpServletResponse自定义配置
*
* @param data 数据列表
* @param clazz 数据类型
* @param fileName 文件名(不含扩展名)
* @param config 配置信息
* @param response HttpServletResponse
* @param <T> 数据类型
*/
public static <T> void exportToResponse(List<T> data, Class<T> clazz,
String fileName, ExcelExportConfig config,
HttpServletResponse response) {
if (data == null) {
data = Collections.emptyList();
}
setResponseHeader(response, fileName, config);
try (OutputStream outputStream = response.getOutputStream()) {
ExcelWriter excelWriter = EasyExcel.write(outputStream, clazz)
.autoCloseStream(config.isAutoCloseStream())
.build();
WriteSheet writeSheet = EasyExcel.writerSheet(config.getDefaultSheetName()).build();
excelWriter.write(data, writeSheet);
excelWriter.finish();
log.info("Excel导出成功文件名{},数据量:{}", fileName, data.size());
} catch (IOException e) {
log.error("Excel导出失败", e);
throw new CzgException("Excel导出失败", e);
}
}
/**
* 导出Excel到文件
*
* @param data 数据列表
* @param clazz 数据类型
* @param filePath 文件路径
* @param <T> 数据类型
*/
public static <T> void exportToFile(List<T> data, Class<T> clazz, String filePath) {
exportToFile(data, clazz, filePath, DEFAULT_CONFIG);
}
/**
* 导出Excel到文件自定义配置
*
* @param data 数据列表
* @param clazz 数据类型
* @param filePath 文件路径
* @param config 配置信息
* @param <T> 数据类型
*/
public static <T> void exportToFile(List<T> data, Class<T> clazz,
String filePath, ExcelExportConfig config) {
if (data == null) {
data = Collections.emptyList();
}
try {
EasyExcel.write(filePath, clazz)
.sheet(config.getDefaultSheetName())
.doWrite(data);
log.info("Excel文件导出成功路径{},数据量:{}", filePath, data.size());
} catch (Exception e) {
log.error("Excel文件导出失败", e);
throw new CzgException("Excel文件导出失败", e);
}
}
/**
* 带样式的Excel导出到Response
*
* @param data 数据列表
* @param clazz 数据类型
* @param fileName 文件名
* @param response HttpServletResponse
* @param <T> 数据类型
*/
public static <T> void exportWithStyleToResponse(List<T> data, Class<T> clazz,
String fileName, HttpServletResponse response) {
if (data == null) {
data = Collections.emptyList();
}
setResponseHeader(response, fileName, DEFAULT_CONFIG);
try (OutputStream outputStream = response.getOutputStream()) {
// 设置表格样式
HorizontalCellStyleStrategy styleStrategy = createCellStyleStrategy();
ExcelWriter excelWriter = EasyExcel.write(outputStream, clazz)
.registerWriteHandler(styleStrategy)
.autoCloseStream(true)
.build();
WriteSheet writeSheet = EasyExcel.writerSheet(DEFAULT_CONFIG.getDefaultSheetName()).build();
excelWriter.write(data, writeSheet);
excelWriter.finish();
log.info("带样式Excel导出成功文件名{},数据量:{}", fileName, data.size());
} catch (IOException e) {
log.error("带样式Excel导出失败", e);
throw new CzgException("Excel导出失败", e);
}
}
/**
* 大数据量分批导出(避免内存溢出)
*
* @param dataSupplier 数据提供者(分页获取数据)
* @param clazz 数据类型
* @param fileName 文件名
* @param response HttpServletResponse
* @param batchSize 每批大小
* @param <T> 数据类型
*/
public static <T> void exportBigDataToResponse(DataSupplier<T> dataSupplier,
Class<T> clazz, String fileName,
HttpServletResponse response, int batchSize) {
setResponseHeader(response, fileName, DEFAULT_CONFIG);
try (OutputStream outputStream = response.getOutputStream()) {
ExcelWriter excelWriter = EasyExcel.write(outputStream, clazz)
.autoCloseStream(true)
.build();
WriteSheet writeSheet = EasyExcel.writerSheet(DEFAULT_CONFIG.getDefaultSheetName()).build();
int pageNum = 1;
List<T> batchData;
boolean hasNext = true;
while (hasNext) {
batchData = dataSupplier.getData(pageNum, batchSize);
if (batchData != null && !batchData.isEmpty()) {
excelWriter.write(batchData, writeSheet);
pageNum++;
} else {
hasNext = false;
}
}
excelWriter.finish();
log.info("大数据量Excel导出成功文件名{},总页数:{}", fileName, pageNum - 1);
} catch (IOException e) {
log.error("大数据量Excel导出失败", e);
throw new CzgException("Excel导出失败", e);
}
}
/**
* 设置响应头
*/
private static void setResponseHeader(HttpServletResponse response, String fileName, ExcelExportConfig config) {
try {
String encodedFileName = URLEncoder.encode(fileName, config.getCharset())
.replaceAll("\\+", "%20");
String contentDisposition = "attachment;filename*=utf-8''" + encodedFileName + ".xlsx";
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding(config.getCharset());
response.setHeader("Content-Disposition", contentDisposition);
} catch (Exception e) {
log.warn("设置响应头失败", e);
}
}
/**
* 创建表格样式策略
*/
private static HorizontalCellStyleStrategy createCellStyleStrategy() {
// 表头样式
WriteCellStyle headStyle = new WriteCellStyle();
headStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
// 内容样式
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
return new HorizontalCellStyleStrategy(headStyle, contentStyle);
}
/**
* 获取数据总行数(用于前端显示进度)
*/
public static <T> int getDataCount(Class<T> clazz) {
Field[] fields = clazz.getDeclaredFields();
int count = 0;
for (Field field : fields) {
if (field.isAnnotationPresent(ExcelProperty.class)) {
count++;
}
}
return count;
}
/**
* 带合并单元格的导出到Response
* 多sheet导出到response
*
* @param sheetDataList 数据列表
* @param fileName 文件名
* @param response HttpServletResponse
*/
public static void exportMultipleSheetsToResponse(List<SheetData> sheetDataList,
String fileName,
HttpServletResponse response) {
if (CollectionUtil.isEmpty(sheetDataList)) {
throw new CzgException("数据列表不能为空");
}
setResponseHeader(response, fileName, DEFAULT_CONFIG);
try (OutputStream outputStream = response.getOutputStream()) {
// 创建样式策略
WriteCellStyle headStyle = new WriteCellStyle();
headStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
headStyle.setVerticalAlignment(VerticalAlignment.CENTER);
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
contentStyle.setVerticalAlignment(VerticalAlignment.CENTER);
HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(headStyle, contentStyle);
// 创建ExcelWriter
ExcelWriterBuilder builder = EasyExcel.write(outputStream)
.autoCloseStream(true)
.registerConverter(new LocalTimeConverter())
.registerWriteHandler(styleStrategy);
ExcelWriter excelWriter = builder.build();
// 逐个写入sheet
for (int i = 0; i < sheetDataList.size(); i++) {
SheetData sheetData = sheetDataList.get(i);
String sheetName = StrUtil.isNotBlank(sheetData.getSheetName())
? sheetData.getSheetName()
: DEFAULT_CONFIG.getDefaultSheetName() + (i + 1);
ExcelWriterSheetBuilder sheetBuilder = EasyExcel.writerSheet(sheetName);
// 注册该sheet的合并处理器
if (sheetData.getHandlers() != null && !sheetData.getHandlers().isEmpty()) {
for (SheetWriteHandler handler : sheetData.getHandlers()) {
sheetBuilder.registerWriteHandler(handler);
}
}
WriteSheet writeSheet = sheetBuilder.head(sheetData.getClazz()).build();
excelWriter.write(sheetData.getData(), writeSheet);
}
excelWriter.finish();
log.info("多sheet商品Excel导出成功文件名{},共{}个sheet", fileName, sheetDataList.size());
} catch (IOException e) {
log.error("多sheet商品Excel导出失败", e);
throw new CzgException("Excel导出失败", e);
}
}
}

View File

@@ -0,0 +1,47 @@
package com.czg.excel;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
/**
* @author yjjie
* @date 2026/1/28 16:16
*/
public class LocalTimeConverter implements Converter<LocalTime> {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss");
@Override
public Class<LocalTime> supportJavaTypeKey() {
return LocalTime.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
String stringValue = cellData.getStringValue();
if (stringValue == null || stringValue.trim().isEmpty()) {
return null;
}
return LocalTime.parse(stringValue, FORMATTER);
}
@Override
public WriteCellData<?> convertToExcelData(LocalTime value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
if (value == null) {
return new WriteCellData<>("");
}
return new WriteCellData<>(value.format(FORMATTER));
}
}

View File

@@ -0,0 +1,21 @@
package com.czg.excel;
import com.alibaba.excel.write.handler.SheetWriteHandler;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.List;
/**
* 多sheet导出数据封装类
* @author yjjie
* @date 2026/1/30 10:53
*/
@Data
@Accessors(chain = true)
public class SheetData {
private List<?> data;
private Class<?> clazz;
private String sheetName;
private List<SheetWriteHandler> handlers;
}