package com.cwk.web.servlet;
import com.cwk.pojo.Attendance;
import com.cwk.pojo.Staff;
import com.cwk.service.AttendanceService;
import com.cwk.service.impl.AttendanceServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.List;
@WebServlet("/attendance/*")
public class AttendanceServlet extends BaseServlet {
AttendanceService attendanceService = new AttendanceServiceImpl();
public void addtowork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
// 获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将当前日期和时间格式化为字符串
String formattedNow = now.format(formatter);
// 假设有一个特定的日期和时间字符串
String specificDateTime = "2024-12-25 8:00:00";
boolean flag = false;
try {
// 解析字符串为LocalDateTime对象
LocalDateTime specificDateTimeObj = LocalDateTime.parse(specificDateTime, formatter);
// 比较当前时间和特定时间
if (now.isAfter(specificDateTimeObj)) {
System.out.println("当前时间晚于特定时间");
//打卡失败
flag = false;
} else if (now.isBefore(specificDateTimeObj)) {
System.out.println("当前时间早于特定时间");
//打卡成功!
flag = true;
} else {
System.out.println("当前时间与特定时间相同");
flag = true;
}
} catch (DateTimeParseException e) {
System.out.println("日期时间格式错误: " + e.getMessage());
}
HttpSession session = req.getSession();
Staff staff = (Staff) session.getAttribute("staff");
String jobid = staff.getJobid();
String name = staff.getName();
String sex = staff.getSex();
String birthday = staff.getBirthday();
String atype = "上班";
Attendance attendance = new Attendance();
attendance.setAtime(formattedNow);
attendance.setJobid(jobid);
attendance.setName(name);
attendance.setSex(sex);
attendance.setBirthday(birthday);
attendance.setAtype(atype);
attendanceService.addAttendance(attendance);
if (flag) {
//打开成功
req.setAttribute("Attendance_msg", "打卡成功!");
}
else {
req.setAttribute("Attendance_msg", "打卡失败!");
}
//返回员工页面
req.getRequestDispatcher("attendance.jsp").forward(req, resp);‘
}
public void addoutwork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
// 获取当前日期和时间
LocalDateTime now = LocalDateTime.now();
// 定义日期时间格式
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 将当前日期和时间格式化为字符串
String formattedNow = now.format(formatter);
// 假设有一个特定的日期和时间字符串
String specificDateTime = "2024-12-25 17:00:00";
boolean flag;
try {
// 解析字符串为LocalDateTime对象
LocalDateTime specificDateTimeObj = LocalDateTime.parse(specificDateTime, formatter);
// 比较当前时间和特定时间
if (now.isAfter(specificDateTimeObj)) {
System.out.println("当前时间晚于特定时间");
//打卡失败
flag = true;
} else if (now.isBefore(specificDateTimeObj)) {
System.out.println("当前时间早于特定时间");
//打卡成功!
flag = false;
} else {
System.out.println("当前时间与特定时间相同");
flag = true;
}
} catch (DateTimeParseException e) {
System.out.println("日期时间格式错误: " + e.getMessage());
flag = false;
}
HttpSession session = req.getSession();
Staff staff = (Staff) session.getAttribute("staff");
String jobid = staff.getJobid();
String name = staff.getName();
String sex = staff.getSex();
String birthday = staff.getBirthday();
String atype = "下班";
Attendance attendance = new Attendance();
attendance.setAtime(formattedNow);
attendance.setJobid(jobid);
attendance.setName(name);
attendance.setSex(sex);
attendance.setBirthday(birthday);
attendance.setAtype(atype);
attendanceService.addAttendance(attendance);
if (flag) {
//打开成功
req.setAttribute("Attendance_msg", "打卡成功!");
}
else {
req.setAttribute("Attendance_msg", "打卡失败!");
}
//返回员工页面
req.getRequestDispatcher("attendance.jsp").forward(req, resp);‘
}
public void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
HttpSession session = req.getSession();
Staff staff = (Staff) session.getAttribute("staff");
String jobid = staff.getJobid();
List<Attendance> attendances = attendanceService.selectByJobid(jobid);
req.setAttribute("attendances", attendances);
req.getRequestDispatcher("selectAttendance.jsp").forward(req, resp);(这里可以根据session中的staff提取其属性department进行跳转)
}
}
标签:逻辑,attendance,String,req,考勤,LocalDateTime,import,打卡,staff From: https://www.cnblogs.com/K-wc2024/p/18631667