zouyu
2026-04-13 8cbb4e01b9226b32797a48489c5e1b30da3e2110
inspect-server/src/main/java/com/ruoyi/inspect/service/impl/StaffAttendanceTrackingRecordServiceImpl.java
@@ -2,12 +2,14 @@
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Result;
import com.google.common.util.concurrent.AtomicDouble;
import com.ruoyi.common.enums.ClockInState;
import com.ruoyi.common.enums.EnterOrExitType;
import com.ruoyi.common.enums.SyncStatus;
@@ -15,13 +17,15 @@
import com.ruoyi.common.utils.api.icc.model.GetResultPageRequest;
import com.ruoyi.common.utils.api.icc.model.GetResultPageResponse;
import com.ruoyi.inspect.dto.StaffAttendanceDTO;
import com.ruoyi.inspect.excel.StaffAttendanceAnnotationTextExcelData;
import com.ruoyi.inspect.excel.StaffAttendanceExcelData;
import com.ruoyi.inspect.excel.handler.CommentWriteHandler;
import com.ruoyi.inspect.mapper.StaffAttendanceTrackingRecordMapper;
import com.ruoyi.inspect.pojo.StaffAttendanceTrackingRecord;
import com.ruoyi.inspect.service.StaffAttendanceTrackingRecordService;
import com.ruoyi.inspect.util.HourDiffCalculator;
import com.ruoyi.inspect.util.TimeDiffCalculator;
import com.ruoyi.inspect.vo.StaffAttendanceVO;
import com.ruoyi.inspect.vo.StaffClockInVO;
import com.ruoyi.performance.dto.PerformanceShiftMapDto;
import com.ruoyi.performance.mapper.PerformanceShiftMapper;
import com.ruoyi.performance.mapper.ShiftTimeMapper;
@@ -29,17 +33,27 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
@@ -75,6 +89,14 @@
    // 数字部分固定长度
    private static final int DIGIT_LENGTH = 6;
    private static final String holidayLeaveKeyword = "休";//休假,调休假班次关键字
    private static final String personalLeaveKeyword = "事";//事假班次关键字
    private static final String annualLeaveKeyword = "年";//年假班次关键字
    private static final String officialTripKeyword = "公";//公差班次关键字
    private static final String marriageLeaveKeyword = "婚";//婚假班次关键字
    private static final String bereavementLeaveKeyword = "丧";//丧假班次关键字
    private static final String sickLeaveKeyword = "病";//病假班次关键字
    /**
     * 同步的门禁设备列表
@@ -142,9 +164,13 @@
        return true;
    }
    @Override
    public IPage<StaffAttendanceVO> pageAttendanceRecord(Page<StaffAttendanceTrackingRecord> page,
            StaffAttendanceDTO staffAttendanceDTO) {
    /**
     * 查询考勤记录
     * @param performanceShifts  班次信息
     * @param staffAttendanceDTO 考勤查询条件
     * @return
     */
    public List<StaffAttendanceVO> getAttendanceRecord(List<PerformanceShiftMapDto> performanceShifts,StaffAttendanceDTO staffAttendanceDTO) {
        // 查询打卡记录
        Wrapper<StaffAttendanceTrackingRecord> queryWrapper = Wrappers.<StaffAttendanceTrackingRecord>lambdaQuery()
                .eq(StaffAttendanceTrackingRecord::getEnableReport, Boolean.TRUE)
@@ -155,9 +181,6 @@
                                .or()
                                .like(StaffAttendanceTrackingRecord::getPersonName, staffAttendanceDTO.getKeyword()));
        List<StaffAttendanceTrackingRecord> recordList = baseMapper.selectList(queryWrapper);
        // 查询班次
        List<PerformanceShiftMapDto> performanceShifts = performanceShiftMapper.selectListByWorkTime(
                staffAttendanceDTO.getStartDate(), staffAttendanceDTO.getEndDate(), staffAttendanceDTO.getKeyword());
        // 组装数据
        List<StaffAttendanceVO> resultList = new ArrayList<>();
        for (int i = 0; i < performanceShifts.size(); i++) {
@@ -185,8 +208,7 @@
                LocalDateTime currentShiftStartDateTime = LocalDateTime.of(p.getWorkTime().toLocalDate(),
                        currentShiftStartTime);
                // 下一班次开始时间
                LocalDateTime nextShiftStartDateTime = getShiftStartDateTime(i + 1, performanceShifts,
                        startDateTime.plusDays(1L));
                LocalDateTime nextShiftStartDateTime = getShiftStartDateTime(p.getPersonCode(), performanceShifts, startDateTime.plusDays(1L));
                if (Double.compare(hourDiff, 0) == -1) {
                    // 如果小时差为负数,表示跨天,结束时间需加一
                    endDateTime = endDateTime.plusDays(1L);
@@ -258,6 +280,7 @@
                vo.setPersonName(p.getUserName());
                //应勤时长
                double plannedWorkHours = Math.abs(hourDiff);
                vo.setDiffHour(hourDiff);
                vo.setPlannedWorkHours(plannedWorkHours);
                vo.setSwingDate(startDateTime);
                vo.setWorkDateTime(workDateTime);
@@ -289,7 +312,16 @@
                    resultList.add(vo);
            }
        }
        return limitPages(page, resultList);
        return resultList;
    }
    @Override
    public IPage<StaffAttendanceVO> pageAttendanceRecord(Page<StaffAttendanceTrackingRecord> page,
            StaffAttendanceDTO staffAttendanceDTO) {
        // 查询班次
        List<PerformanceShiftMapDto> performanceShifts = performanceShiftMapper.selectListByWorkTime(
                staffAttendanceDTO.getStartDate(), staffAttendanceDTO.getEndDate(), staffAttendanceDTO.getKeyword());
        return limitPages(page, getAttendanceRecord(performanceShifts,staffAttendanceDTO));
    }
    @Override
