用户列表导出接口
This commit is contained in:
@@ -28,5 +28,4 @@ public class ShopUserDTO extends ShopUser {
|
||||
private String nextMemberLevelName;
|
||||
private Long nextExperience;
|
||||
private Long pointBalance;
|
||||
private boolean isNew;
|
||||
}
|
||||
|
||||
@@ -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 "是";
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -95,6 +95,11 @@
|
||||
<artifactId>mybatis-flex-processor</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
package com.czg.excel;
|
||||
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.ExcelWriter;
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user