feat(attendance): 新增考勤记录表导出功能
- 在前端API中添加exportAttendanceReport方法用于导出考勤记录 - 在AttendanceRecordsController中添加exportReport接口支持考勤表导出 - 在AttendanceRecordsServiceImpl中实现完整的考勤记录表Excel导出逻辑 - 添加员工分组、按日期汇总打卡时间的业务处理 - 创建多种Excel样式包括标题、表头、日期列和明细行样式 - 在前端页面中新增导出按钮和导出参数设置对话框 - 实现导出前确认同步状态的交互逻辑 - 支持按工号、姓名、部门筛选条件进行考勤表导出
This commit is contained in:
@@ -42,6 +42,17 @@ public class AttendanceRecordsController extends BaseController {
|
||||
ExcelUtil.exportExcel(list, "打卡记录", AttendanceRecordsVo.class, response);
|
||||
}
|
||||
|
||||
@Log(title = "考勤记录表导出", businessType = BusinessType.EXPORT)
|
||||
@PostMapping("/exportReport")
|
||||
public void exportReport(@RequestParam String startTime,
|
||||
@RequestParam String endTime,
|
||||
@RequestParam(required = false) String pin,
|
||||
@RequestParam(required = false) String ename,
|
||||
@RequestParam(required = false) String deptname,
|
||||
HttpServletResponse response) {
|
||||
iAttendanceRecordsService.exportReport(startTime, endTime, pin, ename, deptname, response);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public R<AttendanceRecordsVo> getInfo(@NotNull(message = "主键不能为空")
|
||||
@PathVariable Integer id) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.klp.domain.bo.AttendanceRecordsBo;
|
||||
import com.klp.common.core.page.TableDataInfo;
|
||||
import com.klp.common.core.domain.PageQuery;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
@@ -28,4 +29,9 @@ public interface IAttendanceRecordsService {
|
||||
* 按员工姓名集合 + 时间范围精确查询打卡记录(eq/in,走索引)
|
||||
*/
|
||||
List<AttendanceRecords> queryListByEnamesAndDateRange(List<String> enames, Date startTime, Date endTime);
|
||||
|
||||
/**
|
||||
* 导出考勤记录表(复杂格式)
|
||||
*/
|
||||
void exportReport(String startTime, String endTime, String pin, String ename, String deptname, HttpServletResponse response);
|
||||
}
|
||||
@@ -8,6 +8,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.klp.common.utils.StringUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.klp.domain.bo.AttendanceRecordsBo;
|
||||
import com.klp.domain.vo.AttendanceRecordsVo;
|
||||
@@ -15,14 +17,23 @@ import com.klp.domain.AttendanceRecords;
|
||||
import com.klp.mapper.AttendanceRecordsMapper;
|
||||
import com.klp.service.IAttendanceRecordsService;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Collection;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.ss.util.CellRangeAddress;
|
||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class AttendanceRecordsServiceImpl implements IAttendanceRecordsService {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(AttendanceRecordsServiceImpl.class);
|
||||
private final AttendanceRecordsMapper baseMapper;
|
||||
|
||||
@Override
|
||||
@@ -91,4 +102,246 @@ public class AttendanceRecordsServiceImpl implements IAttendanceRecordsService {
|
||||
.ge(AttendanceRecords::getChecktime, startTime)
|
||||
.le(AttendanceRecords::getChecktime, endTime));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exportReport(String startTime, String endTime, String pin, String ename, String deptname, HttpServletResponse response) {
|
||||
try {
|
||||
// 1. 解析时间范围
|
||||
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||
LocalDate startDate = LocalDate.parse(startTime, fmt);
|
||||
LocalDate endDate = LocalDate.parse(endTime, fmt);
|
||||
int totalDays = startDate.lengthOfMonth();
|
||||
|
||||
Date startDatetime = java.sql.Timestamp.valueOf(startDate.atStartOfDay());
|
||||
Date endDatetime = java.sql.Timestamp.valueOf(endDate.plusDays(1).atStartOfDay().minusNanos(1));
|
||||
|
||||
// 2. 构建查询
|
||||
LambdaQueryWrapper<AttendanceRecords> lqw = Wrappers.lambdaQuery();
|
||||
lqw.ge(AttendanceRecords::getChecktime, startDatetime);
|
||||
lqw.le(AttendanceRecords::getChecktime, endDatetime);
|
||||
if (StringUtils.isNotBlank(pin)) {
|
||||
lqw.eq(AttendanceRecords::getPin, pin);
|
||||
}
|
||||
if (StringUtils.isNotBlank(ename)) {
|
||||
lqw.like(AttendanceRecords::getEname, ename);
|
||||
}
|
||||
if (StringUtils.isNotBlank(deptname)) {
|
||||
lqw.eq(AttendanceRecords::getDeptname, deptname);
|
||||
}
|
||||
lqw.orderByAsc(AttendanceRecords::getPin).orderByAsc(AttendanceRecords::getChecktime);
|
||||
List<AttendanceRecords> records = baseMapper.selectList(lqw);
|
||||
|
||||
// 3. 按员工分组,再按日期分组
|
||||
// key: pin, value: 员工信息 + 按天分组的打卡记录
|
||||
LinkedHashMap<String, EmployeeAttendance> employeeMap = new LinkedHashMap<>();
|
||||
SimpleDateFormat timeFmt = new SimpleDateFormat("HH:mm");
|
||||
|
||||
for (AttendanceRecords r : records) {
|
||||
String pinKey = r.getPin();
|
||||
EmployeeAttendance ea = employeeMap.computeIfAbsent(pinKey, k -> {
|
||||
EmployeeAttendance e = new EmployeeAttendance();
|
||||
e.pin = r.getPin();
|
||||
e.ename = r.getEname();
|
||||
e.deptname = r.getDeptname();
|
||||
e.dailyRecords = new TreeMap<>();
|
||||
return e;
|
||||
});
|
||||
LocalDate checkDate = r.getChecktime().toInstant().atZone(java.time.ZoneId.systemDefault()).toLocalDate();
|
||||
int dayOfMonth = checkDate.getDayOfMonth();
|
||||
String timeStr = timeFmt.format(r.getChecktime());
|
||||
ea.dailyRecords.merge(dayOfMonth, new ArrayList<>(Collections.singletonList(timeStr)), (old, nw) -> {
|
||||
old.addAll(nw);
|
||||
return old;
|
||||
});
|
||||
}
|
||||
|
||||
// 4. 创建 Excel
|
||||
SXSSFWorkbook wb = new SXSSFWorkbook(500);
|
||||
Sheet sheet = wb.createSheet("考勤记录");
|
||||
int currentRow = 0;
|
||||
|
||||
// 样式
|
||||
CellStyle titleStyle = createTitleStyle(wb);
|
||||
CellStyle headerStyle = createHeaderStyle(wb);
|
||||
CellStyle dayHeaderStyle = createDayHeaderStyle(wb);
|
||||
CellStyle infoStyle = createInfoStyle(wb);
|
||||
CellStyle detailStyle = createDetailStyle(wb);
|
||||
|
||||
// --- Row 0: 考勤记录表 ---
|
||||
Row titleRow = sheet.createRow(currentRow++);
|
||||
titleRow.setHeightInPoints(36);
|
||||
Cell titleCell = titleRow.createCell(0);
|
||||
titleCell.setCellValue("考勤记录表");
|
||||
titleCell.setCellStyle(titleStyle);
|
||||
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, totalDays + 2));
|
||||
|
||||
// --- Row 1: 考勤时间 + 制表时间 ---
|
||||
Row timeRow = sheet.createRow(currentRow++);
|
||||
timeRow.setHeightInPoints(22);
|
||||
Cell timeLabelCell = timeRow.createCell(0);
|
||||
timeLabelCell.setCellValue("考勤时间");
|
||||
timeLabelCell.setCellStyle(infoStyle);
|
||||
Cell timeValueCell = timeRow.createCell(1);
|
||||
String periodStr = startDate.format(fmt) + " ~ " + endDate.format(fmt);
|
||||
timeValueCell.setCellValue(periodStr);
|
||||
timeValueCell.setCellStyle(infoStyle);
|
||||
|
||||
Cell makeLabelCell = timeRow.createCell(totalDays / 2 + 1);
|
||||
makeLabelCell.setCellValue("制表时间");
|
||||
makeLabelCell.setCellStyle(infoStyle);
|
||||
Cell makeValueCell = timeRow.createCell(totalDays / 2 + 2);
|
||||
makeValueCell.setCellValue(LocalDate.now().format(fmt));
|
||||
makeValueCell.setCellStyle(infoStyle);
|
||||
|
||||
// --- Row 2: 日期列头 1,2,3,... ---
|
||||
Row dayHeaderRow = sheet.createRow(currentRow++);
|
||||
dayHeaderRow.setHeightInPoints(20);
|
||||
Cell dayCell = dayHeaderRow.createCell(0);
|
||||
dayCell.setCellStyle(dayHeaderStyle);
|
||||
for (int d = 1; d <= totalDays; d++) {
|
||||
Cell c = dayHeaderRow.createCell(d);
|
||||
c.setCellValue(d);
|
||||
c.setCellStyle(dayHeaderStyle);
|
||||
}
|
||||
|
||||
// --- 员工循环 ---
|
||||
int dataColCount = totalDays + 1;
|
||||
for (EmployeeAttendance ea : employeeMap.values()) {
|
||||
// 员工信息行
|
||||
Row infoRow = sheet.createRow(currentRow++);
|
||||
infoRow.setHeightInPoints(22);
|
||||
Cell pinLabel = infoRow.createCell(0);
|
||||
pinLabel.setCellValue("工号:");
|
||||
pinLabel.setCellStyle(infoStyle);
|
||||
Cell pinValue = infoRow.createCell(1);
|
||||
pinValue.setCellValue(ea.pin != null ? ea.pin : "");
|
||||
pinValue.setCellStyle(infoStyle);
|
||||
|
||||
int nameCol = Math.max(3, totalDays / 4);
|
||||
Cell nameLabel = infoRow.createCell(nameCol);
|
||||
nameLabel.setCellValue("姓 名:");
|
||||
nameLabel.setCellStyle(infoStyle);
|
||||
Cell nameValue = infoRow.createCell(nameCol + 1);
|
||||
nameValue.setCellValue(ea.ename != null ? ea.ename : "");
|
||||
nameValue.setCellStyle(infoStyle);
|
||||
|
||||
int deptCol = Math.max(nameCol + 3, totalDays / 2);
|
||||
Cell deptLabel = infoRow.createCell(deptCol);
|
||||
deptLabel.setCellValue("部 门:");
|
||||
deptLabel.setCellStyle(infoStyle);
|
||||
Cell deptValue = infoRow.createCell(deptCol + 1);
|
||||
deptValue.setCellValue(ea.deptname != null ? ea.deptname : "");
|
||||
deptValue.setCellStyle(infoStyle);
|
||||
|
||||
// 打卡明细行
|
||||
Row detailRow = sheet.createRow(currentRow++);
|
||||
detailRow.setHeightInPoints(18 * 4);
|
||||
for (int d = 1; d <= totalDays; d++) {
|
||||
Cell c = detailRow.createCell(d);
|
||||
List<String> times = ea.dailyRecords.get(d);
|
||||
if (times != null && !times.isEmpty()) {
|
||||
c.setCellValue(String.join("\n", times));
|
||||
}
|
||||
c.setCellStyle(detailStyle);
|
||||
}
|
||||
// 最后一行不设边框(区分下一个员工),但保留整体边框
|
||||
}
|
||||
|
||||
// 设置列宽
|
||||
sheet.setColumnWidth(0, 2000);
|
||||
for (int d = 1; d <= totalDays; d++) {
|
||||
sheet.setColumnWidth(d, 2400);
|
||||
}
|
||||
|
||||
// 5. 输出
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = "考勤记录表_" + startTime + "_" + endTime;
|
||||
String encodedFileName = java.net.URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName + ".xlsx");
|
||||
|
||||
OutputStream out = response.getOutputStream();
|
||||
wb.write(out);
|
||||
out.flush();
|
||||
wb.close();
|
||||
} catch (Exception e) {
|
||||
log.error("导出考勤记录表失败", e);
|
||||
throw new RuntimeException("导出考勤记录表失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private CellStyle createTitleStyle(SXSSFWorkbook wb) {
|
||||
CellStyle style = wb.createCellStyle();
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
Font font = wb.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 18);
|
||||
font.setBold(true);
|
||||
style.setFont(font);
|
||||
return style;
|
||||
}
|
||||
|
||||
private CellStyle createHeaderStyle(SXSSFWorkbook wb) {
|
||||
CellStyle style = wb.createCellStyle();
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
Font font = wb.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);
|
||||
style.setFont(font);
|
||||
return style;
|
||||
}
|
||||
|
||||
private CellStyle createDayHeaderStyle(SXSSFWorkbook wb) {
|
||||
CellStyle style = wb.createCellStyle();
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
Font font = wb.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);
|
||||
style.setFont(font);
|
||||
return style;
|
||||
}
|
||||
|
||||
private CellStyle createInfoStyle(SXSSFWorkbook wb) {
|
||||
CellStyle style = wb.createCellStyle();
|
||||
style.setAlignment(HorizontalAlignment.LEFT);
|
||||
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
Font font = wb.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 10);
|
||||
style.setFont(font);
|
||||
return style;
|
||||
}
|
||||
|
||||
private CellStyle createDetailStyle(SXSSFWorkbook wb) {
|
||||
CellStyle style = wb.createCellStyle();
|
||||
style.setAlignment(HorizontalAlignment.CENTER);
|
||||
style.setVerticalAlignment(VerticalAlignment.TOP);
|
||||
style.setBorderBottom(BorderStyle.THIN);
|
||||
style.setBorderTop(BorderStyle.THIN);
|
||||
style.setBorderLeft(BorderStyle.THIN);
|
||||
style.setBorderRight(BorderStyle.THIN);
|
||||
style.setWrapText(true);
|
||||
Font font = wb.createFont();
|
||||
font.setFontName("宋体");
|
||||
font.setFontHeightInPoints((short) 9);
|
||||
style.setFont(font);
|
||||
return style;
|
||||
}
|
||||
|
||||
private static class EmployeeAttendance {
|
||||
String pin;
|
||||
String ename;
|
||||
String deptname;
|
||||
// key: dayOfMonth(1~31), value: 当天打卡时间列表
|
||||
TreeMap<Integer, List<String>> dailyRecords;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user