@@ -369,6 +401,208 @@
        return this.saveOrUpdateBatch(records);
    }
    @Override
    public void exportStaffAttendanceRecords(HttpServletResponse response, StaffAttendanceDTO staffAttendanceDTO) {
        response.reset();
        try{
            List<LocalDate> attendanceDateList = buildAttendanceDateList(staffAttendanceDTO);
            //批注信息坐标信息
            List<StaffAttendanceAnnotationTextExcelData> annotationTextList = new ArrayList<>();
            // 查询班次
            List<PerformanceShiftMapDto> performanceShifts = performanceShiftMapper.selectListByWorkTime(
                    staffAttendanceDTO.getStartDate(), staffAttendanceDTO.getEndDate(), staffAttendanceDTO.getKeyword());
            //获取考勤数据
            List<StaffAttendanceVO> attendanceRecords = getAttendanceRecord(performanceShifts,staffAttendanceDTO);
            //组装导出数据
            List<StaffAttendanceExcelData> excelData = new ArrayList<>();
            Map<Integer, List<PerformanceShiftMapDto>> groupByUserId = performanceShifts.stream().collect(Collectors.groupingBy(PerformanceShiftMapDto::getUserId));
            List<Integer> userIdKeys = groupByUserId.keySet().stream().sorted().collect(Collectors.toList());
            for (int i = 0; i < userIdKeys.size(); i++) {
                List<PerformanceShiftMapDto> shiftMapDtos = groupByUserId.get(userIdKeys.get(i));
                StaffAttendanceExcelData attendanceExcelData = new StaffAttendanceExcelData();
                attendanceExcelData.setExcelIndex(i+1);
                List<String> shiftList = new ArrayList<>();
                attendanceExcelData.setPersonName(shiftMapDtos.get(0).getUserName());
                AtomicInteger holidayCount = new AtomicInteger(0);//休息天数
                AtomicInteger personalLeaveCount = new AtomicInteger(0);//事假天数
                AtomicInteger annualLeaveCount = new AtomicInteger(0);//年假天数
                AtomicInteger officialTripCount = new AtomicInteger(0);//公差天数
                AtomicInteger marriageLeaveCount = new AtomicInteger(0);//婚假天数
                AtomicInteger bereavementLeaveCount = new AtomicInteger(0);//丧假天数
                AtomicInteger sickLeaveCount = new AtomicInteger(0);//病假天数
                AtomicInteger attendanceDayCount = new AtomicInteger(0);//出勤天数
                AtomicDouble attendanceWorkHourCount = new AtomicDouble(0D);//出勤总时间
                for (int j = 0; j < shiftMapDtos.size(); j++) {
                    PerformanceShiftMapDto shiftMapDto = shiftMapDtos.get(j);
                    //统计各假期和公差的天数
                    if(StringUtils.contains(shiftMapDto.getShiftName(),holidayLeaveKeyword)){
                        holidayCount.getAndIncrement();
                    }else if(StringUtils.contains(shiftMapDto.getShiftName(),personalLeaveKeyword)){
                        personalLeaveCount.getAndIncrement();
                    }else if(StringUtils.contains(shiftMapDto.getShiftName(),annualLeaveKeyword)){
                        annualLeaveCount.getAndIncrement();
                    }else if(StringUtils.contains(shiftMapDto.getShiftName(),officialTripKeyword)){
                        officialTripCount.getAndIncrement();
                    }else if(StringUtils.contains(shiftMapDto.getShiftName(),marriageLeaveKeyword)){
                        marriageLeaveCount.getAndIncrement();
                    }else if(StringUtils.contains(shiftMapDto.getShiftName(),bereavementLeaveKeyword)){
                        bereavementLeaveCount.getAndIncrement();
                    }else if(StringUtils.contains(shiftMapDto.getShiftName(),sickLeaveKeyword)){
                        sickLeaveCount.getAndIncrement();
                    }
                    if(StringUtils.isAllBlank(shiftMapDto.getStartTime(),shiftMapDto.getEndTime())){
                        shiftList.add(shiftMapDto.getShiftName());
                    }else{
                        //过滤当前人员的班次信息
                        StaffAttendanceVO vo = attendanceRecords.stream().filter(f->StringUtils.isNotBlank(f.getPersonCode())).filter(f -> StringUtils.equals(f.getPersonCode(), shiftMapDto.getPersonCode()) && f.getSwingDate().isEqual(shiftMapDto.getWorkTime())).findFirst().orElse(null);
                        if(ObjectUtils.isEmpty(vo)){
                            shiftList.add("");
                        }else{
                            String actualWorkHours = Objects.toString(vo.getActualWorkHours(), "");
                            Double diffHour = ObjectUtils.defaultIfNull(vo.getDiffHour(), 0D);
                            if (StringUtils.isBlank(actualWorkHours)) {
                                shiftList.add("");
                            } else {
                                shiftList.add(Double.compare(diffHour, 0D) < 0 ? "-" + actualWorkHours : actualWorkHours);
                                attendanceDayCount.getAndIncrement();
                                attendanceWorkHourCount.getAndAdd(Double.parseDouble(actualWorkHours));
                            }
                        }
                    }
                    if(StringUtils.isNotBlank(shiftMapDto.getAnnotationText())){
                        annotationTextList.add(new StaffAttendanceAnnotationTextExcelData(i,j,shiftMapDto.getAnnotationText()));
                    }
                }
                attendanceExcelData.setShiftList(shiftList);
                attendanceExcelData.setAttendanceDayCount(attendanceDayCount.get());
                attendanceExcelData.setAttendanceWorkHourCount(attendanceWorkHourCount.get());
                //班次考勤天数
                attendanceExcelData.setHolidayCount(holidayCount.get());
                attendanceExcelData.setPersonalLeaveCount(personalLeaveCount.get());
                attendanceExcelData.setAnnualLeaveCount(annualLeaveCount.get());
                attendanceExcelData.setOfficialTripCount(officialTripCount.get());
                attendanceExcelData.setMarriageLeaveCount(marriageLeaveCount.get());
                attendanceExcelData.setBereavementLeaveCount(bereavementLeaveCount.get());
                attendanceExcelData.setSickLeaveCount(sickLeaveCount.get());
                excelData.add(attendanceExcelData);
            }
            //导出
            String fileName = "中天耐丝质量考勤汇总"+ ExcelTypeEnum.XLSX;
            fileName =  URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString());
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            InputStream resourceAsStream = buildAttendanceTemplate(attendanceDateList);
            EasyExcel.write(response.getOutputStream())
                    .withTemplate(resourceAsStream)
                    .registerWriteHandler(new CommentWriteHandler(excelData, annotationTextList))
                    .relativeHeadRowIndex(4)
                    .sheet()
                    .doFill(excelData);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private List<LocalDate> buildAttendanceDateList(StaffAttendanceDTO staffAttendanceDTO) {
        if (staffAttendanceDTO == null || staffAttendanceDTO.getStartDate() == null || staffAttendanceDTO.getEndDate() == null) {
            throw new IllegalArgumentException("导出时间范围不能为空");
        }
        LocalDate startDate = staffAttendanceDTO.getStartDate().toLocalDate();
        LocalDate endDate = staffAttendanceDTO.getEndDate().toLocalDate();
        if (startDate.isAfter(endDate)) {
            throw new IllegalArgumentException("开始时间不能晚于结束时间");
        }
        List<LocalDate> attendanceDateList = new ArrayList<>();
        for (LocalDate currentDate = startDate; !currentDate.isAfter(endDate); currentDate = currentDate.plusDays(1)) {
            attendanceDateList.add(currentDate);
        }
        if (attendanceDateList.size() > 31) {
            throw new IllegalArgumentException("导出时间范围不能超过31天");
        }
        return attendanceDateList;
    }
    private InputStream buildAttendanceTemplate(List<LocalDate> attendanceDateList) throws IOException {
        try (InputStream templateStream = this.getClass().getResourceAsStream("/static/staff_attendance_template.xlsx");
                Workbook workbook = WorkbookFactory.create(templateStream);
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            Sheet sheet = workbook.getSheetAt(0);
            fillAttendanceHeader(sheet, attendanceDateList);
            workbook.write(outputStream);
            return new ByteArrayInputStream(outputStream.toByteArray());
        } catch (Exception e) {
            throw new IOException("构建考勤导出模板失败", e);
        }
    }
    private void fillAttendanceHeader(Sheet sheet, List<LocalDate> attendanceDateList) {
        if (sheet == null || attendanceDateList == null || attendanceDateList.isEmpty()) {
            return;
        }
        Row titleRow = sheet.getRow(1);
        if (titleRow != null) {
            Cell titleCell = titleRow.getCell(0);
            if (titleCell != null) {
                titleCell.setCellValue(attendanceDateList.get(attendanceDateList.size() - 1).format(DateTimeFormatter.ofPattern("yyyy年M月")));
            }
        }
        Row weekRow = sheet.getRow(2);
        Row dayRow = sheet.getRow(3);
        if (weekRow == null || dayRow == null) {
            return;
        }
        final int startColumnIndex = 2;
        final int maxDateColumnCount = 31;
        for (int i = 0; i < maxDateColumnCount; i++) {
            Cell weekCell = getOrCreateCell(weekRow, startColumnIndex + i, startColumnIndex);
            Cell dayCell = getOrCreateCell(dayRow, startColumnIndex + i, startColumnIndex);
            if (i < attendanceDateList.size()) {
                LocalDate currentDate = attendanceDateList.get(i);
                weekCell.setCellValue(resolveWeekOfYear(currentDate));
                dayCell.setCellValue(currentDate.getDayOfMonth());
            } else {
                weekCell.setBlank();
                dayCell.setBlank();
            }
        }
    }
    private Cell getOrCreateCell(Row row, int cellIndex, int templateCellIndex) {
        Cell cell = row.getCell(cellIndex);
        if (cell != null) {
            return cell;
        }
        Cell templateCell = row.getCell(templateCellIndex);
        cell = row.createCell(cellIndex);
        if (templateCell != null && templateCell.getCellStyle() != null) {
            cell.setCellStyle(templateCell.getCellStyle());
        }
        return cell;
    }
    private String resolveWeekOfYear(LocalDate date) {
        switch (date.getDayOfWeek()) {
            case MONDAY:
                return "一";
            case TUESDAY:
                return "二";
            case WEDNESDAY:
                return "三";
            case THURSDAY:
                return "四";
            case FRIDAY:
                return "五";
            case SATURDAY:
                return "六";
            case SUNDAY:
                return "日";
            default:
                return "";
        }
    }
    /**
     * 自定义分页方法
     * 
@@ -416,20 +650,23 @@
    }
    /**
     * 获取指定下标的班次开始时间
     *
     * @param index
     * @param dtoList
     * 获取当前班次日期的下一班次开始时间
     * @param personCode 人员编号
     * @param dtoList 班次列表
     * @param nextShiftTime 下一班次时间
     * @return
     */
    private LocalDateTime getShiftStartDateTime(int index, List<PerformanceShiftMapDto> dtoList,
            LocalDateTime nextShiftTime) {
        if (dtoList.isEmpty() || index >= dtoList.size()) {
    private LocalDateTime getShiftStartDateTime(String personCode,List<PerformanceShiftMapDto> dtoList, LocalDateTime nextShiftTime) {
        if (dtoList.isEmpty()) {
            return LocalDateTime.of(nextShiftTime.toLocalDate(), LocalTime.MAX);
        }
        LocalTime localTime = ObjectUtil.isNull(dtoList.get(index).getStartTime()) ? LocalTime.MAX
                : LocalTime.parse(dtoList.get(index).getStartTime(), HHmm);
        return LocalDateTime.of(nextShiftTime.toLocalDate(), localTime);
        //过滤当前人员的下一班次信息
        PerformanceShiftMapDto shiftMapDto = dtoList.stream().filter(f -> StringUtils.equals(f.getPersonCode(), personCode) && f.getWorkTime().isEqual(nextShiftTime)).findFirst().orElse(new PerformanceShiftMapDto());
        if(StringUtils.isEmpty(shiftMapDto.getStartTime())){
            return LocalDateTime.of(nextShiftTime.toLocalDate(), LocalTime.MAX);
        }
        LocalTime nextShiftStartTime = LocalTime.parse(shiftMapDto.getStartTime(), HHmm);
        return LocalDateTime.of(nextShiftTime.toLocalDate(), nextShiftStartTime);
    }
    /**