| | |
| | | */ |
| | | package me.zhengjie.modules.attendance.service.impl; |
| | | |
| | | import me.zhengjie.modules.attendance.domain.Attendance; |
| | | import me.zhengjie.exception.BadRequestException; |
| | | 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.service.dto.*; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.quartz.service.impl.DingTalkServiceImpl; |
| | | import me.zhengjie.modules.quartz.task.OapiDingTalkMethod; |
| | | import me.zhengjie.modules.system.domain.*; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.utils.ValidationUtil; |
| | | import me.zhengjie.utils.FileUtil; |
| | | import me.zhengjie.modules.system.service.dto.SysConfigHolidayQueryCriteria; |
| | | import me.zhengjie.utils.*; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceRepository; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService; |
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceDto; |
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceQueryCriteria; |
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceMapper; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import me.zhengjie.utils.PageUtil; |
| | | import me.zhengjie.utils.QueryHelp; |
| | | |
| | | import java.sql.Timestamp; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | private final AttendanceRepository attendanceRepository; |
| | | private final AttendanceMapper attendanceMapper; |
| | | private final UserRepository userRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository; |
| | | private final AttendanceLeaveRepository attendanceLeaveRepository; |
| | | private final AttendanceBusinessTravelRepository attendanceBusinessTravelRepository; |
| | | private final AttendanceGoOutRepository attendanceGoOutRepository; |
| | | private final DingTalkServiceImpl dingTalkService; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(AttendanceQueryCriteria criteria, Pageable pageable){ |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AttendanceDto create(Attendance resources) { |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| | | DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss"); |
| | | // 查询用户 |
| | | User user = userRepository.findById(resources.getUserId()).orElseGet(User::new); |
| | | resources.setRosteringLunchStartTime(Timestamp.valueOf(df.format(resources.getAttendanceDate()) + " " + dateTimeFormatter.format(user.getSysRostering().getLunchStartTime()))); |
| | | resources.setRosteringLunchEndTime(Timestamp.valueOf(df.format(resources.getAttendanceDate()) + " " + dateTimeFormatter.format(user.getSysRostering().getLunchEndTime()))); |
| | | return attendanceMapper.toDto(attendanceRepository.save(resources)); |
| | | } |
| | | |
| | |
| | | Attendance attendance = attendanceRepository.findById(resources.getId()).orElseGet(Attendance::new); |
| | | ValidationUtil.isNull( attendance.getId(),"Attendance","id",resources.getId()); |
| | | attendance.copy(resources); |
| | | //获取当前登入人信息 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Role role = user.getRoles().iterator().next(); |
| | | //除超管、总经理只能修改自己信息 |
| | | if(role.getId() != Constant.ROLE_LEVEL_SUPER && role.getId() != Constant.ROLE_LEVEL_GENERAL){ |
| | | if (!Long.valueOf(resources.getUserId()).equals(SecurityUtils.getCurrentUserId())){ |
| | | throw new BadRequestException("只能修改自己的记录"); |
| | | } |
| | | } |
| | | |
| | | attendanceRepository.save(attendance); |
| | | } |
| | | |
| | | /** |
| | | * 计算时间(小时) |
| | | * @param userId |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Integer calculateTimes(Calculate calculate, Long userId) { |
| | | User userInfo = userRepository.getOne(userId); |
| | | SysRostering sysRostering = userInfo.getSysRostering(); |
| | | //计算天数 |
| | | Integer count = calculateDays(calculate); |
| | | Calculate startCalculate = new Calculate(); |
| | | startCalculate.setStart(calculate.getStart()); |
| | | startCalculate.setEnd(calculate.getStart()); |
| | | Integer startDays = calculateDays(startCalculate); |
| | | |
| | | Calculate endCalculate = new Calculate(); |
| | | endCalculate.setStart(calculate.getEnd()); |
| | | endCalculate.setEnd(calculate.getEnd()); |
| | | Integer endDays = calculateDays(endCalculate); |
| | | |
| | | Integer time = 0; |
| | | time = (count - startDays - endDays) * 8 * 3600; |
| | | calculate.setStart(formateDateTime(calculate.getStart(), sysRostering)); |
| | | calculate.setEnd(formateDateTime(calculate.getEnd(), sysRostering)); |
| | | |
| | | if (startDays > 0) { |
| | | Duration startDuration = Duration.between(calculate.getStart(), calculate.getStart().toLocalDate().atTime(sysRostering.getWorkEndTime())); |
| | | Long StartSeconds = startDuration.getSeconds(); |
| | | if (calculate.getStart().toLocalTime().isBefore(LocalTime.NOON)) { |
| | | time += StartSeconds.intValue() - 3600; |
| | | }else if(calculate.getStart().toLocalTime().compareTo(LocalTime.NOON) >= 0 && calculate.getStart().toLocalTime().compareTo(calculate.getStart().toLocalDate().atTime(sysRostering.getLunchEndTime()).toLocalTime()) < 0){ |
| | | //补充,请假开始时间大于等于中午基准休息时间小于休息结束时间 |
| | | time += StartSeconds.intValue() - 3600; |
| | | } else { |
| | | time += StartSeconds.intValue(); |
| | | } |
| | | } |
| | | if (endDays > 0) { |
| | | Duration endDuration = Duration.between(calculate.getEnd().toLocalDate().atTime(sysRostering.getWorkStartTime()), calculate.getEnd()); |
| | | Long endSeconds = endDuration.getSeconds(); |
| | | if (calculate.getEnd().toLocalTime().isAfter(LocalTime.NOON)) { |
| | | time += endSeconds.intValue() - 3600; |
| | | } else { |
| | | time += endSeconds.intValue(); |
| | | } |
| | | } |
| | | return time; |
| | | } |
| | | |
| | | /** |
| | | * 计算时间(天) |
| | | * @param calculate |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Integer calculateDays(Calculate calculate) { |
| | | List<String> holiday = new ArrayList<>(), isWork = new ArrayList<>(); |
| | | //查询节假日表 |
| | | List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll(); |
| | | for(SysConfigHoliday sysConfigHoliday : sysConfigHolidayList){ |
| | | if(sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_WEEKDAY)){ |
| | | //工作日 |
| | | isWork.add(sysConfigHoliday.getHolidayDate().toString().substring(0,10)); |
| | | }else{ |
| | | holiday.add(sysConfigHoliday.getHolidayDate().toString().substring(0,10)); |
| | | } |
| | | } |
| | | Long chronoUnit = ChronoUnit.DAYS.between(calculate.getStart().toLocalDate(), calculate.getEnd().toLocalDate()); |
| | | Integer count = 0; |
| | | for (Integer i = 0; i <= chronoUnit; i++) { |
| | | LocalDate date = calculate.getStart().toLocalDate().plusDays(i); |
| | | Boolean isWeekend = date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY; |
| | | if ((!isWeekend && !holiday.contains(date.toString())) || (isWeekend && isWork.contains(date.toString()))) { |
| | | count++; |
| | | } |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | public LocalDateTime formateDateTime(LocalDateTime localDateTime, SysRostering sysRostering) { |
| | | if (localDateTime.toLocalTime().isBefore(sysRostering.getWorkStartTime())) { |
| | | return localDateTime.toLocalDate().atTime(sysRostering.getWorkStartTime()); |
| | | } else if (localDateTime.toLocalTime().isAfter(sysRostering.getLunchStartTime()) && localDateTime.toLocalTime().isBefore(sysRostering.getLunchEndTime())) { |
| | | return localDateTime.toLocalDate().atTime(sysRostering.getLunchEndTime()); |
| | | } else if (localDateTime.toLocalTime().isAfter(sysRostering.getWorkEndTime())) { |
| | | return localDateTime.toLocalDate().atTime(sysRostering.getWorkEndTime()); |
| | | } |
| | | return localDateTime; |
| | | } |
| | | |
| | | @Override |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | |
| | | //获取当前登入人信息 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Role role = user.getRoles().iterator().next(); |
| | | //根据id查询记录 |
| | | Optional<Attendance> attendance = attendanceRepository.findById(id); |
| | | //除超管、总经理只能删除自己信息 |
| | | if(role.getId() != Constant.ROLE_LEVEL_SUPER && role.getId() != Constant.ROLE_LEVEL_GENERAL){ |
| | | if (!Long.valueOf(attendance.get().getUserId()).equals(SecurityUtils.getCurrentUserId())){ |
| | | throw new BadRequestException("只能删除自己的记录"); |
| | | } |
| | | } |
| | | |
| | | attendanceRepository.deleteById(id); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 补卡判断登录人 |
| | | */ |
| | | @Override |
| | | public void getUserMakeUpCard(String userId) { |
| | | //获取当前登入人信息 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Role role = user.getRoles().iterator().next(); |
| | | //除超管、总经理只能补自己的卡 |
| | | if(role.getId() != Constant.ROLE_LEVEL_SUPER && role.getId() != Constant.ROLE_LEVEL_GENERAL){ |
| | | if (!Long.valueOf(userId).equals(SecurityUtils.getCurrentUserId())){ |
| | | throw new BadRequestException("只能补自己的卡"); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 补充考勤信息 |
| | | * @param attendance |
| | | */ |
| | | @Override |
| | | public void addAttendanceInfo(Attendance attendance) throws Exception { |
| | | // 获取今天日期 |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String todayDate = sdf.format(new Date()); |
| | | if(attendance.getAttendanceDate().compareTo(Timestamp.valueOf(todayDate)) >= 0){ |
| | | throw new BadRequestException("只能更新小于等于今天的考勤信息"); |
| | | } |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String attendanceDate = df.format(attendance.getAttendanceDate()); |
| | | List<User> userList = new ArrayList<>(); |
| | | if(ObjectUtils.isNotEmpty(attendance.getUserId())){ |
| | | User user = userRepository.findById(attendance.getUserId()).orElseGet(User::new); |
| | | userList.add(user); |
| | | } |
| | | // 调用0时定时任务 |
| | | dingTalkService.getDingTalkAttendanceList(attendance.getAttendanceDate(), userList); |
| | | // 选择日期和员工更新员工的考勤信息,未选择员工则更新所有人 |
| | | dingTalkService.getDingTalkAttendanceListOfNoonOrNight(attendanceDate ,userList); |
| | | } |
| | | |
| | | @Override |
| | | public void download(List<AttendanceDto> all, HttpServletResponse response) throws IOException { |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (AttendanceDto attendance : all) { |
| | | String startLocationResults = attendance.getStartLocationResults() != null ? dictDetailRepository.findByDictNameAndValue("location_results", attendance.getStartLocationResults()).getLabel() : ""; |
| | | String startClockResults = attendance.getStartClockResults() != null ? dictDetailRepository.findByDictNameAndValue("attendance_state", attendance.getStartClockResults()).getLabel() : ""; |
| | | String endLocationResults = attendance.getEndLocationResults() != null ? dictDetailRepository.findByDictNameAndValue("location_results", attendance.getEndLocationResults()).getLabel() : ""; |
| | | String endClockResults = attendance.getEndClockResults() != null ? dictDetailRepository.findByDictNameAndValue("attendance_state", attendance.getEndClockResults()).getLabel() : ""; |
| | | String dateType = attendance.getDateType() != null ? dictDetailRepository.findByDictNameAndValue("date_type", attendance.getDateType()).getLabel() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("员工", attendance.getUserName()); |
| | | map.put("考勤日期", attendance.getAttendanceDate()); |
| | | map.put("排班工作开始时间", attendance.getRosteringWorkStartTime()); |
| | | map.put("实际工作开始时间", attendance.getActualWorkStartTime()); |
| | | //开始位置判断 |
| | | if(ObjectUtils.isNotEmpty(attendance.getStartLocationResults()) && attendance.getStartLocationResults().equals(Constant.LOCATION_RESULTS_NORMAL)) { |
| | | map.put("开始位置结果","范围内"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getStartLocationResults()) && attendance.getStartLocationResults().equals(Constant.LOCATION_RESULTS_OUTSIDE)) { |
| | | map.put("开始位置结果","范围外"); |
| | | } else{ |
| | | map.put("开始位置结果","未打卡"); |
| | | } |
| | | //开始打卡判断 |
| | | if(ObjectUtils.isNotEmpty(attendance.getStartClockResults()) && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_WAITFOR)) { |
| | | map.put("开始打卡结果","等待"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getStartClockResults()) && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_NORMAL)){ |
| | | map.put("开始打卡结果","正常"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getStartClockResults()) && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_NOTSIGNED)){ |
| | | map.put("开始打卡结果","缺卡"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getStartClockResults()) && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_GOOUT)){ |
| | | map.put("开始打卡结果","外出"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getStartClockResults()) && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_BUSINESS)){ |
| | | map.put("开始打卡结果","出差"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getStartClockResults()) && attendance.getStartClockResults().equals(Constant.ATTENDANCE_STATE_LEAVE)){ |
| | | map.put("开始打卡结果","请假"); |
| | | } |
| | | map.put("开始位置结果",startLocationResults); |
| | | map.put("开始打卡结果",startClockResults); |
| | | map.put("排班工作结束时间", attendance.getRosteringWorkEndTime()); |
| | | map.put("实际工作结束时间", attendance.getActualWorkEndTime()); |
| | | //结束位置判断 |
| | | if(ObjectUtils.isNotEmpty(attendance.getEndLocationResults()) && attendance.getEndLocationResults().equals(Constant.LOCATION_RESULTS_NORMAL)) { |
| | | map.put("结束位置结果","范围内"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getEndLocationResults()) && attendance.getEndLocationResults().equals(Constant.LOCATION_RESULTS_OUTSIDE)) { |
| | | map.put("结束位置结果","范围外"); |
| | | } else{ |
| | | map.put("结束位置结果","未打卡"); |
| | | } |
| | | //结束打卡判断 |
| | | if(ObjectUtils.isNotEmpty(attendance.getEndClockResults()) && attendance.getEndClockResults().equals(Constant.ATTENDANCE_STATE_WAITFOR)) { |
| | | map.put("结束打卡结果","等待"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getEndClockResults()) && attendance.getEndClockResults().equals(Constant.ATTENDANCE_STATE_NORMAL)){ |
| | | map.put("结束打卡结果","正常"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getEndClockResults()) && attendance.getEndClockResults().equals(Constant.ATTENDANCE_STATE_NOTSIGNED)){ |
| | | map.put("结束打卡结果","缺卡"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getEndClockResults()) && attendance.getEndClockResults().equals(Constant.ATTENDANCE_STATE_GOOUT)){ |
| | | map.put("结束打卡结果","外出"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getEndClockResults()) && attendance.getEndClockResults().equals(Constant.ATTENDANCE_STATE_BUSINESS)){ |
| | | map.put("结束打卡结果","出差"); |
| | | } else if(ObjectUtils.isNotEmpty(attendance.getEndClockResults()) && attendance.getEndClockResults().equals(Constant.ATTENDANCE_STATE_LEAVE)){ |
| | | map.put("结束打卡结果","请假"); |
| | | } |
| | | //日期类型判断 |
| | | if(attendance.getDateType().equals(Constant.DATE_TYPE_WEEKDAY)){ |
| | | map.put("日期类型","工作日"); |
| | | } else if(attendance.getDateType().equals(Constant.DATE_TYPE_HOLIDAYS)){ |
| | | map.put("日期类型","节假日(串休)"); |
| | | } else{ |
| | | map.put("日期类型","节假日(法定)"); |
| | | } |
| | | map.put("结束位置结果",endLocationResults); |
| | | map.put("结束打卡结果",endClockResults); |
| | | map.put("日期类型",dateType); |
| | | map.put("备注", attendance.getRemarks()); |
| | | map.put("创建者", attendance.getCreateBy()); |
| | | map.put("创建时间", attendance.getCreateTime()); |
| | |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | @Override |
| | | public Double queryAttendanceHours(AttendanceQueryCriteria criteria){ |
| | | // 出勤小时数 |
| | | Double attendanceHours = 0D; |
| | | |
| | | // 国际时间转中国时间 |
| | | String targetYearMonth = DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth()); |
| | | |
| | | // 节日检索类 |
| | | SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria(); |
| | | sysConfigHolidayQueryCriteria.setHolidayDateLike(targetYearMonth.substring(0,7)); |
| | | // 获取本月节日列表 |
| | | List<SysConfigHoliday> sysConfigHolidayMonthList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder)); |
| | | |
| | | // 考勤检索类 |
| | | criteria.setAttendanceDateLike(targetYearMonth.substring(0,7)); |
| | | // 获取本月考勤列表 |
| | | List<Attendance> attendanceMonthList = attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)); |
| | | |
| | | // 请假检索类 |
| | | AttendanceLeaveQueryCriteria attendanceLeaveQueryCriteria = new AttendanceLeaveQueryCriteria(); |
| | | attendanceLeaveQueryCriteria.setUserId(criteria.getUserId()); |
| | | attendanceLeaveQueryCriteria.setLeaveType(Constant.LEAVE_TYPE_ANNUAL_LEAVE); |
| | | attendanceLeaveQueryCriteria.setLeaveStartTimeLike(targetYearMonth.substring(0,7)); |
| | | attendanceLeaveQueryCriteria.setLeaveEndTimeLike(targetYearMonth.substring(0,7)); |
| | | attendanceLeaveQueryCriteria.setApprovalStatus(Constant.APPROVAL_STATUS_PASSED); |
| | | // 获取本月请假列表 |
| | | List<AttendanceLeave> attendanceLeaveMonthList = attendanceLeaveRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,attendanceLeaveQueryCriteria,criteriaBuilder)); |
| | | |
| | | // 出差检索类 |
| | | AttendanceBusinessTravelQueryCriteria attendanceBusinessTravelQueryCriteria = new AttendanceBusinessTravelQueryCriteria(); |
| | | attendanceBusinessTravelQueryCriteria.setUserId(criteria.getUserId()); |
| | | attendanceBusinessTravelQueryCriteria.setTravelStartTimeLike(targetYearMonth.substring(0,7)); |
| | | attendanceBusinessTravelQueryCriteria.setTravelEndTimeLike(targetYearMonth.substring(0,7)); |
| | | attendanceBusinessTravelQueryCriteria.setApprovalStatus(Constant.BUSSINESS_APPROVE_STATUS_AGREE); |
| | | // 获取本月出差列表 |
| | | List<AttendanceBusinessTravel> attendanceBusinessTravelMonthList = attendanceBusinessTravelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,attendanceBusinessTravelQueryCriteria,criteriaBuilder)); |
| | | |
| | | // 外出检索类 |
| | | AttendanceGoOutQueryCriteria attendanceGoOutQueryCriteria = new AttendanceGoOutQueryCriteria(); |
| | | attendanceGoOutQueryCriteria.setUserId(criteria.getUserId()); |
| | | attendanceGoOutQueryCriteria.setOutStartTimeLike(targetYearMonth.substring(0,7)); |
| | | attendanceGoOutQueryCriteria.setOutEndTimeLike(targetYearMonth.substring(0,7)); |
| | | attendanceGoOutQueryCriteria.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_AGREE); |
| | | // 获取本月外出列表 |
| | | List<AttendanceGoOut> attendanceGoOutMonthList = attendanceGoOutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,attendanceGoOutQueryCriteria,criteriaBuilder)); |
| | | |
| | | // 日历类 |
| | | Calendar cal = setUpCalendar(targetYearMonth); |
| | | // 获取本月最大天数 |
| | | int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH); |
| | | // 循环本月最大天数 |
| | | for (int i = 1; i <= lastDay; i ++) { |
| | | // 设置天数 |
| | | cal.set(Calendar.DAY_OF_MONTH, i); |
| | | // 本日星期几 |
| | | int week = cal.get(Calendar.DAY_OF_WEEK); |
| | | // 设置本日初时间 |
| | | Calendar startCal = DateUtil.setDayStartTime(cal); |
| | | // 设置本日末时间 |
| | | Calendar endCal = DateUtil.setDayEndTime(cal); |
| | | // 出勤毫秒数 |
| | | Long attendanceTimes = 0L; |
| | | |
| | | // 获取本日节日列表 |
| | | List<SysConfigHoliday> sysConfigHolidayDayList = sysConfigHolidayMonthList.stream().filter(item -> item.getHolidayDate().getTime() == startCal.getTime().getTime()).collect(Collectors.toList()); |
| | | // 判断本日节日列表数量 |
| | | if (sysConfigHolidayDayList.size() > 1) { |
| | | throw new BadRequestException("相同日期存在多条节日记录!"); |
| | | } |
| | | // 判断本日是否为周末 |
| | | if (week == Calendar.SUNDAY || week == Calendar.SATURDAY) { |
| | | // 判断本日节日列表数量 |
| | | if (sysConfigHolidayDayList.size() == 1) { |
| | | // 本日节日 |
| | | SysConfigHoliday sysConfigHolidayDay = sysConfigHolidayDayList.get(0); |
| | | // 判断本日节日日期类型是否为工作日 |
| | | if (!Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDay.getDateType())) { |
| | | // 跳过本次循环 |
| | | continue; |
| | | } |
| | | } else { |
| | | // 跳过本次循环 |
| | | continue; |
| | | } |
| | | } else { |
| | | // 判断本日节日列表数量 |
| | | if (sysConfigHolidayDayList.size() == 1) { |
| | | // 本日节日 |
| | | SysConfigHoliday sysConfigHolidayDay = sysConfigHolidayDayList.get(0); |
| | | // 判断本日节日日期类型是否为工作日 |
| | | if (!Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDay.getDateType())) { |
| | | // 跳过本次循环 |
| | | continue; |
| | | } |
| | | } |
| | | } |
| | | |
| | | // 获取本日考勤列表 |
| | | List<Attendance> attendanceDayList = attendanceMonthList.stream().filter(item -> item.getAttendanceDate().getTime() == startCal.getTime().getTime()).collect(Collectors.toList()); |
| | | // 本日考勤 |
| | | Attendance attendanceDay = null; |
| | | // 判断本日考勤列表数量 |
| | | if (attendanceDayList.size() > 1) { |
| | | throw new BadRequestException("相同日期存在多条考勤记录!"); |
| | | } else if (attendanceDayList.size() == 1) { |
| | | // 本日考勤 |
| | | attendanceDay = attendanceDayList.get(0); |
| | | // 判断本日考勤排班信息是否完整 |
| | | if (attendanceDay.getRosteringWorkStartTime() == null || "".equals(attendanceDay.getRosteringWorkStartTime()) || |
| | | attendanceDay.getRosteringLunchStartTime() == null || "".equals(attendanceDay.getRosteringLunchStartTime()) || |
| | | attendanceDay.getRosteringLunchEndTime() == null || "".equals(attendanceDay.getRosteringLunchEndTime()) || |
| | | attendanceDay.getRosteringWorkEndTime() == null || "".equals(attendanceDay.getRosteringWorkEndTime())) { |
| | | throw new BadRequestException("考勤记录排班信息缺失!"); |
| | | } |
| | | // 判断本日考勤是否有实际工作开始时间、实际工作结束时间 |
| | | if (attendanceDay.getActualWorkStartTime() != null && !"".equals(attendanceDay.getActualWorkStartTime()) && attendanceDay.getActualWorkEndTime() != null && !"".equals(attendanceDay.getActualWorkEndTime())) { |
| | | // 获取有效时长 |
| | | attendanceTimes += getValidDuration(attendanceDay, attendanceDay.getActualWorkStartTime(), attendanceDay.getActualWorkEndTime()); |
| | | } |
| | | } else { |
| | | // 跳过本次循环 |
| | | continue; |
| | | } |
| | | |
| | | // 获取本日请假列表 |
| | | List<AttendanceLeave> attendanceLeaveDayList = attendanceLeaveMonthList.stream().filter(item -> |
| | | item.getLeaveStartTime().getTime() >= startCal.getTime().getTime() && item.getLeaveStartTime().getTime() <= endCal.getTime().getTime() && |
| | | item.getLeaveEndTime().getTime() >= startCal.getTime().getTime() && item.getLeaveEndTime().getTime() <= endCal.getTime().getTime() |
| | | ).collect(Collectors.toList()); |
| | | // 循环本日请假列表 |
| | | for (int j = 0; j < attendanceLeaveDayList.size(); j ++) { |
| | | // 本日请假 |
| | | AttendanceLeave attendanceLeaveDay = attendanceLeaveDayList.get(j); |
| | | // 获取有效时长 |
| | | attendanceTimes += getValidDuration(attendanceDay, attendanceLeaveDay.getLeaveStartTime(), attendanceLeaveDay.getLeaveEndTime()); |
| | | } |
| | | |
| | | // 获取本日出差列表 |
| | | List<AttendanceBusinessTravel> attendanceBusinessTravelDayList = attendanceBusinessTravelMonthList.stream().filter(item -> |
| | | item.getTravelStartTime().getTime() == startCal.getTime().getTime() && item.getTravelEndTime().getTime() == startCal.getTime().getTime() |
| | | ).collect(Collectors.toList()); |
| | | // 循环本日出差列表 |
| | | for (int j = 0; j < attendanceBusinessTravelDayList.size(); j ++) { |
| | | // 本日出差 |
| | | AttendanceBusinessTravel attendanceBusinessTravelDay = attendanceBusinessTravelDayList.get(j); |
| | | // 获取有效时长 |
| | | attendanceTimes += getValidDuration(attendanceDay, attendanceBusinessTravelDay.getTravelStartTime(), attendanceBusinessTravelDay.getTravelEndTime()); |
| | | } |
| | | |
| | | // 获取本日外出列表 |
| | | List<AttendanceGoOut> attendanceGoOutDayList = attendanceGoOutMonthList.stream().filter(item -> |
| | | item.getOutStartTime().getTime() >= startCal.getTime().getTime() && item.getOutStartTime().getTime() <= endCal.getTime().getTime() && |
| | | item.getOutEndTime().getTime() >= startCal.getTime().getTime() && item.getOutEndTime().getTime() <= endCal.getTime().getTime() |
| | | ).collect(Collectors.toList()); |
| | | // 循环本日外出列表 |
| | | for (int j = 0; j < attendanceGoOutDayList.size(); j ++) { |
| | | // 本日外出 |
| | | AttendanceGoOut attendanceGoOutDay = attendanceGoOutDayList.get(j); |
| | | // 获取有效时长 |
| | | attendanceTimes += getValidDuration(attendanceDay, attendanceGoOutDay.getOutStartTime(), attendanceGoOutDay.getOutEndTime()); |
| | | } |
| | | // 出勤小时数 |
| | | attendanceHours += BigDecimal.valueOf(attendanceTimes).divide(BigDecimal.valueOf(60 * 60 * 1000), 2, RoundingMode.HALF_UP).doubleValue(); |
| | | } |
| | | // 出勤小时数 |
| | | return attendanceHours; |
| | | } |
| | | |
| | | /** |
| | | * 设置日历类 |
| | | * @return |
| | | */ |
| | | private Calendar setUpCalendar(String targetYearMonth) { |
| | | // 日历类 |
| | | Calendar cal = Calendar.getInstance(); |
| | | // 设置年份 |
| | | cal.set(Calendar.YEAR, Integer.valueOf(targetYearMonth.substring(0,4))); |
| | | // 设置月份 |
| | | cal.set(Calendar.MONTH, Integer.valueOf(targetYearMonth.substring(5,7)) - 1); |
| | | // 日历类 |
| | | return cal; |
| | | } |
| | | |
| | | /** |
| | | * 获取有效时长 |
| | | * @param attendanceDay |
| | | * @param startTime |
| | | * @param endTime |
| | | * @return |
| | | */ |
| | | private Long getValidDuration(Attendance attendanceDay, Timestamp startTime, Timestamp endTime) { |
| | | // 判断开始时间与结束时间 |
| | | if (endTime.getTime() < startTime.getTime()) { |
| | | throw new BadRequestException("结束时间应大于开始时间!"); |
| | | } |
| | | // 判断开始时间 |
| | | if (startTime.getTime() < attendanceDay.getRosteringWorkStartTime().getTime()) { |
| | | // 判断结束时间 |
| | | if (endTime.getTime() < attendanceDay.getRosteringWorkStartTime().getTime()) { |
| | | // 返回有效时长 = 0 |
| | | return 0L; |
| | | } else if (endTime.getTime() < attendanceDay.getRosteringLunchStartTime().getTime()) { |
| | | // 返回有效时长 = 结束时间 - 排班工作开始时间 |
| | | return endTime.getTime() - attendanceDay.getRosteringWorkStartTime().getTime(); |
| | | } else if (endTime.getTime() < attendanceDay.getRosteringLunchEndTime().getTime()) { |
| | | // 返回有效时长 = 排班午休开始时间 - 排班工作开始时间 |
| | | return attendanceDay.getRosteringLunchStartTime().getTime() - attendanceDay.getRosteringWorkStartTime().getTime(); |
| | | } else if (endTime.getTime() < attendanceDay.getRosteringWorkEndTime().getTime()) { |
| | | // 返回有效时长 = (排班午休开始时间 - 排班工作开始时间) + (结束时间 - 排班午休结束时间) |
| | | return (attendanceDay.getRosteringLunchStartTime().getTime() - attendanceDay.getRosteringWorkStartTime().getTime()) + (endTime.getTime() - attendanceDay.getRosteringLunchEndTime().getTime()); |
| | | } else { |
| | | // 返回有效时长 = (排班午休开始时间 - 排班工作开始时间) + (排班工作结束时间 - 排班午休结束时间) |
| | | return (attendanceDay.getRosteringLunchStartTime().getTime() - attendanceDay.getRosteringWorkStartTime().getTime()) + (attendanceDay.getRosteringWorkEndTime().getTime() - attendanceDay.getRosteringLunchEndTime().getTime()); |
| | | } |
| | | } else if (startTime.getTime() < attendanceDay.getRosteringLunchStartTime().getTime()) { |
| | | // 判断结束时间 |
| | | if (endTime.getTime() < attendanceDay.getRosteringLunchStartTime().getTime()) { |
| | | // 返回有效时长 = 结束时间 - 开始时间 |
| | | return endTime.getTime() - startTime.getTime(); |
| | | } else if (endTime.getTime() < attendanceDay.getRosteringLunchEndTime().getTime()) { |
| | | // 返回有效时长 = 排班午休开始时间 - 开始时间 |
| | | return attendanceDay.getRosteringLunchStartTime().getTime() - startTime.getTime(); |
| | | } else if (endTime.getTime() < attendanceDay.getRosteringWorkEndTime().getTime()) { |
| | | // 返回有效时长 = (排班午休开始时间 - 开始时间) + (结束时间 - 排班午休结束时间) |
| | | return (attendanceDay.getRosteringLunchStartTime().getTime() - startTime.getTime()) + (endTime.getTime() - attendanceDay.getRosteringLunchEndTime().getTime()); |
| | | } else { |
| | | // 返回有效时长 = (排班午休开始时间 - 开始时间) + (排班工作结束时间 - 排班午休结束时间) |
| | | return (attendanceDay.getRosteringLunchStartTime().getTime() - startTime.getTime()) + (attendanceDay.getRosteringWorkEndTime().getTime() - attendanceDay.getRosteringLunchEndTime().getTime()); |
| | | } |
| | | } else if (startTime.getTime() < attendanceDay.getRosteringLunchEndTime().getTime()) { |
| | | // 判断结束时间 |
| | | if (endTime.getTime() < attendanceDay.getRosteringLunchEndTime().getTime()) { |
| | | // 返回有效时长 = 0 |
| | | return 0L; |
| | | } else if (endTime.getTime() < attendanceDay.getRosteringWorkEndTime().getTime()) { |
| | | // 返回有效时长 = 结束时间 - 排班午休结束时间 |
| | | return endTime.getTime() - attendanceDay.getRosteringLunchEndTime().getTime(); |
| | | } else { |
| | | // 返回有效时长 = 排班工作结束时间 - 排班午休结束时间 |
| | | return attendanceDay.getRosteringWorkEndTime().getTime() - attendanceDay.getRosteringLunchEndTime().getTime(); |
| | | } |
| | | } else if (startTime.getTime() < attendanceDay.getRosteringWorkEndTime().getTime()) { |
| | | // 判断结束时间 |
| | | if (endTime.getTime() < attendanceDay.getRosteringWorkEndTime().getTime()) { |
| | | // 返回有效时长 = 结束时间 - 开始时间 |
| | | return endTime.getTime() - startTime.getTime(); |
| | | } else { |
| | | // 返回有效时长 = 排班工作结束时间 - 开始时间 |
| | | return attendanceDay.getRosteringWorkEndTime().getTime() - startTime.getTime(); |
| | | } |
| | | } else { |
| | | // 返回有效时长 = 0 |
| | | return 0L; |
| | | } |
| | | } |
| | | } |