package com.learn.learn.listener.excel;
import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.learn.learn.entity.BaseEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
@EqualsAndHashCode(callSuper = true)
@Data
@ExcelIgnoreUnannotated
public class UserTemplate extends BaseEntity {
@ExcelProperty(value = "用户ID")
private Long userId;
@ExcelProperty(value = "部门ID")
private Long deptId;
@ExcelProperty(value = "用户账号")
private String userName;
@ExcelProperty(value = "用户昵称")
private String nickName;
@ExcelProperty(value = "用户类型")
private String userType;
@ExcelProperty(value = "部门ID")
private String email;
@ExcelProperty(value = "手机号码")
private String phonenumber;
@ExcelProperty(value = "用户性别(0男 1女 2未知")
private String sex;
@ExcelProperty(value = "头像地址")
private String avatar;
@ExcelProperty(value = "密码")
private String password;
@ExcelProperty(value = "账号状态(0正常 1停用)")
private String status;
@ExcelProperty(value = "删除标志(0代表存在 2代表删除)")
private String delFlag;
@ExcelProperty(value = "最后登录IP")
private String loginIp;
@ExcelProperty(value = "最后登录时间")
private Date loginDate;
@ExcelProperty(value = "密码最后更新时间")
private Date pwdUpdateDate;
}
public void exportFile(Integer template, HttpServletResponse response) {
String filenName = "用户信息";
FileServiceImpl.setExportHeader(response, filenName);
try (ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), UserTemplate.class).build()) {
Long count = sysUserMapper.selectCount(null);
if (count == null || count <= 0) {
FileServiceImpl.log.warn("无用户数据可供导出");
return;
}
int pageSize = 1000;
long totalPages = (count + pageSize - 1) / pageSize;
CountDownLatch countDownLatch = new CountDownLatch((int) totalPages);
Map<Integer, List<SysUser>> pageMap = new ConcurrentHashMap<>();
for (int i = 0; i < totalPages; i++) {
int finalI = i;
asyncExecutor.execute(() -> {
try {
Page<SysUser> page = new Page<>();
page.setCurrent(finalI + 1);
page.setSize(pageSize);
Page<SysUser> selectPage = sysUserMapper.selectPage(page, null);
List<SysUser> userList = selectPage.getRecords();
if (userList != null && !userList.isEmpty()) {
pageMap.put(finalI, userList);
}
} catch (Exception e) {
FileServiceImpl.log.error("获取第{}页数据失败", finalI + 1, e);
} finally {
countDownLatch.countDown();
}
});
}
boolean finished = countDownLatch.await(60, TimeUnit.SECONDS);
if (!finished) {
FileServiceImpl.log.error("导出过程中部分页面加载超时");
throw new RuntimeException("导出超时,请稍后再试");
}
for (int i = 0; i < totalPages; i++) {
List<SysUser> list = pageMap.get(i);
if (!list.isEmpty()) {
WriteSheet writeSheet = EasyExcel.writerSheet(i, "第" + (i + 1) + "页").build();
excelWriter.write(list, writeSheet);
} else {
FileServiceImpl.log.warn("第{}页数据为空", i + 1);
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
FileServiceImpl.log.error("导出操作被中断", e);
} catch (Exception e) {
FileServiceImpl.log.error("导出文件失败", e);
}
}
private static void setExportHeader(HttpServletResponse response, String fileName) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
String filename = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + filename + ".xlsx");
}