| | |
| | | package me.zhengjie; |
| | | |
| | | import me.zhengjie.modules.attendance.domain.Attendance;
|
| | | import me.zhengjie.modules.attendance.domain.AttendanceGoOut; |
| | | import me.zhengjie.modules.attendance.domain.AttendanceOvertime; |
| | | import me.zhengjie.modules.attendance.domain.Calculate; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceGoOutRepository;
|
| | | import me.zhengjie.modules.attendance.service.AttendanceGoOutService; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutDto;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutQueryCriteria;
|
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceGoOutMapper;
|
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.quartz.task.AttendanceMonthlyMethod; |
| | | import me.zhengjie.modules.system.domain.SysRostering; |
| | |
| | | import me.zhengjie.modules.system.service.dto.SysConfigHolidayDto; |
| | | import me.zhengjie.utils.SecurityUtils; |
| | | import org.junit.jupiter.api.Test; |
| | | import org.junit.jupiter.api.extension.ExtendWith;
|
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
| | | import org.springframework.boot.test.context.SpringBootTest; |
| | | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
| | | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
| | | import org.springframework.security.core.Authentication;
|
| | | import org.springframework.security.core.context.SecurityContextHolder;
|
| | | import org.springframework.test.annotation.Rollback;
|
| | | import org.springframework.test.context.junit.jupiter.SpringExtension;
|
| | | import org.springframework.test.context.junit4.SpringRunner;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.text.ParseException; |
| | | import java.time.DayOfWeek; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.LocalTime; |
| | | import java.time.*;
|
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.ChronoUnit; |
| | | import java.util.List;
|
| | | import java.util.stream.Stream; |
| | | |
| | | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| | | //@ExtendWith(SpringExtension.class)
|
| | | //@DataJpaTest
|
| | | public class AttendanceTest { |
| | | |
| | | @Autowired |
| | |
| | | public void exec100() throws ParseException { |
| | | attendanceMonthlyMethod.getMonthlyData(); |
| | | } |
| | |
|
| | | @Autowired
|
| | | private AttendanceService attendanceService;
|
| | |
|
| | | @Test
|
| | | public void addAttendanceInfo() {
|
| | | LocalDate startDate = LocalDate.of(2024, 6, 7);
|
| | | LocalDate endDate = LocalDate.of(2024, 6, 7);
|
| | | long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | | Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> {
|
| | | Attendance attendance = new Attendance();
|
| | | attendance.setUserId(44L);
|
| | | attendance.setAttendanceDate(Timestamp.valueOf(localDate.atStartOfDay()));
|
| | | try {
|
| | | attendanceService.addAttendanceInfo(attendance);
|
| | | } catch (Exception e) {
|
| | | throw new RuntimeException(e);
|
| | | }
|
| | | });
|
| | | }
|
| | |
|
| | | @Autowired
|
| | | private AttendanceGoOutMapper attendanceGoOutMapper;
|
| | | @Autowired
|
| | | private AuthenticationManagerBuilder authenticationManagerBuilder;
|
| | | @Autowired
|
| | | private AttendanceGoOutRepository attendanceGoOutRepository;
|
| | |
|
| | | @Test
|
| | | @Transactional
|
| | | @Rollback(false)
|
| | | public void AttendanceGoOutServiceTest() {
|
| | | UsernamePasswordAuthenticationToken authenticationToken =
|
| | | new UsernamePasswordAuthenticationToken("yaozc", "0350111443");
|
| | | Authentication authentication = authenticationManagerBuilder.getObject().authenticate(authenticationToken);
|
| | | SecurityContextHolder.getContext().setAuthentication(authentication);
|
| | |
|
| | | AttendanceGoOutQueryCriteria criteria = new AttendanceGoOutQueryCriteria();
|
| | | criteria.setOutOfOfficeType("go_out_type_personnel");
|
| | | criteria.setOutStartTimeLike("2024-10-%");
|
| | | List<AttendanceGoOutDto> attendanceGoOutDtos = attendanceGoOutService.queryAll(criteria);
|
| | | attendanceGoOutDtos.forEach(attendanceGoOut -> {
|
| | | Calculate calculate = new Calculate();
|
| | | calculate.setStart(attendanceGoOut.getOutStartTime().toLocalDateTime());
|
| | | calculate.setEnd(attendanceGoOut.getOutEndTime().toLocalDateTime());
|
| | |
|
| | | long timeSeconds = calculateTimes(calculate, attendanceGoOut.getUserId());
|
| | |
|
| | | Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
|
| | |
|
| | | if (result.compareTo(attendanceGoOut.getOutDuration()) != 0) {
|
| | | System.out.println(attendanceGoOut.getUserName() + " : " + result + " : " + attendanceGoOut.getOutDuration() + " : " + attendanceGoOut.getOutStartTime() + " : " + attendanceGoOut.getOutEndTime());
|
| | | attendanceGoOut.setOutDuration(result);
|
| | | attendanceGoOutRepository.save(attendanceGoOutMapper.toEntity(attendanceGoOut));
|
| | | }
|
| | | });
|
| | | }
|
| | |
|
| | | private Long calculateTimes(Calculate calculate, Long userId) {
|
| | | User userInfo = userRepository.getOne(userId);
|
| | | SysRostering sysRostering = userInfo.getSysRostering();
|
| | |
|
| | | LocalDate startDate = calculate.getStart().toLocalDate();
|
| | | LocalDate endDate = calculate.getEnd().toLocalDate();
|
| | | LocalTime startTime = calculate.getStart().toLocalTime();
|
| | | LocalTime endTime = calculate.getEnd().toLocalTime();
|
| | | long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | | return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).map(localDate -> {
|
| | | SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
|
| | | if ((sysConfigHolidayDto == null && !isWeekend) ||
|
| | | sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
|
| | |
|
| | | LocalDateTime goOutStartTime = localDate.atTime(startTime);
|
| | | LocalDateTime goOutEndTime = localDate.atTime(endTime);
|
| | | LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
|
| | | LocalDateTime lunchEndTime = localDate.atTime(sysRostering.getLunchEndTime());
|
| | |
|
| | |
|
| | | if (goOutEndTime.isBefore(lunchStartTime) || goOutStartTime.isAfter(lunchEndTime)) {
|
| | | return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
|
| | | } else if (!goOutEndTime.isBefore(lunchStartTime) && !goOutEndTime.isAfter(lunchEndTime)) {
|
| | | return ChronoUnit.SECONDS.between(goOutStartTime, lunchStartTime);
|
| | | } else if (!goOutStartTime.isBefore(lunchStartTime) && !goOutStartTime.isAfter(lunchEndTime)) {
|
| | | return ChronoUnit.SECONDS.between(lunchEndTime, goOutEndTime);
|
| | | } else {
|
| | | return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime) - 3600;
|
| | | }
|
| | | } else {
|
| | | LocalDateTime goOutStartTime = localDate.atTime(startTime);
|
| | | LocalDateTime goOutEndTime = localDate.atTime(endTime);
|
| | | return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
|
| | | }
|
| | | }).reduce(0L, (result, current) -> result + current);
|
| | | }
|
| | | } |