| | |
| | | /* |
| | | * Copyright 2019-2020 Zheng Jie |
| | | * |
| | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | | * you may not use this file except in compliance with the License. |
| | | * You may obtain a copy of the License at |
| | | * |
| | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | | * |
| | | * Unless required by applicable law or agreed to in writing, software |
| | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | * See the License for the specific language governing permissions and |
| | | * limitations under the License. |
| | | */ |
| | | package me.zhengjie.modules.attendance.service.impl; |
| | | |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.attendance.domain.*; |
| | | import me.zhengjie.modules.attendance.repository.*; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService; |
| | | import me.zhengjie.modules.attendance.service.dto.*; |
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceMapper; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.quartz.service.impl.DingTalkServiceImpl; |
| | | import me.zhengjie.modules.system.domain.Role; |
| | | import me.zhengjie.modules.system.domain.SysConfigHoliday; |
| | | import me.zhengjie.modules.system.domain.SysRostering; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.modules.system.service.dto.SysConfigHolidayQueryCriteria; |
| | | import me.zhengjie.utils.*; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | 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.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | import java.util.stream.Stream; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | | * @description 服务实现 |
| | | * @author wcd |
| | | * @date 2022-12-14 |
| | | **/ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class AttendanceServiceImpl implements AttendanceService { |
| | | |
| | | 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 AttendanceOvertimeRepository attendanceOvertimeRepository; |
| | | private final DingTalkServiceImpl dingTalkService; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(AttendanceQueryCriteria criteria, Pageable pageable){ |
| | | Page<Attendance> page = attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | //将实体转换为Dto(重写) |
| | | Function<Attendance, AttendanceDto> func = new Function<Attendance, AttendanceDto>(){ |
| | | |
| | | @Override |
| | | public AttendanceDto apply(Attendance attendance) { |
| | | // 将实体类转换成DTO |
| | | AttendanceDto attendanceDto = attendanceMapper.toDto(attendance); |
| | | //查询用户信息 |
| | | User user = userRepository.findById(attendanceDto.getUserId()).orElseGet(User::new); |
| | | attendanceDto.setUserName(user.getNickName()); |
| | | return attendanceDto; |
| | | } |
| | | }; |
| | | return PageUtil.toPage(page.map(func)); |
| | | } |
| | | |
| | | @Override |
| | | public List<AttendanceDto> queryAll(AttendanceQueryCriteria criteria){ |
| | | return attendanceMapper.toDto(attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public AttendanceDto findById(Long id) { |
| | | Attendance attendance = attendanceRepository.findById(id).orElseGet(Attendance::new); |
| | | ValidationUtil.isNull(attendance.getId(),"Attendance","id",id); |
| | | return attendanceMapper.toDto(attendance); |
| | | } |
| | | |
| | | @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)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(Attendance 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()); |
| | | map.put("开始位置结果",startLocationResults); |
| | | map.put("开始打卡结果",startClockResults); |
| | | map.put("排班工作结束时间", attendance.getRosteringWorkEndTime()); |
| | | map.put("实际工作结束时间", attendance.getActualWorkEndTime()); |
| | | map.put("结束位置结果",endLocationResults); |
| | | map.put("结束打卡结果",endClockResults); |
| | | map.put("日期类型",dateType); |
| | | map.put("备注", attendance.getRemarks()); |
| | | map.put("创建者", attendance.getCreateBy()); |
| | | map.put("创建时间", attendance.getCreateTime()); |
| | | map.put("更新者", attendance.getUpdateBy()); |
| | | map.put("更新时间", attendance.getUpdateTime()); |
| | | list.add(map); |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | @Override |
| | | public Double queryAttendanceHours(Long userId, String date) { |
| | | Attendance attendanceDate = attendanceRepository.findAllByUserIdAndAttendanceDate(userId, date); |
| | | List<Calculate> resultParts = new ArrayList<>(); |
| | | |
| | | LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd")); |
| | | LocalDateTime startOfDate = localDate.atTime(0, 0, 0); |
| | | LocalDateTime endOfDate = localDate.atTime(23, 59, 59); |
| | | |
| | | LocalDateTime actualWorkStartTime = attendanceDate.getActualWorkStartTime() == null ? null : attendanceDate.getActualWorkStartTime().toLocalDateTime(); |
| | | LocalDateTime actualWorkEndTime = attendanceDate.getActualWorkEndTime() == null ? null : attendanceDate.getActualWorkEndTime().toLocalDateTime(); |
| | | LocalDateTime rosteringWorkStartTime = attendanceDate.getRosteringWorkStartTime() == null ? null : attendanceDate.getRosteringWorkStartTime().toLocalDateTime(); |
| | | LocalDateTime rosteringLunchStartTime = attendanceDate.getRosteringLunchStartTime() == null ? null : attendanceDate.getRosteringLunchStartTime().toLocalDateTime(); |
| | | LocalDateTime rosteringLunchEndTime = attendanceDate.getRosteringLunchEndTime() == null ? null : attendanceDate.getRosteringLunchEndTime().toLocalDateTime(); |
| | | LocalDateTime rosteringWorkEndTime = attendanceDate.getRosteringWorkEndTime() == null ? null : attendanceDate.getRosteringWorkEndTime().toLocalDateTime(); |
| | | |
| | | // 出差信息(出差时无打卡信息,用出差信息补充) |
| | | AttendanceBusinessTravel attendanceBusinessTravel = attendanceBusinessTravelRepository.findByUserAndDate(userId, startOfDate, endOfDate); |
| | | if (attendanceBusinessTravel != null) { |
| | | actualWorkStartTime = rosteringWorkStartTime; |
| | | actualWorkEndTime = rosteringWorkEndTime; |
| | | } |
| | | |
| | | if (actualWorkStartTime != null && actualWorkStartTime.isBefore(rosteringWorkStartTime)) { |
| | | actualWorkStartTime = rosteringWorkStartTime; |
| | | } |
| | | // if (actualWorkEndTime != null && actualWorkEndTime.isAfter(rosteringWorkEndTime)) { |
| | | // actualWorkEndTime = rosteringWorkEndTime; |
| | | // } |
| | | |
| | | List<Calculate> attendanceParts = new ArrayList<>(); |
| | | // 当日打卡信息不全不算 |
| | | if (actualWorkStartTime != null && actualWorkEndTime != null) { |
| | | attendanceParts.add(new Calculate(actualWorkStartTime, actualWorkEndTime)); |
| | | } |
| | | |
| | | // 休息时间管理 |
| | | List<Calculate> breakParts = new ArrayList<>(); |
| | | breakParts.add(new Calculate(rosteringLunchStartTime, rosteringLunchEndTime)); |
| | | |
| | | // 外出信息 |
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(userId, startOfDate, endOfDate); |
| | | attendanceParts.addAll( |
| | | attendanceGoOutList.stream().map(attendanceGoOut -> |
| | | new Calculate(attendanceGoOut.getOutStartTime().toLocalDateTime(), attendanceGoOut.getOutEndTime().toLocalDateTime()) |
| | | ).collect(Collectors.toList()) |
| | | ); |
| | | // 周末加班 |
| | | List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(userId, startOfDate, endOfDate); |
| | | attendanceParts.addAll( |
| | | attendanceOvertimeList.stream().map(attendanceOvertime -> |
| | | new Calculate(attendanceOvertime.getOvertimeStartTime().toLocalDateTime(), attendanceOvertime.getOvertimeEndTime().toLocalDateTime()) |
| | | ).collect(Collectors.toList()) |
| | | ); |
| | | attendanceParts.stream().sorted().reduce(new Stack<Calculate>(), (result, current) -> { |
| | | // 删除重复时间,合并相连的时间段 |
| | | if (result.empty()) { |
| | | result.push(current); |
| | | return result; |
| | | } |
| | | Calculate last = result.peek(); |
| | | if (last.getEnd().isBefore(current.getStart())) { |
| | | result.push(current); |
| | | } else if (last.getEnd().isBefore(current.getEnd())) { |
| | | last.setEnd(current.getEnd()); |
| | | } |
| | | return result; |
| | | }, (o1, o2) -> null).forEach(calculate -> { |
| | | // 去除休息时间:午休等 |
| | | for (Calculate breakPart : breakParts) { |
| | | if ((calculate.getStart().isBefore(breakPart.getStart()) && calculate.getEnd().isAfter(breakPart.getStart())) |
| | | || (calculate.getStart().isBefore(breakPart.getEnd()) && calculate.getEnd().isAfter(breakPart.getEnd()))) { |
| | | Calculate calculate1 = new Calculate(calculate.getStart(), breakPart.getStart()); |
| | | Calculate calculate2 = new Calculate(breakPart.getEnd(), calculate.getEnd()); |
| | | if (calculate1.getStart().isBefore(calculate1.getEnd())) { |
| | | resultParts.add(calculate1); |
| | | } |
| | | if (calculate2.getStart().isBefore(calculate2.getEnd())) { |
| | | resultParts.add(calculate2); |
| | | } |
| | | } else { |
| | | resultParts.add(calculate); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 请假信息 |
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.findByUserIdAndDate(userId, startOfDate, endOfDate); |
| | | // // 加班信息 |
| | | // List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(userId, startOfDate, endOfDate); |
| | | |
| | | // 出勤时间累计 |
| | | BigDecimal result = resultParts.stream() |
| | | .map(calculate -> ChronoUnit.SECONDS.between(calculate.getStart(), calculate.getEnd())) |
| | | .reduce(new BigDecimal(0), |
| | | (total, current) -> total.add(BigDecimal.valueOf(current)), |
| | | (o1, o2) -> null).divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP |
| | | ); |
| | | BigDecimal annualLeaveHours = attendanceLeaveList.stream() |
| | | .filter(attendanceLeave -> Constant.LEAVE_TYPE_ANNUAL_LEAVE.equals(attendanceLeave.getLeaveType()) |
| | | || Constant.LEAVE_TYPE_MARRIAGE_HOLIDAY.equals(attendanceLeave.getLeaveType()) |
| | | || Constant.LEAVE_TYPE_BEREAVEMENT_LEAVE.equals(attendanceLeave.getLeaveType()) |
| | | // || Constant.LEAVE_TYPE_REPLACE.equals(attendanceLeave.getLeaveType()) |
| | | ).map(attendanceLeave -> BigDecimal.valueOf(attendanceLeave.getLeaveDuration())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO); |
| | | return result.add(annualLeaveHours).doubleValue(); |
| | | } |
| | | |
| | | @Override |
| | | public Double queryAttendanceHours(AttendanceQueryCriteria criteria) { |
| | | // 节日检索类 |
| | | SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria(); |
| | | sysConfigHolidayQueryCriteria.setHolidayDateLike(criteria.getTargetYearMonth().substring(0,7)); |
| | | // 获取本月节日列表 |
| | | List<SysConfigHoliday> sysConfigHolidayMonthList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder)); |
| | | |
| | | LocalDate startDate = LocalDate.parse(criteria.getTargetYearMonth(), DateTimeFormatter.ofPattern("yyyy-MM-dd")); |
| | | LocalDate endDate = startDate.plusMonths(1).minusDays(1); |
| | | |
| | | Long distance = ChronoUnit.DAYS.between(startDate, endDate); |
| | | |
| | | return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).filter(localDate -> { |
| | | for (SysConfigHoliday sysConfigHoliday : sysConfigHolidayMonthList) { |
| | | if (sysConfigHoliday.getHolidayDate().toLocalDateTime().isEqual(localDate.atStartOfDay())) { |
| | | return Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHoliday.getDateType()); |
| | | } |
| | | } |
| | | return localDate.getDayOfWeek() != DayOfWeek.SATURDAY && localDate.getDayOfWeek() != DayOfWeek.SUNDAY; |
| | | }).map(localDate -> { |
| | | System.out.print("localDate : " + localDate); |
| | | LocalDateTime startOfDate = localDate.atTime(0, 0, 0); |
| | | LocalDateTime endOfDate = localDate.atTime(23, 59, 59); |
| | | AttendanceBusinessTravel attendanceBusinessTravel = attendanceBusinessTravelRepository.findByUserAndDate(criteria.getUserId(), startOfDate, endOfDate); |
| | | if (attendanceBusinessTravel != null) { |
| | | System.out.println("\t hours1 : " + 8); |
| | | return BigDecimal.valueOf(8.0); |
| | | } |
| | | Attendance attendanceDate = attendanceRepository.findAllByUserIdAndAttendanceDate(criteria.getUserId(), localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))); |
| | | if (attendanceDate == null) { |
| | | System.out.println("\t hours2 : " + 0); |
| | | return BigDecimal.valueOf(0.0); |
| | | } |
| | | LocalDateTime actualWorkStartTime = attendanceDate.getActualWorkStartTime() == null ? null : attendanceDate.getActualWorkStartTime().toLocalDateTime(); |
| | | LocalDateTime actualWorkEndTime = attendanceDate.getActualWorkEndTime() == null ? null : attendanceDate.getActualWorkEndTime().toLocalDateTime(); |
| | | |
| | | LocalDateTime rosteringWorkStartTime = attendanceDate.getRosteringWorkStartTime() == null ? null : attendanceDate.getRosteringWorkStartTime().toLocalDateTime(); |
| | | LocalDateTime rosteringLunchStartTime = attendanceDate.getRosteringLunchStartTime() == null ? null : attendanceDate.getRosteringLunchStartTime().toLocalDateTime(); |
| | | LocalDateTime rosteringLunchEndTime = attendanceDate.getRosteringLunchEndTime() == null ? null : attendanceDate.getRosteringLunchEndTime().toLocalDateTime(); |
| | | LocalDateTime rosteringWorkEndTime = attendanceDate.getRosteringWorkEndTime() == null ? null : attendanceDate.getRosteringWorkEndTime().toLocalDateTime(); |
| | | if (actualWorkStartTime != null && actualWorkStartTime.isBefore(rosteringWorkStartTime)) { |
| | | actualWorkStartTime = rosteringWorkStartTime; |
| | | } |
| | | if (actualWorkEndTime != null && actualWorkEndTime.isAfter(rosteringWorkEndTime)) { |
| | | actualWorkEndTime = rosteringWorkEndTime; |
| | | } |
| | | List<Calculate> attendanceParts = new ArrayList<>(); |
| | | // 当日打卡信息不全不算 |
| | | if (actualWorkStartTime != null && actualWorkEndTime != null) { |
| | | attendanceParts.add(new Calculate(actualWorkStartTime, actualWorkEndTime)); |
| | | } |
| | | |
| | | // 休息时间管理 |
| | | List<Calculate> breakParts = new ArrayList<>(); |
| | | breakParts.add(new Calculate(rosteringLunchStartTime, rosteringLunchEndTime)); |
| | | |
| | | // 外出信息 |
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(criteria.getUserId(), startOfDate, endOfDate); |
| | | attendanceParts.addAll( |
| | | attendanceGoOutList.stream().map(attendanceGoOut -> { |
| | | LocalDateTime attendanceGoOutStart = attendanceGoOut.getOutStartTime().toLocalDateTime(); |
| | | LocalDateTime attendanceGoOutEnd = attendanceGoOut.getOutEndTime().toLocalDateTime(); |
| | | if (attendanceGoOutStart.isBefore(rosteringWorkStartTime)) { |
| | | attendanceGoOutStart = rosteringWorkStartTime; |
| | | } |
| | | if (attendanceGoOutEnd.isAfter(rosteringWorkEndTime)) { |
| | | attendanceGoOutEnd = rosteringWorkEndTime; |
| | | } |
| | | return new Calculate(attendanceGoOutStart, attendanceGoOutEnd); |
| | | }).filter(calculate -> calculate.getStart().isBefore(calculate.getEnd())).collect(Collectors.toList()) |
| | | ); |
| | | |
| | | List<Calculate> resultParts = new ArrayList<>(); |
| | | |
| | | attendanceParts.stream().sorted().reduce(new Stack<Calculate>(), (result, current) -> { |
| | | // 删除重复时间,合并相连的时间段 |
| | | if (result.empty()) { |
| | | result.push(current); |
| | | return result; |
| | | } |
| | | Calculate last = result.peek(); |
| | | if (last.getEnd().isBefore(current.getStart())) { |
| | | result.push(current); |
| | | } else if (last.getEnd().isBefore(current.getEnd())) { |
| | | last.setEnd(current.getEnd()); |
| | | } |
| | | return result; |
| | | }, (o1, o2) -> null).forEach(calculate -> { |
| | | // 去除休息时间:午休等 |
| | | for (Calculate breakPart : breakParts) { |
| | | if ((calculate.getStart().isBefore(breakPart.getStart()) && calculate.getEnd().isAfter(breakPart.getStart())) |
| | | || (calculate.getStart().isBefore(breakPart.getEnd()) && calculate.getEnd().isAfter(breakPart.getEnd()))) { |
| | | Calculate calculate1 = new Calculate(calculate.getStart(), breakPart.getStart()); |
| | | Calculate calculate2 = new Calculate(breakPart.getEnd(), calculate.getEnd()); |
| | | if (calculate1.getStart().isBefore(calculate1.getEnd())) { |
| | | resultParts.add(calculate1); |
| | | } |
| | | if (calculate2.getStart().isBefore(calculate2.getEnd())) { |
| | | resultParts.add(calculate2); |
| | | } |
| | | } else { |
| | | resultParts.add(calculate); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 出勤时间累计 |
| | | BigDecimal result = resultParts.stream() |
| | | .map(calculate -> ChronoUnit.SECONDS.between(calculate.getStart(), calculate.getEnd())) |
| | | .reduce(new BigDecimal(0), |
| | | (total, current) -> total.add(BigDecimal.valueOf(current)), |
| | | (o1, o2) -> null).divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP |
| | | ); |
| | | |
| | | // 请假信息 |
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.findByUserIdAndDate(criteria.getUserId(), startOfDate, endOfDate); |
| | | // 年假不扣工时 |
| | | BigDecimal annualLeaveHours = attendanceLeaveList.stream() |
| | | .filter(attendanceLeave -> Constant.LEAVE_TYPE_ANNUAL_LEAVE.equals(attendanceLeave.getLeaveType()) |
| | | || Constant.LEAVE_TYPE_MARRIAGE_HOLIDAY.equals(attendanceLeave.getLeaveType()) |
| | | || Constant.LEAVE_TYPE_BEREAVEMENT_LEAVE.equals(attendanceLeave.getLeaveType()) |
| | | || Constant.LEAVE_TYPE_REPLACE.equals(attendanceLeave.getLeaveType())) |
| | | .map(attendanceLeave -> { |
| | | if (Constant.LEAVE_TIME_TYPE_HOUR.equals(attendanceLeave.getLeaveDurationUnit())) { |
| | | return BigDecimal.valueOf(attendanceLeave.getLeaveDuration()); |
| | | } else { |
| | | return BigDecimal.valueOf(attendanceLeave.getLeaveDuration()).multiply(BigDecimal.valueOf(8)); |
| | | } |
| | | }).reduce(BigDecimal::add).orElse(BigDecimal.ZERO); |
| | | if (result.add(annualLeaveHours).compareTo(BigDecimal.valueOf(8.0)) > 0) { |
| | | System.out.println("\t hours3 : " + 8); |
| | | return BigDecimal.valueOf(8.0); |
| | | } |
| | | System.out.println("\t hours4 : " + result.add(annualLeaveHours)); |
| | | return result.add(annualLeaveHours); |
| | | }).reduce(BigDecimal::add).get().doubleValue(); |
| | | } |
| | | |
| | | /** |
| | | * 设置日历类 |
| | | * @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; |
| | | } |
| | | } |
| | | /*
|
| | | * Copyright 2019-2020 Zheng Jie
|
| | | *
|
| | | * Licensed under the Apache License, Version 2.0 (the "License");
|
| | | * you may not use this file except in compliance with the License.
|
| | | * You may obtain a copy of the License at
|
| | | *
|
| | | * http://www.apache.org/licenses/LICENSE-2.0
|
| | | *
|
| | | * Unless required by applicable law or agreed to in writing, software
|
| | | * distributed under the License is distributed on an "AS IS" BASIS,
|
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| | | * See the License for the specific language governing permissions and
|
| | | * limitations under the License.
|
| | | */
|
| | | package me.zhengjie.modules.attendance.service.impl;
|
| | |
|
| | | import lombok.RequiredArgsConstructor;
|
| | | import me.zhengjie.exception.BadRequestException;
|
| | | import me.zhengjie.modules.attendance.domain.*;
|
| | | import me.zhengjie.modules.attendance.repository.*;
|
| | | import me.zhengjie.modules.attendance.service.AttendanceService;
|
| | | import me.zhengjie.modules.attendance.service.dto.*;
|
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceMapper;
|
| | | import me.zhengjie.modules.constant.Constant;
|
| | | import me.zhengjie.modules.quartz.service.impl.DingTalkServiceImpl;
|
| | | import me.zhengjie.modules.system.domain.Role;
|
| | | import me.zhengjie.modules.system.domain.SysConfigHoliday;
|
| | | import me.zhengjie.modules.system.domain.SysRostering;
|
| | | import me.zhengjie.modules.system.domain.User;
|
| | | import me.zhengjie.modules.system.repository.DictDetailRepository;
|
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository;
|
| | | import me.zhengjie.modules.system.repository.UserRepository;
|
| | | import me.zhengjie.modules.system.service.dto.SysConfigHolidayQueryCriteria;
|
| | | import me.zhengjie.utils.*;
|
| | | import org.apache.commons.lang3.ObjectUtils;
|
| | | import org.springframework.data.domain.Page;
|
| | | import org.springframework.data.domain.Pageable;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import java.io.IOException;
|
| | | import java.math.BigDecimal;
|
| | | import java.math.RoundingMode;
|
| | | 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.util.function.Function;
|
| | | import java.util.stream.Collectors;
|
| | | import java.util.stream.Stream;
|
| | |
|
| | | /**
|
| | | * @website https://eladmin.vip
|
| | | * @description 服务实现
|
| | | * @author wcd
|
| | | * @date 2022-12-14
|
| | | **/
|
| | | @Service
|
| | | @RequiredArgsConstructor
|
| | | public class AttendanceServiceImpl implements AttendanceService {
|
| | |
|
| | | 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 AttendanceOvertimeRepository attendanceOvertimeRepository;
|
| | | private final DingTalkServiceImpl dingTalkService;
|
| | |
|
| | | @Override
|
| | | public Map<String,Object> queryAll(AttendanceQueryCriteria criteria, Pageable pageable){
|
| | | Page<Attendance> page = attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
| | | //将实体转换为Dto(重写)
|
| | | Function<Attendance, AttendanceDto> func = new Function<Attendance, AttendanceDto>(){
|
| | |
|
| | | @Override
|
| | | public AttendanceDto apply(Attendance attendance) {
|
| | | // 将实体类转换成DTO
|
| | | AttendanceDto attendanceDto = attendanceMapper.toDto(attendance);
|
| | | //查询用户信息
|
| | | User user = userRepository.findById(attendanceDto.getUserId()).orElseGet(User::new);
|
| | | attendanceDto.setUserName(user.getNickName());
|
| | | return attendanceDto;
|
| | | }
|
| | | };
|
| | | return PageUtil.toPage(page.map(func));
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<AttendanceDto> queryAll(AttendanceQueryCriteria criteria){
|
| | | return attendanceMapper.toDto(attendanceRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional
|
| | | public AttendanceDto findById(Long id) {
|
| | | Attendance attendance = attendanceRepository.findById(id).orElseGet(Attendance::new);
|
| | | ValidationUtil.isNull(attendance.getId(),"Attendance","id",id);
|
| | | return attendanceMapper.toDto(attendance);
|
| | | }
|
| | |
|
| | | @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));
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public void update(Attendance 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());
|
| | | map.put("开始位置结果",startLocationResults);
|
| | | map.put("开始打卡结果",startClockResults);
|
| | | map.put("排班工作结束时间", attendance.getRosteringWorkEndTime());
|
| | | map.put("实际工作结束时间", attendance.getActualWorkEndTime());
|
| | | map.put("结束位置结果",endLocationResults);
|
| | | map.put("结束打卡结果",endClockResults);
|
| | | map.put("日期类型",dateType);
|
| | | map.put("备注", attendance.getRemarks());
|
| | | map.put("创建者", attendance.getCreateBy());
|
| | | map.put("创建时间", attendance.getCreateTime());
|
| | | map.put("更新者", attendance.getUpdateBy());
|
| | | map.put("更新时间", attendance.getUpdateTime());
|
| | | list.add(map);
|
| | | }
|
| | | FileUtil.downloadExcel(list, response);
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Double queryAttendanceHours(Long userId, String date) {
|
| | | Attendance attendanceDate = attendanceRepository.findAllByUserIdAndAttendanceDate(userId, date);
|
| | | List<Calculate> resultParts = new ArrayList<>();
|
| | |
|
| | | LocalDate localDate = LocalDate.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
| | | LocalDateTime startOfDate = localDate.atTime(0, 0, 0);
|
| | | LocalDateTime endOfDate = localDate.atTime(23, 59, 59);
|
| | |
|
| | | LocalDateTime actualWorkStartTime = attendanceDate.getActualWorkStartTime() == null ? null : attendanceDate.getActualWorkStartTime().toLocalDateTime();
|
| | | LocalDateTime actualWorkEndTime = attendanceDate.getActualWorkEndTime() == null ? null : attendanceDate.getActualWorkEndTime().toLocalDateTime();
|
| | | LocalDateTime rosteringWorkStartTime = attendanceDate.getRosteringWorkStartTime() == null ? null : attendanceDate.getRosteringWorkStartTime().toLocalDateTime();
|
| | | LocalDateTime rosteringLunchStartTime = attendanceDate.getRosteringLunchStartTime() == null ? null : attendanceDate.getRosteringLunchStartTime().toLocalDateTime();
|
| | | LocalDateTime rosteringLunchEndTime = attendanceDate.getRosteringLunchEndTime() == null ? null : attendanceDate.getRosteringLunchEndTime().toLocalDateTime();
|
| | | LocalDateTime rosteringWorkEndTime = attendanceDate.getRosteringWorkEndTime() == null ? null : attendanceDate.getRosteringWorkEndTime().toLocalDateTime();
|
| | |
|
| | | // 出差信息(出差时无打卡信息,用出差信息补充)
|
| | | AttendanceBusinessTravel attendanceBusinessTravel = attendanceBusinessTravelRepository.findByUserAndDate(userId, startOfDate, endOfDate);
|
| | | if (attendanceBusinessTravel != null) {
|
| | | actualWorkStartTime = rosteringWorkStartTime;
|
| | | actualWorkEndTime = rosteringWorkEndTime;
|
| | | }
|
| | |
|
| | | if (actualWorkStartTime != null && actualWorkStartTime.isBefore(rosteringWorkStartTime)) {
|
| | | actualWorkStartTime = rosteringWorkStartTime;
|
| | | }
|
| | | // if (actualWorkEndTime != null && actualWorkEndTime.isAfter(rosteringWorkEndTime)) {
|
| | | // actualWorkEndTime = rosteringWorkEndTime;
|
| | | // }
|
| | |
|
| | | List<Calculate> attendanceParts = new ArrayList<>();
|
| | | // 当日打卡信息不全不算
|
| | | if (actualWorkStartTime != null && actualWorkEndTime != null) {
|
| | | attendanceParts.add(new Calculate(actualWorkStartTime, actualWorkEndTime));
|
| | | }
|
| | |
|
| | | // 休息时间管理
|
| | | List<Calculate> breakParts = new ArrayList<>();
|
| | | breakParts.add(new Calculate(rosteringLunchStartTime, rosteringLunchEndTime));
|
| | |
|
| | | // 外出信息
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(userId, startOfDate, endOfDate);
|
| | | attendanceParts.addAll(
|
| | | attendanceGoOutList.stream().map(attendanceGoOut ->
|
| | | new Calculate(attendanceGoOut.getOutStartTime().toLocalDateTime(), attendanceGoOut.getOutEndTime().toLocalDateTime())
|
| | | ).collect(Collectors.toList())
|
| | | );
|
| | | // 周末加班
|
| | | List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(userId, startOfDate, endOfDate);
|
| | | attendanceParts.addAll(
|
| | | attendanceOvertimeList.stream().map(attendanceOvertime ->
|
| | | new Calculate(attendanceOvertime.getOvertimeStartTime().toLocalDateTime(), attendanceOvertime.getOvertimeEndTime().toLocalDateTime())
|
| | | ).collect(Collectors.toList())
|
| | | );
|
| | | attendanceParts.stream().sorted().reduce(new Stack<Calculate>(), (result, current) -> {
|
| | | // 删除重复时间,合并相连的时间段
|
| | | if (result.empty()) {
|
| | | result.push(current);
|
| | | return result;
|
| | | }
|
| | | Calculate last = result.peek();
|
| | | if (last.getEnd().isBefore(current.getStart())) {
|
| | | result.push(current);
|
| | | } else if (last.getEnd().isBefore(current.getEnd())) {
|
| | | last.setEnd(current.getEnd());
|
| | | }
|
| | | return result;
|
| | | }, (o1, o2) -> null).forEach(calculate -> {
|
| | | // 去除休息时间:午休等
|
| | | for (Calculate breakPart : breakParts) {
|
| | | if ((calculate.getStart().isBefore(breakPart.getStart()) && calculate.getEnd().isAfter(breakPart.getStart()))
|
| | | || (calculate.getStart().isBefore(breakPart.getEnd()) && calculate.getEnd().isAfter(breakPart.getEnd()))) {
|
| | | Calculate calculate1 = new Calculate(calculate.getStart(), breakPart.getStart());
|
| | | Calculate calculate2 = new Calculate(breakPart.getEnd(), calculate.getEnd());
|
| | | if (calculate1.getStart().isBefore(calculate1.getEnd())) {
|
| | | resultParts.add(calculate1);
|
| | | }
|
| | | if (calculate2.getStart().isBefore(calculate2.getEnd())) {
|
| | | resultParts.add(calculate2);
|
| | | }
|
| | | } else {
|
| | | resultParts.add(calculate);
|
| | | }
|
| | | }
|
| | | });
|
| | |
|
| | | // 请假信息
|
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.findByUserIdAndDate(userId, startOfDate, endOfDate);
|
| | | // // 加班信息
|
| | | // List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(userId, startOfDate, endOfDate);
|
| | |
|
| | | // 出勤时间累计
|
| | | BigDecimal result = resultParts.stream()
|
| | | .map(calculate -> ChronoUnit.SECONDS.between(calculate.getStart(), calculate.getEnd()))
|
| | | .reduce(new BigDecimal(0),
|
| | | (total, current) -> total.add(BigDecimal.valueOf(current)),
|
| | | (o1, o2) -> null).divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP
|
| | | );
|
| | | BigDecimal annualLeaveHours = attendanceLeaveList.stream()
|
| | | .filter(attendanceLeave -> Constant.LEAVE_TYPE_ANNUAL_LEAVE.equals(attendanceLeave.getLeaveType())
|
| | | || Constant.LEAVE_TYPE_MARRIAGE_HOLIDAY.equals(attendanceLeave.getLeaveType())
|
| | | || Constant.LEAVE_TYPE_BEREAVEMENT_LEAVE.equals(attendanceLeave.getLeaveType())
|
| | | // || Constant.LEAVE_TYPE_REPLACE.equals(attendanceLeave.getLeaveType())
|
| | | ).map(attendanceLeave -> BigDecimal.valueOf(attendanceLeave.getLeaveDuration())).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
|
| | | return result.add(annualLeaveHours).doubleValue();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Double queryAttendanceHolidaysOverTimeHours(AttendanceQueryCriteria criteria) {
|
| | |
|
| | | LocalDate startDate = LocalDate.parse(criteria.getTargetYearMonth(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
| | | LocalDate endDate = startDate.plusMonths(1).minusDays(1);
|
| | | // 节日检索类
|
| | | SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria();
|
| | | sysConfigHolidayQueryCriteria.setHolidayDateLike(criteria.getTargetYearMonth().substring(0,7));
|
| | | // 获取本月节日列表
|
| | | List<SysConfigHoliday> sysConfigHolidayMonthList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder));
|
| | | if (sysConfigHolidayMonthList.isEmpty()) {
|
| | | return 0.0D;
|
| | | }
|
| | | // 加班信息
|
| | | List<AttendanceOvertime> attendanceOvertimeList = attendanceOvertimeRepository.findByUserIdAndDate(criteria.getUserId(), startDate.atStartOfDay(), endDate.atStartOfDay());
|
| | |
|
| | | return sysConfigHolidayMonthList.stream()
|
| | | .filter(sysConfigHoliday -> Constant.DATE_TYPE_LEGAL_HOLIDAYS.equals(sysConfigHoliday.getDateType()))
|
| | | .mapToDouble(sysConfigHoliday -> {
|
| | | for (AttendanceOvertime attendanceOvertime : attendanceOvertimeList) {
|
| | | if (sysConfigHoliday.getHolidayDate().toLocalDateTime().isEqual(attendanceOvertime.getOvertimeStartTime().toLocalDateTime().truncatedTo(ChronoUnit.DAYS))) {
|
| | | return attendanceOvertime.getOvertimeDuration();
|
| | | }
|
| | | }
|
| | | return 0.0D;
|
| | | }).sum();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Double queryAttendanceOverTimeHours(AttendanceQueryCriteria criteria) {
|
| | | // 节日检索类
|
| | | SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria();
|
| | | sysConfigHolidayQueryCriteria.setHolidayDateLike(criteria.getTargetYearMonth().substring(0,7));
|
| | | // 获取本月节日列表
|
| | | List<SysConfigHoliday> sysConfigHolidayMonthList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder));
|
| | |
|
| | | LocalDate startDate = LocalDate.parse(criteria.getTargetYearMonth(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
| | | LocalDate endDate = startDate.plusMonths(1).minusDays(1);
|
| | |
|
| | | Long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | |
|
| | | return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).filter(localDate -> {
|
| | | for (SysConfigHoliday sysConfigHoliday : sysConfigHolidayMonthList) {
|
| | | if (sysConfigHoliday.getHolidayDate().toLocalDateTime().isEqual(localDate.atStartOfDay())) {
|
| | | return Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHoliday.getDateType());
|
| | | }
|
| | | }
|
| | | return localDate.getDayOfWeek() != DayOfWeek.SATURDAY && localDate.getDayOfWeek() != DayOfWeek.SUNDAY;
|
| | | }).map(localDate -> {
|
| | | LocalDateTime startOfDate = localDate.atTime(0, 0, 0);
|
| | | LocalDateTime endOfDate = localDate.atTime(23, 59, 59);
|
| | | Attendance attendanceDate = attendanceRepository.findAllByUserIdAndAttendanceDate(criteria.getUserId(), localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | if (attendanceDate == null) {
|
| | | return BigDecimal.valueOf(0.0);
|
| | | }
|
| | | LocalDateTime actualWorkStartTime = attendanceDate.getActualWorkStartTime() == null ? null : attendanceDate.getActualWorkStartTime().toLocalDateTime();
|
| | | LocalDateTime actualWorkEndTime = attendanceDate.getActualWorkEndTime() == null ? null : attendanceDate.getActualWorkEndTime().toLocalDateTime();
|
| | |
|
| | | LocalDateTime rosteringWorkStartTime = attendanceDate.getRosteringWorkStartTime() == null ? null : attendanceDate.getRosteringWorkStartTime().toLocalDateTime();
|
| | | LocalDateTime rosteringWorkEndTime = attendanceDate.getRosteringWorkEndTime() == null ? null : attendanceDate.getRosteringWorkEndTime().toLocalDateTime();
|
| | | if (actualWorkStartTime != null && actualWorkStartTime.isBefore(rosteringWorkStartTime)) {
|
| | | actualWorkStartTime = rosteringWorkStartTime;
|
| | | }
|
| | | List<Calculate> attendanceParts = new ArrayList<>();
|
| | | // 当日打卡信息不全不算
|
| | | if (actualWorkStartTime != null && actualWorkEndTime != null) {
|
| | | attendanceParts.add(new Calculate(actualWorkStartTime, actualWorkEndTime));
|
| | | }
|
| | |
|
| | | // 正常时间管理
|
| | | List<Calculate> breakParts = new ArrayList<>();
|
| | | breakParts.add(new Calculate(rosteringWorkStartTime, rosteringWorkEndTime.plusMinutes(30)));
|
| | |
|
| | | // 外出信息
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(criteria.getUserId(), startOfDate, endOfDate);
|
| | | attendanceParts.addAll(
|
| | | attendanceGoOutList.stream().map(attendanceGoOut -> {
|
| | | LocalDateTime attendanceGoOutStart = attendanceGoOut.getOutStartTime().toLocalDateTime();
|
| | | LocalDateTime attendanceGoOutEnd = attendanceGoOut.getOutEndTime().toLocalDateTime();
|
| | | if (attendanceGoOutStart.isBefore(rosteringWorkStartTime)) {
|
| | | attendanceGoOutStart = rosteringWorkStartTime;
|
| | | }
|
| | | return new Calculate(attendanceGoOutStart, attendanceGoOutEnd);
|
| | | }).filter(calculate -> calculate.getStart().isBefore(calculate.getEnd())).collect(Collectors.toList())
|
| | | );
|
| | |
|
| | | List<Calculate> resultParts = new ArrayList<>();
|
| | |
|
| | | attendanceParts.stream().sorted().reduce(new Stack<Calculate>(), (result, current) -> {
|
| | | // 删除重复时间,合并相连的时间段
|
| | | if (result.empty()) {
|
| | | result.push(current);
|
| | | return result;
|
| | | }
|
| | | Calculate last = result.peek();
|
| | | if (last.getEnd().isBefore(current.getStart())) {
|
| | | result.push(current);
|
| | | } else if (last.getEnd().isBefore(current.getEnd())) {
|
| | | last.setEnd(current.getEnd());
|
| | | }
|
| | | return result;
|
| | | }, (o1, o2) -> null).forEach(calculate -> {
|
| | | // 去除休息时间:午休等
|
| | | for (Calculate breakPart : breakParts) {
|
| | | if ((!calculate.getStart().isAfter(breakPart.getStart()) && !calculate.getEnd().isBefore(breakPart.getStart()))
|
| | | || (!calculate.getStart().isAfter(breakPart.getEnd()) && !calculate.getEnd().isBefore(breakPart.getEnd()))) {
|
| | | Calculate calculate1 = new Calculate(calculate.getStart(), breakPart.getStart());
|
| | | Calculate calculate2 = new Calculate(breakPart.getEnd(), calculate.getEnd());
|
| | | if (calculate1.getStart().isBefore(calculate1.getEnd())) {
|
| | | resultParts.add(calculate1);
|
| | | }
|
| | | if (calculate2.getStart().isBefore(calculate2.getEnd())) {
|
| | | resultParts.add(calculate2);
|
| | | }
|
| | | } else if (calculate.getEnd().isBefore(breakPart.getStart()) || calculate.getStart().isAfter(breakPart.getEnd())) {
|
| | | resultParts.add(calculate);
|
| | | }
|
| | | }
|
| | | });
|
| | |
|
| | | // 出勤时间累计
|
| | | Long result = resultParts.stream()
|
| | | .mapToLong(calculate -> ChronoUnit.MINUTES.between(calculate.getStart(), calculate.getEnd()))
|
| | | .sum();
|
| | | // .reduce(new BigDecimal(0),
|
| | | // (total, current) -> total.add(BigDecimal.valueOf(current)),
|
| | | // (o1, o2) -> null).divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP
|
| | | // )
|
| | | // result = result.setScale(1, RoundingMode.FLOOR);
|
| | | // if (result < 30) {
|
| | | // System.out.println("localDate : " + localDate + " hours : " + 0);
|
| | | // return BigDecimal.valueOf(0.0);
|
| | | // } else {
|
| | | // result = result - 30;
|
| | | // }
|
| | | System.out.println("localDate : " + localDate + " hours : " + result);
|
| | | return BigDecimal.valueOf(result).divide(BigDecimal.valueOf(60), 1, RoundingMode.HALF_DOWN);
|
| | | }).reduce(BigDecimal::add).get().doubleValue();
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Double queryAttendanceHours(AttendanceQueryCriteria criteria) {
|
| | | // 节日检索类
|
| | | SysConfigHolidayQueryCriteria sysConfigHolidayQueryCriteria = new SysConfigHolidayQueryCriteria();
|
| | | sysConfigHolidayQueryCriteria.setHolidayDateLike(criteria.getTargetYearMonth().substring(0,7));
|
| | | // 获取本月节日列表
|
| | | List<SysConfigHoliday> sysConfigHolidayMonthList = sysConfigHolidayRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,sysConfigHolidayQueryCriteria,criteriaBuilder));
|
| | |
|
| | | LocalDate startDate = LocalDate.parse(criteria.getTargetYearMonth(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
|
| | | LocalDate endDate = startDate.plusMonths(1).minusDays(1);
|
| | |
|
| | | Long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | |
|
| | | return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).filter(localDate -> {
|
| | | for (SysConfigHoliday sysConfigHoliday : sysConfigHolidayMonthList) {
|
| | | if (sysConfigHoliday.getHolidayDate().toLocalDateTime().isEqual(localDate.atStartOfDay())) {
|
| | | return Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHoliday.getDateType());
|
| | | }
|
| | | }
|
| | | return localDate.getDayOfWeek() != DayOfWeek.SATURDAY && localDate.getDayOfWeek() != DayOfWeek.SUNDAY;
|
| | | }).map(localDate -> {
|
| | | System.out.print("localDate : " + localDate);
|
| | | LocalDateTime startOfDate = localDate.atTime(0, 0, 0);
|
| | | LocalDateTime endOfDate = localDate.atTime(23, 59, 59);
|
| | | AttendanceBusinessTravel attendanceBusinessTravel = attendanceBusinessTravelRepository.findByUserAndDate(criteria.getUserId(), startOfDate, endOfDate);
|
| | | if (attendanceBusinessTravel != null) {
|
| | | System.out.println("\t hours1 : " + 8);
|
| | | return BigDecimal.valueOf(8.0);
|
| | | }
|
| | | Attendance attendanceDate = attendanceRepository.findAllByUserIdAndAttendanceDate(criteria.getUserId(), localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | if (attendanceDate == null) {
|
| | | System.out.println("\t hours2 : " + 0);
|
| | | return BigDecimal.valueOf(0.0);
|
| | | }
|
| | | LocalDateTime actualWorkStartTime = attendanceDate.getActualWorkStartTime() == null ? null : attendanceDate.getActualWorkStartTime().toLocalDateTime();
|
| | | LocalDateTime actualWorkEndTime = attendanceDate.getActualWorkEndTime() == null ? null : attendanceDate.getActualWorkEndTime().toLocalDateTime();
|
| | |
|
| | | LocalDateTime rosteringWorkStartTime = attendanceDate.getRosteringWorkStartTime() == null ? null : attendanceDate.getRosteringWorkStartTime().toLocalDateTime();
|
| | | LocalDateTime rosteringLunchStartTime = attendanceDate.getRosteringLunchStartTime() == null ? null : attendanceDate.getRosteringLunchStartTime().toLocalDateTime();
|
| | | LocalDateTime rosteringLunchEndTime = attendanceDate.getRosteringLunchEndTime() == null ? null : attendanceDate.getRosteringLunchEndTime().toLocalDateTime();
|
| | | LocalDateTime rosteringWorkEndTime = attendanceDate.getRosteringWorkEndTime() == null ? null : attendanceDate.getRosteringWorkEndTime().toLocalDateTime();
|
| | | if (actualWorkStartTime != null && actualWorkStartTime.isBefore(rosteringWorkStartTime)) {
|
| | | actualWorkStartTime = rosteringWorkStartTime;
|
| | | }
|
| | | if (actualWorkEndTime != null && actualWorkEndTime.isAfter(rosteringWorkEndTime)) {
|
| | | actualWorkEndTime = rosteringWorkEndTime;
|
| | | }
|
| | | List<Calculate> attendanceParts = new ArrayList<>();
|
| | | // 当日打卡信息不全不算
|
| | | if (actualWorkStartTime != null && actualWorkEndTime != null) {
|
| | | attendanceParts.add(new Calculate(actualWorkStartTime, actualWorkEndTime));
|
| | | }
|
| | |
|
| | | // 休息时间管理
|
| | | List<Calculate> breakParts = new ArrayList<>();
|
| | | breakParts.add(new Calculate(rosteringLunchStartTime, rosteringLunchEndTime));
|
| | |
|
| | | // 外出信息
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(criteria.getUserId(), startOfDate, endOfDate);
|
| | | attendanceParts.addAll(
|
| | | attendanceGoOutList.stream().map(attendanceGoOut -> {
|
| | | LocalDateTime attendanceGoOutStart = attendanceGoOut.getOutStartTime().toLocalDateTime();
|
| | | LocalDateTime attendanceGoOutEnd = attendanceGoOut.getOutEndTime().toLocalDateTime();
|
| | | if (attendanceGoOutStart.isBefore(rosteringWorkStartTime)) {
|
| | | attendanceGoOutStart = rosteringWorkStartTime;
|
| | | }
|
| | | if (attendanceGoOutEnd.isAfter(rosteringWorkEndTime)) {
|
| | | attendanceGoOutEnd = rosteringWorkEndTime;
|
| | | }
|
| | | return new Calculate(attendanceGoOutStart, attendanceGoOutEnd);
|
| | | }).filter(calculate -> calculate.getStart().isBefore(calculate.getEnd())).collect(Collectors.toList())
|
| | | );
|
| | |
|
| | | List<Calculate> resultParts = new ArrayList<>();
|
| | |
|
| | | attendanceParts.stream().sorted().reduce(new Stack<Calculate>(), (result, current) -> {
|
| | | // 删除重复时间,合并相连的时间段
|
| | | if (result.empty()) {
|
| | | result.push(current);
|
| | | return result;
|
| | | }
|
| | | Calculate last = result.peek();
|
| | | if (last.getEnd().isBefore(current.getStart())) {
|
| | | result.push(current);
|
| | | } else if (last.getEnd().isBefore(current.getEnd())) {
|
| | | last.setEnd(current.getEnd());
|
| | | }
|
| | | return result;
|
| | | }, (o1, o2) -> null).forEach(calculate -> {
|
| | | // 去除休息时间:午休等
|
| | | for (Calculate breakPart : breakParts) {
|
| | | if ((calculate.getStart().isBefore(breakPart.getStart()) && calculate.getEnd().isAfter(breakPart.getStart()))
|
| | | || (calculate.getStart().isBefore(breakPart.getEnd()) && calculate.getEnd().isAfter(breakPart.getEnd()))) {
|
| | | Calculate calculate1 = new Calculate(calculate.getStart(), breakPart.getStart());
|
| | | Calculate calculate2 = new Calculate(breakPart.getEnd(), calculate.getEnd());
|
| | | if (calculate1.getStart().isBefore(calculate1.getEnd())) {
|
| | | resultParts.add(calculate1);
|
| | | }
|
| | | if (calculate2.getStart().isBefore(calculate2.getEnd())) {
|
| | | resultParts.add(calculate2);
|
| | | }
|
| | | } else {
|
| | | resultParts.add(calculate);
|
| | | }
|
| | | }
|
| | | });
|
| | |
|
| | | // 出勤时间累计
|
| | | BigDecimal result = resultParts.stream()
|
| | | .map(calculate -> ChronoUnit.SECONDS.between(calculate.getStart(), calculate.getEnd()))
|
| | | .reduce(new BigDecimal(0),
|
| | | (total, current) -> total.add(BigDecimal.valueOf(current)),
|
| | | (o1, o2) -> null).divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP
|
| | | );
|
| | |
|
| | | // 请假信息
|
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.findByUserIdAndDate(criteria.getUserId(), startOfDate, endOfDate);
|
| | | // 年假不扣工时
|
| | | BigDecimal annualLeaveHours = attendanceLeaveList.stream()
|
| | | .filter(attendanceLeave -> Constant.LEAVE_TYPE_ANNUAL_LEAVE.equals(attendanceLeave.getLeaveType())
|
| | | || Constant.LEAVE_TYPE_MARRIAGE_HOLIDAY.equals(attendanceLeave.getLeaveType())
|
| | | || Constant.LEAVE_TYPE_BEREAVEMENT_LEAVE.equals(attendanceLeave.getLeaveType())
|
| | | || Constant.LEAVE_TYPE_REPLACE.equals(attendanceLeave.getLeaveType()))
|
| | | .map(attendanceLeave -> {
|
| | | if (Constant.LEAVE_TIME_TYPE_HOUR.equals(attendanceLeave.getLeaveDurationUnit())) {
|
| | | return BigDecimal.valueOf(attendanceLeave.getLeaveDuration());
|
| | | } else {
|
| | | return BigDecimal.valueOf(attendanceLeave.getLeaveDuration()).multiply(BigDecimal.valueOf(8));
|
| | | }
|
| | | }).reduce(BigDecimal::add).orElse(BigDecimal.ZERO);
|
| | | if (result.add(annualLeaveHours).compareTo(BigDecimal.valueOf(8.0)) > 0) {
|
| | | System.out.println("\t hours3 : " + 8);
|
| | | return BigDecimal.valueOf(8.0);
|
| | | }
|
| | | System.out.println("\t hours4 : " + result.add(annualLeaveHours));
|
| | | return result.add(annualLeaveHours);
|
| | | }).reduce(BigDecimal::add).get().doubleValue();
|
| | | }
|
| | |
|
| | | /**
|
| | | * 设置日历类
|
| | | * @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;
|
| | | }
|
| | | }
|
| | | } |