首先,在JPA中需要创建一个实体类来表示员工的请假记录,包含员工ID、请假开始时间、请假结束时间、请假类型等属性。同时,需要创建一个对应的Repository接口来实现对数据库中请假记录的增删改查操作。

其次,需要在后端编写一个工具类来实现计算员工请假时间的功能。该工具类需要接收员工ID和请假时间段作为参数,并返回该员工在该时间段内实际请假的天数。

以下是一个简单的工具类示例:

public class LeaveUtils {
    
    /**
     * 计算员工请假天数
     * @param employeeId 员工ID
     * @param startDate 请假开始时间
     * @param endDate 请假结束时间
     * @return 员工请假天数
     */
    public static int calculateLeaveDays(Long employeeId, Date startDate, Date endDate) {
        // 获取请假时间段内的所有日期
        List<Date> dates = getDatesBetween(startDate, endDate);
        // 排除周六日
        List<Date> workDays = excludeWeekend(dates);
        // 获取请假记录
        List<LeaveRecord> records = leaveRecordRepository.findByEmployeeIdAndDateBetween(employeeId, startDate, endDate);
        // 计算实际请假天数
        int leaveDays = 0;
        for (Date date : workDays) {
            boolean isLeaveDay = false;
            for (LeaveRecord record : records) {
                if (record.getDate().equals(date)) {
                    isLeaveDay = true;
                    break;
                }
            }
            if (isLeaveDay) {
                leaveDays++;
            }
        }
        return leaveDays;
    }
    
    /**
     * 获取两个日期之间的所有日期
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @return 所有日期列表
     */
    private static List<Date> getDatesBetween(Date startDate, Date endDate) {
        List<Date> dates = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startDate);
        while (calendar.getTime().before(endDate)) {
            dates.add(calendar.getTime());
            calendar.add(Calendar.DATE, 1);
        }
        dates.add(endDate);
        return dates;
    }
    
    /**
     * 排除周六日
     * @param dates 所有日期列表
     * @return 工作日列表
     */
    private static List<Date> excludeWeekend(List<Date> dates) {
        List<Date> workDays = new ArrayList<>();
        for (Date date : dates) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek != Calendar.SATURDAY && dayOfWeek != Calendar.SUNDAY) {
                workDays.add(date);
            }
        }
        return workDays;
    }
    
}

最后,在Thymeleaf中,可以通过调用该工具类来显示员工的请假天数。例如:

<td th:text="${T(com.example.LeaveUtils).calculateLeaveDays(employee.id, leave.startDate, leave.endDate)}"></td>
``
公司请假系统休假维护中维护的当月周期为上月16日到当月15日并且自动略过周六日写一个工具类完成前端用thymeleaf后端用spring boot+JPA

原文地址: https://www.cveoy.top/t/topic/cRiP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录