| | |
| | | import me.zhengjie.modules.approval.service.TemplateControlFieldService;
|
| | | import me.zhengjie.modules.approval.service.mapstruct.ApproveEventMapper;
|
| | | import me.zhengjie.modules.attendance.domain.*;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceGoOutRepository;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceOvertimeRepository;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceRepository;
|
| | | import me.zhengjie.modules.attendance.repository.OvertimeConsumedRecordRepository;
|
| | | import me.zhengjie.modules.attendance.service.AttendanceOvertimeService;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutDto;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceOvertimeDto;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceOvertimeQueryCriteria;
|
| | | import me.zhengjie.modules.attendance.service.dto.*;
|
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceOvertimeMapper;
|
| | | import me.zhengjie.modules.constant.Constant;
|
| | | import me.zhengjie.modules.salary.domain.NewSalary;
|
| | | import me.zhengjie.modules.salary.repository.NewSalaryRepository;
|
| | | import me.zhengjie.modules.system.domain.*;
|
| | | import me.zhengjie.modules.system.repository.DictDetailRepository;
|
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository;
|
| | |
| | | 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.Duration;
|
| | | import java.time.*;
|
| | | import java.time.format.DateTimeFormatter;
|
| | | import java.util.ArrayList;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.time.temporal.ChronoUnit;
|
| | | import java.util.*;
|
| | | import java.util.function.Function;
|
| | | import java.util.stream.Collectors;
|
| | |
|
| | |
| | | private final UserRepository userRepository;
|
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository;
|
| | | private final ApproveEventRepository approveEventRepository;
|
| | | private final OvertimeConsumedRecordRepository overtimeConsumedRecordRepository;
|
| | | private final AttendanceGoOutRepository attendanceGoOutRepository;
|
| | | private final NewSalaryRepository newSalaryRepository;
|
| | | private final ApproveEventMapper approveEventMapper;
|
| | | private final TemplateControlFieldService templateControlFieldService;
|
| | |
|
| | |
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public AttendanceOvertimeDto create(AttendanceOvertime resources) {
|
| | |
|
| | | //查询考勤表是否有记录
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | String attDate = sdf.format(resources.getOvertimeStartTime());
|
| | | // // 查询考勤表是否有记录
|
| | | // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | // String attDate = sdf.format(resources.getOvertimeStartTime());
|
| | | // Attendance attendance = attendanceRepository.findAllByUserIdAndAttendanceDate(resources.getUserId(),attDate.substring(0,10));
|
| | | // if(ObjectUtils.isEmpty(attendance)){
|
| | | // throw new BadRequestException("考勤记录不存在");
|
| | | // }
|
| | |
|
| | | // 检验数据
|
| | | checkPrecondition(resources);
|
| | |
|
| | | // 查询日期配置
|
| | | SysConfigHoliday sysConfigHoliday = sysConfigHolidayRepository.findByHolidayDate(resources.getOvertimeStartTime().toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | if (sysConfigHoliday != null && sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS)) {
|
| | | resources.setDateType(Constant.DATE_TYPE_LEGAL_HOLIDAYS);
|
| | |
| | | resources.setDateType(Constant.DATE_TYPE_WEEKEND);
|
| | | }
|
| | | resources.setApprovalStatus(Constant.OVERTIME_APPROVAL);
|
| | | resources.setRemainingDuration(resources.getOvertimeDuration());
|
| | |
|
| | | // 判断是否提交企微审批
|
| | | List<DictDetail> siteTypes = dictDetailRepository.findByDictName(Constant.APPROVAL_SITE_TYPE).stream().filter(siteType -> siteType.getValue().toLowerCase().equals("wecom")).collect(Collectors.toList());
|
| | |
| | | ValidationUtil.isNull( attendanceOvertime.getId(),"AttendanceOvertime","id",resources.getId());
|
| | | attendanceOvertime.copy(resources);
|
| | |
|
| | | //审核通过或者退回不能修改
|
| | | // 审核通过或者退回不能修改
|
| | | if(attendanceOvertime.getApprovalStatus().equals(Constant.OVERTIME_PASS) || attendanceOvertime.getApprovalStatus().equals(Constant.OVERTIME_RETURN)){
|
| | | throw new BadRequestException("审核通过或退回的不能进行修改");
|
| | | }
|
| | |
|
| | | // 检验数据
|
| | | checkPrecondition(resources);
|
| | |
|
| | | SysConfigHoliday sysConfigHoliday = sysConfigHolidayRepository.findByHolidayDate(resources.getOvertimeStartTime().toLocalDateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | if (sysConfigHoliday != null && sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS)) {
|
| | |
| | | User userInfo = userRepository.getOne(resources.getUserId());
|
| | | SysRostering sysRostering = userInfo.getSysRostering();
|
| | |
|
| | | long minutes = 0;
|
| | |
|
| | | if (sysRostering.getWorkEndTime().isBefore(resources.getOvertimeEndTime().toLocalDateTime().toLocalTime()) &&
|
| | | sysRostering.getWorkEndTime().plusMinutes(30).isAfter(resources.getOvertimeEndTime().toLocalDateTime().toLocalTime())) {
|
| | | resources.setOvertimeEndTime(Timestamp.valueOf(resources.getOvertimeEndTime().toLocalDateTime().with(sysRostering.getWorkEndTime())));
|
| | | // 获取开始与结束之间所有的日期
|
| | | List<LocalDate> allDates = new ArrayList<>();
|
| | | LocalDate currentDate = resources.getOvertimeStartTime().toLocalDateTime().toLocalDate();
|
| | | while (!currentDate.isAfter(resources.getOvertimeEndTime().toLocalDateTime().toLocalDate())) {
|
| | | allDates.add(currentDate);
|
| | | currentDate = currentDate.plusDays(1);
|
| | | }
|
| | | // 先排除工作日、日期类型为工作日的休息日(节假日和双休日)
|
| | | List<SysConfigHoliday> sysConfigHolidays = sysConfigHolidayRepository.findByHolidayDateBetween(Timestamp.valueOf(resources.getOvertimeStartTime().toLocalDateTime().toLocalDate().atStartOfDay()), Timestamp.valueOf(resources.getOvertimeEndTime().toLocalDateTime().toLocalDate().atStartOfDay()));
|
| | | Map<LocalDate, String> sysConfigHolidaysMap = sysConfigHolidays.stream().collect(Collectors.toMap(sch -> sch.getHolidayDate().toLocalDateTime().toLocalDate(), SysConfigHoliday::getDateType));
|
| | | List<LocalDate> overtimeDates = allDates;
|
| | |
|
| | | if ((resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getLunchStartTime()) || resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchStartTime()))
|
| | | && (resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().isAfter(sysRostering.getLunchEndTime()) || resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchEndTime()) )) {
|
| | | minutes = Duration.between(resources.getOvertimeStartTime().toLocalDateTime(), resources.getOvertimeEndTime().toLocalDateTime()).toMinutes() - 60;
|
| | | } else if (resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getLunchStartTime()) || resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchStartTime()) ||
|
| | | resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isAfter(sysRostering.getLunchEndTime()) || resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchEndTime())) {
|
| | | minutes = Duration.between(resources.getOvertimeStartTime().toLocalDateTime(), resources.getOvertimeEndTime().toLocalDateTime()).toMinutes();
|
| | | } else if (resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getLunchStartTime()) && resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().isAfter(sysRostering.getLunchStartTime()) &&
|
| | | (resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchEndTime()) || resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getLunchEndTime()))){
|
| | | minutes = Duration.between(resources.getOvertimeStartTime().toLocalDateTime().toLocalTime(), sysRostering.getLunchStartTime()).toMinutes();
|
| | | } else if ((resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getLunchStartTime()) || resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchStartTime())) && resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().isAfter(sysRostering.getLunchEndTime())) {
|
| | | minutes = Duration.between(sysRostering.getLunchEndTime(), resources.getOvertimeEndTime().toLocalDateTime()).toMinutes();
|
| | | } else if (resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isAfter(sysRostering.getLunchStartTime()) && (resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getLunchEndTime()) || resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().equals(sysRostering.getLunchEndTime()))) {
|
| | | minutes = Duration.between(sysRostering.getLunchEndTime(), resources.getOvertimeEndTime().toLocalDateTime()).toMinutes();
|
| | | List<Calculate> resultParts = new ArrayList<>();
|
| | | List<Calculate> attendanceOvertimeParts = new ArrayList<>();
|
| | | List<Calculate> breakParts = new ArrayList<>();
|
| | | for (LocalDate date : overtimeDates) {
|
| | | // 休息时间管理
|
| | | if ((sysConfigHolidaysMap.containsKey(date) && sysConfigHolidaysMap.get(date).equals(Constant.DATE_TYPE_WEEKDAY)) ||
|
| | | (!(sysConfigHolidaysMap.containsKey(date) && (sysConfigHolidaysMap.get(date).equals(Constant.DATE_TYPE_WEEKEND) || sysConfigHolidaysMap.get(date).equals(Constant.DATE_TYPE_LEGAL_HOLIDAYS))) && !(date.getDayOfWeek() == DayOfWeek.SATURDAY || date.getDayOfWeek() == DayOfWeek.SUNDAY))
|
| | | ) {
|
| | | // 工作日(过0点上班时间和下班后30分钟,未过0点跳过统计)
|
| | | if (overtimeDates.size() > 1) {
|
| | | breakParts.add(new Calculate(date.atTime(sysRostering.getWorkStartTime()), date.atTime(sysRostering.getWorkEndTime()).plusMinutes(30)));
|
| | | } else {
|
| | | continue;
|
| | | }
|
| | | } else {
|
| | | // 休息日(午休和下班后30分钟)
|
| | | breakParts.add(new Calculate(date.atTime(sysRostering.getLunchStartTime()), date.atTime(sysRostering.getLunchEndTime())));
|
| | | breakParts.add(new Calculate(date.atTime(sysRostering.getWorkEndTime()), date.atTime(sysRostering.getWorkEndTime()).plusMinutes(30)));
|
| | | }
|
| | |
|
| | | // 全部加班时间管理
|
| | | LocalDateTime startTime = date.atStartOfDay();
|
| | | LocalDateTime endTime = date.atTime(LocalTime.MAX);
|
| | | if (resources.getOvertimeStartTime().toLocalDateTime().toLocalDate().equals(date)) {
|
| | | startTime = resources.getOvertimeStartTime().toLocalDateTime();
|
| | | }
|
| | | if (resources.getOvertimeEndTime().toLocalDateTime().toLocalDate().equals(date)) {
|
| | | endTime = resources.getOvertimeEndTime().toLocalDateTime();
|
| | | }
|
| | | attendanceOvertimeParts.add(new Calculate(startTime, endTime));
|
| | | }
|
| | | attendanceOvertimeParts.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 -> {
|
| | | List<Calculate> currentParts = new ArrayList<>();
|
| | | currentParts.add(calculate);
|
| | | // 去除休息时间:午休、下班后30分钟等
|
| | | for (Calculate breakPart : breakParts) {
|
| | | List<Calculate> tempParts = new ArrayList<>();
|
| | | for (Calculate current : currentParts) {
|
| | | if ((current.getStart().isBefore(breakPart.getStart()) && current.getEnd().isAfter(breakPart.getStart()))
|
| | | || (current.getStart().isBefore(breakPart.getEnd()) && current.getEnd().isAfter(breakPart.getEnd()))) {
|
| | | Calculate calculate1 = new Calculate(current.getStart(), breakPart.getStart());
|
| | | Calculate calculate2 = new Calculate(breakPart.getEnd(), current.getEnd());
|
| | | if (calculate1.getStart().isBefore(calculate1.getEnd())) {
|
| | | tempParts.add(calculate1);
|
| | | }
|
| | | if (calculate2.getStart().isBefore(calculate2.getEnd())) {
|
| | | tempParts.add(calculate2);
|
| | | }
|
| | | } else {
|
| | | tempParts.add(current);
|
| | | }
|
| | | }
|
| | | currentParts = tempParts;
|
| | | }
|
| | | // 将处理完所有休息区间后的结果添加到最终结果
|
| | | resultParts.addAll(currentParts);
|
| | |
|
| | | if ((resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().isBefore(sysRostering.getWorkEndTime()) || resources.getOvertimeStartTime().toLocalDateTime().toLocalTime().equals(sysRostering.getWorkEndTime()))
|
| | | && (resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().isAfter(sysRostering.getWorkEndTime().plusMinutes(30)) || resources.getOvertimeEndTime().toLocalDateTime().toLocalTime().equals(sysRostering.getWorkEndTime().plusMinutes(30)) )) {
|
| | | minutes -= 30;
|
| | | }
|
| | | });
|
| | |
|
| | | double hours = new BigDecimal((double) minutes / 60).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
| | | BigDecimal resultTime = resultParts.stream()
|
| | | .map(calculate -> ChronoUnit.SECONDS.between(calculate.getStart(), calculate.getEnd()))
|
| | | .reduce(new BigDecimal(0),
|
| | | (total, current) -> total.add(BigDecimal.valueOf(current)),
|
| | | BigDecimal::add)
|
| | | .divide(BigDecimal.valueOf(3600), 2, RoundingMode.HALF_UP
|
| | | );
|
| | |
|
| | | attendanceOvertimeDto.setOvertimeDuration(hours);
|
| | | attendanceOvertimeDto.setOvertimeDuration(resultTime.max(BigDecimal.ZERO).doubleValue());
|
| | | return attendanceOvertimeDto;
|
| | | }
|
| | |
|
| | |
| | | AttendanceOvertime attendanceOvertime = attendanceOvertimeRepository.findById(resources.getId()).orElseGet(AttendanceOvertime::new);
|
| | | ValidationUtil.isNull( attendanceOvertime.getId(),"AttendanceOvertime","id",resources.getId());
|
| | | attendanceOvertime.copy(resources);
|
| | | if (attendanceOvertime.getApprovalStatus().equals(Constant.OVERTIME_APPROVAL) || attendanceOvertime.getApprovalStatus().equals(Constant.OVERTIME_RETURN)) {
|
| | | OvertimeConsumedRecordQueryCriteria consumedCriteria = new OvertimeConsumedRecordQueryCriteria();
|
| | | consumedCriteria.setOvertimeId(attendanceOvertime.getId());
|
| | | Long haveCount = overtimeConsumedRecordRepository.count((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, consumedCriteria, criteriaBuilder));
|
| | | if (haveCount > 0) {
|
| | | throw new BadRequestException("调休时长已占用,不可撤销");
|
| | | }
|
| | | }
|
| | | attendanceOvertimeRepository.save(attendanceOvertime);
|
| | | }
|
| | |
|
| | | public void checkPrecondition(AttendanceOvertime resources) {
|
| | | // 验证是否有对应的考勤或外出记录
|
| | | Attendance attendance = attendanceRepository.findAllByUserIdAndAttendanceDate(resources.getUserId(), resources.getOvertimeStartTime().toLocalDateTime().toLocalDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(resources.getUserId(), resources.getOvertimeStartTime().toLocalDateTime(), resources.getOvertimeEndTime().toLocalDateTime());
|
| | | Boolean attendanceFlag = attendance == null || attendance.getActualWorkStartTime() == null || attendance.getActualWorkEndTime() == null || attendance.getActualWorkStartTime().toLocalDateTime().compareTo(resources.getOvertimeStartTime().toLocalDateTime()) > 0 || attendance.getActualWorkEndTime().toLocalDateTime().compareTo(resources.getOvertimeEndTime().toLocalDateTime()) < 0;
|
| | | Boolean attendanceGoOutFlag = attendanceGoOutList.isEmpty() || resources.getOvertimeDuration() > attendanceGoOutList.stream().collect(Collectors.summingDouble(AttendanceGoOut::getOutDuration)) || resources.getOvertimeStartTime().compareTo(attendanceGoOutList.stream().map(AttendanceGoOut::getOutStartTime).min(Timestamp::compareTo).orElse(null)) < 0 || resources.getOvertimeEndTime().compareTo(attendanceGoOutList.stream().map(AttendanceGoOut::getOutEndTime).max(Timestamp::compareTo).orElse(null)) > 0;
|
| | | if (attendanceFlag && attendanceGoOutFlag) {
|
| | | throw new BadRequestException("缺少对应的考勤记录或外出记录。");
|
| | | }
|
| | |
|
| | | // 验证工资是否已结算(如已结算不可创建,因调休可能会和加班时间重合)
|
| | | List<NewSalary> salaryList = newSalaryRepository.findByUserIdAndSalaryYearMonth(resources.getUserId(), Timestamp.valueOf(resources.getOvertimeStartTime().toLocalDateTime().toLocalDate().withDayOfMonth(1).atStartOfDay()));
|
| | | if (salaryList.size() > 0) {
|
| | | throw new BadRequestException("该月工资信息已生成,请确认后重试。");
|
| | | }
|
| | | }
|
| | | } |