| | |
| | | package me.zhengjie.modules.quartz.task; |
| | | |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.sun.org.apache.xpath.internal.operations.Bool; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.attendance.domain.Attendance; |
| | | import me.zhengjie.modules.attendance.domain.*; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceBusinessTravelRepository; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceGoOutRepository; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceLeaveRepository; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceRepository; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService; |
| | | import me.zhengjie.modules.attendance.service.impl.AttendanceServiceImpl; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.quartz.service.dto.OapiDingTalkAccessToken; |
| | | import me.zhengjie.modules.quartz.service.OpaiDingTalkService; |
| | |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.Duration; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | |
| | |
| | | * 钉钉定时任务(取考勤记录) |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | @Async |
| | | public class OapiDingTalkMethod { |
| | |
| | | private final OpaiDingTalkService opaiDingTalkService; |
| | | |
| | | private final SysRosteringRepository sysRosteringRepository; |
| | | |
| | | private final AttendanceGoOutRepository attendanceGoOutRepository; |
| | | |
| | | private final AttendanceBusinessTravelRepository attendanceBusinessTravelRepository; |
| | | |
| | | private final AttendanceLeaveRepository attendanceLeaveRepository; |
| | | |
| | | private final AttendanceServiceImpl attendanceService; |
| | | |
| | | /** |
| | | * 每天0点执行,新增用户考勤信息 |
| | |
| | | List<User> userList = getUserList(); |
| | | //获取今天时间 |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String todayDate = df.format(new Date()); |
| | | String todayDate = "2023-01-10"; |
| | | //获取昨天时间 |
| | | Calendar calYesterDay = Calendar.getInstance(); |
| | | calYesterDay.add(Calendar.DATE, -1); |
| | |
| | | }else { |
| | | calDay.setTime(Timestamp.valueOf(yesterday + " 00:00:00")); |
| | | } |
| | | // |
| | | String timeResult = ""; |
| | | //遍历在职员工向考勤记录添加数据 |
| | | for (User user : userList) { |
| | | //用户钉钉id |
| | |
| | | for (OapiDingTalkRecordResult oapiDingTalkRecordResult : oapiDingTalkRecordResultList) { |
| | | if ("NotSigned".equals(oapiDingTalkRecordResult.getTimeResult())) { |
| | | oapiDingTalkRecordResult.setUserCheckTime(null); |
| | | timeResult = "NotSigned"; |
| | | } |
| | | if ("OnDuty".equals(oapiDingTalkRecordResult.getCheckType())) { |
| | | //判断状态 |
| | | String result = ObjectUtils.isEmpty(oapiDingTalkRecordResult.getUserCheckTime()) ? Constant.ATTENDANCE_STATE_NOTSIGNED : Constant.ATTENDANCE_STATE_NORMAL; |
| | | attendance.setStartClockResults(result); |
| | | attendance.setStartClockResults(ObjectUtils.isEmpty(oapiDingTalkRecordResult.getUserCheckTime()) ? oapiDingTalkRecordResult.getTimeResult() : checkResult(user.getSysRostering(), oapiDingTalkRecordResult.getUserCheckTime(), 1)); |
| | | attendance.setStartLocationResults(setLocationResults(oapiDingTalkRecordResult.getLocationResult())); |
| | | attendance.setActualWorkStartTime(oapiDingTalkRecordResult.getUserCheckTime()); |
| | | }else if("OffDuty".equals(oapiDingTalkRecordResult.getCheckType())){ |
| | | //判断状态 |
| | | String result = ObjectUtils.isEmpty(oapiDingTalkRecordResult.getUserCheckTime()) ? Constant.ATTENDANCE_STATE_NOTSIGNED : Constant.ATTENDANCE_STATE_NORMAL; |
| | | attendance.setEndClockResults(result); |
| | | attendance.setEndClockResults(ObjectUtils.isEmpty(oapiDingTalkRecordResult.getUserCheckTime()) ? oapiDingTalkRecordResult.getTimeResult() : checkResult(user.getSysRostering(), oapiDingTalkRecordResult.getUserCheckTime(), 2)); |
| | | attendance.setEndLocationResults(setLocationResults(oapiDingTalkRecordResult.getLocationResult())); |
| | | attendance.setActualWorkEndTime(oapiDingTalkRecordResult.getUserCheckTime()); |
| | | } |
| | | } |
| | | //如果是周六或者周日或节假日,打卡状态为等待的话,就改状态为休息(oapiDingTalkRecordResultList为空的情况下) |
| | | // 如果是周六或者周日或节假日,打卡状态为等待的话,就改状态为休息(oapiDingTalkRecordResultList为空的情况下) |
| | | if(calDay.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || calDay.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY || attendance.getDateType().equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS)){ |
| | | //上午 |
| | | // 上午 |
| | | if(day && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_WAITFOR)){ |
| | | attendance.setStartClockResults(Constant.ATTENDANCE_STATE_REST); |
| | | attendance.setStartLocationResults(Constant.LOCATION_RESULTS_REST); |
| | |
| | | } |
| | | } |
| | | |
| | | // 判断修改考勤状态 |
| | | if(day){ |
| | | attendance = updateRecord(user,attendance,todayDate); |
| | | }else{ |
| | | attendance = updateRecord(user,attendance,yesterday); |
| | | } |
| | | // 修改考勤打卡状态 |
| | | attendance = updateRecord(user, attendance, timeResult, day); |
| | | attendanceRepository.save(attendance); |
| | | } |
| | | }else{ |
| | | throw new BadRequestException(oapiDingTalkAccessToken.getErrmsg()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 查询其他状态表并调用方法修改 |
| | | * @param user |
| | | */ |
| | | public Attendance updateRecord(User user, Attendance attendance, String timeResult, Boolean day){ |
| | | // 查询考勤请假表 |
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.findByUserIdAndDate(user.getId(),attendance.getRosteringWorkStartTime().toLocalDateTime(),attendance.getRosteringWorkEndTime().toLocalDateTime()); |
| | | Double leaveDuration = new Double(0.00); |
| | | //请假优先于出差优先于外出 |
| | | if(attendanceLeaveList.size() < 1){ |
| | | // 查询考勤出差表 |
| | | AttendanceBusinessTravel attendanceBusinessTravel = attendanceBusinessTravelRepository.findByUserAndDate(user.getId(),attendance.getAttendanceDate().toString().substring(0,10),attendance.getAttendanceDate().toString().substring(0,10)); |
| | | Double bussDuration = new Double(0.00); |
| | | if(ObjectUtils.isEmpty(attendanceBusinessTravel)){ |
| | | // 查询考勤外出表 |
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(user.getId(),attendance.getRosteringWorkStartTime().toLocalDateTime(),attendance.getRosteringWorkEndTime().toLocalDateTime()); |
| | | Double outDuration = new Double(0.00); |
| | | for(AttendanceGoOut attendanceGoOut : attendanceGoOutList){ |
| | | // 上午or下午总时长 |
| | | outDuration += getDuration(attendanceGoOut.getOutStartTime(),attendanceGoOut.getOutEndTime(),attendance,day); |
| | | updateAttendanceRecord(outDuration,attendance,timeResult,Constant.ATTENDANCE_STATE_GOOUT, day); |
| | | } |
| | | }else{ |
| | | if(ObjectUtils.isNotEmpty(attendanceBusinessTravel)){ |
| | | // 上午or下午总时长 |
| | | bussDuration += getDuration(attendanceBusinessTravel.getTravelStartTime(),attendance.getRosteringWorkEndTime(),attendance,day); |
| | | updateAttendanceRecord(bussDuration,attendance,timeResult,Constant.ATTENDANCE_STATE_BUSINESS,day); |
| | | } |
| | | } |
| | | }else{ |
| | | for(AttendanceLeave attendanceLeave : attendanceLeaveList){ |
| | | // 上午or下午总时长 |
| | | leaveDuration += getDuration(attendanceLeave.getLeaveStartTime(),attendanceLeave.getLeaveEndTime(),attendance,day); |
| | | //调用方法修改考勤打卡状态 |
| | | updateAttendanceRecord(leaveDuration,attendance,timeResult,Constant.ATTENDANCE_STATE_LEAVE,day); |
| | | } |
| | | } |
| | | return attendance; |
| | | } |
| | | |
| | | /** |
| | | * 获取上午/下午时长(请假、出差、外出) |
| | | * @param startDate |
| | | * @param endDate |
| | | * @param attendance |
| | | * @return |
| | | */ |
| | | public Double getDuration(Timestamp startDate, Timestamp endDate, Attendance attendance,Boolean day){ |
| | | Double duration = new Double(0.00); |
| | | // 上午总时长 |
| | | if(day){ |
| | | if(startDate.toLocalDateTime().isBefore(attendance.getRosteringLunchStartTime().toLocalDateTime()) && endDate.toLocalDateTime().compareTo(attendance.getRosteringLunchStartTime().toLocalDateTime()) <= 0){ |
| | | Integer leaveSTime = calculateTimes(startDate.toLocalDateTime(), endDate.toLocalDateTime()); |
| | | duration += Double.valueOf(new BigDecimal((float) leaveSTime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | } |
| | | if(startDate.toLocalDateTime().compareTo(attendance.getRosteringLunchStartTime().toLocalDateTime()) <= 0 && endDate.toLocalDateTime().compareTo(attendance.getRosteringLunchEndTime().toLocalDateTime()) >= 0 || |
| | | startDate.toLocalDateTime().compareTo(attendance.getRosteringLunchStartTime().toLocalDateTime()) <= 0 && endDate.toLocalDateTime().compareTo(attendance.getRosteringLunchStartTime().toLocalDateTime()) > 0 && endDate.toLocalDateTime().compareTo(attendance.getRosteringLunchEndTime().toLocalDateTime()) <= 0){ |
| | | Integer leaveSTime = calculateTimes(startDate.toLocalDateTime(), attendance.getRosteringLunchStartTime().toLocalDateTime()); |
| | | duration += Double.valueOf(new BigDecimal((float) leaveSTime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | } |
| | | }else{ |
| | | // 下午总时长 |
| | | if(startDate.toLocalDateTime().compareTo(attendance.getRosteringLunchEndTime().toLocalDateTime()) >= 0 && endDate.toLocalDateTime().compareTo(attendance.getRosteringWorkEndTime().toLocalDateTime()) <= 0){ |
| | | Integer leaveETime = calculateTimes(startDate.toLocalDateTime(), endDate.toLocalDateTime()); |
| | | duration += Double.valueOf(new BigDecimal((float) leaveETime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | } |
| | | if(startDate.toLocalDateTime().isBefore(attendance.getRosteringLunchEndTime().toLocalDateTime()) && endDate.toLocalDateTime().compareTo(attendance.getRosteringLunchEndTime().toLocalDateTime()) >= 0){ |
| | | Integer leaveETime = calculateTimes(attendance.getRosteringLunchEndTime().toLocalDateTime(), endDate.toLocalDateTime()); |
| | | duration += Double.valueOf(new BigDecimal((float) leaveETime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | } |
| | | } |
| | | return duration; |
| | | } |
| | | |
| | | /** |
| | | * 修改考勤记录打卡状态 |
| | | * @param attendance |
| | | * @param timeResult |
| | | * @param type |
| | | */ |
| | | public void updateAttendanceRecord(Double duration, Attendance attendance, String timeResult, String type, Boolean day){ |
| | | // 上午排班开始时间到排班午休开始时间的时长 |
| | | Integer workMorningTime = calculateTimes(attendance.getRosteringWorkStartTime().toLocalDateTime(), attendance.getRosteringLunchStartTime().toLocalDateTime()); |
| | | Double result = Double.valueOf(new BigDecimal((float) workMorningTime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | // 上午实际打卡时间到排班午休开始时间的时长 |
| | | Double result1 = new Double(0.00); |
| | | if(ObjectUtils.isNotEmpty(attendance.getActualWorkStartTime()) && attendance.getActualWorkStartTime().compareTo(attendance.getRosteringLunchStartTime()) <= 0){ |
| | | Integer actualTime = calculateTimes(attendance.getActualWorkStartTime().toLocalDateTime(), attendance.getRosteringLunchStartTime().toLocalDateTime()); |
| | | result1 = Double.valueOf(new BigDecimal((float) actualTime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | } |
| | | // 下午休排班结束时间到下午排班结束时间的时长 |
| | | Integer workNoonTime = calculateTimes(attendance.getRosteringLunchEndTime().toLocalDateTime(), attendance.getRosteringWorkEndTime().toLocalDateTime()); |
| | | Double noonResult = Double.valueOf(new BigDecimal((float) workNoonTime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | // 下午排班午休结束时间到下班打卡的实际时间的时长 |
| | | Double noonResult1 = new Double(0.00); |
| | | if(ObjectUtils.isNotEmpty(attendance.getActualWorkEndTime()) && attendance.getActualWorkEndTime().compareTo(attendance.getRosteringLunchEndTime()) >= 0){ |
| | | Integer actualTime = calculateTimes(attendance.getRosteringLunchEndTime().toLocalDateTime(), attendance.getActualWorkEndTime().toLocalDateTime()); |
| | | noonResult1 = Double.valueOf(new BigDecimal((float) actualTime / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | } |
| | | |
| | | // 没打卡的情况下,修改考勤打卡状态 |
| | | if(timeResult.equals("NotSigned")){ |
| | | // 上午 |
| | | if(day){ |
| | | // 上午时长 >= 上午排班时间 改打卡状态 否则 缺时 |
| | | if(duration >= result){ |
| | | attendance.setStartClockResults(type); |
| | | attendance.setStartLocationResults(null); |
| | | }else{ |
| | | attendance.setStartClockResults(Constant.ATTENDANCE_STATE_LACK_OF_TIME); |
| | | } |
| | | }else{ |
| | | // 下午时长 >= 下午排班时间 改打卡状态 否则 缺时 |
| | | if(duration >= noonResult){ |
| | | attendance.setEndClockResults(type); |
| | | attendance.setEndLocationResults(null); |
| | | }else{ |
| | | attendance.setEndClockResults(Constant.ATTENDANCE_STATE_LACK_OF_TIME); |
| | | } |
| | | } |
| | | }else{ |
| | | //打卡的情况,修改考勤状态 |
| | | if(day){ |
| | | if((duration + result1) >= result){ |
| | | attendance.setStartClockResults(type); |
| | | attendance.setStartLocationResults(null); |
| | | }else{ |
| | | attendance.setStartClockResults(Constant.ATTENDANCE_STATE_LACK_OF_TIME); |
| | | } |
| | | }else{ |
| | | if((duration + noonResult1) >= noonResult){ |
| | | attendance.setEndClockResults(type); |
| | | attendance.setEndLocationResults(null); |
| | | }else{ |
| | | attendance.setEndClockResults(Constant.ATTENDANCE_STATE_LACK_OF_TIME); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计算时间差 |
| | | * @param start |
| | | * @param end |
| | | * @return |
| | | */ |
| | | public Integer calculateTimes(LocalDateTime start, LocalDateTime end){ |
| | | Integer time = 0; |
| | | Duration duration = null; |
| | | // if(ObjectUtils.isNotEmpty(date) && date.equals("endDays")){ |
| | | // duration = Duration.between(end.toLocalDate().atTime(start.toLocalTime()), end); |
| | | // }else { |
| | | duration = Duration.between(start, start.toLocalDate().atTime(end.toLocalTime())); |
| | | // } |
| | | Long StartSeconds = duration.getSeconds(); |
| | | time += StartSeconds.intValue(); |
| | | return time; |
| | | } |
| | | |
| | | /** |
| | | * 考勤状态 |
| | | * @param sysRostering |
| | | * @param timestamp |
| | | * @param type |
| | | * @return |
| | | */ |
| | | public String checkResult(SysRostering sysRostering, Timestamp timestamp, Integer type) { |
| | | String result = ObjectUtil.isEmpty(timestamp) ? Constant.ATTENDANCE_STATE_NORMAL : Constant.ATTENDANCE_STATE_NOTSIGNED; |
| | | if (type.equals(1)) { |
| | | if (timestamp.toLocalDateTime().toLocalTime().isAfter(sysRostering.getWorkStartTime())) { |
| | | result = Constant.ATTENDANCE_START_LATE; |
| | | } |
| | | } else { |
| | | if (timestamp.toLocalDateTime().toLocalTime().isBefore(sysRostering.getWorkEndTime())) { |
| | | result = Constant.ATTENDANCE_START_EARLY; |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | |
| | | public List<User> getUserList(){ |
| | | List<User> userList = userRepository.findAllByEnabled(true); |
| | | return userList; |
| | | } |
| | | |
| | | /** |
| | | * 判断修改考勤状态 |
| | | * @param user |
| | | * @param date |
| | | */ |
| | | public Attendance updateRecord(User user, Attendance attendance, String date){ |
| | | //根据日期查询请假表 |
| | | |
| | | //根据日期查询外出表 |
| | | |
| | | //根据日期查询出差表 |
| | | return attendance; |
| | | } |
| | | |
| | | } |