feat(attendance): 新增考勤记录表导出功能

- 在前端API中添加exportAttendanceReport方法用于导出考勤记录
- 在AttendanceRecordsController中添加exportReport接口支持考勤表导出
- 在AttendanceRecordsServiceImpl中实现完整的考勤记录表Excel导出逻辑
- 添加员工分组、按日期汇总打卡时间的业务处理
- 创建多种Excel样式包括标题、表头、日期列和明细行样式
- 在前端页面中新增导出按钮和导出参数设置对话框
- 实现导出前确认同步状态的交互逻辑
- 支持按工号、姓名、部门筛选条件进行考勤表导出
This commit is contained in:
2026-07-03 15:58:43 +08:00
parent b3da00e2e7
commit d03cd926bc
5 changed files with 406 additions and 4 deletions

View File

@@ -56,3 +56,13 @@ export function syncRecords({ starttime, endtime }) {
}
})
}
// 导出考勤记录表
export function exportAttendanceReport(params) {
return request({
url: '/wms/attendanceRecords/exportReport',
method: 'post',
params: params,
responseType: 'blob'
})
}

View File

@@ -69,6 +69,7 @@
<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
<el-button type="warning" icon="el-icon-upload" size="mini" @click="handleSync">同步</el-button>
<el-button type="success" icon="el-icon-download" size="mini" @click="handleExportReport">导出考勤表</el-button>
</el-form-item>
</el-form>
@@ -187,11 +188,42 @@
<el-button @click="syncDialogVisible = false">取消</el-button>
</div>
</el-dialog>
<!-- 导出考勤表对话框 -->
<el-dialog title="导出考勤记录表" :visible.sync="exportDialogVisible" width="480px" append-to-body>
<el-form ref="exportForm" :model="exportFormData" :rules="exportRules" label-width="90px">
<el-form-item label="考勤时间" prop="dateRange">
<el-date-picker
v-model="exportFormData.dateRange"
type="daterange"
range-separator="~"
start-placeholder="开始日期"
end-placeholder="结束日期"
value-format="yyyy-MM-dd"
:picker-options="pickerOptions"
style="width: 100%">
</el-date-picker>
</el-form-item>
<el-form-item label="员工编号" prop="pin">
<el-input v-model="exportFormData.pin" placeholder="请输入员工编号(可选)" clearable />
</el-form-item>
<el-form-item label="姓名" prop="ename">
<el-input v-model="exportFormData.ename" placeholder="请输入姓名(可选)" clearable />
</el-form-item>
<el-form-item label="部门" prop="deptname">
<el-input v-model="exportFormData.deptname" placeholder="请输入部门(可选)" clearable />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button :loading="exportLoading" type="primary" @click="doExportReport">确定导出</el-button>
<el-button @click="exportDialogVisible = false">取消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { listRecords, getRecords, delRecords, addRecords, updateRecords, syncRecords } from "@/api/wms/attendance";
import { listRecords, getRecords, delRecords, addRecords, updateRecords, syncRecords, exportAttendanceReport } from "@/api/wms/attendance";
export default {
name: "Records",
@@ -211,6 +243,41 @@ export default {
syncRules: {
syncMonth: [{ required: true, message: "请选择同步月份", trigger: "change" }]
},
// 导出弹窗
exportDialogVisible: false,
// 导出loading
exportLoading: false,
// 导出表单
exportFormData: {
dateRange: [],
pin: "",
ename: "",
deptname: ""
},
// 导出表单校验
exportRules: {
dateRange: [{ required: true, message: "请选择考勤时间范围", trigger: "change" }]
},
// 日期选择器快捷选项
pickerOptions: {
shortcuts: [{
text: '本月',
onClick(picker) {
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth(), 1);
const end = new Date(now.getFullYear(), now.getMonth() + 1, 0);
picker.$emit('pick', [start, end]);
}
}, {
text: '上月',
onClick(picker) {
const now = new Date();
const start = new Date(now.getFullYear(), now.getMonth() - 1, 1);
const end = new Date(now.getFullYear(), now.getMonth(), 0);
picker.$emit('pick', [start, end]);
}
}]
},
// 遮罩层
loading: true,
// 选中数组
@@ -391,6 +458,61 @@ export default {
this.download('wms/records/export', {
...this.queryParams
}, `records_${new Date().getTime()}.xlsx`)
},
/** 导出考勤记录表 */
handleExportReport() {
this.$modal.confirm(
'导出前请先确保已同步最新的考勤数据,是否继续导出?',
'提示',
{
confirmButtonText: '已同步,继续导出',
cancelButtonText: '去同步',
type: 'info'
}
).then(() => {
this.exportFormData.dateRange = [];
this.exportFormData.pin = "";
this.exportFormData.ename = "";
this.exportFormData.deptname = "";
this.exportDialogVisible = true;
this.$nextTick(() => {
this.$refs.exportForm && this.$refs.exportForm.clearValidate();
});
}).catch(action => {
if (action === 'cancel') {
this.handleSync();
}
});
},
/** 执行导出考勤记录表 */
doExportReport() {
this.$refs.exportForm.validate(valid => {
if (!valid) return;
const [startDate, endDate] = this.exportFormData.dateRange;
this.exportLoading = true;
exportAttendanceReport({
startTime: startDate,
endTime: endDate,
pin: this.exportFormData.pin || undefined,
ename: this.exportFormData.ename || undefined,
deptname: this.exportFormData.deptname || undefined
}).then(res => {
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = `考勤记录表_${startDate}_${endDate}.xlsx`;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
this.$modal.msgSuccess("导出成功");
this.exportLoading = false;
this.exportDialogVisible = false;
}).catch(() => {
this.exportLoading = false;
});
});
}
}
};

View File

@@ -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) {

View File

@@ -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);
}

View File

@@ -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;
}
}