excel

@Override
public void exportExcel(ShopEmployeeSchedulingExportExcelRequestVO requestVO, AsyncResponse response) {
int daysByYearMonth = DateUtil.getDaysByYearMonth(requestVO.getSchedulingDate());
List<List<Object>> contentList = Lists.newArrayList();

//查询列表
EmployeeInfoSearchRequestDTO requestDTO = new EmployeeInfoSearchRequestDTO();
requestDTO.setShopId(requestVO.getShopId());
requestDTO.setEmployeeType("1");
EmployeeInfoSearchResponseDTO employeeInfoSearchResponseDTO = employeeInfoManager.search(requestDTO);
List<EmployeeInfoSearchResponseDTO.Employee> employeeVoList = employeeInfoSearchResponseDTO.getEmployeeVoList();
if (CollectionUtil.isNotEmpty(employeeVoList)) {
//查询要导出的数据信息
ShopEmployeeSchedulingQueryPO query = new ShopEmployeeSchedulingQueryPO();
query.setShopId(requestVO.getShopId());
query.setSchedulingDate(requestVO.getSchedulingDate());
List<ShopEmployeeSchedulingResult> schedulingResults = shopEmployeeSchedulingMapper.search(query);

HashMap<Long, List<String>> employeeDateMap = new HashMap<>();

employeeVoList.forEach(employee -> {
List<String> list = new ArrayList<>(Collections.nCopies(daysByYearMonth, SchedulingConstant.ZERO_STATUS));
list.add(0, employee.getEmployeeName());
employeeDateMap.put(employee.getEmployeeId(), list);
});

if (!CollectionUtils.isEmpty(schedulingResults)) {
//查询班次信息
ShopSchedulingQueryPO queryPO = new ShopSchedulingQueryPO();
queryPO.setShopId(requestVO.getShopId());
queryPO.setSchedulingType("1");
List<ShopSchedulingPO> schedulingPOS = shopSchedulingMapper.search(queryPO);
HashMap<String, String> schedulingHashMap = new HashMap<>();
if (CollectionUtil.isNotEmpty(schedulingPOS)) {
schedulingPOS.forEach(shopSchedulingPO -> {
String[] workDays = shopSchedulingPO.getWorkDay().split(",");
for (String workDay : workDays) {
schedulingHashMap.put(workDay, shopSchedulingPO.getSchedulingName());
}
});
}


//拼装数据
schedulingResults.forEach(shopEmployeeSchedulingResult -> {
Long employeeId = shopEmployeeSchedulingResult.getEmployeeId();
List<String> dataList = employeeDateMap.get(employeeId);

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = shopEmployeeSchedulingResult.getSchedulingDate().format(formatter);

String[] split = formattedDate.split("-");
int index = Integer.parseInt(split[split.length - 1]) - 1;

String dayOfWeekByDate = DateUtil.getWeekDay(shopEmployeeSchedulingResult.getSchedulingDate().getDayOfWeek());

String scheduling = schedulingHashMap.get(dayOfWeekByDate);

String data = dataList.get(index);
if (data.equals(SchedulingConstant.ZERO_STATUS)) {
if (StringUtils.hasLength(scheduling)) {
dataList.set(index, scheduling);
} else {
dataList.set(index, SchedulingConstant.DEFAULT_STATUS);
}
} else {
String dataNew = data + "," + scheduling;
dataList.set(index, dataNew);
}
});
}

for (Map.Entry<Long, List<String>> entry : employeeDateMap.entrySet()) {
List<String> value = entry.getValue();
contentList.add(Lists.newArrayList(value));
}

}


ArrayList<String> title = new ArrayList<>();
title.add("员工姓名");
for (int i = 1; i <= daysByYearMonth; i++) {
String yearMonth = requestVO.getSchedulingDate();
String day = String.valueOf(i);
String date = yearMonth + "-" + day;
title.add(date);
}

//设置返回文件信息
response.setHeader("Content-Disposition", "attachment");
response.setHeader("filename", "员工排班信息列表.xlsx");
response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("charset", "UTF-8");

HttpOutputStream outputStream = response.getOutputStream();
ExcelWriter writer = EasyExcelFactory.write(outputStream).build();

// 动态添加表头,适用一些表头动态变化的场景
WriteSheet sheet = new WriteSheet();
sheet.setSheetName("员工排班信息");
sheet.setSheetNo(0);
// 创建一个表格,用于 Sheet 中使用
WriteTable table = new WriteTable();
table.setTableNo(1);
table.setHead(head(title));
// 写数据
writer.write(contentList, sheet, table);
writer.finish();
}

private List<List<String>> head(List<String> title) {
List<List<String>> headTitles = Lists.newArrayList();
if (CollectionUtil.isNotEmpty(title)) {
title.forEach(t ->
headTitles.add(Lists.newArrayList(t))
);
}

return headTitles;
}
posted @ 2023-09-26 09:53  我来偷家了  阅读(4)  评论(0编辑  收藏  举报